From 75036f2a032203e39e1dfdc17dcb7ffd8907d969 Mon Sep 17 00:00:00 2001 From: Chralu Date: Sun, 22 Dec 2024 21:47:59 +0100 Subject: [PATCH] fix: :bug: Errors on download initialization are correctly dispatched to persistence and UI. --- .tool-versions | 2 +- lib/bloc/podcast/podcast_bloc.dart | 14 +------ lib/services/download/download_service.dart | 2 +- .../download/mobile_download_service.dart | 38 ++++++++++++++++--- pubspec.lock | 6 +-- pubspec.yaml | 1 + 6 files changed, 39 insertions(+), 24 deletions(-) diff --git a/.tool-versions b/.tool-versions index 3b816794..62f61e58 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1 +1 @@ -flutter 3.24.5-stable +flutter 3.27.1-stable diff --git a/lib/bloc/podcast/podcast_bloc.dart b/lib/bloc/podcast/podcast_bloc.dart index a4b6ebc9..91b4de15 100644 --- a/lib/bloc/podcast/podcast_bloc.dart +++ b/lib/bloc/podcast/podcast_bloc.dart @@ -3,7 +3,6 @@ // found in the LICENSE file. import 'package:anytime/bloc/bloc.dart'; -import 'package:anytime/entities/downloadable.dart'; import 'package:anytime/entities/episode.dart'; import 'package:anytime/entities/feed.dart'; import 'package:anytime/entities/podcast.dart'; @@ -245,20 +244,9 @@ class PodcastBloc extends Bloc { var episode = _episodes.firstWhereOrNull((ep) => ep.guid == e.guid); if (episode != null) { - episode.downloadState = e.downloadState = DownloadState.queued; - _refresh(); - var result = await downloadService.downloadEpisode(e); - - // If there was an error downloading the episode, push an error state - // and then restore to none. - if (!result) { - episode.downloadState = e.downloadState = DownloadState.failed; - _refresh(); - episode.downloadState = e.downloadState = DownloadState.none; - _refresh(); - } + await downloadService.downloadEpisode(e); } }); } diff --git a/lib/services/download/download_service.dart b/lib/services/download/download_service.dart index 9e7e1b2f..c39f7a16 100644 --- a/lib/services/download/download_service.dart +++ b/lib/services/download/download_service.dart @@ -5,7 +5,7 @@ import 'package:anytime/entities/episode.dart'; abstract class DownloadService { - Future downloadEpisode(Episode episode); + Future downloadEpisode(Episode episode); Future deleteDownload(Episode episode); Future findEpisodeByTaskId(String taskId); diff --git a/lib/services/download/mobile_download_service.dart b/lib/services/download/mobile_download_service.dart index 766a94c1..5ad7b9c3 100644 --- a/lib/services/download/mobile_download_service.dart +++ b/lib/services/download/mobile_download_service.dart @@ -20,6 +20,7 @@ import 'package:logging/logging.dart'; import 'package:mp3_info/mp3_info.dart'; import 'package:rxdart/rxdart.dart'; import 'package:synchronized/synchronized.dart'; +import 'package:uuid/uuid.dart'; /// An implementation of a [DownloadService] that handles downloading /// of episodes on mobile. @@ -61,7 +62,7 @@ class MobileDownloadService extends DownloadService { } @override - Future downloadEpisode(Episode episode) async { + Future downloadEpisode(Episode episode) async { try { final season = episode.season > 0 ? episode.season.toString() : ''; final epno = episode.episode > 0 ? episode.episode.toString() : ''; @@ -153,15 +154,34 @@ class MobileDownloadService extends DownloadService { await repository.saveEpisode(episode); }); - - return true; } } - - return false; } catch (e, stack) { log.warning('Episode download failed (${episode.title})', e, stack); - return false; + episode.filename = null; + episode.filepath = null; + episode.downloadTaskId = null; + episode.downloadPercentage = 0; + episode.downloadState = DownloadState.none; + + await repository.saveEpisode(episode); + + /// If there was an error downloading the episode, push an error state + /// and then restore to none. + /// + /// If failure happens before download actual start, its [id] will be [null]. + final downloadId = episode.downloadTaskId ?? const Uuid().v4(); + downloadProgress + ..add(DownloadProgress( + downloadId, + 0, + DownloadState.failed, + )) + ..add(DownloadProgress( + downloadId, + 0, + DownloadState.none, + )); } } @@ -197,6 +217,12 @@ class MobileDownloadService extends DownloadService { } } + // downloadProgress.add(DownloadProgress( + // episode.downloadTaskId!, + // 0, + // DownloadState.none, + // )); + return; }); diff --git a/pubspec.lock b/pubspec.lock index 20370e54..6d8839fc 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1184,13 +1184,13 @@ packages: source: hosted version: "3.1.2" uuid: - dependency: transitive + dependency: "direct main" description: name: uuid - sha256: f33d6bb662f0e4f79dcd7ada2e6170f3b3a2530c28fc41f49a411ddedd576a77 + sha256: a5be9ef6618a7ac1e964353ef476418026db906c4facdedaa299b7a2e71690ff url: "https://pub.dev" source: hosted - version: "4.5.0" + version: "4.5.1" vector_graphics: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index af858671..977855eb 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -49,6 +49,7 @@ dependencies: sliver_tools: ^0.2.12 synchronized: ^3.1.0 url_launcher: ^6.1.12 + uuid: ^4.5.1 xml: 6.5.0 flutter: