aboutsummaryrefslogtreecommitdiff
path: root/src/Update/Update.elm
blob: 8fd5cb36f466da16b5e562ac688f13e525bbad25 (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
module Update.Update
  ( Action(..)
  , updates
  , update
  ) where

import Signal
import Dict
import Dict (Dict)
import Time (Time)
import Maybe
import Keyboard (KeyCode)
import Char
import Debug
import List

import Model.Model (..)
import Model.Timer (..)
import Model.Edition.Edition (..)
import Model.Edition.NameEdition (..)
import Model.Edition.TimeEdition (..)
import Model.Id (..)
import Model.IdGenerator (..)

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

import Utils.Maybe (..)

type Action =
  NoOp
  | Initialize
  | AddNewTimer
  | DeltaTime Time
  | UpdateTimer Id TimerAction
  | RemoveTimer Id
  | Edit Id Kind
  | ValidEdition
  | ClickAway
  | KeyPressed KeyCode

updates : Signal.Channel Action
updates = Signal.channel NoOp

logUpdate : Action -> Model -> Model
logUpdate action model =
  case action of
    DeltaTime _ -> update action model
    _ -> update (Debug.log "action" action) model

update : Action -> Model -> Model
update action model =
  case action of
    NoOp -> model
    Initialize ->
      initialModel model.currentTime
    AddNewTimer ->
      let (id, newTimerIdGenerator) = getId model.timerIdGenerator
      in  { model
          | timers <- Dict.insert id (initialTimer model.currentTime) model.timers
          , timerIdGenerator <- newTimerIdGenerator
          }
    DeltaTime delta ->
      { model
      | currentTime <- model.currentTime + delta
      , timers <- Dict.map (\id timer -> updateTimer (SubstractTime delta) timer) model.timers
      }
    UpdateTimer id action ->
      let maybeEdition = filterMaybe (\edition -> edition.id == id) model.edition
          newModel =
            case maybeEdition of
              Just edition ->
                if edition.kind == Time then validEdition model else model
              Nothing ->
                model
      in  { newModel | timers <- Dict.update id (Maybe.map (updateTimer action)) newModel.timers }
    RemoveTimer id ->
      if numberOfTimers model > 1
        then
          { model | timers <- Dict.remove id model.timers }
        else
          model
    Edit id kind ->
      { model
      | edition <- Just (newEdition id kind)
      , timers <- Dict.update id (Maybe.map (updateTimer Pause)) model.timers
      }
    ValidEdition ->
      validEdition model
    ClickAway ->
      { model | edition <- Nothing }
    KeyPressed keyCode ->
      if isRemoveKeyCode keyCode
        then
          { model | edition <- Maybe.map (updateEdition DeleteLast) model.edition }
        else
          { model | edition <- Maybe.map (updateEdition (AddChar keyCode)) model.edition }

validEdition : Model -> Model
validEdition model =
  case model.edition of
    Just edition ->
      if List.isEmpty edition.chars
        then
          model
        else
          let action =
            case edition.kind of
              Name ->
                Rename (renderNameEdition edition.chars)
              Time ->
                SetTime (toTime edition.chars)
          in  { model
              | timers <- Dict.update edition.id (Maybe.map (updateTimer action)) model.timers
              , edition <- Nothing
              }
    Nothing ->
      model

isRemoveKeyCode : KeyCode -> Bool
isRemoveKeyCode = (==) 8