diff options
Diffstat (limited to 'src/Update/Update.elm')
-rw-r--r-- | src/Update/Update.elm | 77 |
1 files changed, 41 insertions, 36 deletions
diff --git a/src/Update/Update.elm b/src/Update/Update.elm index fabe0e8..5a4b12a 100644 --- a/src/Update/Update.elm +++ b/src/Update/Update.elm @@ -16,12 +16,14 @@ import List import Model.Model (..) import Model.Timer (..) -import Model.TimerEdition (..) +import Model.Edition.Edition (..) +import Model.Edition.NameEdition (..) +import Model.Edition.TimeEdition (..) import Model.Id (..) import Model.IdGenerator (..) import Update.UpdateTimer (..) -import Update.UpdateTimerEdition (..) +import Update.UpdateEdition (..) import Utils.Maybe (..) @@ -32,9 +34,9 @@ type Action = | DeltaTime Time | UpdateTimer Id TimerAction | RemoveTimer Id - | EditTimer Id - | ValidTimerEdition - | ReadOnly + | Edit Id Kind + | ValidEdition + | ClickAway | KeyPressed KeyCode updates : Signal.Channel Action @@ -54,9 +56,8 @@ update action model = initialModel model.currentTime AddNewTimer -> let (id, newTimerIdGenerator) = getId model.timerIdGenerator - timerName = "Timer " ++ (toString id) in { model - | timers <- Dict.insert id (initialTimer model.currentTime timerName) model.timers + | timers <- Dict.insert id (initialTimer model.currentTime) model.timers , timerIdGenerator <- newTimerIdGenerator } DeltaTime delta -> @@ -65,14 +66,12 @@ update action model = , timers <- Dict.map (\id timer -> updateTimer (SubstractTime delta) timer) model.timers } UpdateTimer id action -> - let maybeTimerEdition = filterMaybe (\timerEdition -> timerEdition.id == id) model.timerEdition - in case maybeTimerEdition of - Just timerEdition -> + let maybeEdition = filterMaybe (\edition -> edition.id == id) model.edition + in case maybeEdition of + Just edition -> { model - | timers <- - updateTimerTime timerEdition model.timers - |> Dict.update id (Maybe.map (updateTimer action)) - , timerEdition <- Nothing + | timers <- Dict.update id (Maybe.map (updateTimer action)) model.timers + , edition <- Nothing } Nothing -> { model | timers <- Dict.update id (Maybe.map (updateTimer action)) model.timers } @@ -82,36 +81,42 @@ update action model = { model | timers <- Dict.remove id model.timers } else model - EditTimer id -> + Edit id kind -> { model - | timerEdition <- Just (newTimerEdition id) + | edition <- Just (newEdition id kind) , timers <- Dict.update id (Maybe.map (updateTimer Pause)) model.timers } - ValidTimerEdition -> - case model.timerEdition of - Just timerEdition -> - { model - | timers <- updateTimerTime timerEdition model.timers - , timerEdition <- Nothing - } - Nothing -> - { model | timerEdition <- Nothing } - ReadOnly -> - { model | timerEdition <- Nothing } + ValidEdition -> + validEdition model + ClickAway -> + { model | edition <- Nothing } KeyPressed keyCode -> if isRemoveKeyCode keyCode then - { model | timerEdition <- Maybe.map (updateTimerEdition DeleteLast) model.timerEdition } + { model | edition <- Maybe.map (updateEdition DeleteLast) model.edition } else - { model | timerEdition <- Maybe.map (updateTimerEdition (AddNumber keyCode)) model.timerEdition } + { model | edition <- Maybe.map (updateEdition (AddChar keyCode)) model.edition } -updateTimerTime : TimerEdition -> Dict Id Timer -> Dict Id Timer -updateTimerTime timerEdition = - if List.isEmpty timerEdition.numbers - then - identity - else - Dict.update timerEdition.id (Maybe.map (updateTimer (SetTime (toTime timerEdition.numbers)))) +validEdition : Model -> Model +validEdition model = + case model.edition of + Just edition -> + if List.isEmpty edition.chars + then + model + else + let action = + case edition.kind of + Name -> + Rename (renderNameEdition edition.chars) + Time -> + SetTime (toTime edition.chars) + in { model + | timers <- Dict.update edition.id (Maybe.map (updateTimer action)) model.timers + , edition <- Nothing + } + Nothing -> + model isRemoveKeyCode : KeyCode -> Bool isRemoveKeyCode = (==) 8 |