Skip to content

Commit

Permalink
fix: 🐛 Errors on download initialization are correctly dispatched to …
Browse files Browse the repository at this point in the history
…persistence and UI.
  • Loading branch information
Chralu committed Dec 26, 2024
1 parent 46f9679 commit 75036f2
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
flutter 3.24.5-stable
flutter 3.27.1-stable
14 changes: 1 addition & 13 deletions lib/bloc/podcast/podcast_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/services/download/download_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import 'package:anytime/entities/episode.dart';

abstract class DownloadService {
Future<bool> downloadEpisode(Episode episode);
Future<void> downloadEpisode(Episode episode);
Future<void> deleteDownload(Episode episode);

Future<Episode?> findEpisodeByTaskId(String taskId);
Expand Down
38 changes: 32 additions & 6 deletions lib/services/download/mobile_download_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -61,7 +62,7 @@ class MobileDownloadService extends DownloadService {
}

@override
Future<bool> downloadEpisode(Episode episode) async {
Future<void> downloadEpisode(Episode episode) async {
try {
final season = episode.season > 0 ? episode.season.toString() : '';
final epno = episode.episode > 0 ? episode.episode.toString() : '';
Expand Down Expand Up @@ -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,
));
}
}

Expand Down Expand Up @@ -197,6 +217,12 @@ class MobileDownloadService extends DownloadService {
}
}

// downloadProgress.add(DownloadProgress(
// episode.downloadTaskId!,
// 0,
// DownloadState.none,
// ));

return;
});

Expand Down
6 changes: 3 additions & 3 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 75036f2

Please sign in to comment.