11import { writable , get } from "svelte/store" ;
2- import { getCachedPlaylists , setCachedPlaylists , setPlaylistSongs , getCachedPlaylistSongs , appConfiguration , getConfiguration , getCurrentPlaylistId } from "./storageService" ;
2+ import { getCachedPlaylists , setCachedPlaylists , setCachedPlaylistSongs , getCachedPlaylistSongs , appConfiguration , getConfiguration , getCurrentPlaylistId } from "./storageService" ;
33import { fetchPlaylists , fetchPlaylistSongs , fetchNewPlaylist , fetchNewPlaylistSongs , deletePlaylist } from "./api" ;
44import { componentParams , navigateTo } from "./routeService" ;
55import { playOrPauseSong , setPlaylists , currentSong , playPercentage , updateCurrentPlaylist } from "./playbackService" ;
@@ -27,7 +27,7 @@ export async function initializePlaylistService() {
2727 setCachedPlaylists ( fetchedPlaylists ) ;
2828 for ( const playlist of fetchedPlaylists ) {
2929 const songs = await fetchPlaylistSongs ( playlist . id ) ;
30- setPlaylistSongs ( playlist . id , songs ) ;
30+ setCachedPlaylistSongs ( playlist . id , songs ) ;
3131 }
3232 }
3333
@@ -69,16 +69,16 @@ export async function deleteCurrentPlaylist() {
6969 // TODO delete resource from API, images etc
7070 const result = await deletePlaylist ( playlistId ) ;
7171 if ( result . success ) {
72- const currentPlaylist = getCurrentPlaylistId ( ) ;
72+ const currentPlaylistId = getCurrentPlaylistId ( ) ;
7373
7474 // If the deleted playlist is the current playing playlist, stop playback
75- if ( currentPlaylist === playlistId ) {
75+ if ( currentPlaylistId === playlistId ) {
7676 // stop playback
7777 playOrPauseSong ( null ) ;
7878 setPlaylists ( 0 ) ;
79- currentSong . set ( { id : - 999 , title : "" , artist : "" , album : "" , source_id : "" } ) ;
79+ currentSong . set ( { id : - 999 , title : "" , artist : "" , album : "" , source_id : "" } ) ;
8080 }
81-
81+
8282 playPercentage . set ( 0 ) ;
8383
8484 // Remove playlist from cached playlists
@@ -100,11 +100,11 @@ export function removeSongFromPlaylist(songId) {
100100 const playlistId = playlist . id ;
101101 const cachedSongs = getCachedPlaylistSongs ( playlistId ) ;
102102 const songIndex = cachedSongs . findIndex ( ( s ) => s . id === songId ) ;
103- if ( songIndex !== - 1 ) {
104- cachedSongs . splice ( songIndex , 1 ) ;
105- setPlaylistSongs ( playlistId , cachedSongs ) ;
103+ const removed = cachedSongs . splice ( songIndex , 1 ) ;
104+
105+ if ( removed . length > 0 ) {
106+ setCachedPlaylistSongs ( playlistId , cachedSongs ) ;
106107 }
107- updateCurrentPlaylist ( playlistId ) ;
108108 }
109109}
110110
@@ -128,10 +128,15 @@ async function backgroundUpdate() {
128128 const cachedSongs = getCachedPlaylistSongs ( playlistId ) ;
129129 const lastKnowSongPosition = cachedSongs . length ;
130130
131- const newSongs = await fetchNewPlaylistSongs ( playlistId , lastKnowSongPosition ) ;
132- if ( newSongs . length > 0 ) {
133- const updatedSongs = [ ...cachedSongs , ...newSongs ] ;
134- setPlaylistSongs ( playlistId , updatedSongs ) ;
135- }
131+ const newPlaylistSongs = await fetchNewPlaylistSongs ( playlistId , lastKnowSongPosition ) ;
132+
133+ // get the difference between cachedSongs and newPlaylistSongs
134+ const songsToAdd = newPlaylistSongs . filter ( song => ! cachedSongs . some ( cs => cs . id === song . id ) ) ;
135+
136+ if ( songsToAdd . length > 0 ) {
137+ const updatedSongs = [ ...cachedSongs , ...songsToAdd ] ;
138+ setCachedPlaylistSongs ( playlistId , updatedSongs ) ;
139+ }
140+
136141 }
137142}
0 commit comments