aboutsummaryrefslogtreecommitdiff
path: root/src/Update.elm
diff options
context:
space:
mode:
authorJoris2016-09-04 21:21:11 +0200
committerJoris2016-09-04 21:21:31 +0200
commit973a039b54327df74396605410ea9abe19c8a4e7 (patch)
treec702564d17e0a490d56845027238eb4f231be785 /src/Update.elm
parent62fee9133f36f655c1ed83e0c2e85394f9948bf5 (diff)
Upgrade to elm 0.17.1
Diffstat (limited to 'src/Update.elm')
-rw-r--r--src/Update.elm141
1 files changed, 141 insertions, 0 deletions
diff --git a/src/Update.elm b/src/Update.elm
new file mode 100644
index 0000000..42eb682
--- /dev/null
+++ b/src/Update.elm
@@ -0,0 +1,141 @@
+module Update exposing
+ ( update
+ )
+
+import Dict
+
+import Msg exposing (Msg)
+
+import Ring
+
+import Model exposing (Model)
+import Model.Id exposing (..)
+import Model.IdGenerator exposing (..)
+import Model.Keyboard exposing (KeyCode)
+
+import Timer.Model as Timer exposing (..)
+import Timer.Model.State as TimerState
+import Timer.Update exposing (..)
+import Timer.Msg as Timer
+
+import Edition.Model exposing (..)
+import Edition.Model.Name exposing (..)
+import Edition.Model.Time exposing (..)
+import Edition.Update exposing (..)
+import Edition.Msg as Edition
+
+import Utils.Maybe exposing (..)
+
+update : Msg -> Model -> (Model, Cmd Msg)
+update msg model =
+ case msg of
+
+ Msg.NoOp ->
+ (model, Cmd.none)
+
+ Msg.Initialize ->
+ Model.init model.time
+
+ Msg.AddNewTimer ->
+ let (id, newTimerIdGenerator) = getId model.timerIdGenerator
+ in ( { model
+ | timers = Dict.insert id (Timer.init model.time) model.timers
+ , timerIdGenerator = newTimerIdGenerator
+ }
+ , Cmd.none
+ )
+
+ Msg.Time time ->
+ let delta = time - model.time
+ newModel =
+ { model
+ | time = time
+ , timers = Dict.map (\id timer -> updateTimer (Timer.SubstractTime delta) timer) model.timers
+ }
+ ringing =
+ newModel.timers
+ |> Dict.values
+ |> List.map .state
+ |> List.member TimerState.Ringing
+ in ( newModel
+ , Ring.ring ringing
+ )
+
+ Msg.UpdateTimer id timerMsg ->
+ let maybeEdition = filterMaybe (\edition -> edition.id == id) model.edition
+ newModel =
+ case maybeEdition of
+ Just edition ->
+ if edition.kind == Time then validEdition model else model
+ Nothing ->
+ model
+ in ( { newModel | timers = Dict.update id (Maybe.map (updateTimer timerMsg)) newModel.timers }
+ , Cmd.none
+ )
+
+ Msg.RemoveTimer id ->
+ if Model.numberOfTimers model > 1
+ then
+ ( { model | timers = Dict.remove id model.timers }
+ , Cmd.none
+ )
+ else
+ (model, Cmd.none)
+
+ Msg.Edit id kind ->
+ ( { model
+ | edition = Just (newEdition id kind)
+ , timers =
+ if kind == Time
+ then Dict.update id (Maybe.map (updateTimer Timer.Pause)) model.timers
+ else model.timers
+ }
+ , Cmd.none
+ )
+
+ Msg.ClickAway ->
+ ( { model | edition = Nothing }
+ , Cmd.none
+ )
+
+ Msg.KeyPressed keyCode ->
+ if isEnterKeyCode keyCode
+ then
+ (validEdition model, Cmd.none)
+ else
+ let editionAction =
+ if isRemoveKeyCode keyCode
+ then Edition.DeleteLast
+ else Edition.AddChar keyCode
+ in ( { model | edition = Maybe.map (updateEdition editionAction) model.edition }
+ , Cmd.none
+ )
+
+validEdition : Model -> Model
+validEdition model =
+ case model.edition of
+ Just edition ->
+ if isEmpty edition
+ then
+ { model
+ | edition = Nothing
+ }
+ else
+ let timerMsg =
+ case edition.kind of
+ Name ->
+ Timer.Rename (renderNameEdition edition.chars)
+ Time ->
+ Timer.SetTime (toTime edition.chars)
+ in { model
+ | timers = Dict.update edition.id (Maybe.map (updateTimer timerMsg)) model.timers
+ , edition = Nothing
+ }
+ Nothing ->
+ model
+
+isEnterKeyCode : KeyCode -> Bool
+isEnterKeyCode = (==) 13
+
+isRemoveKeyCode : KeyCode -> Bool
+isRemoveKeyCode = (==) 8