diff options
-rw-r--r-- | src/Update/Update.elm | 9 | ||||
-rw-r--r-- | src/Update/UpdateTimerEdition.elm | 35 | ||||
-rw-r--r-- | src/Utils/List.elm | 7 |
3 files changed, 40 insertions, 11 deletions
diff --git a/src/Update/Update.elm b/src/Update/Update.elm index 5e8c13e..76f3f1d 100644 --- a/src/Update/Update.elm +++ b/src/Update/Update.elm @@ -98,8 +98,15 @@ update action model = ReadOnly -> { model | timerEdition <- Nothing } KeyPressed keyCode -> - { model | timerEdition <- Maybe.map (updateTimerEdition (keyCodeToNumberChar keyCode)) model.timerEdition } + if isRemoveKeyCode keyCode + then + { model | timerEdition <- Maybe.map (updateTimerEdition DeleteLast) model.timerEdition } + else + { model | timerEdition <- Maybe.map (updateTimerEdition (AddNumber keyCode)) model.timerEdition } updateTimerTime : TimerEdition -> Dict Id Timer -> Dict Id Timer updateTimerTime timerEdition = Dict.update timerEdition.id (Maybe.map (updateTimer (SetTime (toTime timerEdition.numbers)))) + +isRemoveKeyCode : KeyCode -> Bool +isRemoveKeyCode = (==) 8 diff --git a/src/Update/UpdateTimerEdition.elm b/src/Update/UpdateTimerEdition.elm index cff39f7..e9493eb 100644 --- a/src/Update/UpdateTimerEdition.elm +++ b/src/Update/UpdateTimerEdition.elm @@ -1,19 +1,34 @@ module Update.UpdateTimerEdition ( updateTimerEdition + , TimerEditionAction(..) ) where import Char (..) import Model.TimerEdition (..) -updateTimerEdition : Maybe Char -> TimerEdition -> TimerEdition -updateTimerEdition maybeChar timerEdition = - case maybeChar of - Just char -> - if isDigit char - then - { timerEdition | numbers <- char :: timerEdition.numbers } - else +import Utils.List (..) + +type TimerEditionAction = + DeleteLast + | AddNumber KeyCode + +updateTimerEdition : TimerEditionAction -> TimerEdition -> TimerEdition +updateTimerEdition action timerEdition = + case action of + DeleteLast -> + case maybeTail timerEdition.numbers of + Just tailNumbers -> + { timerEdition | numbers <- tailNumbers } + Nothing -> + timerEdition + AddNumber keyCode -> + case keyCodeToNumberChar keyCode of + Just char -> + if isDigit char + then + { timerEdition | numbers <- char :: timerEdition.numbers } + else + timerEdition + Nothing -> timerEdition - Nothing -> - timerEdition diff --git a/src/Utils/List.elm b/src/Utils/List.elm index 6895d83..64ebaa5 100644 --- a/src/Utils/List.elm +++ b/src/Utils/List.elm @@ -1,6 +1,7 @@ module Utils.List ( repeat , splitAt + , maybeTail ) where import List @@ -15,3 +16,9 @@ repeat count elem = splitAt : Int -> List a -> (List a, List a) splitAt n xs = (List.take n xs, List.drop n xs) + +maybeTail : List a -> Maybe (List a) +maybeTail xs = + case xs of + _ :: tl -> Just tl + _ -> Nothing |