From a6c651bdd2dcaad8eaf6eb77e3d19c2bf1c928b4 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Wed, 28 Oct 2020 16:57:08 +0100 Subject: [PATCH] Better timezone --- elm.json | 1 + src/Core.elm | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/elm.json b/elm.json index a4af880..ec3e93a 100644 --- a/elm.json +++ b/elm.json @@ -15,6 +15,7 @@ "elm/time": "1.0.0", "elm/url": "1.0.0", "jims/html-parser": "1.0.0", + "justinmimbs/timezone-data": "3.0.3", "mdgriffith/elm-ui": "1.1.8", "rtfeldman/elm-iso8601-date-strings": "1.1.3" }, diff --git a/src/Core.elm b/src/Core.elm index 9cbfecc..a48aeb7 100644 --- a/src/Core.elm +++ b/src/Core.elm @@ -10,6 +10,7 @@ import Http import Ports import Task import Time +import TimeZone import Twitch import Url @@ -44,6 +45,7 @@ type Msg | UrlReceived Url.Url | UrlRequested Browser.UrlRequest | SizeReceived Int Int + | TimeZoneReceivedResult (Result TimeZone.Error ( String, Time.Zone )) | TimeZoneReceived Time.Zone | HoverPlaylist Twitch.Playlist | HoverVideo Twitch.Video @@ -55,7 +57,9 @@ init : { width : Int, height : Int } -> Url.Url -> Nav.Key -> ( FullModel, Cmd M init { width, height } url key = ( Unloaded (Element.classifyDevice { width = width, height = height }) url key Time.utc , Cmd.batch - [ Task.perform TimeZoneReceived Time.here + [ Task.attempt TimeZoneReceivedResult TimeZone.getZone + + -- Task.perform TimeZoneReceived Time.here , Twitch.fetchPlaylists resultToMsg ] ) @@ -88,6 +92,18 @@ update msg model = ( TimeZoneReceived z, Unloaded d u k _ ) -> ( Unloaded d u k z, Cmd.none ) + ( TimeZoneReceived z, Loaded m ) -> + ( Loaded { m | zone = z }, Cmd.none ) + + ( TimeZoneReceivedResult (Ok ( _, zone )), Unloaded d u k _ ) -> + ( Unloaded d u k zone, Cmd.none ) + + ( TimeZoneReceivedResult (Ok ( _, zone )), Loaded m ) -> + ( Loaded { m | zone = zone }, Cmd.none ) + + ( TimeZoneReceivedResult (Err _), _ ) -> + ( model, Task.perform TimeZoneReceived Time.here ) + ( TimeReceived p, Loaded m ) -> ( Loaded { m | time = p }, Cmd.none ) @@ -121,9 +137,6 @@ update msg model = Video p v _ -> ( Loaded { m | page = Video p v Nothing }, Cmd.none ) - ( TimeZoneReceived z, Loaded m ) -> - ( Loaded { m | zone = z }, Cmd.none ) - ( SizeReceived w h, Loaded m ) -> ( Loaded { m | device = Element.classifyDevice { width = w, height = h } } , Cmd.none