fix(favorites): ensure favorites sync with Qobuz API and show loading state
Issues fixed: - playbackService was bypassing the favorites store, causing UI desync - Album favorites weren't loaded on login (only track favorites were) - No visual feedback while favorite API call was in progress Changes: - Add toggling state tracking to favoritesStore and albumFavoritesStore - Add isTrackToggling() and isAlbumToggling() exports - Prevent double-toggle while API call is in progress - Update TrackRow and AlbumCard to show pulse animation while toggling - Update playbackService to use store's toggle (centralized API calls) - Load both track and album favorites on login
This commit is contained in:
1 parent
4858faa696
commit
defd2f0fe1
6 files changed
+94
-29
No files matched your search
@@ -5,6 +5,7 @@
|
|||||||
import {
|
import {
|
||||||
subscribe as subscribeAlbumFavorites,
|
subscribe as subscribeAlbumFavorites,
|
||||||
isAlbumFavorite,
|
isAlbumFavorite,
|
||||||
|
isAlbumToggling,
|
||||||
loadAlbumFavorites,
|
loadAlbumFavorites,
|
||||||
toggleAlbumFavorite
|
toggleAlbumFavorite
|
||||||
} from '$lib/stores/albumFavoritesStore';
|
} from '$lib/stores/albumFavoritesStore';
|
||||||
@@ -86,6 +87,7 @@
|
|||||||
const artistDuration = $derived(artistOverflow > 0 ? `${(artistOverflow + 16) / tickerSpeed}s` : '0s');
|
const artistDuration = $derived(artistOverflow > 0 ? `${(artistOverflow + 16) / tickerSpeed}s` : '0s');
|
||||||
|
|
||||||
let favoriteFromStore = $state(false);
|
let favoriteFromStore = $state(false);
|
||||||
|
let isToggling = $state(false);
|
||||||
const isFavorite = $derived(albumId ? favoriteFromStore : false);
|
const isFavorite = $derived(albumId ? favoriteFromStore : false);
|
||||||
const hasMenu = $derived(!!(onPlayNext || onPlayLater || onShareQobuz || onShareSonglink || onDownload));
|
const hasMenu = $derived(!!(onPlayNext || onPlayLater || onShareQobuz || onShareSonglink || onDownload));
|
||||||
const showFavoriteButton = $derived(showFavorite ?? !!albumId);
|
const showFavoriteButton = $derived(showFavorite ?? !!albumId);
|
||||||
@@ -158,8 +160,10 @@
|
|||||||
if (albumId) {
|
if (albumId) {
|
||||||
void loadAlbumFavorites();
|
void loadAlbumFavorites();
|
||||||
favoriteFromStore = isAlbumFavorite(albumId);
|
favoriteFromStore = isAlbumFavorite(albumId);
|
||||||
|
isToggling = isAlbumToggling(albumId);
|
||||||
const unsubscribe = subscribeAlbumFavorites(() => {
|
const unsubscribe = subscribeAlbumFavorites(() => {
|
||||||
favoriteFromStore = isAlbumFavorite(albumId);
|
favoriteFromStore = isAlbumFavorite(albumId);
|
||||||
|
isToggling = isAlbumToggling(albumId);
|
||||||
});
|
});
|
||||||
return () => unsubscribe();
|
return () => unsubscribe();
|
||||||
}
|
}
|
||||||
@@ -217,10 +221,11 @@
|
|||||||
<button
|
<button
|
||||||
class="overlay-btn overlay-btn--minor"
|
class="overlay-btn overlay-btn--minor"
|
||||||
class:is-active={isFavorite}
|
class:is-active={isFavorite}
|
||||||
|
class:is-toggling={isToggling}
|
||||||
class:disabled={!favoriteAvailable}
|
class:disabled={!favoriteAvailable}
|
||||||
type="button"
|
type="button"
|
||||||
aria-disabled={!favoriteAvailable}
|
aria-disabled={!favoriteAvailable || isToggling}
|
||||||
disabled={!favoriteAvailable}
|
disabled={!favoriteAvailable || isToggling}
|
||||||
onclick={handleToggleFavorite}
|
onclick={handleToggleFavorite}
|
||||||
title={isFavorite ? 'Remove from favorites' : 'Add to favorites'}
|
title={isFavorite ? 'Remove from favorites' : 'Add to favorites'}
|
||||||
>
|
>
|
||||||
@@ -466,6 +471,20 @@
|
|||||||
transform: none;
|
transform: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.overlay-btn.is-toggling {
|
||||||
|
cursor: wait;
|
||||||
|
animation: favorite-pulse 0.8s ease-in-out infinite;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes favorite-pulse {
|
||||||
|
0%, 100% {
|
||||||
|
opacity: 0.5;
|
||||||
|
}
|
||||||
|
50% {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.overlay-menu {
|
.overlay-menu {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
import {
|
import {
|
||||||
subscribe as subscribeFavorites,
|
subscribe as subscribeFavorites,
|
||||||
isTrackFavorite,
|
isTrackFavorite,
|
||||||
|
isTrackToggling,
|
||||||
toggleTrackFavorite
|
toggleTrackFavorite
|
||||||
} from '$lib/stores/favoritesStore';
|
} from '$lib/stores/favoritesStore';
|
||||||
import { togglePlay } from '$lib/stores/playerStore';
|
import { togglePlay } from '$lib/stores/playerStore';
|
||||||
@@ -82,6 +83,7 @@
|
|||||||
|
|
||||||
let isHovered = $state(false);
|
let isHovered = $state(false);
|
||||||
let favoriteFromStore = $state(false);
|
let favoriteFromStore = $state(false);
|
||||||
|
let isToggling = $state(false);
|
||||||
|
|
||||||
// Use override if provided, otherwise use store
|
// Use override if provided, otherwise use store
|
||||||
const isFavorite = $derived(isFavoriteOverride ?? favoriteFromStore);
|
const isFavorite = $derived(isFavoriteOverride ?? favoriteFromStore);
|
||||||
@@ -93,8 +95,10 @@
|
|||||||
onMount(() => {
|
onMount(() => {
|
||||||
if (trackId !== undefined) {
|
if (trackId !== undefined) {
|
||||||
favoriteFromStore = isTrackFavorite(trackId);
|
favoriteFromStore = isTrackFavorite(trackId);
|
||||||
|
isToggling = isTrackToggling(trackId);
|
||||||
const unsubscribe = subscribeFavorites(() => {
|
const unsubscribe = subscribeFavorites(() => {
|
||||||
favoriteFromStore = isTrackFavorite(trackId);
|
favoriteFromStore = isTrackFavorite(trackId);
|
||||||
|
isToggling = isTrackToggling(trackId);
|
||||||
});
|
});
|
||||||
return unsubscribe;
|
return unsubscribe;
|
||||||
}
|
}
|
||||||
@@ -208,8 +212,10 @@
|
|||||||
type="button"
|
type="button"
|
||||||
class="favorite-btn"
|
class="favorite-btn"
|
||||||
class:is-favorite={isFavorite}
|
class:is-favorite={isFavorite}
|
||||||
|
class:is-toggling={isToggling}
|
||||||
onclick={handleToggleFavorite}
|
onclick={handleToggleFavorite}
|
||||||
title={isFavorite ? 'Remove from favorites' : 'Add to favorites'}
|
title={isFavorite ? 'Remove from favorites' : 'Add to favorites'}
|
||||||
|
disabled={isToggling}
|
||||||
>
|
>
|
||||||
{#if isFavorite}
|
{#if isFavorite}
|
||||||
<Heart size={14} fill="var(--accent-primary)" color="var(--accent-primary)" />
|
<Heart size={14} fill="var(--accent-primary)" color="var(--accent-primary)" />
|
||||||
@@ -494,6 +500,21 @@
|
|||||||
opacity: 1;
|
opacity: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.favorite-btn.is-toggling {
|
||||||
|
opacity: 1;
|
||||||
|
cursor: wait;
|
||||||
|
animation: favorite-pulse 0.8s ease-in-out infinite;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes favorite-pulse {
|
||||||
|
0%, 100% {
|
||||||
|
opacity: 0.4;
|
||||||
|
}
|
||||||
|
50% {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.download-indicator {
|
.download-indicator {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
|||||||
@@ -41,6 +41,10 @@ import {
|
|||||||
markScrobblesSent,
|
markScrobblesSent,
|
||||||
cleanupSentScrobbles
|
cleanupSentScrobbles
|
||||||
} from '$lib/stores/offlineStore';
|
} from '$lib/stores/offlineStore';
|
||||||
|
import {
|
||||||
|
toggleTrackFavorite as storeToggleTrackFavorite,
|
||||||
|
addTrackFavorite as storeAddTrackFavorite
|
||||||
|
} from '$lib/stores/favoritesStore';
|
||||||
|
|
||||||
// ============ Types ============
|
// ============ Types ============
|
||||||
|
|
||||||
@@ -661,26 +665,17 @@ export async function checkTrackFavorite(trackId: number): Promise<boolean> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Toggle favorite status for a track
|
* Toggle favorite status for a track
|
||||||
|
* Uses the centralized favorites store to ensure UI stays in sync
|
||||||
*/
|
*/
|
||||||
export async function toggleTrackFavorite(
|
export async function toggleTrackFavorite(
|
||||||
trackId: number,
|
trackId: number,
|
||||||
currentlyFavorite: boolean
|
currentlyFavorite: boolean
|
||||||
): Promise<{ success: boolean; isFavorite: boolean }> {
|
): Promise<{ success: boolean; isFavorite: boolean }> {
|
||||||
const newFavoriteState = !currentlyFavorite;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (newFavoriteState) {
|
// Use the store's toggle function - it handles API calls, optimistic updates,
|
||||||
await invoke('add_favorite', { favType: 'track', itemId: String(trackId) });
|
// loading states, and notifies all subscribed components
|
||||||
// Log favorite event for recommendations
|
const newState = await storeToggleTrackFavorite(trackId);
|
||||||
void logRecoEvent({
|
return { success: true, isFavorite: newState };
|
||||||
eventType: 'favorite',
|
|
||||||
itemType: 'track',
|
|
||||||
trackId
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
await invoke('remove_favorite', { favType: 'track', itemId: String(trackId) });
|
|
||||||
}
|
|
||||||
return { success: true, isFavorite: newFavoriteState };
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to toggle favorite:', err);
|
console.error('Failed to toggle favorite:', err);
|
||||||
return { success: false, isFavorite: currentlyFavorite };
|
return { success: false, isFavorite: currentlyFavorite };
|
||||||
@@ -689,17 +684,12 @@ export async function toggleTrackFavorite(
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a track to favorites
|
* Add a track to favorites
|
||||||
|
* Uses the centralized favorites store to ensure UI stays in sync
|
||||||
*/
|
*/
|
||||||
export async function addTrackToFavorites(trackId: number): Promise<boolean> {
|
export async function addTrackToFavorites(trackId: number): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
await invoke('add_favorite', { favType: 'track', itemId: String(trackId) });
|
// Use the store's add function - it handles API calls and UI sync
|
||||||
// Log favorite event for recommendations
|
return await storeAddTrackFavorite(trackId);
|
||||||
void logRecoEvent({
|
|
||||||
eventType: 'favorite',
|
|
||||||
itemType: 'track',
|
|
||||||
trackId
|
|
||||||
});
|
|
||||||
return true;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to add to favorites:', err);
|
console.error('Failed to add to favorites:', err);
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { invoke } from '@tauri-apps/api/core';
|
|||||||
import { logRecoEvent } from '$lib/services/recoService';
|
import { logRecoEvent } from '$lib/services/recoService';
|
||||||
|
|
||||||
let favoriteAlbumIds = new Set<string>();
|
let favoriteAlbumIds = new Set<string>();
|
||||||
|
let togglingAlbumIds = new Set<string>(); // Album IDs currently being toggled (API in progress)
|
||||||
let isLoaded = false;
|
let isLoaded = false;
|
||||||
let isLoading = false;
|
let isLoading = false;
|
||||||
const listeners = new Set<() => void>();
|
const listeners = new Set<() => void>();
|
||||||
@@ -27,6 +28,10 @@ export function isAlbumFavorite(albumId: string): boolean {
|
|||||||
return favoriteAlbumIds.has(albumId);
|
return favoriteAlbumIds.has(albumId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isAlbumToggling(albumId: string): boolean {
|
||||||
|
return togglingAlbumIds.has(albumId);
|
||||||
|
}
|
||||||
|
|
||||||
export function getFavoriteAlbumIds(): string[] {
|
export function getFavoriteAlbumIds(): string[] {
|
||||||
return Array.from(favoriteAlbumIds);
|
return Array.from(favoriteAlbumIds);
|
||||||
}
|
}
|
||||||
@@ -72,14 +77,21 @@ export async function loadAlbumFavorites(): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function toggleAlbumFavorite(albumId: string): Promise<boolean> {
|
export async function toggleAlbumFavorite(albumId: string): Promise<boolean> {
|
||||||
|
// Prevent double-toggling while API call is in progress
|
||||||
|
if (togglingAlbumIds.has(albumId)) {
|
||||||
|
return favoriteAlbumIds.has(albumId);
|
||||||
|
}
|
||||||
|
|
||||||
const wasFavorite = favoriteAlbumIds.has(albumId);
|
const wasFavorite = favoriteAlbumIds.has(albumId);
|
||||||
const newState = !wasFavorite;
|
const newState = !wasFavorite;
|
||||||
|
|
||||||
|
// Optimistic update + mark as toggling
|
||||||
if (newState) {
|
if (newState) {
|
||||||
favoriteAlbumIds.add(albumId);
|
favoriteAlbumIds.add(albumId);
|
||||||
} else {
|
} else {
|
||||||
favoriteAlbumIds.delete(albumId);
|
favoriteAlbumIds.delete(albumId);
|
||||||
}
|
}
|
||||||
|
togglingAlbumIds.add(albumId);
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -100,9 +112,12 @@ export async function toggleAlbumFavorite(albumId: string): Promise<boolean> {
|
|||||||
} else {
|
} else {
|
||||||
favoriteAlbumIds.add(albumId);
|
favoriteAlbumIds.add(albumId);
|
||||||
}
|
}
|
||||||
notifyListeners();
|
|
||||||
console.error('Failed to toggle album favorite:', err);
|
console.error('Failed to toggle album favorite:', err);
|
||||||
return wasFavorite;
|
return wasFavorite;
|
||||||
|
} finally {
|
||||||
|
// Always clear toggling state
|
||||||
|
togglingAlbumIds.delete(albumId);
|
||||||
|
notifyListeners();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import { logRecoEvent } from '$lib/services/recoService';
|
|||||||
|
|
||||||
// State
|
// State
|
||||||
let favoriteTrackIds = new Set<number>();
|
let favoriteTrackIds = new Set<number>();
|
||||||
|
let togglingTrackIds = new Set<number>(); // Track IDs currently being toggled (API in progress)
|
||||||
let isLoaded = false;
|
let isLoaded = false;
|
||||||
let isLoading = false;
|
let isLoading = false;
|
||||||
const listeners = new Set<() => void>();
|
const listeners = new Set<() => void>();
|
||||||
@@ -37,6 +38,13 @@ export function isTrackFavorite(trackId: number): boolean {
|
|||||||
return favoriteTrackIds.has(trackId);
|
return favoriteTrackIds.has(trackId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a track favorite is currently being toggled (API call in progress)
|
||||||
|
*/
|
||||||
|
export function isTrackToggling(trackId: number): boolean {
|
||||||
|
return togglingTrackIds.has(trackId);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all favorite track IDs
|
* Get all favorite track IDs
|
||||||
*/
|
*/
|
||||||
@@ -88,15 +96,21 @@ export async function loadFavorites(): Promise<void> {
|
|||||||
* Returns the new favorite state
|
* Returns the new favorite state
|
||||||
*/
|
*/
|
||||||
export async function toggleTrackFavorite(trackId: number): Promise<boolean> {
|
export async function toggleTrackFavorite(trackId: number): Promise<boolean> {
|
||||||
|
// Prevent double-toggling while API call is in progress
|
||||||
|
if (togglingTrackIds.has(trackId)) {
|
||||||
|
return favoriteTrackIds.has(trackId);
|
||||||
|
}
|
||||||
|
|
||||||
const wasFavorite = favoriteTrackIds.has(trackId);
|
const wasFavorite = favoriteTrackIds.has(trackId);
|
||||||
const newState = !wasFavorite;
|
const newState = !wasFavorite;
|
||||||
|
|
||||||
// Optimistic update
|
// Optimistic update + mark as toggling
|
||||||
if (newState) {
|
if (newState) {
|
||||||
favoriteTrackIds.add(trackId);
|
favoriteTrackIds.add(trackId);
|
||||||
} else {
|
} else {
|
||||||
favoriteTrackIds.delete(trackId);
|
favoriteTrackIds.delete(trackId);
|
||||||
}
|
}
|
||||||
|
togglingTrackIds.add(trackId);
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -118,9 +132,12 @@ export async function toggleTrackFavorite(trackId: number): Promise<boolean> {
|
|||||||
} else {
|
} else {
|
||||||
favoriteTrackIds.add(trackId);
|
favoriteTrackIds.add(trackId);
|
||||||
}
|
}
|
||||||
notifyListeners();
|
|
||||||
console.error('Failed to toggle favorite:', err);
|
console.error('Failed to toggle favorite:', err);
|
||||||
return wasFavorite;
|
return wasFavorite;
|
||||||
|
} finally {
|
||||||
|
// Always clear toggling state
|
||||||
|
togglingTrackIds.delete(trackId);
|
||||||
|
notifyListeners();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,6 +183,7 @@ export function unmarkAsFavorite(trackId: number): void {
|
|||||||
*/
|
*/
|
||||||
export function reset(): void {
|
export function reset(): void {
|
||||||
favoriteTrackIds = new Set();
|
favoriteTrackIds = new Set();
|
||||||
|
togglingTrackIds = new Set();
|
||||||
isLoaded = false;
|
isLoaded = false;
|
||||||
isLoading = false;
|
isLoading = false;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
|
|||||||
@@ -92,6 +92,7 @@
|
|||||||
|
|
||||||
// Favorites state management
|
// Favorites state management
|
||||||
import { loadFavorites } from '$lib/stores/favoritesStore';
|
import { loadFavorites } from '$lib/stores/favoritesStore';
|
||||||
|
import { loadAlbumFavorites } from '$lib/stores/albumFavoritesStore';
|
||||||
import { getDefaultFavoritesTab } from '$lib/utils/favorites';
|
import { getDefaultFavoritesTab } from '$lib/utils/favorites';
|
||||||
import type { FavoritesPreferences, ResolvedMusician } from '$lib/types';
|
import type { FavoritesPreferences, ResolvedMusician } from '$lib/types';
|
||||||
|
|
||||||
@@ -1935,8 +1936,9 @@
|
|||||||
setLoggedIn(info);
|
setLoggedIn(info);
|
||||||
showToast(`Welcome, ${info.userName}!`, 'success');
|
showToast(`Welcome, ${info.userName}!`, 'success');
|
||||||
|
|
||||||
// Load favorites now that login is confirmed
|
// Load favorites now that login is confirmed (sync with Qobuz)
|
||||||
loadFavorites();
|
loadFavorites(); // Track favorites
|
||||||
|
loadAlbumFavorites(); // Album favorites
|
||||||
|
|
||||||
// Refresh offline status now that we're logged in
|
// Refresh offline status now that we're logged in
|
||||||
await refreshOfflineStatus();
|
await refreshOfflineStatus();
|
||||||
|
|||||||
Reference in new issue
Block a user