diff options
Diffstat (limited to 'src/Update')
-rw-r--r-- | src/Update/Update.elm | 32 | ||||
-rw-r--r-- | src/Update/UpdateTimer.elm | 18 |
2 files changed, 32 insertions, 18 deletions
diff --git a/src/Update/Update.elm b/src/Update/Update.elm index 8285760..3bdcc2b 100644 --- a/src/Update/Update.elm +++ b/src/Update/Update.elm @@ -6,6 +6,7 @@ module Update.Update import Signal import Dict +import Dict (Dict) import Time (Time) import Maybe import Keyboard (KeyCode) @@ -20,8 +21,11 @@ import Model.IdGenerator (..) import Update.UpdateTimer (..) import Update.UpdateTimerEdition (..) +import Utils.Maybe (..) + type Action = NoOp + | Initialize | AddNewTimer | DeltaTime Time | UpdateTimer Id TimerAction @@ -38,6 +42,8 @@ update : Action -> Model -> Model update action model = case action of NoOp -> model + Initialize -> + initialModel model.currentTime AddNewTimer -> let (id, newTimerIdGenerator) = getId model.timerIdGenerator timerName = "Timer " ++ (toString id) @@ -51,14 +57,17 @@ update action model = , timers <- substractTimersTime delta model.timers } UpdateTimer id action -> - let inEdition = - model.timerEdition - |> Maybe.map (\timerEdition -> timerEdition.id == id) - |> Maybe.withDefault False - in if inEdition - then - model - else + + let maybeTimerEdition = filterMaybe (\timerEdition -> timerEdition.id == id) model.timerEdition + in case model.timerEdition of + Just timerEdition -> + { model + | timers <- + updateTimerTime timerEdition model.timers + |> Dict.update id (Maybe.map (updateTimer action)) + , timerEdition <- Nothing + } + Nothing -> { model | timers <- Dict.update id (Maybe.map (updateTimer action)) model.timers } RemoveTimer id -> { model | timers <- Dict.remove id model.timers } @@ -71,7 +80,7 @@ update action model = case model.timerEdition of Just timerEdition -> { model - | timers <- Dict.update timerEdition.id (Maybe.map (updateTimer (SetTime (toTime timerEdition.numbers)))) model.timers + | timers <- updateTimerTime timerEdition model.timers , timerEdition <- Nothing } Nothing -> @@ -81,5 +90,6 @@ update action model = KeyPressed keyCode -> { model | timerEdition <- Maybe.map (updateTimerEdition (keyCodeToChar keyCode)) model.timerEdition } -isEnter : KeyCode -> Bool -isEnter = (==) 107 +updateTimerTime : TimerEdition -> Dict Id Timer -> Dict Id Timer +updateTimerTime timerEdition = + Dict.update timerEdition.id (Maybe.map (updateTimer (SetTime (toTime timerEdition.numbers)))) diff --git a/src/Update/UpdateTimer.elm b/src/Update/UpdateTimer.elm index e4671d9..c147d23 100644 --- a/src/Update/UpdateTimer.elm +++ b/src/Update/UpdateTimer.elm @@ -6,6 +6,7 @@ module Update.UpdateTimer import Time (Time) import Model.Timer (..) +import Model.TimerState (..) import Model.Id (..) type TimerAction = @@ -20,19 +21,22 @@ updateTimer action timer = case action of Restart -> { timer - | isRunning <- True - , currentTime <- initTime timer.initialTime + | currentTime <- initTime timer.initialTime + , state <- Running } Pause -> + { timer | state <- Idle } + ToggleRunning -> { timer - | isRunning <- False + | state <- + if timer.currentTime > 0 && timer.state == Idle + then Running + else Idle } - ToggleRunning -> - { timer | isRunning <- timer.currentTime > 0.0 && not timer.isRunning } Stop -> { timer - | isRunning <- False - , currentTime <- initTime timer.initialTime + | currentTime <- initTime timer.initialTime + , state <- Idle } SetTime time -> let augmentedTime = time + 999 |