aboutsummaryrefslogtreecommitdiff
path: root/src/View/Timer.elm
blob: 4523334e825273609bb18d0306a2cc5d68ba8d47 (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
module View.Timer
  ( timerView
  ) where

import Html (..)
import Html.Attributes (..)
import Html.Events (..)
import String
import Time (Time)
import Signal
import Maybe

import Model.Model (..)
import Model.Timer (..)
import Model.TimerEdition (..)
import Model.TimerState (..)
import Model.Id (..)

import Update.Update (..)
import Update.UpdateTimer (..)

import View.ActivatedClasses (..)

import Utils.Maybe (..)

timerView : Model -> (Id, Timer) -> Html
timerView model (id, timer) =
  div
    [ [ (True, "timer")
      , (timer.state == Running, "isRunning")
      , (timer.state == Ringing, "isRinging")
      ]
        |> activatedClasses
    ]
    [ nameBlock (id, timer)
    , timeBlock model (id, timer)
    , playPauseBlock (id, timer)
    , stopBlock (id, timer)
    , removeBlock (id, timer)
    ]

nameBlock : (Id, Timer) -> Html
nameBlock (id, timer) =
  button
    [ class "name block"
    , onClick (stopIfRinging (id, timer) (Signal.send updates NoOp))
    ]
    [ text timer.name ]

timeBlock : Model -> (Id, Timer) -> Html
timeBlock model (id, timer) =
  let maybeEdition = filterMaybe (\te -> te.id == id) model.timerEdition
  in  case maybeEdition of
        Just edition ->
          button
            [ class "time block edition"
            , onClick (stopIfRinging (id, timer) (Signal.send updates ValidTimerEdition))
            ]
            [ text (editionView edition.numbers) ]
        Nothing ->
          button
            [ class "time block"
            , onClick (stopIfRinging (id, timer) (Signal.send updates (EditTimer id)))
            ]
            [ text (timeView timer.currentTime) ]

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

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

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

stopBlock : (Id, Timer) -> Html
stopBlock (id, timer) =
  button
    [ class <| "stop block"
    , onClick (stopIfRinging (id, timer) (Signal.send updates (UpdateTimer id Stop)))
    ]
    [ i [ class "fa fa-fw fa-stop" ] [] ]

removeBlock : (Id, Timer) -> Html
removeBlock (id, timer) =
  button
    [ class <| "remove block"
    , onClick (Signal.send updates (RemoveTimer id))
    ]
    [ i [ class "fa fa-fw fa-remove" ] [] ]

stopIfRinging : (Id, Timer) -> Signal.Message -> Signal.Message
stopIfRinging (id, timer) message =
  if timer.state == Ringing
    then Signal.send updates (UpdateTimer id Stop)
    else message