module Views exposing (viewAccuracy, viewProgress, viewWordLengthOptions, viewWpm) import Array exposing (..) import Base exposing (..) import Css exposing (..) import Html import Html.Styled exposing (..) import Html.Styled.Events exposing (onClick) import Styles exposing (styledListItem, styledUnorderedList) 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 : Model -> String viewProgress model = let soFar = model.currentWord + 1 total = model.config.length in 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 viewWordLengthOptions : Html Msg viewWordLengthOptions = let wl = [ 10, 25, 50, 100 ] in styledUnorderedList [] (List.map (\len -> styledListItem [ onClick (Base.WordLengthChanged len) ] [ text <| String.fromInt len ] ) wl )