fix offline mode and layout bugs
🏗️ Build and Test JellyTau / Run Tests (push) Successful in 4m32s
Traceability Validation / Check Requirement Traces (push) Successful in 22s
Build & Release / Run Tests (push) Successful in 5m21s
🏗️ Build and Test JellyTau / Android Compile Check (push) Successful in 4m25s
Build & Release / Build Linux (push) Successful in 17m31s
Build & Release / Build Android (push) Successful in 22m5s
Build & Release / Create Release (push) Successful in 16s
🏗️ Build and Test JellyTau / Run Tests (push) Successful in 4m32s
Traceability Validation / Check Requirement Traces (push) Successful in 22s
Build & Release / Run Tests (push) Successful in 5m21s
🏗️ Build and Test JellyTau / Android Compile Check (push) Successful in 4m25s
Build & Release / Build Linux (push) Successful in 17m31s
Build & Release / Build Android (push) Successful in 22m5s
Build & Release / Create Release (push) Successful in 16s
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
// Offline catalog service - "browse & queue" for offline mode.
|
||||
//
|
||||
// Two responsibilities:
|
||||
// 1. Full-catalog pre-sync: while online, walk every library and persist all
|
||||
// items to the offline cache so the whole catalog is browsable (greyed out)
|
||||
// offline. Backed by the `syncFullCatalog` Rust command.
|
||||
// 2. Resume-on-reconnect: when the server becomes reachable again, resolve the
|
||||
// stream URLs of downloads that were queued while offline and let the pump
|
||||
// start them. Backed by `resumeQueuedDownloads`.
|
||||
//
|
||||
// It also owns the `showServerCatalog` UI flag (the offline banner toggle that
|
||||
// reveals greyed-out, non-downloaded server media).
|
||||
//
|
||||
// TRACES: UR-002
|
||||
|
||||
import { writable, type Writable } from "svelte/store";
|
||||
import { commands } from "$lib/api/bindings";
|
||||
import { auth } from "$lib/stores/auth";
|
||||
|
||||
/**
|
||||
* When true (and offline), library grids reveal greyed-out versions of media
|
||||
* that exists on the server but isn't downloaded, each with a button to queue
|
||||
* a download for the next reconnect. Toggled from the offline banner.
|
||||
*/
|
||||
export const showServerCatalog: Writable<boolean> = writable(false);
|
||||
|
||||
/** Last time a full catalog sync completed, for a UI hint. */
|
||||
export const lastCatalogSync: Writable<string | null> = writable(null);
|
||||
|
||||
// Guard against overlapping syncs (they can be slow on large libraries).
|
||||
let syncInProgress = false;
|
||||
|
||||
function currentHandle(): string | null {
|
||||
try {
|
||||
return auth.getRepository().getHandle();
|
||||
} catch {
|
||||
return null; // not connected yet
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Walk every library and cache the full catalog. Best-effort and non-blocking:
|
||||
* safe to call on startup (while online) and on reconnect. No-ops if not
|
||||
* connected or a sync is already running.
|
||||
*/
|
||||
export async function syncCatalog(): Promise<void> {
|
||||
if (syncInProgress) return;
|
||||
const handle = currentHandle();
|
||||
if (!handle) return;
|
||||
|
||||
syncInProgress = true;
|
||||
try {
|
||||
const result = await commands.syncFullCatalog(handle);
|
||||
console.info(
|
||||
`[OfflineCatalog] Synced ${result.itemsCached} items (${result.librariesFailed} libraries failed)`
|
||||
);
|
||||
await refreshSyncStatus();
|
||||
} catch (err) {
|
||||
console.warn("[OfflineCatalog] Full catalog sync failed:", err);
|
||||
} finally {
|
||||
syncInProgress = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve URLs for downloads queued while offline and pump them. Call on
|
||||
* reconnect. No-ops if not connected.
|
||||
*/
|
||||
export async function resumeQueued(): Promise<void> {
|
||||
const handle = currentHandle();
|
||||
if (!handle) return;
|
||||
try {
|
||||
const result = await commands.resumeQueuedDownloads(handle);
|
||||
if (result.resolved > 0 || result.failed > 0) {
|
||||
console.info(
|
||||
`[OfflineCatalog] Resumed queued downloads: ${result.resolved} resolved, ${result.failed} failed`
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn("[OfflineCatalog] Failed to resume queued downloads:", err);
|
||||
}
|
||||
}
|
||||
|
||||
/** Refresh the last-synced timestamp from the backend. */
|
||||
export async function refreshSyncStatus(): Promise<void> {
|
||||
try {
|
||||
const status = await commands.catalogSyncStatus();
|
||||
lastCatalogSync.set(status.lastSyncedAt ?? null);
|
||||
} catch (err) {
|
||||
console.debug("[OfflineCatalog] Failed to fetch sync status:", err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the server becomes reachable again: resume queued downloads first
|
||||
* (fast, user-visible), then refresh the catalog in the background.
|
||||
*/
|
||||
export async function onReconnected(): Promise<void> {
|
||||
await resumeQueued();
|
||||
// Fire-and-forget: don't block reconnection handling on a potentially long walk.
|
||||
void syncCatalog();
|
||||
}
|
||||
Reference in New Issue
Block a user