T-013: Download-Manager — Queue mit Bytes-Fortschritt-Events, Abbruch, Wiederaufnahme per Manifest nach Neustart, UI: Laden/Abbrechen auf Cards

This commit is contained in:
2026-11-06 19:49:16 +01:00
parent eace05f180
commit eb97f183e7
8 changed files with 673 additions and 34 deletions

View File

@ -1,5 +1,16 @@
<script lang="ts">
import { authStatus, startLogin, startDecrypt, getLibrary, type Book } from './lib/audible'
import {
authStatus,
startLogin,
startDecrypt,
getLibrary,
downloadList,
downloadEnqueue,
downloadCancel,
onDownloadUpdate,
type Book,
type DlItem,
} from './lib/audible'
type View = 'loading' | 'login' | 'library'
type SortKey = 'added' | 'title' | 'author' | 'runtime' | 'percent'
@ -15,6 +26,8 @@
let query = $state('')
let sortBy = $state<SortKey>('added')
let downloads = $state<Record<string, DlItem>>({})
let localOverride = $state<Record<string, boolean>>({})
const filtered = $derived.by(() => {
const q = query.trim().toLowerCase()
@ -43,6 +56,44 @@
return m ? `${h} h ${m} min` : `${h} h`
}
function mb(bytes: number): string {
return bytes >= 1e6 ? `${(bytes / 1e6).toFixed(1)} MB` : `${Math.round(bytes / 1e3)} KB`
}
function isLocal(b: Book): boolean {
return Boolean(b.local || localOverride[b.asin])
}
function activeDl(b: Book): 'running' | 'queued' | undefined {
const d = downloads[b.asin]
if (!d) return undefined
if (d.status === 'running') return 'running'
if (d.status === 'queued') return 'queued'
return undefined
}
async function laden(b: Book): Promise<void> {
error = ''
info = ''
try {
const it = await downloadEnqueue(b.asin)
downloads = { ...downloads, [b.asin]: it }
info = `„${b.title}“ in der Download-Queue.`
} catch (e) {
error = String(e)
}
}
async function abbrechen(b: Book): Promise<void> {
error = ''
try {
const it = await downloadCancel(b.asin)
downloads = { ...downloads, [b.asin]: it }
} catch (e) {
error = String(e)
}
}
function hideBrokenCover(e: Event): void {
;(e.currentTarget as HTMLElement).style.display = 'none'
}
@ -64,7 +115,7 @@
const msg = String(e)
if (msg.toLowerCase().includes('encrypted')) {
// Auth-Datei verschlüsselt — Entschlüsselung im Terminal anbieten
error = 'Auth-Datei ist verschlüsselt. Bitte einmal entschriften:'
error = 'Auth-Datei ist verschlüsselt. Bitte einmal entsprichern:'
view = 'login'
} else {
error = msg
@ -116,10 +167,25 @@
}
}
// Beim Start prüfen: angemeldet → Bibliothek; sonst Login-Screen.
// Beim Start: auth prüfen, Download-Events abonnieren, Queue-Stand laden.
$effect(() => {
checkAuth()
return clearPoll
const unlisten = onDownloadUpdate((it) => {
downloads = { ...downloads, [it.asin]: it }
if (it.status === 'done') {
localOverride = { ...localOverride, [it.asin]: true }
info = `„Download fertig: ${it.asin}“`
}
})
downloadList().then((list) => {
const m: Record<string, DlItem> = {}
for (const it of list) m[it.asin] = it
downloads = m
})
return () => {
clearPoll()
unlisten.then((f) => f())
}
})
</script>
@ -143,7 +209,7 @@
<button class="ghost" onclick={() => checkAuth()} disabled={checking}>
{checking ? 'Prüfe…' : 'Jetzt prüfen'}
</button>
{#if error === 'Auth-Datei ist verschlüsselt. Bitte einmal entschriften:'}
{#if error === 'Auth-Datei ist verschlüsselt. Bitte einmal entsprichern:'}
<button class="ghost" onclick={() => startDecrypt().catch((e) => (error = String(e)))}>
Entschlüsseln im Terminal…
</button>
@ -186,6 +252,8 @@
{:else}
<div class="grid">
{#each filtered as b (b.asin)}
{@const d = downloads[b.asin]}
{@const dlg = activeDl(b)}
<article class="card">
<div class="cover">
<img
@ -194,18 +262,38 @@
loading="lazy"
onerror={hideBrokenCover}
/>
{#if dlg === 'running'}
<div class="overlay"><span class="muted small">{d ? mb(d.bytes) : ''}</span></div>
{/if}
</div>
<div class="meta">
<div class="t">{b.title}</div>
<div class="muted small">{b.authors}</div>
<div class="chips">
{#if b.local}<span class="chip ok" title="book.m4b liegt lokal vor">lokal</span>{/if}
{#if isLocal(b)}<span class="chip ok" title="book.m4b liegt lokal vor">lokal</span>{/if}
{#if b.is_finished}<span class="chip done">fertig</span>{/if}
{#if b.runtime_length_min > 0}<span class="chip">{fmtRuntime(b.runtime_length_min)}</span>{/if}
{#if d && d.status === 'failed'}<span class="chip err" title={d.msg}>Fehlgeschlagen</span>{/if}
</div>
{#if b.percent_complete > 0}
{#if b.percent_complete > 0 && !dlg}
<div class="progress"><div class="fill" style={`width:${b.percent_complete}%`}></div></div>
{/if}
{#if dlg === 'running'}
<div class="progress indeterminate"><div class="fill"></div></div>
{/if}
<div class="actions">
{#if dlg === 'running' || dlg === 'queued'}
<button class="ghost" onclick={() => abbrechen(b)}>
{dlg === 'queued' ? 'In der Queue — abbrechen?' : 'Abbrechen'}
</button>
{#if dlg === 'queued'}<span class="muted small">wartet…</span>{/if}
{:else if d && d.status === 'failed'}
<button class="ghost" onclick={() => laden(b)}>Nochmal laden</button>
<span class="muted small" title={d.msg}>{d.msg.slice(0, 60)}</span>
{:else if !isLocal(b)}
<button class="primary" onclick={() => laden(b)}>Laden</button>
{/if}
</div>
</div>
</article>
{/each}

View File

@ -102,6 +102,51 @@ button.primary {
font-weight: 600;
}
button.primary,
.actions button {
padding: 0.32rem 0.65rem;
font-size: 0.88rem;
}
.actions {
display: flex;
align-items: center;
gap: 0.45rem;
margin-top: 0.15rem;
min-height: 2rem;
flex-wrap: wrap;
}
.overlay {
position: absolute;
inset: 0;
background: rgba(10, 12, 15, 0.55);
display: flex;
align-items: flex-end;
justify-content: flex-end;
padding: 0.4rem;
color: #fff;
}
.progress.indeterminate .fill {
width: 32%;
animation: dlslide 1.3s linear infinite;
}
@keyframes dlslide {
from {
transform: translateX(-120%);
}
to {
transform: translateX(420%);
}
}
.chip.err {
color: var(--error);
border-color: var(--error);
}
button.ghost {
background: transparent;
}

View File

@ -1,4 +1,5 @@
import { invoke } from '@tauri-apps/api/core'
import { listen } from '@tauri-apps/api/event'
export interface AuthStatus {
logged_in: boolean
@ -23,3 +24,16 @@ export const authStatus = () => invoke<AuthStatus>('auth_status')
export const startLogin = () => invoke<boolean>('start_login')
export const startDecrypt = () => invoke<boolean>('start_decrypt')
export const getLibrary = () => invoke<Book[]>('get_library')
export interface DlItem {
asin: string
status: 'queued' | 'running' | 'done' | 'failed' | 'cancelled'
bytes: number
msg: string
}
export const downloadList = () => invoke<DlItem[]>('download_list')
export const downloadEnqueue = (asin: string) => invoke<DlItem>('download_enqueue', { asin })
export const downloadCancel = (asin: string) => invoke<DlItem>('download_cancel', { asin })
export const onDownloadUpdate = (cb: (it: DlItem) => void) =>
listen<DlItem>('download-update', (ev) => cb(ev.payload))