diff options
Diffstat (limited to 'src/Model/Edition')
-rw-r--r-- | src/Model/Edition/Edition.elm | 34 | ||||
-rw-r--r-- | src/Model/Edition/NameEdition.elm | 15 | ||||
-rw-r--r-- | src/Model/Edition/TimeEdition.elm | 60 |
3 files changed, 109 insertions, 0 deletions
diff --git a/src/Model/Edition/Edition.elm b/src/Model/Edition/Edition.elm new file mode 100644 index 0000000..89a0b52 --- /dev/null +++ b/src/Model/Edition/Edition.elm @@ -0,0 +1,34 @@ +module Model.Edition.Edition + ( Edition + , Kind(..) + , newEdition + , keyCodeToChar + ) where + +import Keyboard (KeyCode) + +import Model.Id (..) +import Model.Edition.NameEdition as NameEdition +import Model.Edition.TimeEdition 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 diff --git a/src/Model/Edition/NameEdition.elm b/src/Model/Edition/NameEdition.elm new file mode 100644 index 0000000..18224ea --- /dev/null +++ b/src/Model/Edition/NameEdition.elm @@ -0,0 +1,15 @@ +module Model.Edition.NameEdition + ( keyCodeToChar + , renderNameEdition + ) where + +import Char +import Keyboard (KeyCode) +import String +import List + +keyCodeToChar : KeyCode -> Maybe Char +keyCodeToChar = Just << Char.fromCode + +renderNameEdition : List Char -> String +renderNameEdition = String.fromList << List.reverse diff --git a/src/Model/Edition/TimeEdition.elm b/src/Model/Edition/TimeEdition.elm new file mode 100644 index 0000000..6999b25 --- /dev/null +++ b/src/Model/Edition/TimeEdition.elm @@ -0,0 +1,60 @@ +module Model.Edition.TimeEdition + ( keyCodeToChar + , toTime + , toMinutesAndSeconds + ) where + +import Time (Time) +import List +import Array +import String +import Keyboard (KeyCode) + +import Utils.List (..) +import Utils.Maybe (..) + +keyCodeToChar : KeyCode -> Maybe Char +keyCodeToChar code = + List.map (flip keyCodeToCharFromZero code) zeroKeyCodes + |> List.foldl orElse Nothing + +zeroKeyCodes = [48, 96] + +keyCodeToCharFromZero : KeyCode -> KeyCode -> Maybe Char +keyCodeToCharFromZero zero code = + let nine = zero + 9 + in if code >= zero && code <= nine + then ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] + |> Array.fromList + |> Array.get (code - zero) + 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 |