diff options
Diffstat (limited to 'src/Edition')
-rw-r--r-- | src/Edition/Model.elm | 45 | ||||
-rw-r--r-- | src/Edition/Model/Name.elm | 19 | ||||
-rw-r--r-- | src/Edition/Model/Time.elm | 52 | ||||
-rw-r--r-- | src/Edition/Msg.elm | 9 | ||||
-rw-r--r-- | src/Edition/Update.elm | 30 |
5 files changed, 155 insertions, 0 deletions
diff --git a/src/Edition/Model.elm b/src/Edition/Model.elm new file mode 100644 index 0000000..5c1b295 --- /dev/null +++ b/src/Edition/Model.elm @@ -0,0 +1,45 @@ +module Edition.Model exposing + ( Edition + , Kind(..) + , newEdition + , keyCodeToChar + , isEmpty + ) + +import String + +import Model.Id exposing (..) +import Model.Keyboard exposing (KeyCode) + +import Edition.Model.Name as NameEdition +import Edition.Model.Time as TimeEdition + +type alias Edition = + { id : Id + , kind : Kind + , chars : List Char + } + +type Kind = + Name + | Time + +newEdition id kind = + { id = id + , kind = kind + , chars = [] + } + +keyCodeToChar : Kind -> KeyCode -> Maybe Char +keyCodeToChar kind = + case kind of + Name -> NameEdition.keyCodeToChar + Time -> TimeEdition.keyCodeToChar + +isEmpty : Edition -> Bool +isEmpty edition = + edition.chars + |> String.fromList + |> String.trim + |> String.length + |> (==) 0 diff --git a/src/Edition/Model/Name.elm b/src/Edition/Model/Name.elm new file mode 100644 index 0000000..19f4c04 --- /dev/null +++ b/src/Edition/Model/Name.elm @@ -0,0 +1,19 @@ +module Edition.Model.Name exposing + ( keyCodeToChar + , renderNameEdition + ) + +import Char +import String +import List + +import Model.Keyboard exposing (KeyCode) + +keyCodeToChar : KeyCode -> Maybe Char +keyCodeToChar = Just << Char.fromCode + +renderNameEdition : List Char -> String +renderNameEdition chars = + chars + |> List.reverse + |> String.fromList diff --git a/src/Edition/Model/Time.elm b/src/Edition/Model/Time.elm new file mode 100644 index 0000000..35971c3 --- /dev/null +++ b/src/Edition/Model/Time.elm @@ -0,0 +1,52 @@ +module Edition.Model.Time exposing + ( keyCodeToChar + , toTime + , toMinutesAndSeconds + ) + +import Time exposing (Time) +import List +import Array +import String +import Char + +import Model.Keyboard exposing (KeyCode) +import Utils.List exposing (..) +import Utils.Maybe exposing (..) + +keyCodeToChar : KeyCode -> Maybe Char +keyCodeToChar code = + let char = Char.fromCode code + in if Char.isDigit char + then Just char + else Nothing + +toTime : List Char -> Time +toTime numbers = + numbers + |> toMinutesAndSeconds + |> \(a, b) -> (stringToInt a, stringToInt b) + |> \(minutes, seconds) -> (toFloat minutes) * 60 * 1000 + (toFloat seconds) * 1000 + +toMinutesAndSeconds : List Char -> (String, String) +toMinutesAndSeconds numbers = + numbers + |> List.take 4 + |> List.reverse + |> completeBegin '0' 4 + |> splitAt 2 + |> \(a, b) -> (String.fromList a, String.fromList b) + +completeBegin : a -> Int -> List a -> List a +completeBegin x count xs = + let length = List.length xs + in List.append (repeat (count - length) x) xs + +stringToInt : String -> Int +stringToInt str = + str + |> String.toInt + |> \res -> + case res of + Ok n -> n + Err _ -> 0 diff --git a/src/Edition/Msg.elm b/src/Edition/Msg.elm new file mode 100644 index 0000000..546e45b --- /dev/null +++ b/src/Edition/Msg.elm @@ -0,0 +1,9 @@ +module Edition.Msg exposing + ( Msg(..) + ) + +import Char exposing (KeyCode) + +type Msg = + DeleteLast + | AddChar KeyCode diff --git a/src/Edition/Update.elm b/src/Edition/Update.elm new file mode 100644 index 0000000..e219629 --- /dev/null +++ b/src/Edition/Update.elm @@ -0,0 +1,30 @@ +module Edition.Update exposing + ( updateEdition + ) + +import Char +import Char exposing (KeyCode) +import List + +import Edition.Model exposing (..) +import Edition.Msg exposing (..) + +updateEdition : Msg -> Edition -> Edition +updateEdition msg edition = + case msg of + + DeleteLast -> + case List.tail edition.chars of + Just tailChars -> + { edition | chars = tailChars } + Nothing -> + edition + + AddChar keyCode -> + case keyCodeToChar edition.kind keyCode of + Just char -> + if keyCode == 32 && List.head edition.chars == Just (Char.fromCode 32) + then edition + else { edition | chars = char :: edition.chars } + Nothing -> + edition |