Skip to content

Commit c26eaa0

Browse files
committed
beta test
1 parent 202a63f commit c26eaa0

5 files changed

Lines changed: 50 additions & 28 deletions

File tree

MyMusicClientSveltePwa/src/lib/components/Song.svelte

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
import { currentSong, isPlaying, isLoading } from "../scripts/playbackService";
66
import { getImageUrl, deleteSongFromPlaylist } from "../scripts/api";
77
import { removeSongFromPlaylist } from "../scripts/playlistService";
8+
import { getCurrentPlaylistId, setCurrentSongIndex, setCurrentSongTime } from "../scripts/storageService";
9+
import { get } from "svelte/store";
10+
11+
$: $isPlaying;
12+
$: $currentSong;
13+
$: $isLoading;
814
915
export let song;
1016
export let playlistId;
@@ -16,20 +22,27 @@
1622
});
1723
1824
async function deleteSong() {
19-
if(confirm(`Are you sure you want to delete the song "${song.name}" from this playlist?`)) {
25+
if (confirm(`Are you sure you want to delete the song "${song.name}" from this playlist?`)) {
2026
var deleted = await deleteSongFromPlaylist(playlistId, song.id);
2127
22-
if(deleted) {
28+
if (deleted) {
2329
removeSongFromPlaylist(song.id);
30+
const currentPlaylistId = getCurrentPlaylistId();
31+
// If the deleted playlist is the current playing playlist, stop playback
32+
const _currentSong = get(currentSong);
33+
if (song.id === _currentSong.id) {
34+
// stop playback
35+
playOrPauseSong(null);
36+
currentSong.set({ id: -999, title: "", artist: "", album: "", source_id: "" });
37+
setCurrentSongTime(0);
38+
setCurrentSongIndex(-1);
39+
}
2440
} else {
2541
alert(`Failed to delete song with ID: ${song.id} from playlist ID: ${playlistId}`);
2642
}
2743
}
2844
}
2945
30-
$: $isPlaying;
31-
$: $currentSong;
32-
$: $isLoading;
3346
</script>
3447

3548
{#if song}
@@ -74,8 +87,7 @@
7487
background-size: cover;
7588
background-position: center;
7689
overflow: hidden;
77-
transition:
78-
transform 0.2s ease,
90+
transition: transform 0.2s ease;
7991
}
8092
8193
.song:hover {
@@ -110,7 +122,7 @@
110122
.blur {
111123
position: absolute;
112124
inset: 0;
113-
background-color: rgba(0, 0, 0, 0.60);
125+
background-color: rgba(0, 0, 0, 0.6);
114126
backdrop-filter: blur(5px);
115127
z-index: 1;
116128
border-radius: 5px;

MyMusicClientSveltePwa/src/lib/pages/Playlists.svelte

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { playOrPauseSong, setPlaylists, updateCurrentPlaylist } from "../scripts/playbackService";
77
import SongComponent from "../components/Song.svelte";
88
9-
const updateIntervalTimeOut = 1500; // Update every second
9+
const updateIntervalTimeOutInMs = 750; // Update every 750 ms
1010
let intervalId
1111
let updating = false
1212
@@ -19,13 +19,12 @@
1919
setContext("playOrPauseSong", playOrPause);
2020
2121
intervalId = setInterval(() => {
22-
23-
if (updating) return; // Prevent multiple updates at the same time
22+
// if (updating) return; // Prevent multiple updates at the same time
2423
updating = true;
2524
songs.set(getCachedPlaylistSongs(playlistId));
2625
updateCurrentPlaylist(playlistId);
2726
updating = false;
28-
}, updateIntervalTimeOut);
27+
}, updateIntervalTimeOutInMs);
2928
});
3029
3130
function playOrPause(songId) {

MyMusicClientSveltePwa/src/lib/scripts/playbackService.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ export function previousSong() {
125125
}
126126

127127
export function playOrPauseSong(songId) {
128+
129+
if (songId < 0) {
130+
// Invalid songId, do nothing
131+
return;
132+
}
133+
128134
const _currentSong = get(currentSong);
129135

130136
if (songId === null || songId === undefined) {

MyMusicClientSveltePwa/src/lib/scripts/playlistService.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { 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";
33
import { fetchPlaylists, fetchPlaylistSongs, fetchNewPlaylist, fetchNewPlaylistSongs, deletePlaylist } from "./api";
44
import { componentParams, navigateTo } from "./routeService";
55
import { 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
}

MyMusicClientSveltePwa/src/lib/scripts/storageService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function setCachedPlaylists(playlists) {
2222
setItem(PlaylistsKey, playlists);
2323
}
2424

25-
export function setPlaylistSongs(playlistId, songs) {
25+
export function setCachedPlaylistSongs(playlistId, songs) {
2626
const key = `${PlaylistSongsKey}${playlistId}`;
2727
setItem(key, songs);
2828
}

0 commit comments

Comments
 (0)