fix(native-worker): harden background queue handling

This commit is contained in:
zarzet
2026-07-24 12:15:55 +07:00
parent fa432266f3
commit 0d8bc7b269
5 changed files with 484 additions and 70 deletions
+5 -6
View File
@@ -241,6 +241,8 @@ class DownloadQueueNotifier extends Notifier<DownloadQueueState> {
final Map<String, Future<bool>> _verificationFlowsByExtension = {};
final Set<String> _rateLimitRetriedItemIds = {};
String? _activeNativeWorkerRunId;
bool get _hasActiveAndroidNativeWorker =>
Platform.isAndroid && _activeNativeWorkerRunId?.isNotEmpty == true;
// Album ReplayGain accumulator: keyed by album identifier.
// Stores per-track loudness data until all album tracks are done,
@@ -1564,8 +1566,7 @@ class DownloadQueueNotifier extends Notifier<DownloadQueueState> {
}
state = state.copyWith(items: [], isPaused: false, currentDownload: null);
if (Platform.isAndroid &&
ref.read(settingsProvider).nativeDownloadWorkerEnabled) {
if (_hasActiveAndroidNativeWorker) {
PlatformBridge.cancelNativeDownloadWorker().catchError((_) {});
}
_notificationService.cancelDownloadNotification();
@@ -1579,8 +1580,7 @@ class DownloadQueueNotifier extends Notifier<DownloadQueueState> {
void pauseQueue() {
if (state.isProcessing && !state.isPaused) {
if (Platform.isAndroid &&
ref.read(settingsProvider).nativeDownloadWorkerEnabled) {
if (_hasActiveAndroidNativeWorker) {
PlatformBridge.pauseNativeDownloadWorker().catchError((_) {});
}
final activeIds = state.items
@@ -1608,8 +1608,7 @@ class DownloadQueueNotifier extends Notifier<DownloadQueueState> {
void resumeQueue() {
if (state.isPaused) {
if (Platform.isAndroid &&
ref.read(settingsProvider).nativeDownloadWorkerEnabled) {
if (_hasActiveAndroidNativeWorker) {
PlatformBridge.resumeNativeDownloadWorker().catchError((_) {});
}
state = state.copyWith(isPaused: false);
@@ -424,6 +424,7 @@ extension _DownloadQueueNativeWorker on DownloadQueueNotifier {
await Future<void>.delayed(const Duration(seconds: 1));
continue;
}
state = state.copyWith(isPaused: snapshot['is_paused'] == true);
await _rebuildPendingNativeWorkerContexts(
contexts,
pendingContextIds,
@@ -522,6 +523,7 @@ extension _DownloadQueueNativeWorker on DownloadQueueNotifier {
'run_id': runId,
'created_at': DateTime.now().toIso8601String(),
'save_download_history': settings.saveDownloadHistory,
'download_network_mode': settings.downloadNetworkMode,
},
);
@@ -604,13 +606,6 @@ extension _DownloadQueueNativeWorker on DownloadQueueNotifier {
}
}
if (_totalQueuedAtStart > 0) {
await _notificationService.showQueueComplete(
completedCount: _completedInSession,
failedCount: _failedInSession,
);
}
final hasQueuedItems = state.items.any(
(item) => item.status == DownloadStatus.queued,
);
@@ -975,13 +970,6 @@ extension _DownloadQueueNativeWorker on DownloadQueueNotifier {
);
},
);
await _notificationService.showDownloadComplete(
trackName: item.track.name,
artistName: item.track.artistName,
completedCount: _completedInSession,
totalCount: _totalQueuedAtStart,
alreadyInLibrary: result['already_exists'] == true,
);
removeItem(item.id);
return;
}
@@ -1221,14 +1209,6 @@ extension _DownloadQueueNativeWorker on DownloadQueueNotifier {
);
},
);
await _notificationService.showDownloadComplete(
trackName: item.track.name,
artistName: item.track.artistName,
completedCount: _completedInSession,
totalCount: _totalQueuedAtStart,
alreadyInLibrary: result['already_exists'] == true,
);
removeItem(item.id);
}