refactor: extract queue state into queueStore
Move queue management into dedicated store: - Queue tracks, shuffle, repeat modes - Local track ID tracking for library playback - Queue operations: add, clear, sync, navigate - Backend queue state synchronization
This commit is contained in:
1 parent
55ec44cdc2
commit
c60cfe0525
2 files changed
+416
-125
No files matched your search
@@ -0,0 +1,338 @@
|
|||||||
|
/**
|
||||||
|
* Queue State Store
|
||||||
|
*
|
||||||
|
* Manages playback queue, shuffle, repeat, and local track tracking.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { invoke } from '@tauri-apps/api/core';
|
||||||
|
|
||||||
|
// ============ Types ============
|
||||||
|
|
||||||
|
export interface QueueTrack {
|
||||||
|
id: string;
|
||||||
|
artwork: string;
|
||||||
|
title: string;
|
||||||
|
artist: string;
|
||||||
|
duration: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface BackendQueueTrack {
|
||||||
|
id: number;
|
||||||
|
title: string;
|
||||||
|
artist: string;
|
||||||
|
album: string;
|
||||||
|
duration_secs: number;
|
||||||
|
artwork_url: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface BackendQueueState {
|
||||||
|
current_track: BackendQueueTrack | null;
|
||||||
|
current_index: number | null;
|
||||||
|
upcoming: BackendQueueTrack[];
|
||||||
|
history: BackendQueueTrack[];
|
||||||
|
shuffle: boolean;
|
||||||
|
repeat: 'Off' | 'All' | 'One';
|
||||||
|
total_tracks: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type RepeatMode = 'off' | 'all' | 'one';
|
||||||
|
|
||||||
|
// ============ State ============
|
||||||
|
|
||||||
|
let queue: QueueTrack[] = [];
|
||||||
|
let queueTotalTracks = 0;
|
||||||
|
let isShuffle = false;
|
||||||
|
let repeatMode: RepeatMode = 'off';
|
||||||
|
|
||||||
|
// Local library track IDs in current queue (for distinguishing from Qobuz tracks)
|
||||||
|
let localTrackIds = new Set<number>();
|
||||||
|
|
||||||
|
// Listeners
|
||||||
|
const listeners = new Set<() => void>();
|
||||||
|
|
||||||
|
function notifyListeners(): void {
|
||||||
|
for (const listener of listeners) {
|
||||||
|
listener();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subscribe to queue state changes
|
||||||
|
*/
|
||||||
|
export function subscribe(listener: () => void): () => void {
|
||||||
|
listeners.add(listener);
|
||||||
|
listener(); // Immediately notify with current state
|
||||||
|
return () => listeners.delete(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============ Getters ============
|
||||||
|
|
||||||
|
export function getQueue(): QueueTrack[] {
|
||||||
|
return queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getQueueTotalTracks(): number {
|
||||||
|
return queueTotalTracks;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getIsShuffle(): boolean {
|
||||||
|
return isShuffle;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getRepeatMode(): RepeatMode {
|
||||||
|
return repeatMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isLocalTrack(trackId: number): boolean {
|
||||||
|
return localTrackIds.has(trackId);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============ State Getters ============
|
||||||
|
|
||||||
|
export interface QueueState {
|
||||||
|
queue: QueueTrack[];
|
||||||
|
queueTotalTracks: number;
|
||||||
|
isShuffle: boolean;
|
||||||
|
repeatMode: RepeatMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getQueueState(): QueueState {
|
||||||
|
return {
|
||||||
|
queue: [...queue],
|
||||||
|
queueTotalTracks,
|
||||||
|
isShuffle,
|
||||||
|
repeatMode
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============ Internal Helpers ============
|
||||||
|
|
||||||
|
function formatDuration(seconds: number): string {
|
||||||
|
const mins = Math.floor(seconds / 60);
|
||||||
|
const secs = Math.floor(seconds % 60);
|
||||||
|
return `${mins}:${secs.toString().padStart(2, '0')}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============ Queue Actions ============
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sync queue state from backend
|
||||||
|
*/
|
||||||
|
export async function syncQueueState(): Promise<void> {
|
||||||
|
try {
|
||||||
|
const queueState = await invoke<BackendQueueState>('get_queue_state');
|
||||||
|
|
||||||
|
// Convert backend queue tracks to frontend format
|
||||||
|
queue = queueState.upcoming.map(t => ({
|
||||||
|
id: String(t.id),
|
||||||
|
artwork: t.artwork_url || '',
|
||||||
|
title: t.title,
|
||||||
|
artist: t.artist,
|
||||||
|
duration: formatDuration(t.duration_secs)
|
||||||
|
}));
|
||||||
|
|
||||||
|
queueTotalTracks = queueState.total_tracks;
|
||||||
|
isShuffle = queueState.shuffle;
|
||||||
|
repeatMode = queueState.repeat.toLowerCase() as RepeatMode;
|
||||||
|
notifyListeners();
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to sync queue state:', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Toggle shuffle mode
|
||||||
|
*/
|
||||||
|
export async function toggleShuffle(): Promise<{ success: boolean; enabled: boolean }> {
|
||||||
|
const newState = !isShuffle;
|
||||||
|
isShuffle = newState;
|
||||||
|
notifyListeners();
|
||||||
|
|
||||||
|
try {
|
||||||
|
await invoke('set_shuffle', { enabled: newState });
|
||||||
|
return { success: true, enabled: newState };
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to set shuffle:', err);
|
||||||
|
// Revert on error
|
||||||
|
isShuffle = !newState;
|
||||||
|
notifyListeners();
|
||||||
|
return { success: false, enabled: !newState };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Toggle repeat mode (off -> all -> one -> off)
|
||||||
|
*/
|
||||||
|
export async function toggleRepeat(): Promise<{ success: boolean; mode: RepeatMode }> {
|
||||||
|
const nextMode: RepeatMode = repeatMode === 'off' ? 'all' : repeatMode === 'all' ? 'one' : 'off';
|
||||||
|
|
||||||
|
try {
|
||||||
|
await invoke('set_repeat', { mode: nextMode });
|
||||||
|
repeatMode = nextMode;
|
||||||
|
notifyListeners();
|
||||||
|
return { success: true, mode: nextMode };
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to set repeat:', err);
|
||||||
|
return { success: false, mode: repeatMode };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add track to play next in queue
|
||||||
|
*/
|
||||||
|
export async function addToQueueNext(track: BackendQueueTrack, isLocal = false): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
await invoke('add_to_queue_next', { track });
|
||||||
|
if (isLocal) {
|
||||||
|
localTrackIds = new Set([...localTrackIds, track.id]);
|
||||||
|
}
|
||||||
|
await syncQueueState();
|
||||||
|
return true;
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to queue track next:', err);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add track to end of queue
|
||||||
|
*/
|
||||||
|
export async function addToQueue(track: BackendQueueTrack, isLocal = false): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
await invoke('add_to_queue', { track });
|
||||||
|
if (isLocal) {
|
||||||
|
localTrackIds = new Set([...localTrackIds, track.id]);
|
||||||
|
}
|
||||||
|
await syncQueueState();
|
||||||
|
return true;
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to add to queue:', err);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add multiple tracks to queue
|
||||||
|
*/
|
||||||
|
export async function addTracksToQueue(tracks: BackendQueueTrack[]): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
await invoke('add_tracks_to_queue', { tracks });
|
||||||
|
await syncQueueState();
|
||||||
|
return true;
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to add tracks to queue:', err);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set queue with new tracks
|
||||||
|
*/
|
||||||
|
export async function setQueue(tracks: BackendQueueTrack[], startIndex: number, clearLocal = true): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
await invoke('set_queue', { tracks, startIndex });
|
||||||
|
if (clearLocal) {
|
||||||
|
localTrackIds = new Set();
|
||||||
|
}
|
||||||
|
await syncQueueState();
|
||||||
|
return true;
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to set queue:', err);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear the queue
|
||||||
|
*/
|
||||||
|
export async function clearQueue(): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
await invoke('clear_queue');
|
||||||
|
queue = [];
|
||||||
|
queueTotalTracks = 0;
|
||||||
|
notifyListeners();
|
||||||
|
return true;
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to clear queue:', err);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Play track at specific index in queue
|
||||||
|
*/
|
||||||
|
export async function playQueueIndex(index: number): Promise<BackendQueueTrack | null> {
|
||||||
|
try {
|
||||||
|
return await invoke<BackendQueueTrack | null>('play_queue_index', { index });
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to play queue index:', err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get next track from queue
|
||||||
|
*/
|
||||||
|
export async function nextTrack(): Promise<BackendQueueTrack | null> {
|
||||||
|
try {
|
||||||
|
return await invoke<BackendQueueTrack | null>('next_track');
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to get next track:', err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get previous track from queue
|
||||||
|
*/
|
||||||
|
export async function previousTrack(): Promise<BackendQueueTrack | null> {
|
||||||
|
try {
|
||||||
|
return await invoke<BackendQueueTrack | null>('previous_track');
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to get previous track:', err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============ Local Track Management ============
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set local track IDs (when playing from local library)
|
||||||
|
*/
|
||||||
|
export function setLocalTrackIds(trackIds: number[]): void {
|
||||||
|
localTrackIds = new Set(trackIds);
|
||||||
|
console.log(`Set ${trackIds.length} local track IDs in queue`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear local track IDs
|
||||||
|
*/
|
||||||
|
export function clearLocalTrackIds(): void {
|
||||||
|
localTrackIds = new Set();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the backend queue state (for advanced queue operations)
|
||||||
|
*/
|
||||||
|
export async function getBackendQueueState(): Promise<BackendQueueState | null> {
|
||||||
|
try {
|
||||||
|
return await invoke<BackendQueueState>('get_queue_state');
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to get backend queue state:', err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============ Cleanup ============
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reset queue state
|
||||||
|
*/
|
||||||
|
export function reset(): void {
|
||||||
|
queue = [];
|
||||||
|
queueTotalTracks = 0;
|
||||||
|
isShuffle = false;
|
||||||
|
repeatMode = 'off';
|
||||||
|
localTrackIds = new Set();
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
+78
-125
@@ -85,6 +85,30 @@
|
|||||||
type PlayerState
|
type PlayerState
|
||||||
} from '$lib/stores/playerStore';
|
} from '$lib/stores/playerStore';
|
||||||
|
|
||||||
|
// Queue state management
|
||||||
|
import {
|
||||||
|
subscribe as subscribeQueue,
|
||||||
|
syncQueueState,
|
||||||
|
toggleShuffle as queueToggleShuffle,
|
||||||
|
toggleRepeat as queueToggleRepeat,
|
||||||
|
addToQueueNext,
|
||||||
|
addToQueue,
|
||||||
|
addTracksToQueue,
|
||||||
|
setQueue,
|
||||||
|
clearQueue,
|
||||||
|
playQueueIndex,
|
||||||
|
nextTrack,
|
||||||
|
previousTrack,
|
||||||
|
setLocalTrackIds,
|
||||||
|
clearLocalTrackIds,
|
||||||
|
isLocalTrack,
|
||||||
|
getBackendQueueState,
|
||||||
|
getQueueState,
|
||||||
|
type QueueTrack,
|
||||||
|
type BackendQueueTrack,
|
||||||
|
type RepeatMode
|
||||||
|
} from '$lib/stores/queueStore';
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
import Sidebar from '$lib/components/Sidebar.svelte';
|
import Sidebar from '$lib/components/Sidebar.svelte';
|
||||||
import NowPlayingBar from '$lib/components/NowPlayingBar.svelte';
|
import NowPlayingBar from '$lib/components/NowPlayingBar.svelte';
|
||||||
@@ -210,24 +234,6 @@
|
|||||||
tracks: Track[];
|
tracks: Track[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface QueueTrack {
|
|
||||||
id: string;
|
|
||||||
artwork: string;
|
|
||||||
title: string;
|
|
||||||
artist: string;
|
|
||||||
duration: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Backend queue track format
|
|
||||||
interface BackendQueueTrack {
|
|
||||||
id: number;
|
|
||||||
title: string;
|
|
||||||
artist: string;
|
|
||||||
album: string;
|
|
||||||
duration_secs: number;
|
|
||||||
artwork_url: string | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface SongLinkResponse {
|
interface SongLinkResponse {
|
||||||
pageUrl: string;
|
pageUrl: string;
|
||||||
title?: string;
|
title?: string;
|
||||||
@@ -238,16 +244,6 @@
|
|||||||
contentType: string;
|
contentType: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface BackendQueueState {
|
|
||||||
current_track: BackendQueueTrack | null;
|
|
||||||
current_index: number | null;
|
|
||||||
upcoming: BackendQueueTrack[];
|
|
||||||
history: BackendQueueTrack[];
|
|
||||||
shuffle: boolean;
|
|
||||||
repeat: 'Off' | 'All' | 'One';
|
|
||||||
total_tracks: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Auth State (from authStore subscription)
|
// Auth State (from authStore subscription)
|
||||||
let isLoggedIn = $state(false);
|
let isLoggedIn = $state(false);
|
||||||
let userInfo = $state<UserInfo | null>(null);
|
let userInfo = $state<UserInfo | null>(null);
|
||||||
@@ -282,17 +278,12 @@
|
|||||||
let volume = $state(75);
|
let volume = $state(75);
|
||||||
let isFavorite = $state(false);
|
let isFavorite = $state(false);
|
||||||
|
|
||||||
// Queue/Shuffle State (will move to queueStore)
|
// Queue/Shuffle State (from queueStore subscription)
|
||||||
let isShuffle = $state(false);
|
let isShuffle = $state(false);
|
||||||
let repeatMode = $state<'off' | 'all' | 'one'>('off');
|
let repeatMode = $state<RepeatMode>('off');
|
||||||
|
|
||||||
// Queue State (synced from backend)
|
|
||||||
let queue = $state<QueueTrack[]>([]);
|
let queue = $state<QueueTrack[]>([]);
|
||||||
let queueTotalTracks = $state(0);
|
let queueTotalTracks = $state(0);
|
||||||
|
|
||||||
// Local library track IDs in current queue (for distinguishing from Qobuz tracks)
|
|
||||||
let localTrackIds = $state<Set<number>>(new Set());
|
|
||||||
|
|
||||||
// Toast State (from store subscription)
|
// Toast State (from store subscription)
|
||||||
let toast = $state<ToastData | null>(null);
|
let toast = $state<ToastData | null>(null);
|
||||||
|
|
||||||
@@ -476,29 +467,19 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function queueTrackNext(queueTrack: BackendQueueTrack, isLocal = false) {
|
async function queueTrackNext(queueTrack: BackendQueueTrack, isLocal = false) {
|
||||||
try {
|
const success = await addToQueueNext(queueTrack, isLocal);
|
||||||
await invoke('add_to_queue_next', { track: queueTrack });
|
if (success) {
|
||||||
if (isLocal) {
|
|
||||||
localTrackIds = new Set([...localTrackIds, queueTrack.id]);
|
|
||||||
}
|
|
||||||
await syncQueueState();
|
|
||||||
showToast('Queued to play next', 'success');
|
showToast('Queued to play next', 'success');
|
||||||
} catch (err) {
|
} else {
|
||||||
console.error('Failed to queue track next:', err);
|
|
||||||
showToast('Failed to queue track', 'error');
|
showToast('Failed to queue track', 'error');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function queueTrackLater(queueTrack: BackendQueueTrack, isLocal = false) {
|
async function queueTrackLater(queueTrack: BackendQueueTrack, isLocal = false) {
|
||||||
try {
|
const success = await addToQueue(queueTrack, isLocal);
|
||||||
await invoke('add_to_queue', { track: queueTrack });
|
if (success) {
|
||||||
if (isLocal) {
|
|
||||||
localTrackIds = new Set([...localTrackIds, queueTrack.id]);
|
|
||||||
}
|
|
||||||
await syncQueueState();
|
|
||||||
showToast('Added to queue', 'success');
|
showToast('Added to queue', 'success');
|
||||||
} catch (err) {
|
} else {
|
||||||
console.error('Failed to queue track:', err);
|
|
||||||
showToast('Failed to add to queue', 'error');
|
showToast('Failed to add to queue', 'error');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -630,19 +611,12 @@
|
|||||||
artwork_url: artwork || null
|
artwork_url: artwork || null
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// Set the queue starting at the clicked track
|
// Set the queue starting at the clicked track (clearLocal=true for Qobuz tracks)
|
||||||
try {
|
const success = await setQueue(queueTracks, trackIndex >= 0 ? trackIndex : 0, true);
|
||||||
await invoke('set_queue', {
|
if (success) {
|
||||||
tracks: queueTracks,
|
|
||||||
startIndex: trackIndex >= 0 ? trackIndex : 0
|
|
||||||
});
|
|
||||||
// Clear local track IDs when playing Qobuz tracks
|
|
||||||
localTrackIds = new Set();
|
|
||||||
console.log(`Queue set with ${queueTracks.length} tracks, starting at index ${trackIndex}`);
|
console.log(`Queue set with ${queueTracks.length} tracks, starting at index ${trackIndex}`);
|
||||||
// Sync queue state to update UI
|
} else {
|
||||||
await syncQueueState();
|
console.error('Failed to set queue');
|
||||||
} catch (err) {
|
|
||||||
console.error('Failed to set queue:', err);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -697,25 +671,17 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function toggleShuffle() {
|
async function toggleShuffle() {
|
||||||
isShuffle = !isShuffle;
|
const result = await queueToggleShuffle();
|
||||||
try {
|
if (result.success) {
|
||||||
await invoke('set_shuffle', { enabled: isShuffle });
|
showToast(result.enabled ? 'Shuffle enabled' : 'Shuffle disabled', 'info');
|
||||||
showToast(isShuffle ? 'Shuffle enabled' : 'Shuffle disabled', 'info');
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Failed to set shuffle:', err);
|
|
||||||
isShuffle = !isShuffle; // Revert
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function toggleRepeat() {
|
async function toggleRepeat() {
|
||||||
const nextMode = repeatMode === 'off' ? 'all' : repeatMode === 'all' ? 'one' : 'off';
|
const result = await queueToggleRepeat();
|
||||||
try {
|
if (result.success) {
|
||||||
await invoke('set_repeat', { mode: nextMode });
|
const messages: Record<RepeatMode, string> = { off: 'Repeat off', all: 'Repeat all', one: 'Repeat one' };
|
||||||
repeatMode = nextMode;
|
showToast(messages[result.mode], 'info');
|
||||||
const messages = { off: 'Repeat off', all: 'Repeat all', one: 'Repeat one' };
|
|
||||||
showToast(messages[repeatMode], 'info');
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Failed to set repeat:', err);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -757,7 +723,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip track handlers - wired to backend queue
|
// Skip track handlers - wired to backend queue via queueStore
|
||||||
async function handleSkipBack() {
|
async function handleSkipBack() {
|
||||||
const playerState = getPlayerState();
|
const playerState = getPlayerState();
|
||||||
if (!playerState.currentTrack || playerState.isSkipping) return;
|
if (!playerState.currentTrack || playerState.isSkipping) return;
|
||||||
@@ -769,7 +735,7 @@
|
|||||||
|
|
||||||
setIsSkipping(true);
|
setIsSkipping(true);
|
||||||
try {
|
try {
|
||||||
const prevTrack = await invoke<BackendQueueTrack | null>('previous_track');
|
const prevTrack = await previousTrack();
|
||||||
if (prevTrack) {
|
if (prevTrack) {
|
||||||
await playQueueTrack(prevTrack);
|
await playQueueTrack(prevTrack);
|
||||||
} else {
|
} else {
|
||||||
@@ -790,7 +756,7 @@
|
|||||||
|
|
||||||
setIsSkipping(true);
|
setIsSkipping(true);
|
||||||
try {
|
try {
|
||||||
const nextTrackResult = await invoke<BackendQueueTrack | null>('next_track');
|
const nextTrackResult = await nextTrack();
|
||||||
if (nextTrackResult) {
|
if (nextTrackResult) {
|
||||||
await playQueueTrack(nextTrackResult);
|
await playQueueTrack(nextTrackResult);
|
||||||
} else {
|
} else {
|
||||||
@@ -809,7 +775,7 @@
|
|||||||
|
|
||||||
// Helper to play a track from the queue
|
// Helper to play a track from the queue
|
||||||
async function playQueueTrack(track: BackendQueueTrack) {
|
async function playQueueTrack(track: BackendQueueTrack) {
|
||||||
const isLocalTrack = localTrackIds.has(track.id);
|
const isLocal = isLocalTrack(track.id);
|
||||||
|
|
||||||
// Reset queue ended flag when playing a new track
|
// Reset queue ended flag when playing a new track
|
||||||
setQueueEnded(false);
|
setQueueEnded(false);
|
||||||
@@ -821,14 +787,14 @@
|
|||||||
album: track.album,
|
album: track.album,
|
||||||
artwork: track.artwork_url || '',
|
artwork: track.artwork_url || '',
|
||||||
duration: track.duration_secs,
|
duration: track.duration_secs,
|
||||||
quality: isLocalTrack ? 'Local' : 'Hi-Res',
|
quality: isLocal ? 'Local' : 'Hi-Res',
|
||||||
isLocal: isLocalTrack
|
isLocal
|
||||||
};
|
};
|
||||||
setCurrentTrack(newTrack);
|
setCurrentTrack(newTrack);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Use appropriate playback command based on track source
|
// Use appropriate playback command based on track source
|
||||||
if (isLocalTrack) {
|
if (isLocal) {
|
||||||
await invoke('library_play_track', { trackId: track.id });
|
await invoke('library_play_track', { trackId: track.id });
|
||||||
} else {
|
} else {
|
||||||
await invoke('play_track', { trackId: track.id });
|
await invoke('play_track', { trackId: track.id });
|
||||||
@@ -851,7 +817,7 @@
|
|||||||
updateLastfmNowPlaying(track.title, track.artist, track.album, track.duration_secs, track.id);
|
updateLastfmNowPlaying(track.title, track.artist, track.album, track.duration_secs, track.id);
|
||||||
|
|
||||||
// Check if track is favorite (for Qobuz tracks only)
|
// Check if track is favorite (for Qobuz tracks only)
|
||||||
if (!isLocalTrack) {
|
if (!isLocal) {
|
||||||
setIsFavorite(await checkTrackFavorite(track.id));
|
setIsFavorite(await checkTrackFavorite(track.id));
|
||||||
} else {
|
} else {
|
||||||
setIsFavorite(false);
|
setIsFavorite(false);
|
||||||
@@ -866,33 +832,16 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sync queue state from backend
|
|
||||||
async function syncQueueState() {
|
|
||||||
try {
|
|
||||||
const queueState = await invoke<BackendQueueState>('get_queue_state');
|
|
||||||
|
|
||||||
// Convert backend queue tracks to frontend format
|
|
||||||
queue = queueState.upcoming.map(t => ({
|
|
||||||
id: String(t.id),
|
|
||||||
artwork: t.artwork_url || '',
|
|
||||||
title: t.title,
|
|
||||||
artist: t.artist,
|
|
||||||
duration: formatDuration(t.duration_secs)
|
|
||||||
}));
|
|
||||||
|
|
||||||
queueTotalTracks = queueState.total_tracks;
|
|
||||||
isShuffle = queueState.shuffle;
|
|
||||||
repeatMode = queueState.repeat.toLowerCase() as 'off' | 'all' | 'one';
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Failed to sync queue state:', err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Play a specific track from the queue panel
|
// Play a specific track from the queue panel
|
||||||
async function handleQueueTrackPlay(trackId: string) {
|
async function handleQueueTrackPlay(trackId: string) {
|
||||||
try {
|
try {
|
||||||
// Find the index in the queue
|
// Find the index in the queue
|
||||||
const queueState = await invoke<BackendQueueState>('get_queue_state');
|
const queueState = await getBackendQueueState();
|
||||||
|
if (!queueState) {
|
||||||
|
showToast('Failed to play track', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const allTracks = [queueState.current_track, ...queueState.upcoming].filter(Boolean) as BackendQueueTrack[];
|
const allTracks = [queueState.current_track, ...queueState.upcoming].filter(Boolean) as BackendQueueTrack[];
|
||||||
const trackIndex = allTracks.findIndex(t => String(t.id) === trackId);
|
const trackIndex = allTracks.findIndex(t => String(t.id) === trackId);
|
||||||
|
|
||||||
@@ -908,7 +857,7 @@
|
|||||||
? queueState.current_index + trackIndex
|
? queueState.current_index + trackIndex
|
||||||
: trackIndex;
|
: trackIndex;
|
||||||
|
|
||||||
const track = await invoke<BackendQueueTrack | null>('play_queue_index', { index: actualIndex });
|
const track = await playQueueIndex(actualIndex);
|
||||||
if (track) {
|
if (track) {
|
||||||
await playQueueTrack(track);
|
await playQueueTrack(track);
|
||||||
}
|
}
|
||||||
@@ -921,13 +870,10 @@
|
|||||||
|
|
||||||
// Clear the queue
|
// Clear the queue
|
||||||
async function handleClearQueue() {
|
async function handleClearQueue() {
|
||||||
try {
|
const success = await clearQueue();
|
||||||
await invoke('clear_queue');
|
if (success) {
|
||||||
queue = [];
|
|
||||||
queueTotalTracks = 0;
|
|
||||||
showToast('Queue cleared', 'info');
|
showToast('Queue cleared', 'info');
|
||||||
} catch (err) {
|
} else {
|
||||||
console.error('Failed to clear queue:', err);
|
|
||||||
showToast('Failed to clear queue', 'error');
|
showToast('Failed to clear queue', 'error');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -970,12 +916,10 @@
|
|||||||
artwork_url: artwork || null
|
artwork_url: artwork || null
|
||||||
}));
|
}));
|
||||||
|
|
||||||
try {
|
const success = await addTracksToQueue(queueTracks);
|
||||||
await invoke('add_tracks_to_queue', { tracks: queueTracks });
|
if (success) {
|
||||||
await syncQueueState();
|
|
||||||
showToast(`Added ${queueTracks.length} tracks to queue`, 'success');
|
showToast(`Added ${queueTracks.length} tracks to queue`, 'success');
|
||||||
} catch (err) {
|
} else {
|
||||||
console.error('Failed to add to queue:', err);
|
|
||||||
showToast('Failed to add to queue', 'error');
|
showToast('Failed to add to queue', 'error');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1244,9 +1188,8 @@
|
|||||||
|
|
||||||
// Handle setting queue from local library (tracks need different playback command)
|
// Handle setting queue from local library (tracks need different playback command)
|
||||||
function handleSetLocalQueue(trackIds: number[]) {
|
function handleSetLocalQueue(trackIds: number[]) {
|
||||||
// Clear Qobuz tracks and set local track IDs
|
// Set local track IDs via queueStore
|
||||||
localTrackIds = new Set(trackIds);
|
setLocalTrackIds(trackIds);
|
||||||
console.log(`Set ${trackIds.length} local track IDs in queue`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// System Notification for track changes
|
// System Notification for track changes
|
||||||
@@ -1492,9 +1435,18 @@
|
|||||||
isFavorite = playerState.isFavorite;
|
isFavorite = playerState.isFavorite;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Subscribe to queue state changes
|
||||||
|
const unsubscribeQueue = subscribeQueue(() => {
|
||||||
|
const queueState = getQueueState();
|
||||||
|
queue = queueState.queue;
|
||||||
|
queueTotalTracks = queueState.queueTotalTracks;
|
||||||
|
isShuffle = queueState.isShuffle;
|
||||||
|
repeatMode = queueState.repeatMode;
|
||||||
|
});
|
||||||
|
|
||||||
// Set up track ended callback for auto-advance
|
// Set up track ended callback for auto-advance
|
||||||
setOnTrackEnded(async () => {
|
setOnTrackEnded(async () => {
|
||||||
const nextTrackResult = await invoke<BackendQueueTrack | null>('next_track');
|
const nextTrackResult = await nextTrack();
|
||||||
if (nextTrackResult) {
|
if (nextTrackResult) {
|
||||||
await playQueueTrack(nextTrackResult);
|
await playQueueTrack(nextTrackResult);
|
||||||
} else {
|
} else {
|
||||||
@@ -1537,6 +1489,7 @@
|
|||||||
unsubscribeAuth();
|
unsubscribeAuth();
|
||||||
unsubscribeNav();
|
unsubscribeNav();
|
||||||
unsubscribePlayer();
|
unsubscribePlayer();
|
||||||
|
unsubscribeQueue();
|
||||||
stopPolling();
|
stopPolling();
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user