aboutsummaryrefslogtreecommitdiff
path: root/src/Main.elm
blob: 9a356315dea0f739c60c3a0c51643a839b47ccfe (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
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
module Main exposing (..)

import Array exposing (..)
import Browser
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput)
import Task
import Time


main =
    Browser.element
        { init = init
        , view = view
        , update = update
        , subscriptions = subscriptions
        }


type WordStatus
    = Correct
    | Wrong
    | Todo
    | CurrentWord


type alias Word =
    { content : String
    , status : WordStatus
    }


type alias Model =
    { begin : Maybe Time.Posix
    , end : Maybe Time.Posix
    , words : Array Word
    , accuracy : Maybe Float
    , length : Int
    , currentWord : Int
    , inputBox : String
    }


type Msg
    = Started Time.Posix
    | Finished Time.Posix
    | CorrectInput
    | CorrectSoFar
    | NextWord
    | WrongInput
    | InputChanged String
    | Redo


subscriptions : Model -> Sub Msg
subscriptions model =
    Sub.none


generateWords : Array Word
generateWords =
    "this is some sample text for the demoz"
        |> String.split " "
        |> Array.fromList
        |> Array.map (\w -> Word w Todo)


init : () -> ( Model, Cmd Msg )
init _ =
    let
        words =
            generateWords
    in
    ( Model Nothing Nothing words Nothing (Array.length words) 0 ""
    , Cmd.none
    )


setStartTime : Cmd Msg
setStartTime =
    Task.perform Started Time.now


setEndTime : Cmd Msg
setEndTime =
    Task.perform Finished Time.now


firstWord : Model -> Bool
firstWord model =
    String.length model.inputBox == 1 && model.currentWord == 0


lastWord : Model -> Bool
lastWord model =
    model.currentWord == model.length - 1


wordEnded : String -> Bool
wordEnded s =
    String.endsWith " " s


handleWordEnded : Model -> ( Model, Cmd Msg )
handleWordEnded model =
    let
        s =
            model.inputBox

        current =
            Array.get model.currentWord model.words |> Maybe.withDefault (Word "" Todo)

        currentContent =
            current.content

        prevWordStatus =
            if s == currentContent then
                Correct

            else
                Wrong

        newWords =
            Array.set model.currentWord { current | status = prevWordStatus } model.words

        newModel =
            { model | inputBox = "", words = newWords, currentWord = model.currentWord + 1 }

        cmd =
            if lastWord model then
                setEndTime

            else
                Cmd.none
    in
    ( newModel, cmd )


isNothing : Maybe a -> Bool
isNothing p =
    case p of
        Nothing ->
            True

        Just a ->
            False


isJust : Maybe a -> Bool
isJust p =
    not (isNothing p)


flip : (a -> b -> c) -> (b -> a -> c)
flip f =
    \x y -> f y x


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        Started t ->
            ( { model | begin = Just t }
            , Cmd.none
            )

        Finished t ->
            ( { model | end = Just t }
            , Cmd.none
            )

        InputChanged s ->
            let
                cmd =
                    if firstWord model && isNothing model.begin then
                        setStartTime

                    else if s == currentContents model && lastWord model then
                        setEndTime

                    else
                        Cmd.none
            in
            ( { model | inputBox = s }
            , cmd
            )

        NextWord ->
            if isJust model.end then
                ( model, Cmd.none )

            else
                handleWordEnded model

        _ ->
            ( model
            , Cmd.none
            )


displayTime : Time.Posix -> String
displayTime t =
    let
        hh =
            Time.toHour Time.utc t

        mm =
            Time.toMinute Time.utc t

        ss =
            Time.toSecond Time.utc t
    in
    String.join ":" (List.map String.fromInt [ hh, mm, ss ])


wordStyle : WordStatus -> Attribute msg
wordStyle w =
    case w of
        Correct ->
            style "color" "green"

        Wrong ->
            style "color" "red"

        Todo ->
            style "color" "black"

        CurrentWord ->
            style "color" "magenta"


toPara : Array Word -> Int -> String -> Html Msg
toPara words current content =
    words
        |> Array.map (\w -> span [ wordStyle w.status ] [ text (w.content ++ " ") ])
        |> Array.set current (span [ wordStyle CurrentWord ] [ text (content ++ " ") ])
        |> Array.toList
        |> p []


currentContents : Model -> String
currentContents model =
    Array.get model.currentWord model.words
        |> Maybe.map .content
        |> Maybe.withDefault ""


diffDuration : Time.Posix -> Time.Posix -> Float
diffDuration t1 t2 =
    let
        m1 =
            Time.posixToMillis t1

        m2 =
            Time.posixToMillis t2
    in
    toFloat (m2 - m1) / 1000


viewWpm : Model -> String
viewWpm model =
    let
        t1 =
            model.begin

        t2 =
            model.end

        duration =
            Maybe.map2 diffDuration t1 t2

        correctWords =
            wordCount model.words ((==) Correct)

        wpm =
            Maybe.map (String.fromInt << truncate << (*) 60 << (/) (toFloat correctWords)) duration
    in
    Maybe.withDefault "XX" wpm


wordCount : Array Word -> (WordStatus -> Bool) -> Int
wordCount words predicate =
    words |> Array.map .status |> Array.filter predicate |> Array.length


viewProgress : Model -> String
viewProgress model =
    String.fromInt model.currentWord ++ "/" ++ String.fromInt model.length


viewAccuracy : Model -> String
viewAccuracy model =
    let
        wordsAttempted =
            toFloat <| wordCount model.words ((/=) Todo)

        correctCount =
            toFloat <| wordCount model.words ((==) Correct)

        accuracy =
            if wordsAttempted == 0.0 then
                Nothing

            else
                Just <| correctCount / wordsAttempted * 100
    in
    case accuracy of
        Nothing ->
            "XX"

        Just a ->
            String.fromInt <| truncate a


view : Model -> Html Msg
view model =
    div []
        [ p [] [ text (String.fromInt model.currentWord) ]
        , toPara model.words model.currentWord (currentContents model)

        -- , p [] [ text (Maybe.withDefault "XX" (Maybe.map displayTime model.begin)) ]
        -- , p [] [ text (Maybe.withDefault "XX" (Maybe.map displayTime model.end)) ]
        , p [] [ text ("WPM: " ++ viewWpm model) ]
        , p [] [ text ("ACC: " ++ viewAccuracy model) ]
        , input [ onInput handleInputChanged, value model.inputBox ] []
        ]


handleInputChanged : String -> Msg
handleInputChanged s =
    if wordEnded s then
        NextWord

    else
        InputChanged s