feat: add download UI for offline listening
- DownloadButton component with status indicators - TrackRow now shows download button on hover - downloadState store for managing download states - Tauri event listeners for download progress - Integration with AlbumDetailView
This commit is contained in:
1 parent
45fb498b8b
commit
7149cf5c4b
5 files changed
+443
-2
No files matched your search
@@ -0,0 +1,127 @@
|
||||
<script lang="ts">
|
||||
import { Download, Check, Loader, AlertCircle } from 'lucide-svelte';
|
||||
|
||||
type DownloadStatus = 'none' | 'queued' | 'downloading' | 'ready' | 'failed';
|
||||
|
||||
interface Props {
|
||||
status?: DownloadStatus;
|
||||
progress?: number;
|
||||
size?: number;
|
||||
onDownload?: () => void;
|
||||
onRemove?: () => void;
|
||||
}
|
||||
|
||||
let {
|
||||
status = 'none',
|
||||
progress = 0,
|
||||
size = 16,
|
||||
onDownload,
|
||||
onRemove
|
||||
}: Props = $props();
|
||||
|
||||
function handleClick(e: MouseEvent) {
|
||||
e.stopPropagation();
|
||||
|
||||
if (status === 'none' || status === 'failed') {
|
||||
onDownload?.();
|
||||
} else if (status === 'ready') {
|
||||
onRemove?.();
|
||||
}
|
||||
}
|
||||
|
||||
const title = $derived(() => {
|
||||
switch (status) {
|
||||
case 'none': return 'Download for offline';
|
||||
case 'queued': return 'Queued for download';
|
||||
case 'downloading': return `Downloading ${progress}%`;
|
||||
case 'ready': return 'Downloaded (click to remove)';
|
||||
case 'failed': return 'Download failed (click to retry)';
|
||||
default: return '';
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<button
|
||||
class="download-button"
|
||||
class:downloading={status === 'downloading' || status === 'queued'}
|
||||
class:ready={status === 'ready'}
|
||||
class:failed={status === 'failed'}
|
||||
onclick={handleClick}
|
||||
title={title()}
|
||||
disabled={status === 'queued'}
|
||||
>
|
||||
{#if status === 'ready'}
|
||||
<Check {size} />
|
||||
{:else if status === 'downloading' || status === 'queued'}
|
||||
<div class="progress-ring" style="--progress: {progress}">
|
||||
<Loader {size} class="spinning" />
|
||||
</div>
|
||||
{:else if status === 'failed'}
|
||||
<AlertCircle {size} />
|
||||
{:else}
|
||||
<Download {size} />
|
||||
{/if}
|
||||
</button>
|
||||
|
||||
<style>
|
||||
.download-button {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
border: none;
|
||||
background: transparent;
|
||||
color: var(--text-muted);
|
||||
cursor: pointer;
|
||||
border-radius: 4px;
|
||||
transition: all 150ms ease;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.download-button:hover:not(:disabled) {
|
||||
color: var(--text-primary);
|
||||
background: var(--bg-hover);
|
||||
}
|
||||
|
||||
.download-button:disabled {
|
||||
cursor: default;
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
.download-button.ready {
|
||||
color: var(--success, #22c55e);
|
||||
}
|
||||
|
||||
.download-button.ready:hover {
|
||||
color: var(--error, #ef4444);
|
||||
}
|
||||
|
||||
.download-button.failed {
|
||||
color: var(--error, #ef4444);
|
||||
}
|
||||
|
||||
.download-button.downloading {
|
||||
color: var(--accent-primary);
|
||||
}
|
||||
|
||||
.progress-ring {
|
||||
position: relative;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.progress-ring :global(.spinning) {
|
||||
animation: spin 1s linear infinite;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
from {
|
||||
transform: rotate(0deg);
|
||||
}
|
||||
to {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -1,6 +1,9 @@
|
||||
<script lang="ts">
|
||||
import { Play, Heart } from 'lucide-svelte';
|
||||
import TrackMenu from './TrackMenu.svelte';
|
||||
import DownloadButton from './DownloadButton.svelte';
|
||||
|
||||
type DownloadStatus = 'none' | 'queued' | 'downloading' | 'ready' | 'failed';
|
||||
|
||||
interface Props {
|
||||
number: number;
|
||||
@@ -10,7 +13,11 @@
|
||||
quality?: string;
|
||||
isPlaying?: boolean;
|
||||
isFavorite?: boolean;
|
||||
downloadStatus?: DownloadStatus;
|
||||
downloadProgress?: number;
|
||||
onPlay?: () => void;
|
||||
onDownload?: () => void;
|
||||
onRemoveDownload?: () => void;
|
||||
menuActions?: TrackMenuActions;
|
||||
}
|
||||
|
||||
@@ -34,7 +41,11 @@
|
||||
quality,
|
||||
isPlaying = false,
|
||||
isFavorite = false,
|
||||
downloadStatus = 'none',
|
||||
downloadProgress = 0,
|
||||
onPlay,
|
||||
onDownload,
|
||||
onRemoveDownload,
|
||||
menuActions
|
||||
}: Props = $props();
|
||||
|
||||
@@ -93,6 +104,17 @@
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<!-- Download Indicator -->
|
||||
<div class="download-indicator" class:has-download={downloadStatus !== 'none'}>
|
||||
<DownloadButton
|
||||
status={downloadStatus}
|
||||
progress={downloadProgress}
|
||||
size={14}
|
||||
onDownload={onDownload}
|
||||
onRemove={onRemoveDownload}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="track-actions">
|
||||
<TrackMenu
|
||||
onPlayNow={playNowAction}
|
||||
@@ -246,6 +268,23 @@
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.download-indicator {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 28px;
|
||||
opacity: 0;
|
||||
transition: opacity 150ms ease;
|
||||
}
|
||||
|
||||
.download-indicator.has-download {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.track-row:hover .download-indicator {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.track-actions {
|
||||
margin-left: auto;
|
||||
display: flex;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
import { ArrowLeft, Play, Shuffle, Heart, Plus, MoreHorizontal } from 'lucide-svelte';
|
||||
import TrackRow from '../TrackRow.svelte';
|
||||
import { getDownloadState, type DownloadStatus } from '$lib/stores/downloadState';
|
||||
|
||||
interface Track {
|
||||
id: number;
|
||||
@@ -47,6 +48,9 @@
|
||||
onShuffleAll?: () => void;
|
||||
onAddToQueue?: () => void;
|
||||
onAddTrackToPlaylist?: (trackId: number) => void;
|
||||
onTrackDownload?: (track: Track) => void;
|
||||
onTrackRemoveDownload?: (trackId: number) => void;
|
||||
getTrackDownloadStatus?: (trackId: number) => { status: DownloadStatus; progress: number };
|
||||
}
|
||||
|
||||
let {
|
||||
@@ -64,7 +68,10 @@
|
||||
onPlayAll,
|
||||
onShuffleAll,
|
||||
onAddToQueue,
|
||||
onAddTrackToPlaylist
|
||||
onAddTrackToPlaylist,
|
||||
onTrackDownload,
|
||||
onTrackRemoveDownload,
|
||||
getTrackDownloadStatus
|
||||
}: Props = $props();
|
||||
|
||||
let currentTrack = $state<number | null>(null);
|
||||
@@ -202,6 +209,7 @@
|
||||
<!-- Track Rows -->
|
||||
<div class="tracks">
|
||||
{#each album.tracks as track}
|
||||
{@const downloadInfo = getTrackDownloadStatus?.(track.id) ?? { status: 'none' as const, progress: 0 }}
|
||||
<TrackRow
|
||||
number={track.number}
|
||||
title={track.title}
|
||||
@@ -210,10 +218,14 @@
|
||||
quality={track.quality}
|
||||
isPlaying={currentTrack === track.id}
|
||||
isFavorite={favoriteTrackIds.has(track.id)}
|
||||
downloadStatus={downloadInfo.status}
|
||||
downloadProgress={downloadInfo.progress}
|
||||
onPlay={() => {
|
||||
currentTrack = track.id;
|
||||
onTrackPlay?.(track);
|
||||
}}
|
||||
onDownload={onTrackDownload ? () => onTrackDownload(track) : undefined}
|
||||
onRemoveDownload={onTrackRemoveDownload ? () => onTrackRemoveDownload(track.id) : undefined}
|
||||
menuActions={{
|
||||
onPlayNow: () => {
|
||||
currentTrack = track.id;
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
import { listen, type UnlistenFn } from '@tauri-apps/api/event';
|
||||
|
||||
export type DownloadStatus = 'none' | 'queued' | 'downloading' | 'ready' | 'failed';
|
||||
|
||||
export interface DownloadInfo {
|
||||
status: DownloadStatus;
|
||||
progress: number;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export interface CachedTrackInfo {
|
||||
trackId: number;
|
||||
title: string;
|
||||
artist: string;
|
||||
album?: string;
|
||||
albumId?: string;
|
||||
durationSecs: number;
|
||||
fileSizeBytes: number;
|
||||
quality: string;
|
||||
bitDepth?: number;
|
||||
sampleRate?: number;
|
||||
status: DownloadStatus;
|
||||
progressPercent: number;
|
||||
errorMessage?: string;
|
||||
createdAt: string;
|
||||
lastAccessedAt: string;
|
||||
}
|
||||
|
||||
export interface DownloadCacheStats {
|
||||
totalTracks: number;
|
||||
readyTracks: number;
|
||||
downloadingTracks: number;
|
||||
failedTracks: number;
|
||||
totalSizeBytes: number;
|
||||
limitBytes?: number;
|
||||
cachePath: string;
|
||||
}
|
||||
|
||||
// Track download states by track ID
|
||||
const downloadStates = new Map<number, DownloadInfo>();
|
||||
|
||||
// Listeners for state changes
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
// Event unsubscribe functions
|
||||
let unlisteners: UnlistenFn[] = [];
|
||||
|
||||
export function getDownloadState(trackId: number): DownloadInfo {
|
||||
return downloadStates.get(trackId) || { status: 'none', progress: 0 };
|
||||
}
|
||||
|
||||
export function setDownloadState(trackId: number, info: DownloadInfo): void {
|
||||
downloadStates.set(trackId, info);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
export function subscribe(listener: () => void): () => void {
|
||||
listeners.add(listener);
|
||||
return () => listeners.delete(listener);
|
||||
}
|
||||
|
||||
function notifyListeners(): void {
|
||||
for (const listener of listeners) {
|
||||
listener();
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize download states from backend
|
||||
export async function initDownloadStates(): Promise<void> {
|
||||
try {
|
||||
const tracks = await invoke<CachedTrackInfo[]>('get_downloaded_tracks');
|
||||
for (const track of tracks) {
|
||||
downloadStates.set(track.trackId, {
|
||||
status: track.status,
|
||||
progress: track.progressPercent,
|
||||
error: track.errorMessage,
|
||||
});
|
||||
}
|
||||
notifyListeners();
|
||||
} catch (err) {
|
||||
console.error('Failed to init download states:', err);
|
||||
}
|
||||
}
|
||||
|
||||
// Start listening for download events
|
||||
export async function startDownloadEventListeners(): Promise<void> {
|
||||
try {
|
||||
const unlistenStarted = await listen<{ trackId: number }>('download:started', (event) => {
|
||||
console.log('Download started:', event.payload.trackId);
|
||||
setDownloadState(event.payload.trackId, { status: 'queued', progress: 0 });
|
||||
});
|
||||
|
||||
const unlistenProgress = await listen<{
|
||||
trackId: number;
|
||||
progressPercent: number;
|
||||
bytesDownloaded: number;
|
||||
totalBytes?: number;
|
||||
status: string;
|
||||
}>('download:progress', (event) => {
|
||||
const { trackId, progressPercent } = event.payload;
|
||||
setDownloadState(trackId, { status: 'downloading', progress: progressPercent });
|
||||
});
|
||||
|
||||
const unlistenCompleted = await listen<{ trackId: number; size: number }>('download:completed', (event) => {
|
||||
console.log('Download completed:', event.payload.trackId);
|
||||
setDownloadState(event.payload.trackId, { status: 'ready', progress: 100 });
|
||||
});
|
||||
|
||||
const unlistenFailed = await listen<{ trackId: number; error: string }>('download:failed', (event) => {
|
||||
console.error('Download failed:', event.payload.trackId, event.payload.error);
|
||||
setDownloadState(event.payload.trackId, {
|
||||
status: 'failed',
|
||||
progress: 0,
|
||||
error: event.payload.error,
|
||||
});
|
||||
});
|
||||
|
||||
unlisteners = [unlistenStarted, unlistenProgress, unlistenCompleted, unlistenFailed];
|
||||
} catch (err) {
|
||||
console.error('Failed to setup download event listeners:', err);
|
||||
}
|
||||
}
|
||||
|
||||
// Stop listening for events
|
||||
export function stopDownloadEventListeners(): void {
|
||||
for (const unlisten of unlisteners) {
|
||||
unlisten();
|
||||
}
|
||||
unlisteners = [];
|
||||
}
|
||||
|
||||
// Start a download
|
||||
export async function downloadTrack(track: {
|
||||
id: number;
|
||||
title: string;
|
||||
artist: string;
|
||||
album?: string;
|
||||
albumId?: string;
|
||||
durationSecs: number;
|
||||
quality: string;
|
||||
bitDepth?: number;
|
||||
sampleRate?: number;
|
||||
}): Promise<void> {
|
||||
try {
|
||||
setDownloadState(track.id, { status: 'queued', progress: 0 });
|
||||
await invoke('download_track', {
|
||||
trackId: track.id,
|
||||
title: track.title,
|
||||
artist: track.artist,
|
||||
album: track.album,
|
||||
albumId: track.albumId,
|
||||
durationSecs: track.durationSecs,
|
||||
quality: track.quality,
|
||||
bitDepth: track.bitDepth,
|
||||
sampleRate: track.sampleRate,
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Failed to start download:', err);
|
||||
setDownloadState(track.id, { status: 'failed', progress: 0, error: String(err) });
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
// Remove a download
|
||||
export async function removeDownload(trackId: number): Promise<void> {
|
||||
try {
|
||||
await invoke('remove_downloaded_track', { trackId });
|
||||
downloadStates.delete(trackId);
|
||||
notifyListeners();
|
||||
} catch (err) {
|
||||
console.error('Failed to remove download:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
// Get cache stats
|
||||
export async function getDownloadCacheStats(): Promise<DownloadCacheStats> {
|
||||
return invoke<DownloadCacheStats>('get_download_cache_stats');
|
||||
}
|
||||
|
||||
// Clear all downloads
|
||||
export async function clearDownloadCache(): Promise<void> {
|
||||
await invoke('clear_download_cache');
|
||||
downloadStates.clear();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
// Open cache folder
|
||||
export async function openDownloadCacheFolder(): Promise<void> {
|
||||
await invoke('open_download_cache_folder');
|
||||
}
|
||||
|
||||
// Set cache limit
|
||||
export async function setDownloadCacheLimit(limitMb: number | null): Promise<void> {
|
||||
await invoke('set_download_cache_limit', { limitMb });
|
||||
}
|
||||
Reference in new issue
Block a user