Skip to content

Commit ca00f1e

Browse files
authored
fix: handle play status correctly in tray menu (#199)
* refactor: use elm json encoder for message * chore: add session status to state * chore: remove useless port * chore: elm review
1 parent 298f5f0 commit ca00f1e

File tree

4 files changed

+78
-58
lines changed

4 files changed

+78
-58
lines changed

src-elm/Json.elm

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1-
module Json exposing (configEncoder, elmMessageBuilder, elmMessageEncoder, externalMessageDecoder, sessionTypeDecoder, soundMessageEncoder)
1+
module Json exposing (configEncoder, currentStateEncoder, elmMessageBuilder, elmMessageEncoder, externalMessageDecoder, sessionTypeDecoder, soundMessageEncoder)
22

33
import Json.Decode as Decode
44
import Json.Decode.Pipeline as Pipe
55
import Json.Encode as Encode
66
import Themes exposing (Theme, ThemeColors)
7-
import Types exposing (Config, ElmMessage, ExternalMessage(..), InitData, PomodoroSession, PomodoroState, SessionStatus(..), SessionType(..), SoundMessageValue, sessionTypeToString)
7+
import Types
8+
exposing
9+
( Config
10+
, CurrentState
11+
, ElmMessage
12+
, ExternalMessage(..)
13+
, InitData
14+
, PomodoroSession
15+
, PomodoroState
16+
, SessionStatus(..)
17+
, SessionType(..)
18+
, SoundMessageValue
19+
, sessionStatusToString
20+
, sessionTypeToString
21+
)
822

923

1024
elmMessageEncoder : ElmMessage -> Encode.Value
@@ -58,6 +72,16 @@ themesDecoder =
5872
Decode.list themeDecoder
5973

6074

75+
currentStateEncoder : CurrentState -> Encode.Value
76+
currentStateEncoder currentState =
77+
Encode.object
78+
[ ( "color", Encode.string currentState.color )
79+
, ( "percentage", Encode.float currentState.percentage )
80+
, ( "paused", Encode.bool currentState.paused )
81+
, ( "sessionStatus", Encode.string (sessionStatusToString currentState.sessionStatus) )
82+
]
83+
84+
6185
soundMessageEncoder : SoundMessageValue -> Encode.Value
6286
soundMessageEncoder soundMessageValue =
6387
Encode.object

src-elm/Main.elm

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Browser
44
import ColorHelper exposing (colorForSessionType, computeCurrentColor, fromCSSHexToRGB, fromRGBToCSSHex)
55
import Html exposing (Html, div)
66
import Html.Attributes exposing (id)
7-
import Json exposing (configEncoder, elmMessageBuilder, elmMessageEncoder, externalMessageDecoder, sessionTypeDecoder, soundMessageEncoder)
7+
import Json exposing (configEncoder, currentStateEncoder, elmMessageBuilder, elmMessageEncoder, externalMessageDecoder, sessionTypeDecoder, soundMessageEncoder)
88
import Json.Decode as Decode
99
import Json.Encode as Encode
1010
import ListWithCurrent exposing (ListWithCurrent(..))
@@ -13,7 +13,6 @@ import TimeHelper exposing (getCurrentMaxTime)
1313
import Types
1414
exposing
1515
( Config
16-
, CurrentState
1716
, Defaults
1817
, ExternalMessage(..)
1918
, Model
@@ -44,19 +43,6 @@ main =
4443
}
4544

4645

47-
sessionStatusToString : SessionStatus -> String
48-
sessionStatusToString sessionStatus =
49-
case sessionStatus of
50-
Paused ->
51-
"paused"
52-
53-
Running ->
54-
"running"
55-
56-
NotStarted ->
57-
"not_started"
58-
59-
6046
type alias Flags =
6147
{ alwaysOnTop : Bool
6248
, appVersion : String
@@ -102,6 +88,7 @@ init flags =
10288
{ color = theme.colors.focusRound
10389
, percentage = 1
10490
, paused = False
91+
, sessionStatus = NotStarted
10592
}
10693
in
10794
( { appVersion = flags.appVersion
@@ -152,8 +139,7 @@ init flags =
152139
, volumeSliderHidden = True
153140
}
154141
, Cmd.batch
155-
[ updateCurrentState currentState
156-
, updateSessionStatus (NotStarted |> sessionStatusToString)
142+
[ sendMessageFromElm (elmMessageBuilder "update_current_state" currentState currentStateEncoder)
157143
, sendMessageFromElm (elmMessageEncoder { name = "get_init_data", value = Nothing })
158144
, setThemeColors <| theme.colors
159145
]
@@ -204,7 +190,7 @@ update msg ({ config } as model) =
204190
, Cmd.batch
205191
[ setThemeColors theme.colors
206192
, sendMessageFromElm (elmMessageBuilder "update_config" newConfig configEncoder)
207-
, updateCurrentState newState
193+
, sendMessageFromElm (elmMessageBuilder "update_current_state" newState currentStateEncoder)
208194
]
209195
)
210196

@@ -264,20 +250,16 @@ update msg ({ config } as model) =
264250
|> update (ProcessExternalMessage (RustStateMsg c.pomodoroState))
265251

266252
( modelWithTheme, cmdWithTheme ) =
267-
let
268-
baseCmd =
269-
[ newCmd, updateSessionStatus (NotStarted |> sessionStatusToString) ]
270-
in
271253
case newThemes |> ListWithCurrent.getCurrent of
272254
Just currentTheme ->
273255
let
274256
( updatedModel, updatedCmd ) =
275257
update (ChangeTheme currentTheme) newModel
276258
in
277-
( updatedModel, Cmd.batch (updatedCmd :: baseCmd) )
259+
( updatedModel, Cmd.batch [ updatedCmd, newCmd ] )
278260

279261
_ ->
280-
( newModel, Cmd.batch baseCmd )
262+
( newModel, newCmd )
281263
in
282264
-- Auto start if config is set
283265
if modelWithTheme.config.autoStartOnAppStartup then
@@ -353,6 +335,7 @@ update msg ({ config } as model) =
353335
, percentage = percent
354336
, paused =
355337
pomodoroState.currentSession.status == Paused
338+
, sessionStatus = pomodoroState.currentSession.status
356339
}
357340

358341
cmds =
@@ -412,7 +395,8 @@ update msg ({ config } as model) =
412395
, currentState = currentState
413396
, pomodoroState = Just pomodoroState
414397
}
415-
, Cmd.batch (updateCurrentState currentState :: cmds)
398+
, Cmd.batch
399+
(sendMessageFromElm (elmMessageBuilder "update_current_state" currentState currentStateEncoder) :: cmds)
416400
)
417401

