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