blob: 3bdcc2bb7a0a3337de4c6f82deee336dd082512d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
module Update.Update
( Action(..)
, updates
, update
) where
import Signal
import Dict
import Dict (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 (..)
import Utils.Maybe (..)
type Action =
NoOp
| Initialize
| AddNewTimer
| DeltaTime Time
| UpdateTimer Id TimerAction
| RemoveTimer Id
| EditTimer Id
| ValidTimerEdition
| ReadOnly
| KeyPressed KeyCode
updates : Signal.Channel Action
updates = Signal.channel NoOp
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)
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 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 }
EditTimer id ->
{ model
| timerEdition <- Just (newTimerEdition id)
, 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 }
KeyPressed keyCode ->
{ model | timerEdition <- Maybe.map (updateTimerEdition (keyCodeToChar keyCode)) model.timerEdition }
updateTimerTime : TimerEdition -> Dict Id Timer -> Dict Id Timer
updateTimerTime timerEdition =
Dict.update timerEdition.id (Maybe.map (updateTimer (SetTime (toTime timerEdition.numbers))))
|