forked from mariellefoster/lightsout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.elm
107 lines (71 loc) · 1.73 KB
/
Timer.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
module Timer exposing (..)
import Time exposing (Time, second)
import Html
import Html exposing (Html, text)
import Task
import Debug
import String
main =
Html.program
{ init = init -----values----
, update = update
, view = view
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
{ startTime : Time
, currentTime : Time
}
elapsedTime : Model -> Time
elapsedTime { startTime, currentTime } =
currentTime - startTime
init : ( Model, Cmd Msg )
init =
( { startTime = 0, currentTime = 0 }, getStartTime )
getStartTime : Cmd Msg
getStartTime =
Task.perform StartTime Time.now
-- UPDATE
type Msg
= Tick Time
| StartTime Time
update : Msg -> Model -> ( Model, Cmd Msg )
update message ({ startTime, currentTime } as model) =
let
newModel =
case message of
Tick newTime ->
{ model | currentTime = newTime }
StartTime startTime ->
{ startTime = startTime, currentTime = startTime }
in
( newModel, Cmd.none )
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Time.every second Tick
-- VIEW
niceTimeDisplay : Time -> String
niceTimeDisplay time =
let
seconds =
time
|> Time.inSeconds
|> round
|> (\x -> x % 60)
|> toString
|> String.padLeft 2 '0'
minutes =
time
|> Time.inMinutes
|> floor
|> toString
in
minutes ++ ":" ++ seconds
view : Model -> Html Msg
view model =
model
|> elapsedTime
|> niceTimeDisplay
|> text