Better requests, code needs cleaning

This commit is contained in:
Thomas Forgione 2020-10-04 23:03:38 +02:00
parent 65c8e5935b
commit 72b0e8775c
4 changed files with 107 additions and 37 deletions

View File

@ -19,6 +19,7 @@
var lastId, player;
if (app.ports !== undefined && app.ports.registerVideo !== undefined) {
app.ports.registerVideo.subscribe(function(args) {
var time = parseInt(args[2], 10) || undefined;
@ -35,6 +36,7 @@
}
});
});
}
</script>
</body>
</html>

View File

@ -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 )

View File

@ -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

View File

@ -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 ) =