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:
vicrodh committed 2026-01-20 08:24:00 -06:00
1 parent ea03553718
commit 85f1b2da6f
3 files changed
+89 -13

No files matched your search

+10 -3
View File
@@ -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 {
+8 -9
View File
@@ -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');
+71 -1
View File
@@ -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<number> {
return invoke<number>('create_pending_playlist', {
name,
description,
isPublic,
trackIds,
localTrackIds,
});
}
@@ -419,6 +422,73 @@ export async function cleanupSentScrobbles(olderThanDays?: number): Promise<numb
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 ============
export interface OfflineState {