Skip to content

Commit

Permalink
Fix issue #470 Add group parameter to resumeAll and add assert statem…
Browse files Browse the repository at this point in the history
…ents to ..all functions
  • Loading branch information
781flyingdutchman committed Mar 5, 2025
1 parent 63b2989 commit 24b51c9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
12 changes: 8 additions & 4 deletions lib/src/base_downloader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,11 @@ abstract base class BaseDownloader {
/// Returns true if all cancellations were successful
Future<bool> cancelAll({Iterable<Task>? tasks, String? group}) async {
final tasksToCancel = switch ((tasks, group)) {
(Iterable<Task> tasks, _) => tasks,
(_, String group) => await FileDownloader().allTasks(group: group),
(Iterable<Task> tasks, null) => tasks,
(null, String group) => await FileDownloader().allTasks(group: group),
(null, null) => await FileDownloader().allTasks(),
_ => throw AssertionError(
"Either 'tasks' or 'group' must be provided, or neither, but not both.")
};
return cancelTasksWithIds(tasksToCancel.map((task) => task.taskId));
}
Expand Down Expand Up @@ -558,10 +560,12 @@ abstract base class BaseDownloader {
Future<List<DownloadTask>> pauseAll(
{Iterable<DownloadTask>? tasks, String? group}) async {
final tasksToPause = switch ((tasks, group)) {
(Iterable<DownloadTask> tasks, _) => tasks,
(_, String group) =>
(Iterable<DownloadTask> tasks, null) => tasks,
(null, String group) =>
(await FileDownloader().allTasks(group: group)) as Iterable<Task>,
(null, null) => (await FileDownloader().allTasks()) as Iterable<Task>,
_ => throw AssertionError(
"Either 'tasks' or 'group' must be provided, or neither, but not both.")
}
.whereType<DownloadTask>()
.where((task) => task.allowPause && task.post == null)
Expand Down
16 changes: 13 additions & 3 deletions lib/src/file_downloader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -698,15 +698,25 @@ interface class FileDownloader {
/// task from scratch, or fail.
Future<bool> resume(DownloadTask task) => _downloader.resume(task);

/// Resume all [tasks], or all paused tasks if [tasks] is omitted
/// Resume all paused tasks, or those in [tasks], or paused tasks in
/// group [group]
///
/// Calls to resume will be spaced out over time by [interval], defaults to 50ms
Future<List<Task>> resumeAll(
{Iterable<DownloadTask>? tasks,
String? group,
Duration interval = const Duration(milliseconds: 50)}) async {
final results = <Task>[];
final tasksToResume =
tasks ?? (await _downloader.getPausedTasks()).whereType<DownloadTask>();
final tasksToResume = switch ((tasks, group)) {
(Iterable<DownloadTask> tasks, null) => tasks,
(null, String group) => (await _downloader.getPausedTasks())
.whereType<DownloadTask>()
.where((task) => task.group == group),
(null, null) =>
(await _downloader.getPausedTasks()).whereType<DownloadTask>(),
_ => throw AssertionError(
"Either 'tasks' or 'group' must be provided, or neither, but not both.")
};
for (final task in tasksToResume) {
if (await resume(task)) {
results.add(task);
Expand Down

0 comments on commit 24b51c9

Please sign in to comment.