-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCounter.elm
121 lines (93 loc) · 2.34 KB
/
Counter.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
module Main exposing (..)
import VirtualDom.Element
exposing
( Element
, Attribute
, column
, row
, button
, label
, map
, beginnerProgram
)
import VirtualDom.Attributes
exposing
( text
, textColor
, flexGrow
, justifyContent
, alignItems
, marginHorizontal
, marginBottom
)
import VirtualDom.Events exposing (onTouchUpInside)
import VirtualDom.Lazy exposing (lazy)
import Color exposing (Color)
main : Program Never Model Msg
main =
beginnerProgram
{ model = 0
, view = lazy view
, update = update
}
-- MODEL
type alias Model =
Int
-- UPDATE
type Msg
= Increment
| Decrement
update : Msg -> Model -> Model
update msg model =
case msg of
Increment ->
model + 1
Decrement ->
model - 1
-- VIEW
view : Model -> Element Msg
view model =
mapIfOdd model <|
column [ flexGrow 1, justifyContent "center", alignItems "center" ]
[ label <| maybeCons (labelColor model) [ text <| toString model, marginBottom 25 ]
, row []
[ lazy
(\_ ->
button
[ text "Increment"
, textColor Color.green
, onTouchUpInside Increment
, marginHorizontal 20
]
)
model
, map (always Decrement) <|
button
[ text "Decrement"
, textColor Color.red
, onTouchUpInside "foo"
, marginHorizontal 20
]
]
]
labelColor : Model -> Maybe (Attribute msg)
labelColor model =
if model >= 10 then
Just <| textColor Color.green
else if model <= -10 then
Just <| textColor Color.red
else
Nothing
maybeCons : Maybe a -> List a -> List a
maybeCons maybe list =
case maybe of
Just x ->
x :: list
Nothing ->
list
mapIfOdd : Model -> (Element msg -> Element msg)
mapIfOdd model =
if model % 2 == 1 then
map identity
else
identity