aboutsummaryrefslogtreecommitdiff
path: root/src/Views.elm
blob: b74bf98d2d82e609300f3ad20a048fa176b379bb (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
{-
   - This file is part of `typers`.
   -
   - `typers` is free software: you can redistribute it and/or modify
   - it under the terms of the GNU Affero Public License as published by
   - the Free Software Foundation, either version 3 of the License, or
   - (at your option) any later version.
   -
   - `typers` is distributed in the hope that it will be useful,
   - but WITHOUT ANY WARRANTY; without even the implied warranty of
   - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   - GNU Affero Public License for more details.
   -
   - You should have received a copy of the GNU Affero Public License
   - along with `typers`.  If not, see <https://www.gnu.org/licenses/>.
-}


module Views exposing (..)

import Array exposing (..)
import Base exposing (..)
import Css exposing (..)
import Html
import Html.Styled exposing (..)
import Html.Styled.Attributes exposing (css, href)
import Html.Styled.Events exposing (onClick)
import Styles exposing (..)
import Time exposing (Posix, toHour, toMinute, toSecond, utc)
import Utils exposing (diffDuration, isNothing, 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 -> Maybe String
viewWpm model =
    let
        t1 =
            model.begin

        t2 =
            model.end

        duration =
            Maybe.map2 diffDuration t1 t2

        correctWords =
            wordCountWith model.words ((==) Correct)
    in
    Maybe.map (String.fromInt << truncate << (*) 60 << (/) (toFloat correctWords)) duration


viewProgress : Model -> String
viewProgress model =
    let
        soFar =
            model.currentWord + 1

        total =
            model.config.length
    in
    String.fromInt soFar ++ ":" ++ String.fromInt total


viewAccuracy : Model -> Maybe String
viewAccuracy model =
    if isNothing model.end then
        Nothing

    else
        let
            words =
                model.words

            wordsAttempted =
                toFloat <| wordCountWith words ((/=) Todo)

            correctCount =
                toFloat <| wordCountWith words ((==) Correct)

            accuracy =
                if wordsAttempted == 0.0 then
                    Nothing

                else
                    Just <| String.fromInt <| truncate <| correctCount / wordsAttempted * 100
        in
        accuracy


viewWordLengthOptions : Base.Config -> Html Msg
viewWordLengthOptions config =
    let
        wl =
            [ 10, 25, 50, 100 ]
    in
    ul
        [ css
            [ margin zero
            , listStyle Css.none
            , display inlineBlock
            , paddingLeft (px 0)
            ]
        ]
        (List.map
            (\len ->
                li
                    [ css [ display inline ] ]
                    [ styledButton
                        [ onClick (Base.WordLengthChanged len)
                        , css <|
                            List.singleton <|
                                if config.length == len then
                                    textDecoration underline

                                else
                                    textDecoration Css.none
                        ]
                        [ text <| String.fromInt len ]
                    ]
            )
            wl
        )


viewStats : Model -> Html Msg
viewStats model =
    let
        wpm =
            viewWpm model

        acc =
            viewAccuracy model

        stats =
            Maybe.map2 (\w a -> w ++ " words per minute · " ++ a ++ "% accuracy") wpm acc
    in
    p []
        [ text (Maybe.withDefault "" stats) ]


viewFooter : Html Msg
viewFooter =
    let
        footerItems =
            [ ( "src", "https://git.peppe.rs/web/typers/about" )
            , ( "license", "https://git.peppe.rs/web/typers/tree/COPYING" )
            ]
    in
    styledUnorderedList []
        (List.map
            (\( inner, hr ) ->
                styledListItem []
                    [ styledExternalLink [ href hr ] [ text inner ] ]
            )
            footerItems
        )