module Update.Update ( Action(..) , updates , update ) where import Signal import Dict import Time (Time) import Maybe import Keyboard (KeyCode) import Char import Model.Model (..) import Model.Timer (..) import Model.TimerEdition (..) import Model.Id (..) import Model.IdGenerator (..) import Update.UpdateTimer (..) import Update.UpdateTimerEdition (..) type Action = NoOp | AddNewTimer | DeltaTime Time | UpdateTimer Id TimerAction | RemoveTimer Id | EditTimer Id | ReadOnly | KeyPressed KeyCode updates : Signal.Channel Action updates = Signal.channel NoOp update : Action -> Model -> Model update action model = case action of NoOp -> model AddNewTimer -> let (id, newTimerIdGenerator) = getId model.timerIdGenerator timerName = "Timer " ++ (toString id) in { model | timers <- Dict.insert id (initialTimer model.currentTime timerName) model.timers , timerIdGenerator <- newTimerIdGenerator } DeltaTime delta -> { model | currentTime <- model.currentTime + delta , 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 { model | timers <- Dict.update id (Maybe.map (updateTimer action)) model.timers } RemoveTimer id -> { model | timers <- Dict.remove id model.timers } EditTimer id -> { model | timerEdition <- Just (newTimerEdition id) , timers <- Dict.update id (Maybe.map (updateTimer Pause)) model.timers } ReadOnly -> { model | timerEdition <- Nothing } KeyPressed keyCode -> if isEnter keyCode then case model.timerEdition of Just timerEdition -> { model | timers <- Dict.update timerEdition.id (Maybe.map (updateTimer (SetTime (toTime timerEdition.numbers)))) model.timers , timerEdition <- Nothing } Nothing -> { model | timerEdition <- Nothing } else { model | timerEdition <- Maybe.map (updateTimerEdition (keyCodeToChar keyCode)) model.timerEdition } isEnter : KeyCode -> Bool isEnter = (==) 107