-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.elm
335 lines (239 loc) · 7.56 KB
/
Main.elm
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
port module Main exposing (main)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick, onInput)
import Time exposing (Time, every, second)
import Task
main =
program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
port playSound : String -> Cmd msg
port soundEnded : (String -> msg) -> Sub msg
-- MODEL ###################################################
type alias Model =
{ page : Page
, totalNbOfParticipants : Int
, remainingNbOfParticipants : Int
, totalTime : Time
, startTime : Maybe Time
, remainingTime : Time
}
type Page
= PageSetup
| PageReady
| PageRunning
init : ( Model, Cmd Msg )
init =
( Model PageSetup 0 0 0 Nothing 0
, Cmd.none
)
-- UPDATE ##################################################
type Msg
= Reset
| SetupMsg SetupMsg
| ReadyMsg ReadyMsg
| RunningMsg RunningMsg
| EndSound String
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case ( msg, model.startTime ) of
( Reset, _ ) ->
init
( SetupMsg setupMsg, _ ) ->
updateSetup setupMsg model
( ReadyMsg readyMsg, _ ) ->
updateReady readyMsg model
( RunningMsg runningMsg, Just startTime ) ->
updateRunning startTime runningMsg model
( EndSound endMsg, _ ) ->
let
_ =
Debug.log "Timer ended" endMsg
in
( model, Cmd.none )
_ ->
( model, Cmd.none )
-- UPDATE SETUP
type SetupMsg
= ChangeNumberOfParticipants (Result String Int)
| ChangeTotalTime (Result String Time)
| SetupReady
updateSetup : SetupMsg -> Model -> ( Model, Cmd Msg )
updateSetup msg model =
case msg of
ChangeNumberOfParticipants (Ok n) ->
( { model | totalNbOfParticipants = n }
, Cmd.none
)
ChangeTotalTime (Ok time) ->
( { model | totalTime = time }, Cmd.none )
SetupReady ->
( { model | page = PageReady }, Cmd.none )
_ ->
( model, Cmd.none )
-- UPDATE READY
type ReadyMsg
= Start
| StartedAt Time
updateReady : ReadyMsg -> Model -> ( Model, Cmd Msg )
updateReady msg model =
case msg of
Start ->
( { model | remainingNbOfParticipants = model.totalNbOfParticipants }
, Task.perform (ReadyMsg << StartedAt) Time.now
)
StartedAt time ->
( { model | page = PageRunning, startTime = Just time }
, Cmd.none
)
-- UPDATE RUNNING
type RunningMsg
= Tick Time
updateRunning : Time -> RunningMsg -> Model -> ( Model, Cmd Msg )
updateRunning startTime msg model =
case msg of
Tick time ->
let
( timePerParticipant, remainingTime ) =
( model.totalTime / toFloat model.totalNbOfParticipants
, model.totalTime - (time - startTime)
)
remainingNbOfParticipants =
ceiling (remainingTime / timePerParticipant)
|> clamp 0 model.totalNbOfParticipants
in
( { model
| remainingNbOfParticipants = remainingNbOfParticipants
, remainingTime = remainingTime
}
, if remainingNbOfParticipants == 0 then
commandWhenRunningEnd
else if remainingNbOfParticipants < model.remainingNbOfParticipants then
commandWhenParticipantChange
else
Cmd.none
)
{-| Replace this by whatever we want to do when the participant changes
-}
commandWhenParticipantChange : Cmd Msg
commandWhenParticipantChange =
playSound "bell_ring"
{-| Replace this by whatever we want to do when the timer end
-}
commandWhenRunningEnd : Cmd Msg
commandWhenRunningEnd =
Cmd.none
-- SUBSCRIPTIONS ###########################################
subscriptions : Model -> Sub Msg
subscriptions model =
let
tSub =
if model.page == PageRunning && model.remainingNbOfParticipants > 0 then
every (second / 10) (RunningMsg << Tick)
else
Sub.none
in
Sub.batch [ tSub, soundEnded EndSound ]
-- VIEW ####################################################
view : Model -> Html Msg
view model =
case model.page of
PageSetup ->
viewPage (pageSetupContent model)
PageReady ->
viewPage (pageReadyContent model)
PageRunning ->
viewPage (pageRunningContent model)
viewPage : Html Msg -> Html Msg
viewPage pageContent =
div [ id "timer-component" ]
[ viewHeader
, pageContent
]
viewHeader : Html Msg
viewHeader =
div []
[ h1 [] [ text "Elm Singapore" ]
, h2 [] [ text "- TIMER -" ]
]
-- VIEW SETUP
pageSetupContent : Model -> Html Msg
pageSetupContent model =
div []
[ p []
[ label [ for "nb-participants" ] [ text "Number of participants" ]
, input
[ id "nb-participants"
, type_ "number"
, onInput (SetupMsg << ChangeNumberOfParticipants << String.toInt)
]
[]
]
, p []
[ label [ for "total-time" ] [ text "Total time" ]
, input
[ id "total-time"
, type_ "number"
, onInput (String.toFloat >> Result.map ((*) second) >> ChangeTotalTime >> SetupMsg)
]
[]
]
, p [] [ button [ onClick (SetupMsg SetupReady) ] [ text "Ready" ] ]
]
-- VIEW READY
pageReadyContent : Model -> Html Msg
pageReadyContent model =
let
timePerParticipant =
(model.totalTime / toFloat model.totalNbOfParticipants)
|> Time.inSeconds
|> round
in
div []
[ p [] [ text <| toString timePerParticipant ++ " seconds per participant" ]
, p []
[ button [ onClick Reset ] [ text "Reset" ]
, button [ onClick (ReadyMsg Start) ] [ text "Start" ]
]
]
-- VIEW RUNNING
pageRunningContent : Model -> Html Msg
pageRunningContent model =
let
timePerParticipant =
model.totalTime / toFloat model.totalNbOfParticipants
timeForOtherParticipants =
timePerParticipant * toFloat (model.remainingNbOfParticipants - 1)
participantRemainingTime =
model.remainingTime - timeForOtherParticipants
in
if model.remainingNbOfParticipants == 0 then
div []
[ p [] [ text "Time's up" ]
, p [] [ button [ onClick Reset ] [ text "Reset" ] ]
]
else
div []
[ p [] [ text (formatTime participantRemainingTime) ]
, p [] [ text (toString model.remainingNbOfParticipants ++ " participants left") ]
, p [] [ button [ onClick Reset ] [ text "Reset" ] ]
]
formatTime : Time -> String
formatTime time =
let
minutes =
floor <| Time.inMinutes time
seconds =
floor <| Time.inSeconds (time - (toFloat minutes) * Time.minute)
in
format2 minutes ++ " : " ++ format2 seconds
format2 : Int -> String
format2 n =
if n >= 10 then
toString n
else
"0" ++ toString n