-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.elm
149 lines (116 loc) · 3.88 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
port module Main exposing (main)
import Browser
import Browser.Dom exposing (getViewport)
import Browser.Events exposing (onResize)
import Browser.Navigation as Navigation exposing (Key)
import Clip exposing (Clip, Word)
import Dict exposing (Dict)
import Http
import Message exposing (..)
import Model exposing (ClipState(..), Model, SoundState(..))
import Random
import Task
import Url exposing (Url)
import Video exposing (Video)
import View
{-| port for sending text to measure out to JavaScript
-}
port measure : { font : String, text : String } -> Cmd msg
{-| port for listening for measurements from JavaScript
-}
port measurements : (Word -> msg) -> Sub msg
main : Program () Model Msg
main =
Browser.application
{ init = init
, view = View.view
, update = update
, subscriptions =
always
(Sub.batch
[ onResize WindowSize
, measurements Measured
]
)
, onUrlRequest = always Noop
, onUrlChange = .fragment >> LoadClip
}
init : () -> Url -> Key -> ( Model, Cmd Msg )
init _ url key =
( Model.initial url.fragment key
, Cmd.batch
[ Http.get "/videos.json" Video.videos
|> Http.send VideosLoad
, Task.perform (\{ viewport } -> WindowSize (round viewport.width) (round viewport.height)) getViewport
]
)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
VideosLoad videos ->
loadClip { model | videos = Result.toMaybe videos }
PlayRandom ->
navigateToRandomVideo model
ShowSoundButton ->
( { model | sound = Shown }, Cmd.none )
EnableSound ->
( { model | sound = Enabled }, Cmd.none )
NavigateTo slug ->
( model, Navigation.pushUrl model.key ("#" ++ slug) )
LoadClip slug ->
case slug of
Just slug_ ->
loadClip { model | clip = Slug slug_ }
Nothing ->
loadClip { model | clip = Initial }
Measured word ->
case model.clip of
Loaded clip ->
let
( newClip, cmd ) =
Clip.update word clip
in
( { model | clip = Loaded newClip }
, case cmd of
Just text ->
measure { font = Clip.font, text = text }
Nothing ->
Cmd.none
)
_ ->
( model, Cmd.none )
WindowSize width height ->
( { model | width = width, height = height }, Cmd.none )
Noop ->
( model, Cmd.none )
loadClip : Model -> ( Model, Cmd Msg )
loadClip model =
case ( model.videos, model.clip ) of
( Just videos, Slug slug ) ->
initClip videos slug model
( Just videos, Initial ) ->
navigateToRandomVideo model
_ ->
( model, Cmd.none )
initClip : Dict String Video -> String -> Model -> ( Model, Cmd Msg )
initClip videos slug model =
case Maybe.map Clip.initial (Dict.get slug videos) of
Just ( clip, cmd ) ->
( { model | clip = Loaded clip }
, case cmd of
Just text ->
measure { font = Clip.font, text = text }
Nothing ->
Cmd.none
)
Nothing ->
navigateToRandomVideo model
navigateToRandomVideo : Model -> ( Model, Cmd Msg )
navigateToRandomVideo model =
case model.videos of
Just videos ->
( { model | count = model.count + 1 }
, Random.generate NavigateTo (Video.random videos)
)
Nothing ->
( model, Cmd.none )