-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.elm
424 lines (308 loc) · 8.67 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
module Main exposing (..)
import Html exposing (..)
import Html.App as App
import Html.Attributes exposing (class, src, href)
import Html.Events exposing (onClick)
import Time exposing (Time, second)
import String
-- Model
type Status
= Relax
| Focus
type Mode
= Elapsed
| Remaining
type alias Model =
{ counting : Bool
, timerStatus : Status
, timerMode : Mode
, seconds : Int
, pomsCompleted : Int
, chilloutMode : Bool
}
-- Number of seconds in chillout mode
chilloutLimit : Int
chilloutLimit =
20 * 60
-- Number of seconds for a normal relax mode
relaxLimit : Int
relaxLimit =
5 * 60
-- Number of seconds for a normal focus mode
focusLimit : Int
focusLimit =
25 * 60
-- Update
type Msg
= Tick Time
| Start
| Pause
| Clear
| ElapsedMode
| RemainingMode
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
-- I don't care about the time, I just want the tick
Start ->
( model
|> startCounting
, Cmd.none
)
Pause ->
( model
|> stopCounting
, Cmd.none
)
Clear ->
( { model | timerStatus = Focus }
|> stopCounting
|> zeroClock
|> resetPomsCompleted
, Cmd.none
)
ElapsedMode ->
( { model | timerMode = Elapsed }, Cmd.none )
RemainingMode ->
( { model | timerMode = Remaining }, Cmd.none )
Tick _ ->
case
( model.counting
, model.timerStatus
, (model.seconds == focusLimit)
, (model.seconds == relaxLimit)
, (model.seconds == chilloutLimit)
, model.chilloutMode
)
of
-- Not counting, so do nothing
( False, _, _, _, _, _ ) ->
( model, Cmd.none )
-- Counting and the clock has struck 25 minutes in Focus
( True, Focus, True, _, _, _ ) ->
( model
|> flipStatus
|> zeroClock
, Cmd.none
)
-- Counting and clock has struck 5 minutes in Relax
( True, Relax, _, True, _, False ) ->
( model
|> flipStatus
|> zeroClock
|> markPomsCompleted
, Cmd.none
)
-- Exit chilloutMode
( True, Relax, _, _, True, True ) ->
( model
|> flipStatus
|> zeroClock
|> markPomsCompleted
|> disengageChilloutMode
, Cmd.none
)
-- Ordinary counting
( True, _, _, _, _, _ ) ->
( model
|> tickSecond model.seconds
, Cmd.none
)
disengageChilloutMode : Model -> Model
disengageChilloutMode model =
{ model | chilloutMode = False }
resetPomsCompleted : Model -> Model
resetPomsCompleted model =
{ model | pomsCompleted = 0 }
stopCounting : Model -> Model
stopCounting model =
{ model | counting = False }
startCounting : Model -> Model
startCounting model =
{ model | counting = True }
tickSecond : Int -> Model -> Model
tickSecond s model =
{ model | seconds = s + 1 }
flipStatus : Model -> Model
flipStatus model =
case
( model.timerStatus
, (rem model.pomsCompleted 4 == 0)
&& (model.pomsCompleted > 0)
)
of
( Focus, False ) ->
{ model | timerStatus = Relax }
( Focus, True ) ->
{ model
| timerStatus = Relax
, chilloutMode = True
}
( Relax, _ ) ->
{ model
| timerStatus = Focus
, chilloutMode = False
}
zeroClock : Model -> Model
zeroClock model =
{ model | seconds = 0 }
markPomsCompleted : Model -> Model
markPomsCompleted model =
{ model | pomsCompleted = model.pomsCompleted + 1 }
-- View
view : Model -> Html Msg
view model =
div [ class "appWindow" ]
[ makeHeader
, p [ class "counter" ]
[ text <| "Routines Completed: " ++ toString model.pomsCompleted ]
, makeMainPage model
--- Purely for sanity checking will be removed later
, makeFooter
]
makeMainPage : Model -> Html Msg
makeMainPage model =
div []
[ makeClock model
, makeButtonCluster
]
makeButtonCluster : Html Msg
makeButtonCluster =
div [ class "btncluster" ]
[ button [ onClick Start ] [ text "Start" ]
, button [ onClick Pause ] [ text "Pause" ]
, button [ onClick Clear ] [ text "Clear" ]
]
makeClock : Model -> Html Msg
makeClock model =
div []
[ div [ bezelChecker model.timerStatus ]
[ div [ statusChecker model.timerStatus ]
[ text <| toString model.timerStatus
]
, div [ gaugeChecker model.timerStatus ]
[ text <| timeMaker model
]
, bezelButtonMaker "Elapsed" ElapsedMode model
, bezelButtonMaker "Remaining" RemainingMode model
]
]
timeMaker : Model -> String
timeMaker model =
case
( model.timerMode
, model.timerStatus
, model.chilloutMode
, model.seconds
)
of
( Elapsed, _, _, s ) ->
getClockString s
( Remaining, Relax, False, s ) ->
getClockString <| (relaxLimit - s)
( Remaining, Relax, True, s ) ->
getClockString <| (chilloutLimit - s)
( Remaining, Focus, _, s ) ->
getClockString <| (focusLimit - s)
getClockString : Int -> String
getClockString sec =
let
formatter x =
if (String.length <| toString x) == 1 then
"0" ++ toString x
else
toString x
madeMinutes =
sec // 60
madeSeconds =
rem sec 60
in
formatter madeMinutes ++ " : " ++ formatter madeSeconds
bezelChecker : Status -> Html.Attribute Msg
bezelChecker status =
case status of
Relax ->
class "bezelrelax"
Focus ->
class "bezelfocus"
statusChecker : Status -> Html.Attribute Msg
statusChecker status =
case status of
Relax ->
class "statusrelax"
Focus ->
class "statusfocus"
gaugeChecker : Status -> Html.Attribute Msg
gaugeChecker status =
case status of
Relax ->
class "gaugerelax"
Focus ->
class "gaugefocus"
bezelButtonMaker : String -> Msg -> Model -> Html Msg
bezelButtonMaker btnName msg model =
button
[ onClick msg, getBezelBtnClass btnName model ]
[ text btnName ]
getBezelBtnClass : String -> Model -> Html.Attribute Msg
getBezelBtnClass btnName model =
if btnName == (toString model.timerMode) then
class "activebezelbtn"
else
class "inactivebezelbtn"
makeHeader : Html Msg
makeHeader =
header [ class "full-width-bar" ]
[ div []
[ h2 [] [ text "Focus Optimizer" ]
]
]
makeFooter : Html Msg
makeFooter =
footer [ class "full-width-bar-bottom" ]
[ div [ class "right" ]
[ div [] [ linkMaker ]
]
, div []
[ img [ src "static/Signature.png", class "center-cropped" ] [] ]
]
linkMaker : Html Msg
linkMaker =
ul [] <| List.map linkRenderer getLinks
getLinks : List ( String, String )
getLinks =
[ ( "Medium", "https://medium.com/@NewMountain" )
, ( "Twitter", "https://twitter.com/@nyberg_c" )
, ( "GitHub", "https://github.com/NewMountain" )
]
linkRenderer : ( String, String ) -> Html Msg
linkRenderer ( name, url' ) =
li []
[ a [ href url' ] [ text name ]
]
-- Init
init : ( Model, Cmd Msg )
init =
( { counting = False
, timerStatus = Focus
, timerMode = Elapsed
, seconds = 0
, pomsCompleted = 0
, chilloutMode = False
}
, Cmd.none
)
-- Subscription
-- Trust me on this one (we can make MUVIS instead!!!)
subscriptions : Model -> Sub Msg
subscriptions model =
Time.every second Tick
-- Main
main : Program Never
main =
App.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}