Skip to content

Commit

Permalink
amugofjava#94 Downloads failing immediately correctly update UI
Browse files Browse the repository at this point in the history
Add a Lock to DownloadService. This ensures that download status updates are not processed while a download start/stop is requested (they are processed right after).
  • Loading branch information
Chralu committed Sep 15, 2023
1 parent d9e121a commit 8ac85c8
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions lib/services/download/mobile_download_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:collection/collection.dart' show IterableExtension;
import 'package:logging/logging.dart';
import 'package:mp3_info/mp3_info.dart';
import 'package:rxdart/rxdart.dart';
import 'package:synchronized/synchronized.dart';

/// An implementation of a [DownloadService] that handles downloading
/// of episodes on mobile.
Expand All @@ -24,17 +25,27 @@ class MobileDownloadService extends DownloadService {
final log = Logger('MobileDownloadService');
final Repository repository;
final DownloadManager downloadManager;
late final StreamSubscription _downloadProgressSubscription;

/// Lock ensures we wait for task creation and local save
/// before handling subsequent [Download update events].
final _downloadProgressLock = Lock();

MobileDownloadService({required this.repository, required this.downloadManager}) {
downloadManager.downloadProgress.pipe(downloadProgress);
downloadProgress.listen((progress) {
_updateDownloadProgress(progress);
});
_downloadProgressSubscription = downloadManager.downloadProgress.listen(
(event) async => await _downloadProgressLock.synchronized(
() {
downloadProgress.add(event);
_updateDownloadProgress(event);
},
),
);
}

@override
void dispose() {
downloadManager.dispose();
_downloadProgressSubscription.cancel();
}

@override
Expand Down Expand Up @@ -91,16 +102,22 @@ class MobileDownloadService extends DownloadService {
/// the URL before calling download and ensure it is https.
var url = await resolveUrl(episode.contentUrl!, forceHttps: true);

final taskId = await downloadManager.enqueueTask(url, downloadPath, filename);

// Update the episode with download data
episode.filepath = episodePath;
episode.filename = filename;
episode.downloadTaskId = taskId;
episode.downloadState = DownloadState.downloading;
episode.downloadPercentage = 0;

await repository.saveEpisode(episode);
await _downloadProgressLock.synchronized(() async {
final taskId = await downloadManager.enqueueTask(
url,
downloadPath,
filename!,
);

// Update the episode with download data
episode.filepath = episodePath;
episode.filename = filename;
episode.downloadTaskId = taskId;
episode.downloadState = DownloadState.downloading;
episode.downloadPercentage = 0;

await repository.saveEpisode(episode);
});

return Future.value(true);
}
Expand Down

0 comments on commit 8ac85c8

Please sign in to comment.