Implement offline playlist creation with local tracks
- Update createPendingPlaylist to accept localTrackIds parameter - Modify PlaylistModal to separate Qobuz and local tracks in offline mode - Implement syncPendingPlaylists function to sync playlists when back online - Register sync callback in bootstrap to auto-sync on online transition - Properly adds both Qobuz tracks and local tracks to synced playlists - Shows toast notifications for successful/failed syncs
This commit is contained in:
1 parent
ea03553718
commit
85f1b2da6f
3 files changed
+89
-13
No files matched your search
@@ -10,7 +10,7 @@ import { getCurrentWebview } from '@tauri-apps/api/webview';
|
|||||||
import { goBack, goForward } from '$lib/stores/navigationStore';
|
import { goBack, goForward } from '$lib/stores/navigationStore';
|
||||||
import { loadToastsPreference } from '$lib/stores/toastStore';
|
import { loadToastsPreference } from '$lib/stores/toastStore';
|
||||||
import { loadSystemNotificationsPreference, flushScrobbleQueue } from '$lib/services/playbackService';
|
import { loadSystemNotificationsPreference, flushScrobbleQueue } from '$lib/services/playbackService';
|
||||||
import { initOfflineStore, cleanupOfflineStore, onOnlineTransition } from '$lib/stores/offlineStore';
|
import { initOfflineStore, cleanupOfflineStore, onOnlineTransition, syncPendingPlaylists } from '$lib/stores/offlineStore';
|
||||||
import { getNextZoomLevel } from '$lib/utils/zoom';
|
import { getNextZoomLevel } from '$lib/utils/zoom';
|
||||||
import { getZoom, setZoom } from '$lib/stores/zoomStore';
|
import { getZoom, setZoom } from '$lib/stores/zoomStore';
|
||||||
|
|
||||||
@@ -141,14 +141,21 @@ export function bootstrapApp(): BootstrapResult {
|
|||||||
// Initialize offline store (async, fire-and-forget)
|
// Initialize offline store (async, fire-and-forget)
|
||||||
initOfflineStore();
|
initOfflineStore();
|
||||||
|
|
||||||
// Register callback to flush scrobble queue when transitioning to online
|
// Register callback to flush scrobble queue and sync playlists when transitioning to online
|
||||||
onOnlineTransition(() => {
|
onOnlineTransition(() => {
|
||||||
console.log('[Bootstrap] Online transition detected - flushing scrobble queue');
|
console.log('[Bootstrap] Online transition detected - flushing scrobble queue and syncing playlists');
|
||||||
|
|
||||||
|
// Flush scrobble queue
|
||||||
flushScrobbleQueue().then(({ sent, failed }) => {
|
flushScrobbleQueue().then(({ sent, failed }) => {
|
||||||
if (sent > 0 || failed > 0) {
|
if (sent > 0 || failed > 0) {
|
||||||
console.log(`[Bootstrap] Scrobble queue flush complete: ${sent} sent, ${failed} failed`);
|
console.log(`[Bootstrap] Scrobble queue flush complete: ${sent} sent, ${failed} failed`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Sync pending playlists
|
||||||
|
syncPendingPlaylists().catch(err => {
|
||||||
|
console.error('[Bootstrap] Failed to sync pending playlists:', err);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -109,7 +109,8 @@
|
|||||||
name.trim(),
|
name.trim(),
|
||||||
description.trim() || null,
|
description.trim() || null,
|
||||||
isPublic,
|
isPublic,
|
||||||
[]
|
[], // No Qobuz tracks for empty playlist
|
||||||
|
[] // No local tracks for empty playlist
|
||||||
);
|
);
|
||||||
showToast('Playlist created offline - will sync when back online', 'info');
|
showToast('Playlist created offline - will sync when back online', 'info');
|
||||||
// Create a temporary playlist object for UI
|
// Create a temporary playlist object for UI
|
||||||
@@ -245,19 +246,17 @@
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (isOffline()) {
|
if (isOffline()) {
|
||||||
// In offline mode, can only create pending playlists with Qobuz tracks
|
// In offline mode, create pending playlist with both Qobuz and local tracks
|
||||||
if (isLocalTracks) {
|
const qobuzTrackIds = isLocalTracks ? [] : trackIds;
|
||||||
error = 'Cannot create playlist with local tracks in offline mode';
|
const localTrackIds = isLocalTracks ? trackIds : [];
|
||||||
loading = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create pending playlist with Qobuz tracks for sync when back online
|
// Create pending playlist for sync when back online
|
||||||
const pendingId = await createPendingPlaylist(
|
const pendingId = await createPendingPlaylist(
|
||||||
name.trim(),
|
name.trim(),
|
||||||
description.trim() || null,
|
description.trim() || null,
|
||||||
false,
|
false,
|
||||||
trackIds
|
qobuzTrackIds,
|
||||||
|
localTrackIds
|
||||||
);
|
);
|
||||||
showToast(`Playlist "${name.trim()}" created offline - will sync when back online`, 'info');
|
showToast(`Playlist "${name.trim()}" created offline - will sync when back online`, 'info');
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ export interface PendingPlaylist {
|
|||||||
description: string | null;
|
description: string | null;
|
||||||
isPublic: boolean;
|
isPublic: boolean;
|
||||||
trackIds: number[];
|
trackIds: number[];
|
||||||
|
localTrackIds: number[];
|
||||||
createdAt: number;
|
createdAt: number;
|
||||||
synced: boolean;
|
synced: boolean;
|
||||||
qobuzPlaylistId: number | null;
|
qobuzPlaylistId: number | null;
|
||||||
@@ -321,13 +322,15 @@ export async function createPendingPlaylist(
|
|||||||
name: string,
|
name: string,
|
||||||
description: string | null,
|
description: string | null,
|
||||||
isPublic: boolean,
|
isPublic: boolean,
|
||||||
trackIds: number[]
|
trackIds: number[],
|
||||||
|
localTrackIds: number[]
|
||||||
): Promise<number> {
|
): Promise<number> {
|
||||||
return invoke<number>('create_pending_playlist', {
|
return invoke<number>('create_pending_playlist', {
|
||||||
name,
|
name,
|
||||||
description,
|
description,
|
||||||
isPublic,
|
isPublic,
|
||||||
trackIds,
|
trackIds,
|
||||||
|
localTrackIds,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -419,6 +422,73 @@ export async function cleanupSentScrobbles(olderThanDays?: number): Promise<numb
|
|||||||
return invoke<number>('cleanup_sent_scrobbles', { olderThanDays });
|
return invoke<number>('cleanup_sent_scrobbles', { olderThanDays });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sync all pending playlists to Qobuz when back online
|
||||||
|
*/
|
||||||
|
export async function syncPendingPlaylists(): Promise<void> {
|
||||||
|
try {
|
||||||
|
console.log('[Offline] Syncing pending playlists...');
|
||||||
|
const pending = await getPendingPlaylists();
|
||||||
|
|
||||||
|
if (pending.length === 0) {
|
||||||
|
console.log('[Offline] No pending playlists to sync');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`[Offline] Found ${pending.length} pending playlists to sync`);
|
||||||
|
|
||||||
|
for (const playlist of pending) {
|
||||||
|
try {
|
||||||
|
console.log(`[Offline] Syncing playlist: ${playlist.name}`);
|
||||||
|
|
||||||
|
// Create playlist on Qobuz
|
||||||
|
const createdPlaylist = await invoke<{ id: number }>('create_playlist', {
|
||||||
|
name: playlist.name,
|
||||||
|
description: playlist.description,
|
||||||
|
isPublic: playlist.isPublic
|
||||||
|
});
|
||||||
|
|
||||||
|
const qobuzPlaylistId = createdPlaylist.id;
|
||||||
|
console.log(`[Offline] Created playlist on Qobuz with ID: ${qobuzPlaylistId}`);
|
||||||
|
|
||||||
|
// Add Qobuz tracks if any
|
||||||
|
if (playlist.trackIds.length > 0) {
|
||||||
|
console.log(`[Offline] Adding ${playlist.trackIds.length} Qobuz tracks`);
|
||||||
|
await invoke('add_tracks_to_playlist', {
|
||||||
|
playlistId: qobuzPlaylistId,
|
||||||
|
trackIds: playlist.trackIds
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add local tracks if any
|
||||||
|
if (playlist.localTrackIds.length > 0) {
|
||||||
|
console.log(`[Offline] Adding ${playlist.localTrackIds.length} local tracks`);
|
||||||
|
for (let i = 0; i < playlist.localTrackIds.length; i++) {
|
||||||
|
await invoke('playlist_add_local_track', {
|
||||||
|
playlistId: qobuzPlaylistId,
|
||||||
|
localTrackId: playlist.localTrackIds[i],
|
||||||
|
position: playlist.trackIds.length + i // Position after Qobuz tracks
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark as synced
|
||||||
|
await markPendingPlaylistSynced(playlist.id, qobuzPlaylistId);
|
||||||
|
console.log(`[Offline] Successfully synced playlist: ${playlist.name}`);
|
||||||
|
|
||||||
|
showToast(`Synced playlist "${playlist.name}"`, 'success');
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`[Offline] Failed to sync playlist "${playlist.name}":`, err);
|
||||||
|
showToast(`Failed to sync playlist "${playlist.name}"`, 'error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('[Offline] Finished syncing pending playlists');
|
||||||
|
} catch (err) {
|
||||||
|
console.error('[Offline] Error syncing pending playlists:', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ============ State Getter ============
|
// ============ State Getter ============
|
||||||
|
|
||||||
export interface OfflineState {
|
export interface OfflineState {
|
||||||
|
|||||||
Reference in new issue
Block a user