aboutsummaryrefslogtreecommitdiff
path: root/src/view/timer.ts
blob: 3cdf0d76ffbc32a777618d7e66bc5959aa92ca7f (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
import * as Config from 'config'
import * as Step from 'step'
import * as Arc from 'arc'
import * as Router from 'router'
import * as Audio from 'audio'
import h from 'h'

let interval: number | undefined = undefined

const endDuration: number = 4

export function clearInterval() {
  if (interval !== undefined) {
    window.clearInterval(interval)
    interval = undefined
  }
}

interface State {
  isPlaying: boolean,
  elapsed: number
}

export function view(config: Config.Config, showPage: (route: Router.Route) => void) {

  const formUrl = `${Router.toString({ name: 'form', config })}`
  const duration = Config.getDuration(config)

  // State
  let state = {
    isPlaying: true,
    elapsed: 0
  }

  // Elements
  const initStep = Step.getAt(config, state.elapsed)
  const section = h('section', { className: timerClass(initStep) })
  const stepElt = document.createTextNode(Step.prettyPrint(initStep))
  const arcPathElt = h('path', { class: 'g-Timer__ArcProgress' })

  const updateDom = () => {
    const step = Step.getAt(config, state.elapsed)
    const angle = Math.min(359.999, state.elapsed / duration * 360)
    arcPathElt.setAttribute("d", Arc.describe(0, 0, 90, 0, angle))
    section.className = timerClass(step)
    stepElt.textContent = Step.prettyPrint(step)
    Audio.playFromStep(config, step, state.elapsed)
  }

  const quit = () => {
    const formRoute: Router.Route = { name: 'form', config }
    history.pushState({}, '', Router.toString(formRoute))
    clearInterval()
    showPage(formRoute)
  }

  const update = () => {
    if (state.isPlaying) {
      state.elapsed = state.elapsed + 1
      state.elapsed >= duration + endDuration
        ? quit()
        : updateDom()
    }
  }

  // Start timer
  if (interval !== undefined) {
    window.clearInterval(interval)
    interval = undefined
  }
  interval = window.setInterval(update, 1000)

  // Play initial audio
  Audio.playFromStep(config, initStep, state.elapsed)

  section.append(
    h('div',
      { className: 'g-Timer__Dial' },
      h('svg',
        { class: 'g-Timer__Arc',
          viewBox: '-100 -100 200 200'
        },
        h('path',
          { class: 'g-Timer__ArcTotal',
            d: Arc.describe(0, 0, 90.0, 0, 359.999)
          }
        ),
        ...arcPaths(config),
        arcPathElt
      ),
      stepElt
    ),
    h('div',
      { className: 'g-Timer__Buttons' },
      h('button',
        { className: 'g-Button',
          onclick: (e: MouseEvent) => {
            state.isPlaying = !state.isPlaying
            const elt = e.target as HTMLElement
            elt.textContent = state.isPlaying
              ? 'pause'
              : 'resume'
            elt.className = state.isPlaying
              ? 'g-Button'
              : 'g-Button g-Button--Active'
          }
        },
        'Pause'
      ),
      h('a',
        { className: 'g-Button',
          href: formUrl
        },
        'Quit'
      )
    )
  )

  return section
}

function arcPaths(config: Config.Config): Element[] {
  const paths = []

  let t = 0
  const totalDuration = Config.getDuration(config)

  let arc = (kind: string, duration: number): Element => {
    const startAngle = 360 * t / totalDuration
    const endAngle = Math.min(360 * (t + duration) / totalDuration, 359.999)

    t += duration

    return h('path',
      { class: `g-Timer__Arc${kind}`,
        d: Arc.describe(0, 0, 90.0, startAngle, endAngle)
      }
    )
  }

  if (config.warmup > 0) {
    paths.push(arc('WarmUp', config.warmup))
  }

  for (let tabata = 0; tabata < config.tabatas.length; tabata++) {
    paths.push(arc('Prepare', config.prepare))
    for (let cycle = 0; cycle < config.cycles; cycle++) {
      paths.push(arc('Work', config.work))
      paths.push(arc('Rest', config.rest))
    }
  }

  return paths
}

function timerClass(step: Step.Step): string {
  switch (step.name) {
    case 'warmup':
      return 'g-Layout__Page g-Timer g-Timer--WarmUp'
    case 'prepare':
      return 'g-Layout__Page g-Timer g-Timer--Prepare'
    case 'work':
      return 'g-Layout__Page g-Timer g-Timer--Work'
    case 'rest':
      return 'g-Layout__Page g-Timer g-Timer--Rest'
    case 'end':
      return 'g-Layout__Page g-Timer'
  }
}