/
Main.elm
567 lines (453 loc) · 16.2 KB
/
Main.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
port module Main exposing (DisplayTime, main, millisToDisplayTime)
import Browser exposing (UrlRequest)
import Browser.Navigation
import Dict
import Html exposing (Attribute, Html, a, button, details, div, fieldset, footer, h1, h2, i, input, label, legend, li, main_, nav, p, small, span, summary, text, textarea, ul)
import Html.Attributes as A exposing (attribute, checked, class, for, href, id, name, readonly, rows, step, style, type_, value)
import Html.Events exposing (onClick, onInput)
import Json.Encode as E
import Time
import Url
import Url.Builder as UB
import Url.Parser as UP
import Url.Parser.Query as Query
type alias Model =
{ timeMillis : Int
, paused : Bool
, current : Maybe Time.Posix
, setting : Setting
, key : Browser.Navigation.Key
}
type alias Setting =
{ bgColor : BgColor
, fgColor : String
, initialTimeSeconds : Int
, showHour : Bool
}
defaultSetting : Setting
defaultSetting =
{ fgColor = "#415462"
, bgColor = GreenBack
, initialTimeSeconds = -10
, showHour = True
}
type alias DisplayTime =
{ isMinus : Bool
, hours : Int
, minutes : Int
, seconds : Int
}
millisToDisplayTime : Int -> DisplayTime
millisToDisplayTime t =
let
absSeconds =
toFloat t / 1000 |> floor |> abs
in
{ isMinus = t < 0
, hours = absSeconds // 3600
, minutes = absSeconds // 60 |> modBy 60
, seconds = absSeconds |> modBy 60
}
type BgColor
= Transparent
| GreenBack
| BlueBack
encodeBgColor : BgColor -> String
encodeBgColor bg =
case bg of
GreenBack ->
"gb"
BlueBack ->
"bb"
Transparent ->
"tp"
dictBgColor : Dict.Dict String BgColor
dictBgColor =
let
pairwise bgColor =
( encodeBgColor bgColor, bgColor )
in
Dict.fromList <| List.map pairwise [ GreenBack, BlueBack, Transparent ]
encodeShowHour : Bool -> String
encodeShowHour b =
if b then
"true"
else
"false"
dictShowHour : Dict.Dict String Bool
dictShowHour =
Dict.fromList [ ( "true", True ), ( "false", False ) ]
parserWithDefault : a -> Query.Parser (Maybe a) -> Query.Parser a
parserWithDefault default =
Query.map <| Maybe.withDefault default
queryParser : Query.Parser Setting
queryParser =
Query.map4
Setting
(parserWithDefault defaultSetting.bgColor <| Query.enum "bg" dictBgColor)
(parserWithDefault defaultSetting.fgColor <| Query.string "fg")
(parserWithDefault defaultSetting.initialTimeSeconds <| Query.int "init")
(parserWithDefault defaultSetting.showHour <| Query.enum "h" dictShowHour)
initialModel : flag -> Url.Url -> Browser.Navigation.Key -> ( Model, Cmd Msg )
initialModel _ url key =
let
urlParser =
UP.query queryParser
initSetting =
UP.parse urlParser url |> Maybe.withDefault defaultSetting
in
( { timeMillis = initSetting.initialTimeSeconds * 1000
, paused = True
, current = Nothing
, setting = initSetting
, key = key
}
, Cmd.none
)
type Msg
= Start
| Pause
| Reset
| UpdateTime Int Time.Posix
| UpdateResetTime Int
| SetBgColor BgColor
| SetFgColor String
| ToggleShowHour
| LinkClicked UrlRequest
| NoOp
urlFromSetting : Setting -> String
urlFromSetting setting =
UB.toQuery
[ UB.string "fg" setting.fgColor
, UB.string "bg" <| encodeBgColor setting.bgColor
, UB.int "init" setting.initialTimeSeconds
, UB.string "h" <| encodeShowHour setting.showHour
]
update : Msg -> Model -> ( Model, Cmd Msg )
update msg ({ setting } as model) =
case msg of
Start ->
( { model | paused = False }, timerStartEvent model.timeMillis )
Pause ->
( { model | paused = True, current = Nothing }, timerPauseEvent model.timeMillis )
Reset ->
( { model
| timeMillis = model.setting.initialTimeSeconds * 1000
, paused = True
, current = Nothing
}
, timerResetEvent <| model.setting.initialTimeSeconds * 1000
)
UpdateTime millis current ->
( { model | timeMillis = millis, current = Just current }, Cmd.none )
UpdateResetTime millis ->
( { model | setting = { setting | initialTimeSeconds = millis } }
, Browser.Navigation.replaceUrl model.key <|
urlFromSetting { setting | initialTimeSeconds = millis }
)
SetBgColor bgColor ->
( { model | setting = { setting | bgColor = bgColor } }
, Browser.Navigation.replaceUrl model.key <|
urlFromSetting { setting | bgColor = bgColor }
)
SetFgColor fgColor ->
( { model | setting = { setting | fgColor = fgColor } }
, Browser.Navigation.replaceUrl model.key <|
urlFromSetting { setting | fgColor = fgColor }
)
ToggleShowHour ->
( { model | setting = { setting | showHour = not setting.showHour } }
, Browser.Navigation.replaceUrl model.key <|
urlFromSetting { setting | showHour = not setting.showHour }
)
LinkClicked urlRequest ->
case urlRequest of
Browser.Internal url ->
( model, Browser.Navigation.load (Url.toString url) )
Browser.External href ->
( model, Browser.Navigation.load href )
NoOp ->
( model, Cmd.none )
view : Model -> Browser.Document Msg
view model =
{ title = "Sync Timer - 同時視聴用タイマー"
, body =
[ main_ [ class "container" ]
[ viewHeader
, viewTimer model
, viewTimerSettings model.setting
]
, viewFooter
]
}
viewHeader : Html Msg
viewHeader =
nav []
[ ul []
[ li []
[ h1 [] [ text "Sync Timer" ] ]
]
, ul []
[ li []
[ a [ href "/about/?utm_source=top", role "button", class "outline" ]
[ text "SyncTimerとは?"
]
]
]
]
viewTimer : Model -> Html Msg
viewTimer model =
div [ class "grid" ]
[ viewTimerDigits model.timeMillis model.setting
, viewTimerControls model
]
viewTimerDigits : Int -> Setting -> Html Msg
viewTimerDigits millis setting =
let
displayTime =
millisToDisplayTime millis
displaySegments =
if setting.showHour || displayTime.hours > 0 then
[ displayTime.hours, displayTime.minutes, displayTime.seconds ]
else
[ displayTime.minutes, displayTime.seconds ]
in
div [ class "timer", timerBgColorClass setting.bgColor, style "color" setting.fgColor ]
(List.concat <|
[ [ span (styleTimerSign displayTime.isMinus) [ text "-" ] ]
, List.foldr joinWithSegment [] <| List.map renderBig2Digits displaySegments
]
)
joinWithSegment : List (Html Msg) -> List (Html Msg) -> List (Html Msg)
joinWithSegment a b =
case b of
[] ->
a
_ ->
List.concat [ a, [ span styleTimerSep [ text ":" ] ], b ]
timerBgColorClass : BgColor -> Attribute Msg
timerBgColorClass bgColor =
case bgColor of
Transparent ->
class "transparent"
GreenBack ->
class "greenback"
BlueBack ->
class "blueback"
padZero : Int -> Int -> String
padZero wt digits =
String.fromInt digits |> String.padLeft wt '0'
styleTimerSep : List (Html.Attribute Msg)
styleTimerSep =
[ class "sep" ]
styleTimerDigit : List (Html.Attribute Msg)
styleTimerDigit =
[ class "digit" ]
styleTimerSign : Bool -> List (Html.Attribute Msg)
styleTimerSign isMinus =
let
visibility =
style "visibility" <|
if isMinus then
"visible"
else
"hidden"
in
visibility :: styleTimerDigit
renderBig2Digits : Int -> List (Html Msg)
renderBig2Digits digits =
padZero 2 digits
|> String.split ""
|> List.map (\d -> span styleTimerDigit [ text d ])
viewTimerControls : Model -> Html Msg
viewTimerControls model =
div [ class "controls" ]
[ div [] [ startPauseButton model.paused ]
, div [] [ resetButton model.setting.initialTimeSeconds ]
, div [] [ initialTimeSlider model.setting.initialTimeSeconds ]
]
startPauseButton : Bool -> Html Msg
startPauseButton paused =
if paused then
button
[ onClick Start ]
[ i [ class "fas", class "fa-play", class "button-icon" ] []
, text "開始"
]
else
button
[ onClick Pause ]
[ i [ class "fas", class "fa-pause", class "button-icon" ] []
, text "一時停止"
]
resetButton : Int -> Html Msg
resetButton initialTimeSeconds =
button [ class "secondary", onClick Reset ]
[ text <| String.fromInt initialTimeSeconds ++ " 秒にリセット" ]
role : String -> Attribute Msg
role s =
attribute "role" s
viewTimerSettings : Setting -> Html Msg
viewTimerSettings setting =
details [ class "settings", attribute "open" "true" ]
[ summary [] [ text "表示設定" ]
, div [ class "grid" ]
[ div []
[ label [ for "fgColorPicker" ]
[ text "文字色"
, input [ type_ "color", id "fgColorPicker", value setting.fgColor, onInput SetFgColor ] []
]
]
, div []
[ label [ for "fgColorText" ]
[ text "文字色(RGB指定)"
, input [ type_ "text", id "fgColorText", value setting.fgColor, onInput SetFgColor ] []
]
]
]
, div []
[ fieldset [ class "bgColor" ]
[ legend [] [ text "背景色" ]
, label [ for "greenback" ]
[ input [ type_ "radio", id "greenback", name "bgcolor", checked <| setting.bgColor == GreenBack, onClick <| SetBgColor GreenBack ] []
, text "GB"
]
, label [ for "blueback" ]
[ input [ type_ "radio", id "blueback", name "bgcolor", checked <| setting.bgColor == BlueBack, onClick <| SetBgColor BlueBack ] []
, text "BB"
]
, label [ for "transparent" ]
[ input [ type_ "radio", id "transparent", name "bgcolor", checked <| setting.bgColor == Transparent, onClick <| SetBgColor Transparent ] []
, text "なし"
]
]
]
, div
[]
[ label [ for "showHour" ]
[ input [ type_ "checkbox", id "showHour", role "switch", checked setting.showHour, onClick ToggleShowHour ] []
, text "時間を表示する"
]
]
]
initialTimeSlider : Int -> Html Msg
initialTimeSlider initialTimeSeconds =
div [ class "delay-slider" ]
[ input
[ type_ "range"
, A.min "-30"
, A.max "30"
, step "1"
, onInput (String.toInt >> Maybe.withDefault 0 >> UpdateResetTime)
, value <| String.fromInt initialTimeSeconds
, tooltip <| "タイマーの開始時間: " ++ String.fromInt initialTimeSeconds ++ "秒"
]
[]
]
tooltip : String -> Attribute Msg
tooltip =
attribute "data-tooltip"
viewFooter : Html Msg
viewFooter =
footer []
[ div [ class "container" ]
[ p []
[ text "機能要望や質問・感想などは"
, a [ A.href "https://docs.google.com/forms/d/e/1FAIpQLSfgmFqq-t-vv6gC1YpgoH3nCK1b7gI0ROC25K1NX9r5jGtndg/viewform?usp=sf_link", A.target "_blank", A.rel "noopener noreferrer" ] [ text "こちらのフォームからどうぞ" ]
]
, div [] [ small [] [ text "よろしければ概要欄でタイマーの紹介をお願いします(必須ではありません)" ] ]
, textarea [ readonly True, rows 2, id "introduce-request" ] [ text "同時視聴用タイマー SyncTimer を利用しています\nhttps://sync-timer.netlify.app/" ]
, div [ class "grid" ]
[ div [ class "footer-left" ]
[ h2 [] [ text "更新情報" ]
, ul []
[ li [] [ text "2022-11-18 時間の表示をON/OFFできるようにしました" ]
, li [] [ text "2022-11-14 YouTubeでの利用例を紹介しました" ]
]
]
, div [ class "footer-right" ]
[ div []
[ a [ href "https://youtube.com/playlist?list=PLzz5NMXBDKoZ8UoGCgQI9mdcfmyjugbwz", A.target "_blank", A.rel "noopener noreferrer" ]
[ i [ class "fab", class "fa-youtube", class "button-icon" ] []
, text "YouTubeでの利用例"
]
]
, div []
[ a [ href "https://twitter.com/intent/user?user_id=62148177", A.target "_blank", A.rel "noopener noreferrer" ]
[ i [ class "fab", class "fa-twitter", class "button-icon" ] []
, text "@mather314 (開発者)"
]
]
, div []
[ a [ href "https://github.com/mather/sync-timer", class "secondary", A.target "_blank", A.rel "noopener noreferrer" ]
[ i [ class "fab", class "fa-github", class "button-icon" ] []
, text "mather/sync-timer"
]
]
, div [] [ text "© Eisuke Kuwahata" ]
]
]
]
]
subscriptions : Model -> Sub Msg
subscriptions model =
let
tick now =
UpdateTime (Time.posixToMillis now - (Time.posixToMillis <| Maybe.withDefault now model.current) + model.timeMillis) now
in
if model.paused then
Sub.none
else
Time.every 33 tick
onUrlRequest : UrlRequest -> Msg
onUrlRequest =
LinkClicked
onUrlChange : Url.Url -> Msg
onUrlChange _ =
NoOp
port sendAnalyticsEvent : String -> Cmd msg
encodeAnalyticsEvent : String -> String -> String -> Maybe Int -> String
encodeAnalyticsEvent category action label value =
E.encode 0 <|
E.object <|
[ ( "category", E.string category )
, ( "action", E.string action )
, ( "label", E.string label )
]
++ (Maybe.map (\v -> [ ( "value", E.int v ) ]) value |> Maybe.withDefault [])
formatTimeForAnalytics : Int -> String
formatTimeForAnalytics t =
let
dt =
millisToDisplayTime t
in
[ if dt.isMinus then
"-"
else
""
, String.fromInt dt.hours |> String.padLeft 2 '0'
, ":"
, String.fromInt dt.minutes |> String.padLeft 2 '0'
, ":"
, String.fromInt dt.seconds |> String.padLeft 2 '0'
]
|> String.concat
timerStartEvent : Int -> Cmd msg
timerStartEvent currentTime =
sendAnalyticsEvent <| encodeAnalyticsEvent "sync_timer" "sync_timer_start" (formatTimeForAnalytics currentTime) Nothing
timerPauseEvent : Int -> Cmd msg
timerPauseEvent currentTime =
sendAnalyticsEvent <| encodeAnalyticsEvent "sync_timer" "sync_timer_pause" (formatTimeForAnalytics currentTime) Nothing
timerResetEvent : Int -> Cmd msg
timerResetEvent resetTime =
sendAnalyticsEvent <| encodeAnalyticsEvent "sync_timer" "sync_timer_reset" (formatTimeForAnalytics resetTime) Nothing
main : Program () Model Msg
main =
Browser.application
{ init = initialModel
, view = view
, update = update
, subscriptions = subscriptions
, onUrlRequest = onUrlRequest
, onUrlChange = onUrlChange
}