From 72b0e8775c562e7d015aa0a0dbe8952b9adbb6d2 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Sun, 4 Oct 2020 23:03:38 +0200 Subject: [PATCH] Better requests, code needs cleaning --- index.html | 28 ++++++++++-------- src/Core.elm | 80 ++++++++++++++++++++++++++++++++++++++++---------- src/Twitch.elm | 26 ++++++++++++++-- src/Views.elm | 10 +++---- 4 files changed, 107 insertions(+), 37 deletions(-) diff --git a/index.html b/index.html index 5f7eb68..be6e215 100644 --- a/index.html +++ b/index.html @@ -19,22 +19,24 @@ var lastId, player; - app.ports.registerVideo.subscribe(function(args) { - var time = parseInt(args[2], 10) || undefined; + if (app.ports !== undefined && app.ports.registerVideo !== undefined) { + app.ports.registerVideo.subscribe(function(args) { + var time = parseInt(args[2], 10) || undefined; - requestAnimationFrame(function() { - if (args[0] !== lastId) { - lastId = args[0]; + requestAnimationFrame(function() { + if (args[0] !== lastId) { + lastId = args[0]; - player = vd.setup(args[0], { - v: args[1] + "/manifest.mpd", - t: parseInt(args[2], 10) || 0 - }); - } else if (time !== undefined ){ - player.currentTime(time); - } + player = vd.setup(args[0], { + v: args[1] + "/manifest.mpd", + t: parseInt(args[2], 10) || 0 + }); + } else if (time !== undefined ){ + player.currentTime(time); + } + }); }); - }); + } diff --git a/src/Core.elm b/src/Core.elm index 026a1b4..199390c 100644 --- a/src/Core.elm +++ b/src/Core.elm @@ -27,16 +27,18 @@ type alias Model = type Page = Home - | Playlist Twitch.Playlist - | Video Twitch.Playlist Twitch.Video + | Playlist Twitch.PlaylistWithVideos + | Video Twitch.PlaylistWithVideos Twitch.Video type Msg = Noop | PlaylistsReceived ( List Twitch.Playlist, Time.Zone ) | HomeClicked - | PlaylistClicked Twitch.Playlist - | VideoClicked Twitch.Playlist Twitch.Video + | PlaylistClicked String + | PlaylistReceived Twitch.PlaylistWithVideos + | PlaylistReceivedVideo String (Maybe Int) Twitch.PlaylistWithVideos + | VideoClicked Twitch.PlaylistWithVideos Twitch.Video | UrlReceived Url.Url | SizeReceived Int Int @@ -76,7 +78,7 @@ update msg model = ( PlaylistClicked playlist, Loaded m ) -> ( model - , Nav.pushUrl m.key ("#" ++ playlist.url) + , Nav.pushUrl m.key ("#" ++ playlist) ) ( VideoClicked playlist video, Loaded m ) -> @@ -92,10 +94,14 @@ update msg model = ( split, args ) = case splits of h1 :: h2 :: _ -> - ( String.split "/" h1, parseQueryString h2 ) + ( String.split "/" h1 |> List.filter (not << String.isEmpty) + , parseQueryString h2 + ) h1 :: _ -> - ( String.split "/" h1, Dict.empty ) + ( String.split "/" h1 |> List.filter (not << String.isEmpty) + , Dict.empty + ) _ -> ( [], Dict.empty ) @@ -123,31 +129,73 @@ update msg model = ( Nothing, Nothing ) playlist = - List.head (List.filter (\x -> Just x.url == playlistName) m.playlists) + case m.page of + Playlist p -> + Just p - video = + Video p _ -> + Just p + + _ -> + Nothing + + realVideo = case playlist of Just p -> - List.head (List.filter (\x -> Just (Twitch.videoName x) == videoName) p.videos) + case List.head (List.filter (\x -> Just (Twitch.videoName x) == videoName) p.videos) of + Just v -> + Just v + + _ -> + Nothing _ -> Nothing ( page, cmd ) = - case ( playlist, video ) of - ( Just p, Just v ) -> - ( Video p v - , Ports.registerVideo ( Twitch.videoId v, v.url, time ) + case ( ( playlist, realVideo ), ( playlistName, videoName ) ) of + ( ( Just p, _ ), ( Just _, Nothing ) ) -> + ( Playlist p + , Cmd.none ) - ( Just p, Nothing ) -> - ( Playlist p, Cmd.none ) + ( ( Just p, Just video ), ( Just _, Just _ ) ) -> + ( Video p video + , Ports.registerVideo ( Twitch.videoId video, video.url, time ) + ) + + ( ( _, _ ), ( Just name, Nothing ) ) -> + ( m.page + , Task.perform + PlaylistReceived + (Twitch.fetchPlaylistWithVideos name) + ) + + ( ( _, _ ), ( Just name, Just video ) ) -> + ( m.page + , Task.perform + (PlaylistReceivedVideo video time) + (Twitch.fetchPlaylistWithVideos name) + ) _ -> ( Home, Cmd.none ) in ( Loaded { m | page = page }, cmd ) + ( PlaylistReceived p, Loaded m ) -> + ( Loaded { m | page = Playlist p }, Cmd.none ) + + ( PlaylistReceivedVideo video time playlist, Loaded m ) -> + case List.head (List.filter (\x -> Twitch.videoName x == video) playlist.videos) of + Just v -> + ( Loaded { m | page = Video playlist v } + , Ports.registerVideo ( Twitch.videoId v, v.url, time ) + ) + + _ -> + ( Loaded { m | page = Home }, Cmd.none ) + _ -> ( model, Cmd.none ) diff --git a/src/Twitch.elm b/src/Twitch.elm index 54423d9..2af19ea 100644 --- a/src/Twitch.elm +++ b/src/Twitch.elm @@ -1,6 +1,8 @@ module Twitch exposing ( Playlist + , PlaylistWithVideos , Video + , fetchPlaylistWithVideos , fetchPlaylists , playlistMiniatureUrl , videoId @@ -17,6 +19,13 @@ import Time type alias Playlist = + { url : String + , name : String + , videos : List String + } + + +type alias PlaylistWithVideos = { url : String , name : String , videos : List Video @@ -57,7 +66,7 @@ playlistMiniatureUrl : Playlist -> String playlistMiniatureUrl playlist = case List.head playlist.videos of Just v -> - v.url ++ "miniature-050.png" + "videos/" ++ playlist.url ++ v ++ "miniature-050.png" _ -> "" @@ -70,7 +79,7 @@ videoMiniatureUrl video = sortPlaylist : Playlist -> Playlist sortPlaylist playlist = - { playlist | videos = List.sortBy .url playlist.videos |> List.reverse } + { playlist | videos = List.sort playlist.videos |> List.reverse } sortPlaylists : List Playlist -> List Playlist @@ -105,11 +114,22 @@ fetchPlaylist name = |> Task.andThen (\a -> fetchPlaylistVideoPaths name - |> Task.andThen (\c -> fetchVideos name c) + -- |> Task.andThen (\c -> fetchVideos name c) |> Task.map (Playlist name a) ) +fetchPlaylistWithVideos : String -> Task x PlaylistWithVideos +fetchPlaylistWithVideos name = + fetchPlaylistName name + |> Task.andThen + (\a -> + fetchPlaylistVideoPaths name + |> Task.andThen (\c -> fetchVideos name c) + |> Task.map (PlaylistWithVideos name a) + ) + + fetchVideo : String -> String -> Task x Video fetchVideo playlist video = let diff --git a/src/Views.elm b/src/Views.elm index 028e28f..81f5f51 100644 --- a/src/Views.elm +++ b/src/Views.elm @@ -171,13 +171,13 @@ playlistView playlist = button = Input.button [ Element.width Element.fill, Element.alignTop ] { label = display - , onPress = Just (Core.PlaylistClicked playlist) + , onPress = Just (Core.PlaylistClicked playlist.url) } in button -videoMiniaturesView : Element.Device -> Time.Zone -> Twitch.Playlist -> Element Core.Msg +videoMiniaturesView : Element.Device -> Time.Zone -> Twitch.PlaylistWithVideos -> Element Core.Msg videoMiniaturesView device zone playlist = let empty = @@ -231,7 +231,7 @@ videoMiniature video = image -videoMiniatureView : Time.Zone -> Twitch.Playlist -> Twitch.Video -> Element Core.Msg +videoMiniatureView : Time.Zone -> Twitch.PlaylistWithVideos -> Twitch.Video -> Element Core.Msg videoMiniatureView zone playlist video = let display = @@ -249,7 +249,7 @@ videoMiniatureView zone playlist video = button -videoInList : Time.Zone -> Twitch.Playlist -> Twitch.Video -> Twitch.Video -> Element Core.Msg +videoInList : Time.Zone -> Twitch.PlaylistWithVideos -> Twitch.Video -> Twitch.Video -> Element Core.Msg videoInList zone playlist activeVideo video = let ( msg, attr ) = @@ -276,7 +276,7 @@ videoInList zone playlist activeVideo video = Input.button attr { label = label, onPress = msg } -videoView : Element.Device -> Time.Zone -> Twitch.Playlist -> Twitch.Video -> Element Core.Msg +videoView : Element.Device -> Time.Zone -> Twitch.PlaylistWithVideos -> Twitch.Video -> Element Core.Msg videoView device zone playlist video = let ( builder, contentPadding ) =