diff --git a/CHANGELOG.md b/CHANGELOG.md index cacffa71..3c7edd8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,13 +5,14 @@ - **Adds album-covers view** (switch to and from list view with icon in nav bar) - Added new background images and replaced two older ones - Adds a keyboard shortcut `L` to quickly select a playlist +- Adds ability to sync tags for individual tracks - Adds keyboard shortcuts to switch screens - Adds the ability to migrate between data-storage methods - Dark mode for the about page - Fixes issue with shuffle algorithm - Fixes playback issues (eg. clicking same track multiple times) - Fixes various issues with add-to-playlist overlay -- Only generated playlists for enabled sources +- Only show generated playlists for enabled sources - Removes support for Blockstack & Textile ## 2.5.3 diff --git a/src/Applications/Brain.elm b/src/Applications/Brain.elm index d83f1d73..23e8c4b0 100644 --- a/src/Applications/Brain.elm +++ b/src/Applications/Brain.elm @@ -120,12 +120,18 @@ update msg = RemoveTracksFromCache a -> Tracks.removeFromCache a + ReplaceTrackTags a -> + Tracks.replaceTags a + Search a -> Tracks.search a StoreTracksInCache a -> Tracks.storeInCache a + SyncTrackTags a -> + Tracks.syncTrackTags a + UpdateSearchIndex a -> Tracks.updateSearchIndex a @@ -159,6 +165,7 @@ subscriptions model = , Ports.makeArtworkTrackUrls MakeArtworkTrackUrls , Ports.receiveSearchResults GotSearchResults , Ports.receiveTags (ProcessingMsg << Processing.TagsStep) + , Ports.replaceTags ReplaceTrackTags , Ports.savedHypaethralBit (\_ -> UserMsg User.SaveNextHypaethralBit) -- @@ -264,6 +271,9 @@ translateAlienData tag data = Alien.StoreTracksInCache -> StoreTracksInCache data + Alien.SyncTrackTags -> + SyncTrackTags data + Alien.ToCache -> ToCache data diff --git a/src/Applications/Brain/Ports.elm b/src/Applications/Brain/Ports.elm index 984e39e5..62acf1a4 100644 --- a/src/Applications/Brain/Ports.elm +++ b/src/Applications/Brain/Ports.elm @@ -2,7 +2,7 @@ port module Brain.Ports exposing (..) import Alien import Json.Encode as Json -import Sources.Processing exposing (ContextForTags) +import Sources.Processing exposing (ContextForTags, ContextForTagsSync) @@ -33,6 +33,9 @@ port requestTags : ContextForTags -> Cmd msg port storeTracksInCache : Json.Value -> Cmd msg +port syncTags : ContextForTagsSync -> Cmd msg + + port toCache : Alien.Event -> Cmd msg @@ -89,4 +92,7 @@ port receiveSearchResults : (List String -> msg) -> Sub msg port receiveTags : (ContextForTags -> msg) -> Sub msg +port replaceTags : (ContextForTagsSync -> msg) -> Sub msg + + port savedHypaethralBit : (Json.Value -> msg) -> Sub msg diff --git a/src/Applications/Brain/Tracks/State.elm b/src/Applications/Brain/Tracks/State.elm index 1a52c9d3..9dfc15a5 100644 --- a/src/Applications/Brain/Tracks/State.elm +++ b/src/Applications/Brain/Tracks/State.elm @@ -14,7 +14,7 @@ import Queue import Return exposing (andThen, return) import Return.Ext as Return import Sources exposing (Source) -import Sources.Processing exposing (HttpMethod(..)) +import Sources.Processing exposing (ContextForTagsSync, HttpMethod(..), TagUrls) import Sources.Services import Time import Tracks exposing (Track) @@ -160,6 +160,46 @@ removeFromCache data = Return.communicate (Ports.removeTracksFromCache data) +replaceTags : ContextForTagsSync -> Manager +replaceTags context model = + model.hypaethralUserData.tracks + |> List.foldr + (\track ( acc, trackIds, tags ) -> + case List.elemIndex track.id trackIds of + Just idx -> + let + newTags = + tags + |> List.getAt idx + |> Maybe.andThen identity + |> Maybe.withDefault track.tags + in + ( { track | tags = newTags } :: acc + , List.removeAt idx trackIds + , List.removeAt idx tags + ) + + Nothing -> + ( track :: acc + , trackIds + , tags + ) + ) + ( [] + , context.trackIds + , context.receivedTags + ) + |> (\( a, _, _ ) -> + User.saveTracksAndUpdateSearchIndex a model + ) + |> andThen + (\m -> + m.hypaethralUserData.tracks + |> Json.Encode.list Tracks.Encoding.encodeTrack + |> (\data -> Common.giveUI Alien.ReloadTracks data m) + ) + + search : Json.Value -> Manager search encodedSearchTerm = encodedSearchTerm @@ -174,6 +214,74 @@ storeInCache data = Return.communicate (Ports.storeTracksInCache data) +syncTrackTags : Json.Value -> Manager +syncTrackTags data model = + let + result = + Json.decodeValue + (Json.list <| + Json.map3 + (\path sourceId trackId -> + { path = path + , sourceId = sourceId + , trackId = trackId + } + ) + (Json.field "path" Json.string) + (Json.field "sourceId" Json.string) + (Json.field "trackId" Json.string) + ) + data + + ( sources, _ ) = + result + |> Result.withDefault [] + |> List.foldl + (\{ path, sourceId } ( dict, acc ) -> + if List.member sourceId acc then + ( dict, acc ) + + else + case List.find (.id >> (==) sourceId) model.hypaethralUserData.sources of + Just source -> + ( Dict.insert sourceId source dict, sourceId :: acc ) + + Nothing -> + ( dict, sourceId :: acc ) + ) + ( Dict.empty, [] ) + in + case result of + Ok list -> + list + |> List.foldr + (\{ path, sourceId, trackId } ( accPaths, accUrls, accIds ) -> + sources + |> Dict.get sourceId + |> Maybe.map + (tagUrls model.currentTime path) + |> Maybe.map + (\urls -> + ( path :: accPaths, urls :: accUrls, trackId :: accIds ) + ) + |> Maybe.withDefault + ( accPaths, accUrls, accIds ) + ) + ( [], [], [] ) + |> (\( accPaths, accUrls, accIds ) -> + Ports.syncTags + { receivedFilePaths = accPaths + , receivedTags = [] + , trackIds = accIds + , urlsForTags = accUrls + } + ) + |> return model + + Err _ -> + Return.singleton model + + updateSearchIndex : Json.Value -> Manager updateSearchIndex data = Return.communicate (Ports.updateSearchIndex data) @@ -204,3 +312,14 @@ makeTrackUrl timestamp trackPath maybeSource httpMethod = Nothing -> "" + + +tagUrls : Time.Posix -> String -> Source -> TagUrls +tagUrls currentTime path source = + let + maker = + Sources.Services.makeTrackUrl source.service currentTime source.data + in + { getUrl = maker Get path + , headUrl = maker Head path + } diff --git a/src/Applications/Brain/Types.elm b/src/Applications/Brain/Types.elm index 2b30b3c4..de43b43a 100644 --- a/src/Applications/Brain/Types.elm +++ b/src/Applications/Brain/Types.elm @@ -53,8 +53,10 @@ type Msg | MakeArtworkTrackUrls Json.Value | RemoveTracksBySourceId Json.Value | RemoveTracksFromCache Json.Value + | ReplaceTrackTags Processing.ContextForTagsSync | Search Json.Value | StoreTracksInCache Json.Value + | SyncTrackTags Json.Value | UpdateSearchIndex Json.Value ----------------------------------------- -- 🦉 Nested diff --git a/src/Applications/UI.elm b/src/Applications/UI.elm index 845475b3..a17f5876 100644 --- a/src/Applications/UI.elm +++ b/src/Applications/UI.elm @@ -622,6 +622,9 @@ translateAlienData tag data = Alien.NotAuthenticated -> AuthenticationMsg Authentication.NotAuthenticated + Alien.ReloadTracks -> + TracksMsg (Tracks.Reload data) + Alien.RemoveTracksByPath -> TracksMsg (Tracks.RemoveByPaths data) diff --git a/src/Applications/UI/Common/State.elm b/src/Applications/UI/Common/State.elm index d218076f..5b8a9118 100644 --- a/src/Applications/UI/Common/State.elm +++ b/src/Applications/UI/Common/State.elm @@ -84,7 +84,7 @@ showNotification : Notification Msg -> Manager showNotification notification model = model.notifications |> UI.Notifications.show notification - |> Return.map (\n -> { model | isLoading = False, notifications = n }) + |> Return.map (\n -> { model | notifications = n }) showNotificationWithModel : UI.Model -> Notification Msg -> ( UI.Model, Cmd UI.Msg ) diff --git a/src/Applications/UI/Tracks/ContextMenu.elm b/src/Applications/UI/Tracks/ContextMenu.elm index 3d182691..e35ca48b 100644 --- a/src/Applications/UI/Tracks/ContextMenu.elm +++ b/src/Applications/UI/Tracks/ContextMenu.elm @@ -33,7 +33,7 @@ trackMenu : -> ContextMenu Msg trackMenu { cached, cachingInProgress, currentTime, selectedPlaylist, lastModifiedPlaylistName, showAlternativeMenu, sources } tracks = if showAlternativeMenu then - [ temporaryUrlActions + [ alternativeMenuActions currentTime sources tracks @@ -64,6 +64,34 @@ trackMenu { cached, cachingInProgress, currentTime, selectedPlaylist, lastModifi |> ContextMenu +alternativeMenuActions : + Time.Posix + -> List Source + -> List IdentifiedTrack + -> List (ContextMenu.Item Msg) +alternativeMenuActions timestamp sources tracks = + case tracks of + [ ( i, t ) ] -> + [ Item + { icon = Icons.link + , label = "Copy temporary url" + , msg = CopyToClipboard (Queue.makeTrackUrl timestamp sources t) + , active = False + } + + -- + , Item + { icon = Icons.sync + , label = "Sync tags" + , msg = TracksMsg (Tracks.SyncTags [ t ]) + , active = False + } + ] + + _ -> + [] + + cacheAction : { cached : List String, cachingInProgress : List String } -> List IdentifiedTrack @@ -224,26 +252,6 @@ queueActions identifiedTracks = ] -temporaryUrlActions : - Time.Posix - -> List Source - -> List IdentifiedTrack - -> List (ContextMenu.Item Msg) -temporaryUrlActions timestamp sources tracks = - case tracks of - [ ( i, t ) ] -> - [ Item - { icon = Icons.link - , label = "Copy temporary url" - , msg = CopyToClipboard (Queue.makeTrackUrl timestamp sources t) - , active = False - } - ] - - _ -> - [] - - -- VIEW MENU diff --git a/src/Applications/UI/Tracks/State.elm b/src/Applications/UI/Tracks/State.elm index b8595ccd..31cf67cc 100644 --- a/src/Applications/UI/Tracks/State.elm +++ b/src/Applications/UI/Tracks/State.elm @@ -63,6 +63,9 @@ update msg = ScrollToNowPlaying -> scrollToNowPlaying + SyncTags a -> + syncTags a + ToggleCachedOnly -> toggleCachedOnly @@ -102,6 +105,9 @@ update msg = Add a -> add a + Reload a -> + reload a + RemoveByPaths a -> removeByPaths a @@ -178,14 +184,13 @@ update msg = add : Json.Value -> Manager add encodedTracks model = - model - |> reviseCollection - (encodedTracks - |> Json.decodeValue (Json.list Encoding.trackDecoder) - |> Result.withDefault [] - |> Collection.add - ) - |> andThen search + reviseCollection + (encodedTracks + |> Json.decodeValue (Json.list Encoding.trackDecoder) + |> Result.withDefault [] + |> Collection.add + ) + model changeScene : Scene -> Manager @@ -408,6 +413,17 @@ markAsSelected indexInList { shiftKey } model = Return.singleton { model | selectedTrackIndexes = selection } +reload : Json.Value -> Manager +reload encodedTracks model = + reviseCollection + (encodedTracks + |> Json.decodeValue (Json.list Encoding.trackDecoder) + |> Result.withDefault model.tracks.untouched + |> Collection.replace + ) + model + + removeByPaths : Json.Value -> Manager removeByPaths encodedParams model = let @@ -724,6 +740,22 @@ storedInCache json maybeError = |> Common.showNotification +syncTags : List Track -> Manager +syncTags tracks = + tracks + |> Json.Encode.list + (\track -> + Json.Encode.object + [ ( "path", Json.Encode.string track.path ) + , ( "sourceId", Json.Encode.string track.sourceId ) + , ( "trackId", Json.Encode.string track.id ) + ] + ) + |> Alien.broadcast Alien.SyncTrackTags + |> Ports.toBrain + |> Return.communicate + + toggleCachedOnly : Manager toggleCachedOnly model = { model | cachedTracksOnly = not model.cachedTracksOnly } @@ -905,7 +937,7 @@ whenArrangementChanges = whenCollectionChanges = - andThen Common.generateDirectoryPlaylists >> whenArrangementChanges + andThen search >> andThen Common.generateDirectoryPlaylists >> whenArrangementChanges scrollContext : Model -> String @@ -922,21 +954,13 @@ scrollContext model = importHypaethral : HypaethralData -> Maybe Playlist -> Manager importHypaethral data selectedPlaylist model = - let - adjustedModel = - { model - | favourites = data.favourites - , hideDuplicates = Maybe.unwrap False .hideDuplicates data.settings - , selectedPlaylist = selectedPlaylist - , tracks = { emptyCollection | untouched = data.tracks } - } - in - adjustedModel - |> resolveParcel - (adjustedModel - |> makeParcel - |> Collection.identify - ) + { model + | favourites = data.favourites + , hideDuplicates = Maybe.unwrap False .hideDuplicates data.settings + , selectedPlaylist = selectedPlaylist + , tracks = { emptyCollection | untouched = data.tracks } + } + |> reviseCollection Collection.identify |> andThen search |> (case model.searchTerm of Just _ -> diff --git a/src/Applications/UI/Tracks/Types.elm b/src/Applications/UI/Tracks/Types.elm index 673be73f..0b07fe92 100644 --- a/src/Applications/UI/Tracks/Types.elm +++ b/src/Applications/UI/Tracks/Types.elm @@ -17,6 +17,7 @@ type Msg | Harvest | MarkAsSelected Int { shiftKey : Bool } | ScrollToNowPlaying + | SyncTags (List Track) | ToggleCachedOnly | ToggleFavouritesOnly | ToggleHideDuplicates @@ -36,6 +37,7 @@ type Msg -- Collection ----------------------------------------- | Add Json.Value + | Reload Json.Value | RemoveByPaths Json.Value | RemoveBySourceId String | SortBy SortBy diff --git a/src/Javascript/Brain/index.js b/src/Javascript/Brain/index.js index c52a92a1..676f98a1 100644 --- a/src/Javascript/Brain/index.js +++ b/src/Javascript/Brain/index.js @@ -236,3 +236,11 @@ app.ports.requestTags.subscribe(context => { app.ports.receiveTags.send(newContext) }) }) + + +app.ports.syncTags.subscribe(context => { + processing.processContext(context).then(newContext => { + console.log(newContext) + app.ports.replaceTags.send(newContext) + }) +}) diff --git a/src/Library/Alien.elm b/src/Library/Alien.elm index f03f0a68..5fe495ec 100644 --- a/src/Library/Alien.elm +++ b/src/Library/Alien.elm @@ -54,6 +54,7 @@ type Tag | StopProcessing | StoreTracksInCache | SyncHypaethralData + | SyncTrackTags | ToCache | UpdateEncryptionKey ----------------------------------------- @@ -68,6 +69,7 @@ type Tag | LoadHypaethralUserData | MissingSecretKey | NotAuthenticated + | ReloadTracks | RemoveTracksByPath | ReportProcessingError | ReportProcessingProgress @@ -108,6 +110,7 @@ enum = , ( "STOP_PROCESSING", StopProcessing ) , ( "STORE_TRACKS_IN_CACHE", StoreTracksInCache ) , ( "SYNC_HYPAETHRAL_DATA", SyncHypaethralData ) + , ( "SYNC_TRACK_TAGS", SyncTrackTags ) , ( "TO_CACHE", ToCache ) , ( "UPDATE_ENCRYPTION_KEY", UpdateEncryptionKey ) @@ -123,6 +126,7 @@ enum = , ( "LOAD_HYPAETHRAL_USER_DATA", LoadHypaethralUserData ) , ( "MISSING_SECRET_KEY", MissingSecretKey ) , ( "NOT_AUTHENTICATED", NotAuthenticated ) + , ( "RELOAD_TRACKS", ReloadTracks ) , ( "REMOVE_TRACKS_BY_PATH", RemoveTracksByPath ) , ( "REPORT_PROCESSING_ERROR", ReportProcessingError ) , ( "REPORT_PROCESSING_PROGRESS", ReportProcessingProgress ) diff --git a/src/Library/Sources/Processing.elm b/src/Library/Sources/Processing.elm index 4cfbcc3a..8bf84267 100644 --- a/src/Library/Sources/Processing.elm +++ b/src/Library/Sources/Processing.elm @@ -1,4 +1,4 @@ -module Sources.Processing exposing (Arguments, Context, ContextForTags, HttpMethod(..), Marker(..), PrepationAnswer, Status(..), TagUrls, TreeAnswer, httpMethod) +module Sources.Processing exposing (..) import Sources exposing (Source, SourceData) import Tracks exposing (Tags, Track) @@ -64,6 +64,14 @@ type alias ContextForTags = } +type alias ContextForTagsSync = + { receivedFilePaths : List String + , receivedTags : List (Maybe Tags) + , trackIds : List String + , urlsForTags : List TagUrls + } + + type alias TagUrls = { getUrl : String , headUrl : String diff --git a/src/Library/Tracks/Collection.elm b/src/Library/Tracks/Collection.elm index 550c674a..33b91f17 100644 --- a/src/Library/Tracks/Collection.elm +++ b/src/Library/Tracks/Collection.elm @@ -1,4 +1,4 @@ -module Tracks.Collection exposing (add, arrange, harvest, identifiedTracksChanged, identify, map, tracksChanged) +module Tracks.Collection exposing (add, arrange, harvest, identifiedTracksChanged, identify, map, replace, tracksChanged) import Tracks exposing (IdentifiedTrack, Parcel, Track, emptyCollection) import Tracks.Collection.Internal as Internal @@ -46,6 +46,14 @@ add tracks ( deps, { untouched } ) = ) +replace : List Track -> Parcel -> Parcel +replace tracks ( deps, { untouched } ) = + identify + ( deps + , { emptyCollection | untouched = tracks } + ) + + -- ⚗️