418402
ProcessExternalMessage (SoundFilePath sessionType path) ->
@@ -444,12 +428,12 @@ update msg ({ config } as model) =
444428
, percentage = 1
445429
, paused =
446430
model.pomodoroState |> Maybe.map (\state -> state.currentSession.status == Paused) |> Maybe.withDefault False
431+
, sessionStatus = NotStarted
447432
}
448433
in
449434
( model
450435
, Cmd.batch
451-
[ updateCurrentState currentState
452-
, updateSessionStatus (NotStarted |> sessionStatusToString)
436+
[ sendMessageFromElm (elmMessageBuilder "update_current_state" currentState currentStateEncoder)
453437
, sendMessageFromElm (elmMessageEncoder { name = "reset", value = Nothing })
454438
]
455439
)
@@ -467,10 +451,7 @@ update msg ({ config } as model) =
467451
( { model
468452
| config = newConfig
469453
}
470-
, Cmd.batch
471-
[ updateSessionStatus (NotStarted |> sessionStatusToString)
472-
, sendMessageFromElm (elmMessageEncoder { name = "reset", value = Nothing })
473-
]
454+
, sendMessageFromElm (elmMessageEncoder { name = "reset", value = Nothing })
474455
)
475456

476457
ResetAudioFile sessionType ->
@@ -579,16 +560,17 @@ update msg ({ config } as model) =
579560
else
580561
1
581562
, paused = True
563+
, sessionStatus = Paused
582564
}
583565
in
584566
( { model | currentState = currentState }
585567
, Cmd.batch
586-
[ updateCurrentState currentState
568+
[ sendMessageFromElm (elmMessageBuilder "update_current_state" currentState currentStateEncoder)
587569
, sendMessageFromElm (elmMessageEncoder { name = "pause", value = Nothing })
588570
]
589571
)
590572

591-
_ ->
573+
status ->
592574
let
593575
maxTime =
594576
getCurrentMaxTime config state
@@ -607,11 +589,12 @@ update msg ({ config } as model) =
607589
else
608590
1
609591
, paused = False
592+
, sessionStatus = status
610593
}
611594
in
612595
( { model | currentState = currentState }
613596
, Cmd.batch
614-
[ updateCurrentState currentState
597+
[ sendMessageFromElm (elmMessageBuilder "update_current_state" currentState currentStateEncoder)
615598
, sendMessageFromElm (elmMessageEncoder { name = "play", value = Nothing })
616599
]
617600
)
@@ -796,12 +779,6 @@ port minimizeWindow : () -> Cmd msg
796779
port hideWindow : () -> Cmd msg
797780

798781

799-
port updateCurrentState : CurrentState -> Cmd msg
800-
801-
802-
port updateSessionStatus : String -> Cmd msg
803-
804-
805782
port notify : Notification -> Cmd msg
806783

807784

src-elm/Types.elm

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module Types exposing
1818
, SettingTab(..)
1919
, SettingType(..)
2020
, SoundMessageValue
21+
, sessionStatusToString
2122
, sessionTypeFromString
2223
, sessionTypeToString
2324
)
@@ -122,7 +123,11 @@ type alias InitData =
122123

