Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Fix download queue and notification failure resilience (#40)

Kevin GATERAd13bda92b311
diff --git a/android/app/proguard-rules.pro b/android/app/proguard-rules.pro
index 0129127f..83555133 100644
--- a/android/app/proguard-rules.pro
+++ b/android/app/proguard-rules.pro
@@ -14,8 +14,13 @@
# Keep all annotations
-keepattributes *Annotation*
+-keepattributes Signature
+
+# flutter_local_notifications + Gson generic signatures
+-keep class com.dexterous.flutterlocalnotifications.** { *; }
+-keep class * extends com.google.gson.reflect.TypeToken
# Prevent stripping of native methods
-keepclasseswithmembernames class * {
native <methods>;
-}
\ No newline at end of file
+}
diff --git a/lib/services/download_notification_service.dart b/lib/services/download_notification_service.dart
index 7268b100..ba487d8a 100644
--- a/lib/services/download_notification_service.dart
+++ b/lib/services/download_notification_service.dart
@@ -1,4 +1,3 @@
-import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
@@ -178,7 +177,11 @@ class DownloadNotificationService {
}
_foregroundActive = false;
}
- await _plugin.cancel(_progressNotifId);
+ try {
+ await _plugin.cancel(_progressNotifId);
+ } catch (e) {
+ debugPrint('[DownloadNotif] Cancel progress failed: $e');
+ }
}
/// Show a heads-up completion notification with sound.
@@ -244,6 +247,10 @@ class DownloadNotificationService {
/// Dismiss all download notifications and stop the foreground service.
Future<void> dismiss() async {
await stopForeground();
- await _plugin.cancel(_alertNotifId);
+ try {
+ await _plugin.cancel(_alertNotifId);
+ } catch (e) {
+ debugPrint('[DownloadNotif] Cancel alert failed: $e');
+ }
}
}
diff --git a/lib/services/download_service.dart b/lib/services/download_service.dart
index 876f2973..11acc918 100644
--- a/lib/services/download_service.dart
+++ b/lib/services/download_service.dart
@@ -437,21 +437,30 @@ class DownloadService extends ChangeNotifier {
);
notifyListeners();
// Ensure queue processor is running
- if (!_processingQueue) _processQueue();
+ if (!_processingQueue) unawaited(_processQueue());
return null;
}
await _executeDownload(
- api: api, itemId: itemId, title: title,
- author: author, coverUrl: coverUrl, episodeId: episodeId,
+ api: api,
+ itemId: itemId,
+ title: title,
+ author: author,
+ coverUrl: coverUrl,
+ episodeId: episodeId,
);
// Process any queued downloads
- if (_queue.isNotEmpty && !_processingQueue) _processQueue();
+ if (_queue.isNotEmpty && !_processingQueue) {
+ unawaited(_processQueue());
+ }
return null;
}
Future<void> _processQueue() async {
_processingQueue = true;
+ while (_activeDownloadId != null) {
+ await Future<void>.delayed(const Duration(milliseconds: 100));
+ }
while (_queue.isNotEmpty) {
final next = _queue.removeAt(0);
// Skip if cancelled/removed while waiting
@@ -486,7 +495,11 @@ class DownloadService extends ChangeNotifier {
// Show persistent download notification via foreground service
final notif = DownloadNotificationService();
- await notif.startForeground(title: title, author: author);
+ try {
+ await notif.startForeground(title: title, author: author);
+ } catch (e) {
+ debugPrint('[Download] startForeground non-fatal error: $e');
+ }
try {
// For episodes, itemId is a composite key like 'podcastId-episodeId'.
@@ -539,6 +552,23 @@ class DownloadService extends ChangeNotifier {
int _lastNotifPercent = -1;
DateTime _lastUIUpdate = DateTime.now();
+ Future<void> _showProgressSafe(
+ DownloadNotificationService notif,
+ String title,
+ String? author,
+ double progress,
+ ) async {
+ try {
+ await notif.showProgress(
+ title: title,
+ author: author,
+ progress: progress,
+ );
+ } catch (e) {
+ debugPrint('[Download] showProgress non-fatal error: $e');
+ }
+ }
+
void _updateProgress() {
final overall = trackProgress.reduce((a, b) => a + b) / audioTracks.length;
final now = DateTime.now();
@@ -559,7 +589,7 @@ class DownloadService extends ChangeNotifier {
final pct = (overall * 50).round();
if (pct != _lastNotifPercent) {
_lastNotifPercent = pct;
- notif.showProgress(title: title, author: author, progress: overall);
+ unawaited(_showProgressSafe(notif, title, author, overall));
}
}
@@ -681,7 +711,11 @@ class DownloadService extends ChangeNotifier {
coverUrl: coverUrl,
);
// Show error notification
- await notif.showError(title: title, message: 'Download failed: $title');
+ try {
+ await notif.showError(title: title, message: 'Download failed: $title');
+ } catch (notifErr) {
+ debugPrint('[Download] showError non-fatal error: $notifErr');
+ }
}
}
@@ -689,7 +723,6 @@ class DownloadService extends ChangeNotifier {
_httpClient = null;
notifyListeners();
- return null;
}
Future<void> deleteDownload(String itemId) async {
@@ -720,7 +753,11 @@ class DownloadService extends ChangeNotifier {
_httpClient?.close();
_httpClient = null;
_activeDownloadId = null;
- DownloadNotificationService().dismiss();
+ unawaited(
+ DownloadNotificationService().dismiss().catchError((Object e) {
+ debugPrint('[Download] dismiss non-fatal error: $e');
+ }),
+ );
}
// Remove from queue if it was waiting
_queue.removeWhere((q) => q.itemId == itemId);