aboutsummaryrefslogtreecommitdiff
path: root/src/Timer/View.elm
blob: 662173a8ad99b8d5dd0052737e4c5d1c6aab64d2 (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
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
module Timer.View exposing
  ( view
  )

import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Time exposing (Time)
import Maybe
import List
import String

import Msg exposing (Msg)

import Model exposing (..)
import Model.Id exposing (..)

import Timer.Model exposing (..)
import Timer.Model.State exposing (..)
import Timer.Update exposing (..)
import Timer.Msg as Timer

import Edition.Model exposing (..)
import Edition.Model.Name exposing (..)
import Edition.Model.Time exposing (..)

import Update exposing (..)

import Utils.Maybe exposing (..)

view : Model -> (Id, Timer) -> Html Msg
view model (id, timer) =
  div
    [ [ ("timer", True)
      , ("isRinging", timer.state == Ringing)
      , ("isRunning", timer.state == Running)
      ]
        |> classList
    ]
    [ renderMaybeEdition model id Name (nameBlockReadOnly id timer) (nameBlockEdition id timer)
    , renderMaybeEdition model id Time (timeBlockReadOnly (id, timer)) (timeBlockEdition (id, timer))
    , playPauseTimer (id, timer)
    , stopTimer (id, timer)
    , removeTimer (id, timer)
    ]

nameBlockReadOnly : Id -> Timer -> Html Msg
nameBlockReadOnly id timer =
  div
    [ class "name"
    , onClick (Msg.Edit id Name)
    ]
    [ text (timerName id timer) ]

nameBlockEdition : Id -> Timer -> Edition -> Html Msg
nameBlockEdition id timer edition =
  div
    [ [ ("name", True)
      , ("edition", True)
      , ("empty", isEmpty edition)
      ]
        |> classList
    ]
    [ if isEmpty edition
        then
          text (timerName id timer)
        else
          edition.chars
            |> renderNameEdition
            |> flip String.append "_"
            |> text
    ]

timerName : Id -> Timer -> String
timerName id = Maybe.withDefault ("Timer " ++ toString id) << .name

timeBlockReadOnly : (Id, Timer) -> Html Msg
timeBlockReadOnly (id, timer) =
  div
    [ class "time"
    ]
    [ timeWithProgressBar (id, timer) ]

timeBlockEdition : (Id, Timer) -> Edition -> Html Msg
timeBlockEdition (id, timer) edition =
  div
    [ [ ("time", True)
      , ("edition", True)
      , ("empty", isEmpty edition)
      ]
        |> classList
    ]
    [ if isEmpty edition
        then
          timeWithProgressBar (id, timer)
        else
          text (editionView edition.chars)
    ]

editionView : List Char -> String
editionView numbers =
  let (minutes, seconds) = toMinutesAndSeconds numbers
  in  minutes ++ " : " ++ seconds

timeView : Time -> String
timeView time =
  let totalSeconds = ceiling (time / 1000)
      totalMinutes = totalSeconds // 60
      restSeconds = totalSeconds `rem` 60
  in  (String.padLeft 2 '0' (toString totalMinutes)) ++ " : " ++ (String.padLeft 2 '0' (toString restSeconds))

timeWithProgressBar : (Id, Timer) -> Html Msg
timeWithProgressBar (id, timer) =
  div
    []
    [ span
        [ class "progressBar"
        , let width =
                1 - (timer.time / timer.initialTime)
                  |> (*) 100
                  |> toString
                  |> flip String.append "%"
          in style [ ("width", width) ]
        , onClick (if timer.state == Ringing then Msg.UpdateTimer id Timer.Stop else Msg.NoOp)
        ]
        []
    , span
        [ class "text"
        , onClick (Msg.Edit id Time)
        ]
        [ text (timeView timer.time) ]
    ]

playPauseTimer : (Id, Timer) -> Html Msg
playPauseTimer (id, timer) =
  button
    [ class <| "playPause"
    , onClick (Msg.UpdateTimer id Timer.ToggleRunning)
    ]
    [ let icon = if timer.state == Running then "fa-pause" else "fa-play"
      in  i
            [ class <| "fa fa-fw " ++ icon ]
            []
    ]

stopTimer : (Id, Timer) -> Html Msg
stopTimer (id, timer) =
  button
    [ class <| "stop"
    , onClick (Msg.UpdateTimer id Timer.Stop)
    ]
    [ i [ class "fa fa-fw fa-stop" ] [] ]

removeTimer : (Id, Timer) -> Html Msg
removeTimer (id, timer) =
  button
    [ class <| "remove"
    , onClick (Msg.RemoveTimer id)
    ]
    [ i [ class "fa fa-fw fa-remove" ] [] ]

renderMaybeEdition : Model -> Id -> Kind -> Html Msg -> (Edition -> Html Msg) -> Html Msg
renderMaybeEdition model id kind readOnlyView editionView =
  let maybeEdition = filterMaybe (\edition -> edition.id == id) model.edition
  in  case maybeEdition of
        Just edition ->
          if edition.kind == kind
            then
              editionView edition
            else
              readOnlyView
        Nothing ->
          readOnlyView