From 589a9c03eeee61b6e1bc9b6ac145c177a59258be Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 22 Nov 2020 22:07:48 +0530 Subject: add app config, word generation --- src/Base.elm | 22 +++++++++++++- src/Data.elm | 21 +++++++------- src/Main.elm | 95 ++++++++++++++++++++++++++++++++++++++---------------------- 3 files changed, 91 insertions(+), 47 deletions(-) diff --git a/src/Base.elm b/src/Base.elm index c3557d3..480147a 100644 --- a/src/Base.elm +++ b/src/Base.elm @@ -22,7 +22,27 @@ type alias Model = , end : Maybe Posix , words : Array Word , accuracy : Maybe Float - , length : Int + , config : Config , currentWord : Int , inputBox : String } + + +type alias Config = + { punctuation : Bool + , capitals : Bool + , length : Int + } + + +type Msg + = Started Time.Posix + | Finished Time.Posix + | CorrectInput + | CorrectSoFar + | NextWord + | WrongInput + | InputChanged String + | LoadWords (List String) + | WordLengthChanged Int + | Redo diff --git a/src/Data.elm b/src/Data.elm index c3e71ae..95c92c1 100644 --- a/src/Data.elm +++ b/src/Data.elm @@ -1,14 +1,13 @@ -module Data exposing (punctuations, randomWord, textData) +module Data exposing (defaultConfig, randomParagraph) +import Base exposing (Config) import Random import Utils exposing (listIdx, triple) -type alias Config = - { punctuation : Bool - , capitals : Bool - , length : Int - } +defaultConfig : Config +defaultConfig = + Config True True 50 textData : List String @@ -22,9 +21,9 @@ punctuations = ", ! . ? ... ; :" |> String.split " " -randomBool : Random.Generator Bool -randomBool = - Random.uniform True [ False ] +falseWeightedBool : Random.Generator Bool +falseWeightedBool = + Random.weighted ( 20, True ) [ ( 80, False ) ] randomWord : Config -> Random.Generator ( Int, Bool, Bool ) @@ -35,14 +34,14 @@ randomWord config = punct = if config.punctuation then - randomBool + falseWeightedBool else Random.constant False caps = if config.capitals then - randomBool + falseWeightedBool else Random.constant False diff --git a/src/Main.elm b/src/Main.elm index a91e12b..620ef7c 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -3,9 +3,15 @@ module Main exposing (..) import Array exposing (..) import Base exposing (..) import Browser -import Html exposing (..) -import Html.Attributes exposing (..) -import Html.Events exposing (onInput) +import Css +import Css.Global exposing (global, selector) +import Data exposing (..) +import Html +import Html.Styled exposing (..) +import Html.Styled.Attributes exposing (..) +import Html.Styled.Events exposing (onClick, onInput) +import Random +import Styles exposing (styledButton, styledInput, styledTextBox) import Task import Time import Utils exposing (isJust, isNothing) @@ -15,44 +21,36 @@ import Views exposing (..) main = Browser.element { init = init - , view = view + , view = view >> toUnstyled , update = update , subscriptions = subscriptions } -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) +stringsToWords : List String -> List Word +stringsToWords ls = + List.map (\w -> Word w Todo) ls + + +generateWords : Config -> Cmd Msg +generateWords config = + Random.generate LoadWords (randomParagraph config) + + +defaultModel : Model +defaultModel = + Model Nothing Nothing Array.empty Nothing defaultConfig 0 "" init : () -> ( Model, Cmd Msg ) init _ = - let - words = - generateWords - in - ( Model Nothing Nothing words Nothing (Array.length words) 0 "" - , Cmd.none + ( defaultModel + , generateWords defaultConfig ) @@ -73,7 +71,7 @@ firstWord model = lastWord : Model -> Bool lastWord model = - model.currentWord == model.length - 1 + model.currentWord == model.config.length - 1 wordEnded : String -> Bool @@ -129,6 +127,11 @@ update msg model = , Cmd.none ) + LoadWords ls -> + ( { model | words = Array.fromList <| stringsToWords ls } + , Cmd.none + ) + InputChanged s -> let cmd = @@ -145,6 +148,28 @@ update msg model = , cmd ) + WordLengthChanged n -> + let + oldConfig = + model.config + + newConfig = + { oldConfig | length = n } + in + ( { defaultModel | config = newConfig } + , generateWords newConfig + ) + + Redo -> + let + oldConfig = + model.config + + newModel = + { defaultModel | config = oldConfig } + in + ( newModel, generateWords oldConfig ) + NextWord -> if isJust model.end then ( model, Cmd.none ) @@ -180,7 +205,7 @@ toPara words current content = |> Array.map (\w -> span [ wordStyle w.status ] [ text (w.content ++ " ") ]) |> Array.set current (span [ wordStyle CurrentWord ] [ text (content ++ " ") ]) |> Array.toList - |> p [] + |> styledTextBox [] currentContents : Model -> String @@ -193,14 +218,14 @@ currentContents model = view : Model -> Html Msg view model = pre [] - [ 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 ("POS: " ++ viewProgress (model.currentWord + 1) model.length) ] + [ p [] [ text ("POS: " ++ viewProgress model) ] , p [] [ text ("WPM: " ++ viewWpm model) ] , p [] [ text ("ACC: " ++ viewAccuracy model.words) ] - , input [ onInput handleInputChanged, value model.inputBox ] [] + , viewWordLengthOptions + , toPara model.words model.currentWord (currentContents model) + , styledInput [ onInput handleInputChanged, value model.inputBox ] [] + , styledButton [ onClick Redo ] [ text "redo" ] + , global [ selector "li:not(:last-child)::after" [ Css.property "content" "' / '" ] ] ] -- cgit v1.2.3