aboutsummaryrefslogtreecommitdiff
path: root/src/Views.elm
blob: c5cc159eb504ba6ae95c505899b76991e752a7ce (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
module Views exposing (viewAccuracy, viewProgress, viewWpm)

import Array exposing (..)
import Base exposing (..)
import Time exposing (Posix, toHour, toMinute, toSecond, utc)
import Utils exposing (diffDuration, wordCountWith)


viewTime : Posix -> String
viewTime t =
    let
        hh =
            toHour utc t

        mm =
            toMinute utc t

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


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

        t2 =
            model.end

        duration =
            Maybe.map2 diffDuration t1 t2

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

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


viewProgress : Int -> Int -> String
viewProgress soFar total =
    String.fromInt soFar ++ "/" ++ String.fromInt total


viewAccuracy : Array Word -> String
viewAccuracy words =
    let
        wordsAttempted =
            toFloat <| wordCountWith words ((/=) Todo)

        correctCount =
            toFloat <| wordCountWith 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