fix(v2): migrate favorites cache, session, and context stores to V2 commands

- Add v2_save_session_state and v2_load_session_state commands
- Update favoritesStore to use V2 cache commands
- Update albumFavoritesStore to use V2 cache commands
- Update artistFavoritesStore to use V2 cache commands
- Update sessionService to use V2 session commands
- Update playbackContextStore to use V2 context commands
This commit is contained in:
vicrodh committed 2026-02-15 21:55:15 -06:00
1 parent 35758d07df
commit 68aec71fdf
7 files changed
+79 -31

No files matched your search

+46
View File
@@ -3065,6 +3065,52 @@ pub async fn v2_save_session_playback_mode(
.map_err(|e| RuntimeError::Internal(e)) .map_err(|e| RuntimeError::Internal(e))
} }
/// Save session state - full state (V2)
#[tauri::command]
pub async fn v2_save_session_state(
queueTracks: Vec<crate::session_store::PersistedQueueTrack>,
currentIndex: Option<usize>,
currentPositionSecs: u64,
volume: f32,
shuffleEnabled: bool,
repeatMode: String,
wasPlaying: bool,
session_state: State<'_, crate::session_store::SessionStoreState>,
) -> Result<(), RuntimeError> {
let session = crate::session_store::PersistedSession {
queue_tracks: queueTracks,
current_index: currentIndex,
current_position_secs: currentPositionSecs,
volume,
shuffle_enabled: shuffleEnabled,
repeat_mode: repeatMode,
was_playing: wasPlaying,
saved_at: 0, // Will be set by save_session
};
let guard = session_state.store.lock()
.map_err(|e| RuntimeError::Internal(format!("Lock error: {}", e)))?;
let store = guard.as_ref()
.ok_or_else(|| RuntimeError::Internal("No active session".to_string()))?;
store.save_session(&session)
.map_err(|e| RuntimeError::Internal(e))?;
log::debug!("[V2] save_session_state: index={:?} pos={}", currentIndex, currentPositionSecs);
Ok(())
}
/// Load session state (V2)
#[tauri::command]
pub async fn v2_load_session_state(
session_state: State<'_, crate::session_store::SessionStoreState>,
) -> Result<crate::session_store::PersistedSession, RuntimeError> {
let guard = session_state.store.lock()
.map_err(|e| RuntimeError::Internal(format!("Lock error: {}", e)))?;
let store = guard.as_ref()
.ok_or_else(|| RuntimeError::Internal("No active session".to_string()))?;
store.load_session()
.map_err(|e| RuntimeError::Internal(e))
}
/// Clear session (V2) /// Clear session (V2)
#[tauri::command] #[tauri::command]
pub async fn v2_clear_session( pub async fn v2_clear_session(
+2
View File
@@ -1233,6 +1233,8 @@ pub fn run() {
commands_v2::v2_clear_playback_context, commands_v2::v2_clear_playback_context,
commands_v2::v2_has_playback_context, commands_v2::v2_has_playback_context,
// Session Persistence V2 // Session Persistence V2
commands_v2::v2_save_session_state,
commands_v2::v2_load_session_state,
commands_v2::v2_save_session_position, commands_v2::v2_save_session_position,
commands_v2::v2_save_session_volume, commands_v2::v2_save_session_volume,
commands_v2::v2_save_session_playback_mode, commands_v2::v2_save_session_playback_mode,
+6 -6
View File
@@ -46,7 +46,7 @@ export async function saveSessionState(
wasPlaying: boolean wasPlaying: boolean
): Promise<void> { ): Promise<void> {
try { try {
await invoke('save_session_state', { await invoke('v2_save_session_state', {
queueTracks, queueTracks,
currentIndex, currentIndex,
currentPositionSecs, currentPositionSecs,
@@ -66,7 +66,7 @@ export async function saveSessionState(
*/ */
export async function loadSessionState(): Promise<PersistedSession | null> { export async function loadSessionState(): Promise<PersistedSession | null> {
try { try {
const session = await invoke<PersistedSession>('load_session_state'); const session = await invoke<PersistedSession>('v2_load_session_state');
console.log('[Session] State loaded:', { console.log('[Session] State loaded:', {
tracks: session.queue_tracks.length, tracks: session.queue_tracks.length,
currentIndex: session.current_index, currentIndex: session.current_index,
@@ -85,7 +85,7 @@ export async function loadSessionState(): Promise<PersistedSession | null> {
*/ */
export async function saveSessionPosition(positionSecs: number): Promise<void> { export async function saveSessionPosition(positionSecs: number): Promise<void> {
try { try {
await invoke('save_session_position', { positionSecs }); await invoke('v2_save_session_position', { positionSecs });
} catch (err) { } catch (err) {
console.error('[Session] Failed to save position:', err); console.error('[Session] Failed to save position:', err);
} }
@@ -96,7 +96,7 @@ export async function saveSessionPosition(positionSecs: number): Promise<void> {
*/ */
export async function saveSessionVolume(volume: number): Promise<void> { export async function saveSessionVolume(volume: number): Promise<void> {
try { try {
await invoke('save_session_volume', { volume }); await invoke('v2_save_session_volume', { volume });
} catch (err) { } catch (err) {
console.error('[Session] Failed to save volume:', err); console.error('[Session] Failed to save volume:', err);
} }
@@ -110,7 +110,7 @@ export async function saveSessionPlaybackMode(
repeatMode: string repeatMode: string
): Promise<void> { ): Promise<void> {
try { try {
await invoke('save_session_playback_mode', { shuffle, repeatMode }); await invoke('v2_save_session_playback_mode', { shuffle, repeatMode });
} catch (err) { } catch (err) {
console.error('[Session] Failed to save playback mode:', err); console.error('[Session] Failed to save playback mode:', err);
} }
@@ -121,7 +121,7 @@ export async function saveSessionPlaybackMode(
*/ */
export async function clearSession(): Promise<void> { export async function clearSession(): Promise<void> {
try { try {
await invoke('clear_session'); await invoke('v2_clear_session');
console.log('[Session] Session cleared'); console.log('[Session] Session cleared');
} catch (err) { } catch (err) {
console.error('[Session] Failed to clear session:', err); console.error('[Session] Failed to clear session:', err);
+7 -7
View File
@@ -56,7 +56,7 @@ export async function loadAlbumFavorites(): Promise<void> {
try { try {
// Step 1: Load from local cache for instant UI // Step 1: Load from local cache for instant UI
try { try {
const cachedIds = await invoke<string[]>('get_cached_favorite_albums'); const cachedIds = await invoke<string[]>('v2_get_cached_favorite_albums');
if (cachedIds.length > 0) { if (cachedIds.length > 0) {
favoriteAlbumIds = new Set(cachedIds); favoriteAlbumIds = new Set(cachedIds);
isLoaded = true; isLoaded = true;
@@ -109,7 +109,7 @@ export async function syncFromApi(): Promise<void> {
} }
// Sync to local cache // Sync to local cache
await invoke('sync_cached_favorite_albums', { albumIds: allAlbumIds }); await invoke('v2_sync_cached_favorite_albums', { albumIds: allAlbumIds });
// Update in-memory store // Update in-memory store
favoriteAlbumIds = new Set(allAlbumIds); favoriteAlbumIds = new Set(allAlbumIds);
@@ -142,7 +142,7 @@ export async function toggleAlbumFavorite(albumId: string): Promise<boolean> {
try { try {
if (newState) { if (newState) {
await invoke('v2_add_favorite', { favType: 'album', itemId: albumId }); await invoke('v2_add_favorite', { favType: 'album', itemId: albumId });
await invoke('cache_favorite_album', { albumId }); await invoke('v2_cache_favorite_album', { albumId });
favoriteAlbumIds.add(albumId); favoriteAlbumIds.add(albumId);
void logRecoEvent({ void logRecoEvent({
eventType: 'favorite', eventType: 'favorite',
@@ -151,7 +151,7 @@ export async function toggleAlbumFavorite(albumId: string): Promise<boolean> {
}); });
} else { } else {
await invoke('v2_remove_favorite', { favType: 'album', itemId: albumId }); await invoke('v2_remove_favorite', { favType: 'album', itemId: albumId });
await invoke('uncache_favorite_album', { albumId }); await invoke('v2_uncache_favorite_album', { albumId });
favoriteAlbumIds.delete(albumId); favoriteAlbumIds.delete(albumId);
} }
return newState; return newState;
@@ -170,7 +170,7 @@ export async function toggleAlbumFavorite(albumId: string): Promise<boolean> {
*/ */
export async function syncCache(albumIds: string[]): Promise<void> { export async function syncCache(albumIds: string[]): Promise<void> {
try { try {
await invoke('sync_cached_favorite_albums', { albumIds }); await invoke('v2_sync_cached_favorite_albums', { albumIds });
favoriteAlbumIds = new Set(albumIds); favoriteAlbumIds = new Set(albumIds);
notifyListeners(); notifyListeners();
} catch (err) { } catch (err) {
@@ -181,7 +181,7 @@ export async function syncCache(albumIds: string[]): Promise<void> {
export function markAsFavorite(albumId: string): void { export function markAsFavorite(albumId: string): void {
if (!favoriteAlbumIds.has(albumId)) { if (!favoriteAlbumIds.has(albumId)) {
favoriteAlbumIds.add(albumId); favoriteAlbumIds.add(albumId);
invoke('cache_favorite_album', { albumId }).catch(() => {}); invoke('v2_cache_favorite_album', { albumId }).catch(() => {});
notifyListeners(); notifyListeners();
} }
} }
@@ -189,7 +189,7 @@ export function markAsFavorite(albumId: string): void {
export function unmarkAsFavorite(albumId: string): void { export function unmarkAsFavorite(albumId: string): void {
if (favoriteAlbumIds.has(albumId)) { if (favoriteAlbumIds.has(albumId)) {
favoriteAlbumIds.delete(albumId); favoriteAlbumIds.delete(albumId);
invoke('uncache_favorite_album', { albumId }).catch(() => {}); invoke('v2_uncache_favorite_album', { albumId }).catch(() => {});
notifyListeners(); notifyListeners();
} }
} }
+7 -7
View File
@@ -56,7 +56,7 @@ export async function loadArtistFavorites(): Promise<void> {
try { try {
// Step 1: Load from local cache for instant UI // Step 1: Load from local cache for instant UI
try { try {
const cachedIds = await invoke<number[]>('get_cached_favorite_artists'); const cachedIds = await invoke<number[]>('v2_get_cached_favorite_artists');
if (cachedIds.length > 0) { if (cachedIds.length > 0) {
favoriteArtistIds = new Set(cachedIds); favoriteArtistIds = new Set(cachedIds);
isLoaded = true; isLoaded = true;
@@ -109,7 +109,7 @@ export async function syncFromApi(): Promise<void> {
} }
// Sync to local cache // Sync to local cache
await invoke('sync_cached_favorite_artists', { artistIds: allArtistIds }); await invoke('v2_sync_cached_favorite_artists', { artistIds: allArtistIds });
// Update in-memory store // Update in-memory store
favoriteArtistIds = new Set(allArtistIds); favoriteArtistIds = new Set(allArtistIds);
@@ -142,7 +142,7 @@ export async function toggleArtistFavorite(artistId: number): Promise<boolean> {
try { try {
if (newState) { if (newState) {
await invoke('v2_add_favorite', { favType: 'artist', itemId: String(artistId) }); await invoke('v2_add_favorite', { favType: 'artist', itemId: String(artistId) });
await invoke('cache_favorite_artist', { artistId }); await invoke('v2_cache_favorite_artist', { artistId });
favoriteArtistIds.add(artistId); favoriteArtistIds.add(artistId);
void logRecoEvent({ void logRecoEvent({
eventType: 'favorite', eventType: 'favorite',
@@ -151,7 +151,7 @@ export async function toggleArtistFavorite(artistId: number): Promise<boolean> {
}); });
} else { } else {
await invoke('v2_remove_favorite', { favType: 'artist', itemId: String(artistId) }); await invoke('v2_remove_favorite', { favType: 'artist', itemId: String(artistId) });
await invoke('uncache_favorite_artist', { artistId }); await invoke('v2_uncache_favorite_artist', { artistId });
favoriteArtistIds.delete(artistId); favoriteArtistIds.delete(artistId);
} }
return newState; return newState;
@@ -170,7 +170,7 @@ export async function toggleArtistFavorite(artistId: number): Promise<boolean> {
*/ */
export async function syncCache(artistIds: number[]): Promise<void> { export async function syncCache(artistIds: number[]): Promise<void> {
try { try {
await invoke('sync_cached_favorite_artists', { artistIds }); await invoke('v2_sync_cached_favorite_artists', { artistIds });
favoriteArtistIds = new Set(artistIds); favoriteArtistIds = new Set(artistIds);
notifyListeners(); notifyListeners();
} catch (err) { } catch (err) {
@@ -181,7 +181,7 @@ export async function syncCache(artistIds: number[]): Promise<void> {
export function markAsFavorite(artistId: number): void { export function markAsFavorite(artistId: number): void {
if (!favoriteArtistIds.has(artistId)) { if (!favoriteArtistIds.has(artistId)) {
favoriteArtistIds.add(artistId); favoriteArtistIds.add(artistId);
invoke('cache_favorite_artist', { artistId }).catch(() => {}); invoke('v2_cache_favorite_artist', { artistId }).catch(() => {});
notifyListeners(); notifyListeners();
} }
} }
@@ -189,7 +189,7 @@ export function markAsFavorite(artistId: number): void {
export function unmarkAsFavorite(artistId: number): void { export function unmarkAsFavorite(artistId: number): void {
if (favoriteArtistIds.has(artistId)) { if (favoriteArtistIds.has(artistId)) {
favoriteArtistIds.delete(artistId); favoriteArtistIds.delete(artistId);
invoke('uncache_favorite_artist', { artistId }).catch(() => {}); invoke('v2_uncache_favorite_artist', { artistId }).catch(() => {});
notifyListeners(); notifyListeners();
} }
} }
+7 -7
View File
@@ -75,7 +75,7 @@ export async function loadFavorites(): Promise<void> {
try { try {
// Step 1: Load from local cache for instant UI (if available) // Step 1: Load from local cache for instant UI (if available)
try { try {
const cachedIds = await invoke<number[]>('get_cached_favorite_tracks'); const cachedIds = await invoke<number[]>('v2_get_cached_favorite_tracks');
if (cachedIds.length > 0) { if (cachedIds.length > 0) {
favoriteTrackIds = new Set(cachedIds); favoriteTrackIds = new Set(cachedIds);
isLoaded = true; isLoaded = true;
@@ -132,7 +132,7 @@ export async function syncFromApi(): Promise<void> {
} }
// Sync to local cache // Sync to local cache
await invoke('sync_cached_favorite_tracks', { trackIds: allTrackIds }); await invoke('v2_sync_cached_favorite_tracks', { trackIds: allTrackIds });
// Update in-memory store // Update in-memory store
favoriteTrackIds = new Set(allTrackIds); favoriteTrackIds = new Set(allTrackIds);
@@ -169,7 +169,7 @@ export async function toggleTrackFavorite(trackId: number): Promise<boolean> {
if (newState) { if (newState) {
await invoke('v2_add_favorite', { favType: 'track', itemId: String(trackId) }); await invoke('v2_add_favorite', { favType: 'track', itemId: String(trackId) });
// API succeeded - update local cache // API succeeded - update local cache
await invoke('cache_favorite_track', { trackId }); await invoke('v2_cache_favorite_track', { trackId });
// Update in-memory store // Update in-memory store
favoriteTrackIds.add(trackId); favoriteTrackIds.add(trackId);
// Log for recommendations // Log for recommendations
@@ -181,7 +181,7 @@ export async function toggleTrackFavorite(trackId: number): Promise<boolean> {
} else { } else {
await invoke('v2_remove_favorite', { favType: 'track', itemId: String(trackId) }); await invoke('v2_remove_favorite', { favType: 'track', itemId: String(trackId) });
// API succeeded - update local cache // API succeeded - update local cache
await invoke('uncache_favorite_track', { trackId }); await invoke('v2_uncache_favorite_track', { trackId });
// Update in-memory store // Update in-memory store
favoriteTrackIds.delete(trackId); favoriteTrackIds.delete(trackId);
} }
@@ -234,7 +234,7 @@ export async function syncCache(trackIds: number[]): Promise<void> {
*/ */
export async function reset(): Promise<void> { export async function reset(): Promise<void> {
try { try {
await invoke('clear_favorites_cache'); await invoke('v2_clear_favorites_cache');
} catch (err) { } catch (err) {
console.debug('[Favorites] Failed to clear cache:', err); console.debug('[Favorites] Failed to clear cache:', err);
} }
@@ -250,7 +250,7 @@ export function markAsFavorite(trackId: number): void {
if (!favoriteTrackIds.has(trackId)) { if (!favoriteTrackIds.has(trackId)) {
favoriteTrackIds.add(trackId); favoriteTrackIds.add(trackId);
// Also update cache // Also update cache
invoke('cache_favorite_track', { trackId }).catch(() => {}); invoke('v2_cache_favorite_track', { trackId }).catch(() => {});
notifyListeners(); notifyListeners();
} }
} }
@@ -259,7 +259,7 @@ export function unmarkAsFavorite(trackId: number): void {
if (favoriteTrackIds.has(trackId)) { if (favoriteTrackIds.has(trackId)) {
favoriteTrackIds.delete(trackId); favoriteTrackIds.delete(trackId);
// Also update cache // Also update cache
invoke('uncache_favorite_track', { trackId }).catch(() => {}); invoke('v2_uncache_favorite_track', { trackId }).catch(() => {});
notifyListeners(); notifyListeners();
} }
} }
+4 -4
View File
@@ -48,7 +48,7 @@ export async function setPlaybackContext(
): Promise<void> { ): Promise<void> {
console.log('[PlaybackContext] Setting context:', { contextType, id, label, trackIds: trackIds.length, startPosition }); console.log('[PlaybackContext] Setting context:', { contextType, id, label, trackIds: trackIds.length, startPosition });
await invoke('set_playback_context', { await invoke('v2_set_playback_context', {
contextType, contextType,
id, id,
label, label,
@@ -76,7 +76,7 @@ export async function setPlaybackContext(
*/ */
export async function clearPlaybackContext(): Promise<void> { export async function clearPlaybackContext(): Promise<void> {
console.log('[PlaybackContext] Clearing context'); console.log('[PlaybackContext] Clearing context');
await invoke('clear_playback_context'); await invoke('v2_clear_playback_context');
currentContext = null; currentContext = null;
console.log('[PlaybackContext] Context cleared, notifying listeners'); console.log('[PlaybackContext] Context cleared, notifying listeners');
notifyListeners(); notifyListeners();
@@ -86,7 +86,7 @@ export async function clearPlaybackContext(): Promise<void> {
* Get the current playback context * Get the current playback context
*/ */
export async function getPlaybackContext(): Promise<PlaybackContext | null> { export async function getPlaybackContext(): Promise<PlaybackContext | null> {
const context = await invoke<PlaybackContext | null>('get_playback_context'); const context = await invoke<PlaybackContext | null>('v2_get_playback_context');
currentContext = context; currentContext = context;
notifyListeners(); notifyListeners();
return context; return context;
@@ -96,7 +96,7 @@ export async function getPlaybackContext(): Promise<PlaybackContext | null> {
* Check if a context is active * Check if a context is active
*/ */
export async function hasPlaybackContext(): Promise<boolean> { export async function hasPlaybackContext(): Promise<boolean> {
return await invoke<boolean>('has_playback_context'); return await invoke<boolean>('v2_has_playback_context');
} }
/** /**