From 4eff66922ec380c836446ed2b4247cad87b56345 Mon Sep 17 00:00:00 2001 From: Steven Vandevelde Date: Wed, 29 Jan 2025 18:54:56 +0100 Subject: [PATCH] feat: Add additional command to palette and close #438 --- src/Core/Brain/Tracks/State.elm | 39 +++++++++++---- src/Core/UI/Commands/Alfred.elm | 71 ++++++++++++++++++++++++--- src/Core/UI/Playlists/ContextMenu.elm | 21 ++++---- src/Core/UI/Tracks/State.elm | 7 +-- src/Core/UI/Tracks/Types.elm | 2 +- src/Library/Tracks.elm | 5 ++ 6 files changed, 113 insertions(+), 32 deletions(-) diff --git a/src/Core/Brain/Tracks/State.elm b/src/Core/Brain/Tracks/State.elm index fde0b89c..966065a5 100644 --- a/src/Core/Brain/Tracks/State.elm +++ b/src/Core/Brain/Tracks/State.elm @@ -45,10 +45,14 @@ add list model = download : Json.Value -> Manager download json model = let - ( zipName, trackIds ) = + { prefixTrackNumber, trackIds, zipName } = json |> Json.decodeValue downloadParamsDecoder - |> Result.withDefault ( "?", [] ) + |> Result.withDefault + { prefixTrackNumber = False + , trackIds = [] + , zipName = "failed-to-decode-json" + } in model.hypaethralUserData.tracks |> Tracks.pick trackIds @@ -57,10 +61,14 @@ download json model = (\( idx, track ) -> Json.Encode.object [ ( "filename" - , [ (idx + 1) - |> String.fromInt - |> String.padLeft 2 '0' - , " - " + , [ if prefixTrackNumber then + (idx + 1) + |> String.fromInt + |> String.padLeft 2 '0' + |> (\s -> s ++ " - ") + + else + "" , track.tags.artist |> Maybe.map (\a -> a ++ " - ") |> Maybe.withDefault "" @@ -293,12 +301,23 @@ updateSearchIndex data = -- ⚗️ -downloadParamsDecoder : Decoder ( String, List String ) +downloadParamsDecoder : + Decoder + { prefixTrackNumber : Bool + , trackIds : List String + , zipName : String + } downloadParamsDecoder = - Json.map2 - Tuple.pair - (Json.field "zipName" <| Json.string) + Json.map3 + (\a b c -> + { prefixTrackNumber = a + , trackIds = b + , zipName = c + } + ) + (Json.field "prefixTrackNumber" <| Json.bool) (Json.field "trackIds" <| Json.list Json.string) + (Json.field "zipName" <| Json.string) makeTrackUrl : Time.Posix -> String -> Maybe Source -> HttpMethod -> String diff --git a/src/Core/UI/Commands/Alfred.elm b/src/Core/UI/Commands/Alfred.elm index a117ea00..716da491 100644 --- a/src/Core/UI/Commands/Alfred.elm +++ b/src/Core/UI/Commands/Alfred.elm @@ -4,6 +4,7 @@ import Alfred exposing (..) import Conditional exposing (ifThenElse) import List.Extra as List import Material.Icons.Round as Icons +import Playlists.Matching import Tracks exposing (Grouping(..), SortBy(..)) import UI.Page as Page import UI.Queue.Types as Queue @@ -161,22 +162,76 @@ playlistCommands model = selection = case model.selectedPlaylist of Just playlist -> - [ -- DeselectPlaylist - { icon = Just (Icons.waves 16) + let + identifiedTracksFromPlaylist = + model.tracks.identified + |> Playlists.Matching.match playlist + |> Tuple.first + + tracksFromPlaylist = + identifiedTracksFromPlaylist + |> (if playlist.collection then + identity + + else + Tracks.sortByIndexInPlaylist + ) + |> List.map Tuple.second + in + [ { icon = Just (Icons.waves 16) , title = "Deactivate " ++ ifThenElse playlist.collection "collection" "playlist" , value = Command UI.DeselectPlaylist } + + -- + , { icon = Just (Icons.update 16) + , title = "Add to queue" + , value = + { inFront = False, tracks = identifiedTracksFromPlaylist } + |> Queue.AddTracks + |> UI.QueueMsg + |> Command + } + + -- + , { icon = Just (Icons.offline_bolt 16) + , title = "Store in cache" + , value = + tracksFromPlaylist + |> Tracks.StoreInCache + |> UI.TracksMsg + |> Command + } + + -- + , { icon = Just (Icons.archive 16) + , title = "Download as zip file" + , value = + tracksFromPlaylist + |> Tracks.Download + { prefixTrackNumber = not playlist.collection + , zipName = playlist.name + } + |> UI.TracksMsg + |> Command + } ] Nothing -> [] in - selection - ++ [ { icon = Just (Icons.waves 16) - , title = "Select collection or playlist" - , value = Command UI.AssistWithSelectingPlaylist - } - ] + [ { icon = Just (Icons.waves 16) + , title = + case model.selectedPlaylist of + Just _ -> + "Select other collection or playlist" + + Nothing -> + "Select collection or playlist" + , value = Command UI.AssistWithSelectingPlaylist + } + ] + ++ selection selectionCommands model = diff --git a/src/Core/UI/Playlists/ContextMenu.elm b/src/Core/UI/Playlists/ContextMenu.elm index a7ce6c0e..97ed6518 100644 --- a/src/Core/UI/Playlists/ContextMenu.elm +++ b/src/Core/UI/Playlists/ContextMenu.elm @@ -36,16 +36,14 @@ listMenu playlist allTracks confirmation coordinates = |> Tuple.first tracksFromPlaylist = - case playlist.autoGenerated of - Just _ -> - identifiedTracksFromPlaylist - |> Tracks.Sorting.sort Album Asc - |> List.map Tuple.second + identifiedTracksFromPlaylist + |> (if playlist.collection then + identity - Nothing -> - identifiedTracksFromPlaylist - |> List.sortBy (\( i, t ) -> Maybe.withDefault (t.tags.disc * 1000 + t.tags.nr) i.indexInPlaylist) - |> List.map Tuple.second + else + Tracks.sortByIndexInPlaylist + ) + |> List.map Tuple.second menuMsg = ShowPlaylistListMenu @@ -119,7 +117,10 @@ downloadAsZip tracksFromPlaylist playlist = , label = "Download as zip file" , msg = tracksFromPlaylist - |> Tracks.Download playlist.name + |> Tracks.Download + { prefixTrackNumber = not playlist.collection + , zipName = playlist.name + } |> TracksMsg -- diff --git a/src/Core/UI/Tracks/State.elm b/src/Core/UI/Tracks/State.elm index 84d30a1f..4b9a8863 100644 --- a/src/Core/UI/Tracks/State.elm +++ b/src/Core/UI/Tracks/State.elm @@ -266,8 +266,8 @@ deselectCover model = { model | selectedCover = Nothing } -download : String -> List Track -> Manager -download zipName tracks model = +download : { prefixTrackNumber : Bool, zipName : String } -> List Track -> Manager +download { prefixTrackNumber, zipName } tracks model = let notification = Notifications.stickyCasual "Downloading tracks ..." @@ -275,12 +275,13 @@ download zipName tracks model = downloading = Just { notificationId = Notifications.id notification } in - [ ( "zipName", Json.Encode.string zipName ) + [ ( "prefixTrackNumber", Json.Encode.bool prefixTrackNumber ) , ( "trackIds" , tracks |> List.map .id |> Json.Encode.list Json.Encode.string ) + , ( "zipName", Json.Encode.string zipName ) ] |> Json.Encode.object |> Alien.broadcast Alien.DownloadTracks diff --git a/src/Core/UI/Tracks/Types.elm b/src/Core/UI/Tracks/Types.elm index 6da70d70..eecaf6e8 100644 --- a/src/Core/UI/Tracks/Types.elm +++ b/src/Core/UI/Tracks/Types.elm @@ -12,7 +12,7 @@ import Tracks exposing (..) type Msg - = Download String (List Track) + = Download { prefixTrackNumber : Bool, zipName : String } (List Track) | DownloadFinished | Harvest | MarkAsSelected Int { shiftKey : Bool } diff --git a/src/Library/Tracks.elm b/src/Library/Tracks.elm index 27083ecd..372fba7d 100644 --- a/src/Library/Tracks.elm +++ b/src/Library/Tracks.elm @@ -450,6 +450,11 @@ playlistTrackFromTrack track = } +sortByIndexInPlaylist : List IdentifiedTrack -> List IdentifiedTrack +sortByIndexInPlaylist = + List.sortBy (\( i, t ) -> Maybe.withDefault (t.tags.disc * 1000 + t.tags.nr) i.indexInPlaylist) + + toPlaylistTracks : List IdentifiedTrack -> List PlaylistTrackWithoutMetadata toPlaylistTracks = List.map (Tuple.second >> playlistTrackFromTrack) -- 2.51.2