123124

124125
type alias CurrentState =
125-
{ color : String, percentage : Float, paused : Bool }
126+
{ color : String
127+
, percentage : Float
128+
, paused : Bool
129+
, sessionStatus : SessionStatus
130+
}
126131

127132

128133
type alias Notification =
@@ -235,3 +240,16 @@ sessionTypeFromString string =
235240

236241
_ ->
237242
Nothing
243+
244+
245+
sessionStatusToString : SessionStatus -> String
246+
sessionStatusToString sessionStatus =
247+
case sessionStatus of
248+
Paused ->
249+
"paused"
250+
251+
Running ->
252+
"running"
253+
254+
NotStarted ->
255+
"not_started"

src-ts/main.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type ElmState = {
2222
color: string;
2323
percentage: number;
2424
paused: boolean;
25+
sessionStatus: string;
2526
};
2627
type SoundMessage = {
2728
soundId: string;
@@ -30,7 +31,7 @@ type SoundMessage = {
3031

3132
type Message = {
3233
name: string;
33-
value: string | ElmConfig | SoundMessage;
34+
value: string | ElmConfig | SoundMessage | ElmState;
3435
};
3536

3637
type Notification = {
@@ -236,6 +237,20 @@ app.ports.sendMessageFromElm.subscribe(async function (message: Message) {
236237
invoke("quit");
237238
break;
238239

240+
case "update_current_state":
241+
let state: ElmState = message.value as ElmState;
242+
invoke("change_icon", {
243+
red: hexToRgb(state.color)?.r,
244+
green: hexToRgb(state.color)?.g,
245+
blue: hexToRgb(state.color)?.b,
246+
fillPercentage: state.percentage,
247+
paused: state.paused,
248+
});
249+
250+
invoke("update_session_status", { status: state.sessionStatus });
251+
252+
break;
253+
239254
case "get_init_data":
240255
console.log("Getting init data from Rust");
241256

@@ -306,20 +321,6 @@ app.ports.sendMessageFromElm.subscribe(async function (message: Message) {
306321
}
307322
});
308323

309-
app.ports.updateCurrentState.subscribe(function (state: ElmState) {
310-
invoke("change_icon", {
311-
red: hexToRgb(state.color)?.r,
312-
green: hexToRgb(state.color)?.g,
313-
blue: hexToRgb(state.color)?.b,
314-
fillPercentage: state.percentage,
315-
paused: state.paused,
316-
});
317-
});
318-
319-
app.ports.updateSessionStatus.subscribe(function (status: String) {
320-
invoke("update_session_status", { status });
321-
});
322-
323324
app.ports.setThemeColors.subscribe(function (themeColors: ThemeColors) {
324325
let mainHtmlElement = document.documentElement;
325326
mainHtmlElement.style.setProperty(

0 commit comments

Comments
 (0)