diff options
author | Joris Guyonvarch | 2015-03-18 00:32:17 +0100 |
---|---|---|
committer | Joris Guyonvarch | 2015-03-18 00:32:17 +0100 |
commit | 2613aeeae0f4de44842ff0e796603d0316a61a14 (patch) | |
tree | 722a21c69597e6d2547ad9f991e7c73afae78058 /src/Model/TimerEdition.elm | |
parent | 0075abf51db5d1b54117525d7a7f9b06e31c9484 (diff) |
Can edit the time, unfortunately the enter key is overriden so the '+' key of the numpad is used instead
Diffstat (limited to 'src/Model/TimerEdition.elm')
-rw-r--r-- | src/Model/TimerEdition.elm | 56 |
1 files changed, 55 insertions, 1 deletions
diff --git a/src/Model/TimerEdition.elm b/src/Model/TimerEdition.elm index c7b90c5..5feea33 100644 --- a/src/Model/TimerEdition.elm +++ b/src/Model/TimerEdition.elm @@ -1,16 +1,70 @@ module Model.TimerEdition ( TimerEdition + , Numbers , newTimerEdition + , keyCodeToChar + , toTime + , toMinutesAndSeconds ) where +import Time (Time) +import List +import Array +import String +import Keyboard (KeyCode) + import Model.Id (..) +import Utils.List (..) + type alias TimerEdition = { id : Id - , numbers : List Int + , numbers : Numbers } +type alias Numbers = List Char + newTimerEdition id = { id = id , numbers = [] } + +keyCodeToChar : KeyCode -> Maybe Char +keyCodeToChar code = + let zero = 96 + 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 : Numbers -> Time +toTime numbers = + numbers + |> toMinutesAndSeconds + |> \(a, b) -> (stringToInt a, stringToInt b) + |> \(minutes, seconds) -> (toFloat minutes) * 60 * 1000 + (toFloat seconds) * 1000 + +toMinutesAndSeconds : Numbers -> (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 |