From 90ad495bafd94a9246f7214270f1f5d822a001bf Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Sun, 4 Oct 2020 16:02:54 +0200 Subject: [PATCH] Navigation works --- src/Core.elm | 70 ++++++++++++++++++++++++++++++++++++++++++-------- src/Main.elm | 15 ++--------- src/Twitch.elm | 6 +++++ src/Views.elm | 2 +- 4 files changed, 69 insertions(+), 24 deletions(-) diff --git a/src/Core.elm b/src/Core.elm index 23e8a5d..e532e93 100644 --- a/src/Core.elm +++ b/src/Core.elm @@ -1,6 +1,6 @@ module Core exposing (FullModel(..), Model, Msg(..), Page(..), init, update) -import Browser.Navigation +import Browser.Navigation as Nav import Json.Decode as Decode import Task import Time @@ -9,7 +9,7 @@ import Url type FullModel - = Unloaded + = Unloaded Url.Url Nav.Key | Loaded Model @@ -17,6 +17,7 @@ type alias Model = { playlists : List Twitch.Playlist , zone : Time.Zone , page : Page + , key : Nav.Key } @@ -32,11 +33,12 @@ type Msg | HomeClicked | PlaylistClicked Twitch.Playlist | VideoClicked Twitch.Playlist Twitch.Video + | UrlReceived Url.Url -init : Decode.Value -> Url.Url -> Browser.Navigation.Key -> ( FullModel, Cmd Msg ) -init _ _ _ = - ( Unloaded +init : Decode.Value -> Url.Url -> Nav.Key -> ( FullModel, Cmd Msg ) +init _ url key = + ( Unloaded url key , Task.perform PlaylistsReceived Twitch.fetchPlaylists ) @@ -47,17 +49,65 @@ update msg model = ( Noop, _ ) -> ( model, Cmd.none ) - ( PlaylistsReceived ( playlists, zone ), _ ) -> - ( Loaded { playlists = playlists, zone = zone, page = Home }, Cmd.none ) + ( PlaylistsReceived ( playlists, zone ), Unloaded url key ) -> + update + (UrlReceived url) + (Loaded { key = key, playlists = playlists, zone = zone, page = Home }) ( HomeClicked, Loaded m ) -> - ( Loaded { m | page = Home }, Cmd.none ) + ( Loaded { m | page = Home } + , Nav.pushUrl m.key "#" + ) ( PlaylistClicked playlist, Loaded m ) -> - ( Loaded { m | page = Playlist playlist }, Cmd.none ) + ( Loaded { m | page = Playlist playlist } + , Nav.pushUrl m.key ("#" ++ playlist.url) + ) ( VideoClicked playlist video, Loaded m ) -> - ( Loaded { m | page = Video playlist video }, Cmd.none ) + ( Loaded { m | page = Video playlist video } + , Nav.pushUrl m.key ("#" ++ playlist.url ++ Twitch.videoName video) + ) + + ( UrlReceived url, Loaded m ) -> + let + split = + String.split "/" (Maybe.withDefault "" url.fragment) + + ( playlistName, videoName ) = + case split of + p :: v :: _ -> + ( Just (p ++ "/"), Just (v ++ "/") ) + + p :: _ -> + ( Just (p ++ "/"), Nothing ) + + _ -> + ( Nothing, Nothing ) + + playlist = + List.head (List.filter (\x -> Just x.url == playlistName) m.playlists) + + video = + case playlist of + Just p -> + List.head (List.filter (\x -> Just (Twitch.videoName x) == videoName) p.videos) + + _ -> + Nothing + + page = + case ( playlist, video ) of + ( Just p, Just v ) -> + Video p v + + ( Just p, Nothing ) -> + Playlist p + + _ -> + Home + in + ( Loaded { m | page = page }, Cmd.none ) _ -> ( model, Cmd.none ) diff --git a/src/Main.elm b/src/Main.elm index 6cdfde0..db787e8 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -3,7 +3,6 @@ module Main exposing (main) import Browser import Core import Json.Decode as Decode -import Url import Views @@ -14,16 +13,6 @@ main = , update = Core.update , view = Views.view , subscriptions = \_ -> Sub.none - , onUrlChange = onUrlChange - , onUrlRequest = onUrlRequest + , onUrlChange = Core.UrlReceived + , onUrlRequest = \_ -> Core.Noop } - - -onUrlChange : Url.Url -> Core.Msg -onUrlChange _ = - Core.Noop - - -onUrlRequest : Browser.UrlRequest -> Core.Msg -onUrlRequest _ = - Core.Noop diff --git a/src/Twitch.elm b/src/Twitch.elm index 3aeadcc..b16c54b 100644 --- a/src/Twitch.elm +++ b/src/Twitch.elm @@ -4,6 +4,7 @@ module Twitch exposing , fetchPlaylists , playlistMiniatureUrl , videoMiniatureUrl + , videoName ) import Html.Parser @@ -29,6 +30,11 @@ type alias Video = } +videoName : Video -> String +videoName video = + String.join "/" (List.drop 3 (String.split "/" video.url)) + + get : { url : String, resolver : Http.Resolver x a } -> Task x a get { url, resolver } = Http.task diff --git a/src/Views.elm b/src/Views.elm index 63ac877..146804e 100644 --- a/src/Views.elm +++ b/src/Views.elm @@ -29,7 +29,7 @@ viewContent model = let content = case model of - Core.Unloaded -> + Core.Unloaded _ _ -> Element.none Core.Loaded submodel ->