From 5b902ec915e901b15c8c33481de8bbc692395892 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Fri, 6 Nov 2020 11:15:54 +0100 Subject: [PATCH] Sort playlists by last video date --- src/Twitch.elm | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Twitch.elm b/src/Twitch.elm index 8fb9a38..e388c19 100644 --- a/src/Twitch.elm +++ b/src/Twitch.elm @@ -50,7 +50,32 @@ decodePlaylist = decodePlaylists : Decode.Decoder (List Playlist) decodePlaylists = - Decode.map (List.sortBy .url >> List.reverse) (Decode.list decodePlaylist) + Decode.map (sortPlaylists >> List.reverse) (Decode.list decodePlaylist) + + +mostRecentVideo : List Video -> Maybe Time.Posix +mostRecentVideo videos = + case ( videos, List.map .date videos ) of + ( _, (Just t) :: _ ) -> + Just t + + ( _ :: t, Nothing :: _ ) -> + mostRecentVideo t + + _ -> + Nothing + + +playlistDate : Playlist -> Int +playlistDate playlist = + mostRecentVideo playlist.videos + |> Maybe.map Time.posixToMillis + |> Maybe.withDefault 0 + + +sortPlaylists : List Playlist -> List Playlist +sortPlaylists list = + List.sortBy playlistDate list fetchPlaylists : (Result Http.Error (List Playlist) -> msg) -> Cmd msg