diff --git a/src/lib/app/bootstrap.ts b/src/lib/app/bootstrap.ts index e8fd30856..04b570f71 100644 --- a/src/lib/app/bootstrap.ts +++ b/src/lib/app/bootstrap.ts @@ -10,7 +10,7 @@ import { getCurrentWebview } from '@tauri-apps/api/webview'; import { goBack, goForward } from '$lib/stores/navigationStore'; import { loadToastsPreference } from '$lib/stores/toastStore'; 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 { getZoom, setZoom } from '$lib/stores/zoomStore'; @@ -141,14 +141,21 @@ export function bootstrapApp(): BootstrapResult { // Initialize offline store (async, fire-and-forget) 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(() => { - 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 }) => { if (sent > 0 || failed > 0) { 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 { diff --git a/src/lib/components/PlaylistModal.svelte b/src/lib/components/PlaylistModal.svelte index f1ced069a..30da227a1 100644 --- a/src/lib/components/PlaylistModal.svelte +++ b/src/lib/components/PlaylistModal.svelte @@ -109,7 +109,8 @@ name.trim(), description.trim() || null, isPublic, - [] + [], // No Qobuz tracks for empty playlist + [] // No local tracks for empty playlist ); showToast('Playlist created offline - will sync when back online', 'info'); // Create a temporary playlist object for UI @@ -245,19 +246,17 @@ try { if (isOffline()) { - // In offline mode, can only create pending playlists with Qobuz tracks - if (isLocalTracks) { - error = 'Cannot create playlist with local tracks in offline mode'; - loading = false; - return; - } + // In offline mode, create pending playlist with both Qobuz and local tracks + const qobuzTrackIds = isLocalTracks ? [] : trackIds; + const localTrackIds = isLocalTracks ? trackIds : []; - // Create pending playlist with Qobuz tracks for sync when back online + // Create pending playlist for sync when back online const pendingId = await createPendingPlaylist( name.trim(), description.trim() || null, false, - trackIds + qobuzTrackIds, + localTrackIds ); showToast(`Playlist "${name.trim()}" created offline - will sync when back online`, 'info'); diff --git a/src/lib/stores/offlineStore.ts b/src/lib/stores/offlineStore.ts index 23b8e048d..79bab00bd 100644 --- a/src/lib/stores/offlineStore.ts +++ b/src/lib/stores/offlineStore.ts @@ -19,6 +19,7 @@ export interface PendingPlaylist { description: string | null; isPublic: boolean; trackIds: number[]; + localTrackIds: number[]; createdAt: number; synced: boolean; qobuzPlaylistId: number | null; @@ -321,13 +322,15 @@ export async function createPendingPlaylist( name: string, description: string | null, isPublic: boolean, - trackIds: number[] + trackIds: number[], + localTrackIds: number[] ): Promise { return invoke('create_pending_playlist', { name, description, isPublic, trackIds, + localTrackIds, }); } @@ -419,6 +422,73 @@ export async function cleanupSentScrobbles(olderThanDays?: number): Promise('cleanup_sent_scrobbles', { olderThanDays }); } +/** + * Sync all pending playlists to Qobuz when back online + */ +export async function syncPendingPlaylists(): Promise { + 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 ============ export interface OfflineState {