aboutsummaryrefslogtreecommitdiff
path: root/src/Update
diff options
context:
space:
mode:
Diffstat (limited to 'src/Update')
-rw-r--r--src/Update/Update.elm77
-rw-r--r--src/Update/UpdateEdition.elm30
-rw-r--r--src/Update/UpdateTimer.elm5
-rw-r--r--src/Update/UpdateTimerEdition.elm34
4 files changed, 75 insertions, 71 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
diff --git a/src/Update/UpdateEdition.elm b/src/Update/UpdateEdition.elm
new file mode 100644
index 0000000..911a4c1
--- /dev/null
+++ b/src/Update/UpdateEdition.elm
@@ -0,0 +1,30 @@
+module Update.UpdateEdition
+ ( updateEdition
+ , EditionAction(..)
+ ) where
+
+import Char (..)
+
+import Model.Edition.Edition (..)
+
+import Utils.List (..)
+
+type EditionAction =
+ DeleteLast
+ | AddChar KeyCode
+
+updateEdition : EditionAction -> Edition -> Edition
+updateEdition action edition =
+ case action of
+ DeleteLast ->
+ case maybeTail edition.chars of
+ Just tailChars ->
+ { edition | chars <- tailChars }
+ Nothing ->
+ edition
+ AddChar keyCode ->
+ case keyCodeToChar edition.kind keyCode of
+ Just char ->
+ { edition | chars <- char :: edition.chars }
+ Nothing ->
+ edition
diff --git a/src/Update/UpdateTimer.elm b/src/Update/UpdateTimer.elm
index 2e0d9af..30603cd 100644
--- a/src/Update/UpdateTimer.elm
+++ b/src/Update/UpdateTimer.elm
@@ -10,7 +10,8 @@ import Model.TimerState (..)
import Model.Id (..)
type TimerAction =
- Pause
+ Rename String
+ | Pause
| ToggleRunning
| Stop
| SetTime Time
@@ -19,6 +20,8 @@ type TimerAction =
updateTimer : TimerAction -> Timer -> Timer
updateTimer action timer =
case action of
+ Rename name ->
+ { timer | name <- Just name }
Pause ->
{ timer | state <- Idle }
ToggleRunning ->
diff --git a/src/Update/UpdateTimerEdition.elm b/src/Update/UpdateTimerEdition.elm
deleted file mode 100644
index e9493eb..0000000
--- a/src/Update/UpdateTimerEdition.elm
+++ /dev/null
@@ -1,34 +0,0 @@
-module Update.UpdateTimerEdition
- ( updateTimerEdition
- , TimerEditionAction(..)
- ) where
-
-import Char (..)
-
-import Model.TimerEdition (..)
-
-import Utils.List (..)
-
-type TimerEditionAction =
- DeleteLast
- | AddNumber KeyCode
-
-updateTimerEdition : TimerEditionAction -> TimerEdition -> TimerEdition
-updateTimerEdition action timerEdition =
- case action of
- DeleteLast ->
- case maybeTail timerEdition.numbers of
- Just tailNumbers ->
- { timerEdition | numbers <- tailNumbers }
- Nothing ->
- timerEdition
- AddNumber keyCode ->
- case keyCodeToNumberChar keyCode of
- Just char ->
- if isDigit char
- then
- { timerEdition | numbers <- char :: timerEdition.numbers }
- else
- timerEdition
- Nothing ->
- timerEdition