Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src-elm/Json.elm
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
module Json exposing (configEncoder, elmMessageBuilder, elmMessageEncoder, externalMessageDecoder, sessionTypeDecoder, soundMessageEncoder)
module Json exposing (configEncoder, currentStateEncoder, elmMessageBuilder, elmMessageEncoder, externalMessageDecoder, sessionTypeDecoder, soundMessageEncoder)

import Json.Decode as Decode
import Json.Decode.Pipeline as Pipe
import Json.Encode as Encode
import Themes exposing (Theme, ThemeColors)
import Types exposing (Config, ElmMessage, ExternalMessage(..), InitData, PomodoroSession, PomodoroState, SessionStatus(..), SessionType(..), SoundMessageValue, sessionTypeToString)
import Types
exposing
( Config
, CurrentState
, ElmMessage
, ExternalMessage(..)
, InitData
, PomodoroSession
, PomodoroState
, SessionStatus(..)
, SessionType(..)
, SoundMessageValue
, sessionStatusToString
, sessionTypeToString
)


elmMessageEncoder : ElmMessage -> Encode.Value
Expand Down Expand Up @@ -58,6 +72,16 @@ themesDecoder =
Decode.list themeDecoder


currentStateEncoder : CurrentState -> Encode.Value
currentStateEncoder currentState =
Encode.object
[ ( "color", Encode.string currentState.color )
, ( "percentage", Encode.float currentState.percentage )
, ( "paused", Encode.bool currentState.paused )
, ( "sessionStatus", Encode.string (sessionStatusToString currentState.sessionStatus) )
]


soundMessageEncoder : SoundMessageValue -> Encode.Value
soundMessageEncoder soundMessageValue =
Encode.object
Expand Down
57 changes: 17 additions & 40 deletions src-elm/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Browser
import ColorHelper exposing (colorForSessionType, computeCurrentColor, fromCSSHexToRGB, fromRGBToCSSHex)
import Html exposing (Html, div)
import Html.Attributes exposing (id)
import Json exposing (configEncoder, elmMessageBuilder, elmMessageEncoder, externalMessageDecoder, sessionTypeDecoder, soundMessageEncoder)
import Json exposing (configEncoder, currentStateEncoder, elmMessageBuilder, elmMessageEncoder, externalMessageDecoder, sessionTypeDecoder, soundMessageEncoder)
import Json.Decode as Decode
import Json.Encode as Encode
import ListWithCurrent exposing (ListWithCurrent(..))
Expand All @@ -13,7 +13,6 @@ import TimeHelper exposing (getCurrentMaxTime)
import Types
exposing
( Config
, CurrentState
, Defaults
, ExternalMessage(..)
, Model
Expand Down Expand Up @@ -44,19 +43,6 @@ main =
}


sessionStatusToString : SessionStatus -> String
sessionStatusToString sessionStatus =
case sessionStatus of
Paused ->
"paused"

Running ->
"running"

NotStarted ->
"not_started"


type alias Flags =
{ alwaysOnTop : Bool
, appVersion : String
Expand Down Expand Up @@ -102,6 +88,7 @@ init flags =
{ color = theme.colors.focusRound
, percentage = 1
, paused = False
, sessionStatus = NotStarted
}
in
( { appVersion = flags.appVersion
Expand Down Expand Up @@ -152,8 +139,7 @@ init flags =
, volumeSliderHidden = True
}
, Cmd.batch
[ updateCurrentState currentState
, updateSessionStatus (NotStarted |> sessionStatusToString)
[ sendMessageFromElm (elmMessageBuilder "update_current_state" currentState currentStateEncoder)
, sendMessageFromElm (elmMessageEncoder { name = "get_init_data", value = Nothing })
, setThemeColors <| theme.colors
]
Expand Down Expand Up @@ -204,7 +190,7 @@ update msg ({ config } as model) =
, Cmd.batch
[ setThemeColors theme.colors
, sendMessageFromElm (elmMessageBuilder "update_config" newConfig configEncoder)
, updateCurrentState newState
, sendMessageFromElm (elmMessageBuilder "update_current_state" newState currentStateEncoder)
]
)

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

( modelWithTheme, cmdWithTheme ) =
let
baseCmd =
[ newCmd, updateSessionStatus (NotStarted |> sessionStatusToString) ]
in
case newThemes |> ListWithCurrent.getCurrent of
Just currentTheme ->
let
( updatedModel, updatedCmd ) =
update (ChangeTheme currentTheme) newModel
in
( updatedModel, Cmd.batch (updatedCmd :: baseCmd) )
( updatedModel, Cmd.batch [ updatedCmd, newCmd ] )

_ ->
( newModel, Cmd.batch baseCmd )
( newModel, newCmd )
in
-- Auto start if config is set
if modelWithTheme.config.autoStartOnAppStartup then
Expand Down Expand Up @@ -353,6 +335,7 @@ update msg ({ config } as model) =
, percentage = percent
, paused =
pomodoroState.currentSession.status == Paused
, sessionStatus = pomodoroState.currentSession.status
}

cmds =
Expand Down Expand Up @@ -412,7 +395,8 @@ update msg ({ config } as model) =
, currentState = currentState
, pomodoroState = Just pomodoroState
}
, Cmd.batch (updateCurrentState currentState :: cmds)
, Cmd.batch
(sendMessageFromElm (elmMessageBuilder "update_current_state" currentState currentStateEncoder) :: cmds)
)

