From 541a13946aceca44df6b60d55196a29b7aaeffb6 Mon Sep 17 00:00:00 2001 From: Steven Vandevelde Date: Fri, 24 May 2019 16:12:47 +0200 Subject: [PATCH] Arrange playlists --- src/Library/Playlists.elm | 3 +- .../Tracks/Collection/Internal/Arrange.elm | 78 ++++++++++++++++++- 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/src/Library/Playlists.elm b/src/Library/Playlists.elm index be75018c..10b1e923 100644 --- a/src/Library/Playlists.elm +++ b/src/Library/Playlists.elm @@ -22,5 +22,4 @@ type alias IdentifiedPlaylistTrack = type alias Identifiers = - { index : Int - } + { index : Int } diff --git a/src/Library/Tracks/Collection/Internal/Arrange.elm b/src/Library/Tracks/Collection/Internal/Arrange.elm index d0f93295..1db9a85a 100644 --- a/src/Library/Tracks/Collection/Internal/Arrange.elm +++ b/src/Library/Tracks/Collection/Internal/Arrange.elm @@ -4,6 +4,7 @@ import Conditional exposing (ifThenElse) import Dict exposing (Dict) import List.Extra as List import Maybe.Extra as Maybe +import Playlists exposing (..) import Time import Time.Ext as Time import Tracks exposing (..) @@ -16,6 +17,24 @@ import Tracks.Sorting as Sorting arrange : Parcel -> Parcel arrange ( deps, collection ) = + case deps.selectedPlaylist of + Just playlist -> + if playlist.autoGenerated then + arrangeByGroup ( deps, collection ) + + else + arrangeByPlaylist ( deps, collection ) playlist + + Nothing -> + arrangeByGroup ( deps, collection ) + + + +-- GROUPING + + +arrangeByGroup : Parcel -> Parcel +arrangeByGroup ( deps, collection ) = case deps.grouping of Just AddedOnGroups -> ( deps, groupByInsertedAt deps collection ) @@ -33,10 +52,6 @@ arrange ( deps, collection ) = |> (\x -> ( deps, x )) - --- GROUPING - - addToList : a -> Maybe (List a) -> Maybe (List a) addToList item maybeList = case maybeList of @@ -173,3 +188,58 @@ groupByYearFolder ( i, t ) = Dict.update (Maybe.withDefault 0 t.tags.year) (addToList item) + + + +-- PLAYLISTS + + +arrangeByPlaylist : Parcel -> Playlist -> Parcel +arrangeByPlaylist ( deps, collection ) playlist = + collection.identified + |> matchWithPlaylist deps playlist + |> dealWithMissingPlaylistTracks + |> Sorting.sort PlaylistIndex Asc + |> (\x -> { collection | arranged = x }) + |> (\x -> ( deps, x )) + + +matchWithPlaylist : CollectionDependencies -> Playlist -> List IdentifiedTrack -> ( List IdentifiedTrack, List IdentifiedPlaylistTrack ) +matchWithPlaylist deps playlist = + List.foldl + (\( i, t ) ( identifiedTracks, remainingPlaylistTracks ) -> + let + imaginaryPlaylistTrack = + { album = t.tags.album + , artist = t.tags.artist + , title = t.tags.title + } + + ( matches, remainingPlaylistTracksWithoutMatches ) = + List.foldl + (\( pi, pt ) -> + if imaginaryPlaylistTrack == pt then + Tuple.mapBoth + ((::) ( { i | indexInPlaylist = Just pi.index }, t )) + identity + + else + Tuple.mapBoth + identity + ((::) ( pi, pt )) + ) + ( [], [] ) + remainingPlaylistTracks + in + ( identifiedTracks ++ matches + , remainingPlaylistTracksWithoutMatches + ) + ) + ( [] + , List.indexedMap (\idx -> Tuple.pair { index = idx }) playlist.tracks + ) + + +dealWithMissingPlaylistTracks : ( List IdentifiedTrack, List IdentifiedPlaylistTrack ) -> List IdentifiedTrack +dealWithMissingPlaylistTracks ( identifiedTracks, remainingPlaylistTracks ) = + identifiedTracks -- knotmirror 0.0.1