From 4c91c3f4af02eb9ca98817caafdbf17c829dcc5d Mon Sep 17 00:00:00 2001 From: Steven Vandevelde Date: Mon, 17 Jun 2019 15:31:04 +0200 Subject: [PATCH] Improve the saving of the tracks collection --- src/Applications/Brain.elm | 40 +++++++++++++++++++ src/Applications/Brain/Core.elm | 1 + src/Applications/Brain/Reply.elm | 4 ++ src/Applications/Brain/Sources/Processing.elm | 19 +++++---- src/Applications/UI.elm | 16 +++++++- src/Applications/UI/Reply.elm | 1 + src/Applications/UI/Tracks.elm | 6 +-- src/Library/Alien.elm | 2 + src/Library/Tracks.elm | 22 +++++++++- src/Library/Tracks/Collection.elm | 25 +++++------- 10 files changed, 108 insertions(+), 28 deletions(-) diff --git a/src/Applications/Brain.elm b/src/Applications/Brain.elm index 22082c81..40058b57 100644 --- a/src/Applications/Brain.elm +++ b/src/Applications/Brain.elm @@ -17,6 +17,7 @@ import Return2 exposing (..) import Return3 import Sources.Encoding as Sources import Sources.Processing.Encoding as Processing +import Tracks import Tracks.Encoding as Tracks @@ -159,6 +160,13 @@ update msg model = |> returnWithModel { model | hypaethralUserData = decodedData } |> andThen (updateSearchIndex encodedTracks) + RemoveTracksBySourceId sourceId -> + model.hypaethralUserData.tracks + |> Tracks.removeBySourceId sourceId + |> hypaethralLenses.setTracks model + |> updateSearchIndexWithModel + |> andThen saveHypaethralData + SaveHypaethralData -> model.hypaethralUserData |> Authentication.encodeHypaethral @@ -216,6 +224,15 @@ updateSearchIndex value model = |> updateWithModel model +updateSearchIndexWithModel : Model -> ( Model, Cmd Msg ) +updateSearchIndexWithModel model = + model.hypaethralUserData.tracks + |> Json.Encode.list Tracks.encodeTrack + |> Tracks.UpdateSearchIndex + |> TracksMsg + |> updateWithModel model + + -- 📣 ░░ REPLIES @@ -226,6 +243,23 @@ translateReply reply model = FabricatedNewSecretKey -> update SaveHypaethralData model + ----------------------------------------- + -- Tracks + ----------------------------------------- + AddTracks tracks -> + tracks + |> (++) model.hypaethralUserData.tracks + |> hypaethralLenses.setTracks model + |> updateSearchIndexWithModel + |> andThen saveHypaethralData + + RemoveTracksByPaths args -> + model.hypaethralUserData.tracks + |> Tracks.removeByPaths args + |> hypaethralLenses.setTracks model + |> updateSearchIndexWithModel + |> andThen saveHypaethralData + ----------------------------------------- -- To UI ----------------------------------------- @@ -396,6 +430,12 @@ translateAlienData tag data = |> Result.withDefault "" |> RedirectToBlockstackSignIn + Alien.RemoveTracksBySourceId -> + data + |> Json.decodeValue Json.string + |> Result.withDefault "" + |> RemoveTracksBySourceId + Alien.SaveEnclosedUserData -> AuthenticationMsg (Authentication.SaveEnclosedData data) diff --git a/src/Applications/Brain/Core.elm b/src/Applications/Brain/Core.elm index 213fe729..f2e0a7b3 100644 --- a/src/Applications/Brain/Core.elm +++ b/src/Applications/Brain/Core.elm @@ -53,6 +53,7 @@ type Msg -- User data ----------------------------------------- | LoadHypaethralUserData Json.Value + | RemoveTracksBySourceId String | SaveHypaethralData | SaveFavourites Json.Value | SavePlaylists Json.Value diff --git a/src/Applications/Brain/Reply.elm b/src/Applications/Brain/Reply.elm index 9717ce27..7c8abbdc 100644 --- a/src/Applications/Brain/Reply.elm +++ b/src/Applications/Brain/Reply.elm @@ -2,6 +2,7 @@ module Brain.Reply exposing (Reply(..)) import Alien import Json.Encode as Json +import Tracks exposing (Track) @@ -10,6 +11,9 @@ import Json.Encode as Json type Reply = FabricatedNewSecretKey + -- Tracks + | AddTracks (List Track) + | RemoveTracksByPaths { sourceId : String, paths : List String } -- UI | GiveUI Alien.Tag Json.Value | NudgeUI Alien.Tag diff --git a/src/Applications/Brain/Sources/Processing.elm b/src/Applications/Brain/Sources/Processing.elm index d283a261..9f85e773 100644 --- a/src/Applications/Brain/Sources/Processing.elm +++ b/src/Applications/Brain/Sources/Processing.elm @@ -133,7 +133,9 @@ update msg model = in ( model , Cmd.none - , [ GiveUI Alien.RemoveTracksByPath encodedData ] + , [ GiveUI Alien.RemoveTracksByPath encodedData + , RemoveTracksByPaths { sourceId = sourceId, paths = filePaths } + ] ) ----------------------------------------- @@ -161,12 +163,15 @@ update msg model = [] False -> - tagsContext - |> tracksFromTagsContext - |> List.map (\track -> { track | insertedAt = model.currentTime }) - |> Encode.list Tracks.Encoding.encodeTrack - |> GiveUI Alien.AddTracks - |> List.singleton + let + tracksToAdd = + tagsContext + |> tracksFromTagsContext + |> List.map (\track -> { track | insertedAt = model.currentTime }) + in + [ GiveUI Alien.AddTracks (Encode.list Tracks.Encoding.encodeTrack tracksToAdd) + , AddTracks tracksToAdd + ] ) ----------------------------------------- diff --git a/src/Applications/UI.elm b/src/Applications/UI.elm index 0a121ebb..338e8c5c 100644 --- a/src/Applications/UI.elm +++ b/src/Applications/UI.elm @@ -345,7 +345,7 @@ update msg model = model |> translateReply SaveFavourites |> andThen (translateReply SaveSources) - |> andThen (translateReply SaveTracks) + |> andThen (translateReply SaveTracksFromCache) |> andThen (translateReply <| ShowWarningNotification "Syncing") ----------------------------------------- @@ -968,10 +968,18 @@ translateReply reply model = |> andThen (UI.Notifications.show notification) RemoveTracksWithSourceId sourceId -> + let + cmd = + sourceId + |> Json.Encode.string + |> Alien.broadcast Alien.RemoveTracksBySourceId + |> Ports.toBrain + in sourceId |> Tracks.RemoveBySourceId |> TracksMsg |> updateWithModel model + |> addCommand cmd ReplaceSourceInCollection source -> let @@ -1055,6 +1063,12 @@ translateReply reply model = |> Ports.toBrain |> returnWithModel model + SaveTracksFromCache -> + Alien.SaveTracks + |> Alien.trigger + |> Ports.toBrain + |> returnWithModel model + -- 📰 diff --git a/src/Applications/UI/Reply.elm b/src/Applications/UI/Reply.elm index 1d16d8ff..afbb8766 100644 --- a/src/Applications/UI/Reply.elm +++ b/src/Applications/UI/Reply.elm @@ -78,3 +78,4 @@ type Reply | SaveSettings | SaveSources | SaveTracks + | SaveTracksFromCache diff --git a/src/Applications/UI/Tracks.elm b/src/Applications/UI/Tracks.elm index de37a910..29d9cd55 100644 --- a/src/Applications/UI/Tracks.elm +++ b/src/Applications/UI/Tracks.elm @@ -220,12 +220,12 @@ update msg model = |> Result.withDefault ( [], missingId ) in reviseCollection - (removeByPaths sourceId paths) + (Collection.removeByPaths sourceId paths) model RemoveBySourceId sourceId -> reviseCollection - (removeBySourceId sourceId) + (Collection.removeBySourceId sourceId) model ----------------------------------------- @@ -470,7 +470,7 @@ resolveParcel model ( _, newCollection ) = -- Reply -------- , if collectionChanged then - [ GenerateDirectoryPlaylists, ResetQueue, SaveTracks ] + [ GenerateDirectoryPlaylists, ResetQueue ] else if harvestChanged then [ ResetQueue ] diff --git a/src/Library/Alien.elm b/src/Library/Alien.elm index a16514ee..92082fa2 100644 --- a/src/Library/Alien.elm +++ b/src/Library/Alien.elm @@ -37,6 +37,7 @@ type Tag -- from UI | ProcessSources | RedirectToBlockstackSignIn + | RemoveTracksBySourceId | SaveEnclosedUserData | SaveFavourites | SavePlaylists @@ -79,6 +80,7 @@ enum = ----------------------------------------- , ( "PROCESS_SOURCES", ProcessSources ) , ( "REDIRECT_TO_BLOCKSTACK_SIGN_IN", RedirectToBlockstackSignIn ) + , ( "REMOVE_TRACKS_BY_SOURCE_ID", RemoveTracksBySourceId ) , ( "SAVE_ENCLOSED_USER_DATA", SaveEnclosedUserData ) , ( "SAVE_FAVOURITES", SaveFavourites ) , ( "SAVE_PLAYLISTS", SavePlaylists ) diff --git a/src/Library/Tracks.elm b/src/Library/Tracks.elm index 51e492fa..a50521df 100644 --- a/src/Library/Tracks.elm +++ b/src/Library/Tracks.elm @@ -1,4 +1,4 @@ -module Tracks exposing (Collection, CollectionDependencies, Favourite, Grouping(..), IdentifiedTrack, Identifiers, Parcel, SortBy(..), SortDirection(..), Tags, Track, emptyCollection, emptyIdentifiedTrack, emptyIdentifiers, emptyTags, emptyTrack, isNowPlaying, makeTrack, missingId, removeFromPlaylist, toPlaylistTracks) +module Tracks exposing (Collection, CollectionDependencies, Favourite, Grouping(..), IdentifiedTrack, Identifiers, Parcel, SortBy(..), SortDirection(..), Tags, Track, emptyCollection, emptyIdentifiedTrack, emptyIdentifiers, emptyTags, emptyTrack, isNowPlaying, makeTrack, missingId, removeByPaths, removeBySourceId, removeFromPlaylist, toPlaylistTracks) import Base64 import List.Extra as List @@ -204,6 +204,26 @@ makeTrack sourceId ( path, tags ) = } +removeByPaths : { sourceId : String, paths : List String } -> List Track -> List Track +removeByPaths { sourceId, paths } tracks = + tracks + |> List.foldr + (\t ( acc, remainingPaths ) -> + if t.sourceId == sourceId && List.member t.path remainingPaths then + ( acc, List.remove t.path remainingPaths ) + + else + ( t :: acc, remainingPaths ) + ) + ( [], paths ) + |> Tuple.first + + +removeBySourceId : String -> List Track -> List Track +removeBySourceId sourceId = + List.filter (.sourceId >> (/=) sourceId) + + removeFromPlaylist : List IdentifiedTrack -> Playlist -> Playlist removeFromPlaylist tracks playlist = playlist.tracks diff --git a/src/Library/Tracks/Collection.elm b/src/Library/Tracks/Collection.elm index ec3fdbbe..1a823e95 100644 --- a/src/Library/Tracks/Collection.elm +++ b/src/Library/Tracks/Collection.elm @@ -1,7 +1,7 @@ module Tracks.Collection exposing (add, arrange, harvest, harvestChanged, identify, map, removeByPaths, removeBySourceId, tracksChanged) import List.Extra as List -import Tracks exposing (..) +import Tracks exposing (IdentifiedTrack, Parcel, Track, emptyCollection) import Tracks.Collection.Internal as Internal @@ -49,22 +49,12 @@ add tracks ( deps, { untouched } ) = removeByPaths : String -> List String -> Parcel -> Parcel removeByPaths sourceId paths ( deps, { untouched } ) = - let - ( filtered, _ ) = - List.foldr - (\t ( acc, remainingPaths ) -> - if t.sourceId == sourceId && List.member t.path remainingPaths then - ( acc, List.remove t.path remainingPaths ) - - else - ( t :: acc, remainingPaths ) - ) - ( [], paths ) - untouched - in identify ( deps - , { emptyCollection | untouched = filtered } + , { emptyCollection + | untouched = + Tracks.removeByPaths { sourceId = sourceId, paths = paths } untouched + } ) @@ -72,7 +62,10 @@ removeBySourceId : String -> Parcel -> Parcel removeBySourceId sourceId ( deps, { untouched } ) = identify ( deps - , { emptyCollection | untouched = List.filter (.sourceId >> (/=) sourceId) untouched } + , { emptyCollection + | untouched = + Tracks.removeBySourceId sourceId untouched + } ) -- knotmirror 0.0.1