ProcessExternalMessage (SoundFilePath sessionType path) ->
Expand Down Expand Up @@ -444,12 +428,12 @@ update msg ({ config } as model) =
, percentage = 1
, paused =
model.pomodoroState |> Maybe.map (\state -> state.currentSession.status == Paused) |> Maybe.withDefault False
, sessionStatus = NotStarted
}
in
( model
, Cmd.batch
[ updateCurrentState currentState
, updateSessionStatus (NotStarted |> sessionStatusToString)
[ sendMessageFromElm (elmMessageBuilder "update_current_state" currentState currentStateEncoder)
, sendMessageFromElm (elmMessageEncoder { name = "reset", value = Nothing })
]
)
Expand All @@ -467,10 +451,7 @@ update msg ({ config } as model) =
( { model
| config = newConfig
}
, Cmd.batch
[ updateSessionStatus (NotStarted |> sessionStatusToString)
, sendMessageFromElm (elmMessageEncoder { name = "reset", value = Nothing })
]
, sendMessageFromElm (elmMessageEncoder { name = "reset", value = Nothing })
)

ResetAudioFile sessionType ->
Expand Down Expand Up @@ -579,16 +560,17 @@ update msg ({ config } as model) =
else
1
, paused = True
, sessionStatus = Paused
}
in
( { model | currentState = currentState }
, Cmd.batch
[ updateCurrentState currentState
[ sendMessageFromElm (elmMessageBuilder "update_current_state" currentState currentStateEncoder)
, sendMessageFromElm (elmMessageEncoder { name = "pause", value = Nothing })
]
)

_ ->
status ->
let
maxTime =
getCurrentMaxTime config state
Expand All @@ -607,11 +589,12 @@ update msg ({ config } as model) =
else
1
, paused = False
, sessionStatus = status
}
in
( { model | currentState = currentState }
, Cmd.batch
[ updateCurrentState currentState
[ sendMessageFromElm (elmMessageBuilder "update_current_state" currentState currentStateEncoder)
, sendMessageFromElm (elmMessageEncoder { name = "play", value = Nothing })
]
)
Expand Down Expand Up @@ -796,12 +779,6 @@ port minimizeWindow : () -> Cmd msg
port hideWindow : () -> Cmd msg


port updateCurrentState : CurrentState -> Cmd msg


port updateSessionStatus : String -> Cmd msg


port notify : Notification -> Cmd msg


Expand Down
20 changes: 19 additions & 1 deletion src-elm/Types.elm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Types exposing
, SettingTab(..)
, SettingType(..)
, SoundMessageValue
, sessionStatusToString
, sessionTypeFromString
, sessionTypeToString
)
Expand Down Expand Up @@ -122,7 +123,11 @@ type alias InitData =


type alias CurrentState =
{ color : String, percentage : Float, paused : Bool }
{ color : String
, percentage : Float
, paused : Bool
, sessionStatus : SessionStatus
}


type alias Notification =
Expand Down Expand Up @@ -235,3 +240,16 @@ sessionTypeFromString string =

_ ->
Nothing


sessionStatusToString : SessionStatus -> String
sessionStatusToString sessionStatus =
case sessionStatus of
Paused ->
"paused"

Running ->
"running"

NotStarted ->
"not_started"
31 changes: 16 additions & 15 deletions src-ts/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ElmState = {
color: string;
percentage: number;
paused: boolean;
sessionStatus: string;
};
type SoundMessage = {
soundId: string;
Expand All @@ -30,7 +31,7 @@ type SoundMessage = {

type Message = {
name: string;
value: string | ElmConfig | SoundMessage;
value: string | ElmConfig | SoundMessage | ElmState;
};

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

case "update_current_state":
let state: ElmState = message.value as ElmState;
invoke("change_icon", {
red: hexToRgb(state.color)?.r,
green: hexToRgb(state.color)?.g,
blue: hexToRgb(state.color)?.b,
fillPercentage: state.percentage,
paused: state.paused,
});

invoke("update_session_status", { status: state.sessionStatus });

break;

case "get_init_data":
console.log("Getting init data from Rust");

Expand Down Expand Up @@ -306,20 +321,6 @@ app.ports.sendMessageFromElm.subscribe(async function (message: Message) {
}
});

app.ports.updateCurrentState.subscribe(function (state: ElmState) {
invoke("change_icon", {
red: hexToRgb(state.color)?.r,
green: hexToRgb(state.color)?.g,
blue: hexToRgb(state.color)?.b,
fillPercentage: state.percentage,
paused: state.paused,
});
});

app.ports.updateSessionStatus.subscribe(function (status: String) {
invoke("update_session_status", { status });
});

app.ports.setThemeColors.subscribe(function (themeColors: ThemeColors) {
let mainHtmlElement = document.documentElement;
mainHtmlElement.style.setProperty(
Expand Down