fix(download): recover queue after worker timeout (#524)

This commit is contained in:
zarzet
2026-08-11 17:23:29 +07:00
parent 1d363d5166
commit 95f2879110
6 changed files with 125 additions and 4 deletions
@@ -75,6 +75,17 @@ bool canStartForegroundDownloadForLifecycle(AppLifecycleState? lifecycleState) {
return lifecycleState == AppLifecycleState.resumed;
}
String nativeWorkerStatusAfterSnapshotStop({
required bool workerRunning,
required String status,
}) {
if (workerRunning) return status;
return switch (status) {
'preparing' || 'downloading' || 'finalizing' => 'queued',
_ => status,
};
}
/// Keeps a download in its finalizing state until its durable Library record
/// has been written. If persistence fails, completion is deliberately not
/// published so the queue can surface the error instead of losing the file
@@ -863,6 +863,7 @@ extension _DownloadQueueNativeWorker on DownloadQueueNotifier {
if (itemSnapshots.isEmpty) {
return;
}
final workerRunning = snapshot['is_running'] == true;
for (final itemSnapshot in itemSnapshots) {
final itemId = itemSnapshot['item_id']?.toString() ?? '';
@@ -872,7 +873,10 @@ extension _DownloadQueueNativeWorker on DownloadQueueNotifier {
final context = contexts[itemId];
if (context == null) continue;
final status = itemSnapshot['status']?.toString() ?? 'queued';
final status = nativeWorkerStatusAfterSnapshotStop(
workerRunning: workerRunning,
status: itemSnapshot['status']?.toString() ?? 'queued',
);
final progress = ((itemSnapshot['progress'] as num?)?.toDouble() ?? 0.0)
.clamp(0.0, 1.0)
.toDouble();