Skip to content

Commit

Permalink
Added cancelAll
Browse files Browse the repository at this point in the history
  • Loading branch information
781flyingdutchman committed Mar 2, 2025
1 parent 449cebb commit 79cfa8b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class BDPlugin: NSObject, FlutterPlugin, UNUserNotificationCenterDelegate
static var progressInfo = [String: (lastProgressUpdateTime: TimeInterval,
lastProgressValue: Double,
lastTotalBytesDone: Int64,
lastNetworkSpeed: Double)]() // time, bytes, speed
lastNetworkSpeed: Double)]() // upadtetime, progress %, bytes, speed
static var uploaderForUrlSessionTaskIdentifier = [Int:Uploader]() // maps from UrlSessionTask TaskIdentifier
static var haveregisteredNotificationCategories = false
static var requireWiFi = RequireWiFi.asSetByTask // global setting
Expand Down Expand Up @@ -589,8 +589,13 @@ public class BDPlugin: NSObject, FlutterPlugin, UNUserNotificationCenterDelegate
if let notificationConfigJsonString = BDPlugin.notificationConfigJsonStrings[taskId],
let notificationConfig = notificationConfigFrom(jsonString: notificationConfigJsonString)
{
updateNotification(task: task, notificationType: .paused, notificationConfig: notificationConfig)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
updateNotification(task: task, notificationType: .paused, notificationConfig: notificationConfig)
}
}
BDPlugin.propertyLock.withLock({
_ = BDPlugin.progressInfo.removeValue(forKey: taskId) // ensure .running update on resume
})
return true
} else {
os_log("Could not post resume data for taskId %@: task paused but cannot be resumed", log: log, type: .info, taskId)
Expand Down
30 changes: 21 additions & 9 deletions lib/src/base_downloader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ abstract base class BaseDownloader {
}

/// Enqueue a list of tasks
Future<List<bool>> enqueueAll(List<Task> tasks);
Future<List<bool>> enqueueAll(Iterable<Task> tasks);

/// Enqueue the [task] and wait for completion
///
Expand Down Expand Up @@ -402,7 +402,7 @@ abstract base class BaseDownloader {
///
/// Returns true if all cancellations were successful
@mustCallSuper
Future<bool> cancelTasksWithIds(List<String> taskIds) async {
Future<bool> cancelTasksWithIds(Iterable<String> taskIds) async {
final matchingTasksWaitingToRetry = tasksWaitingToRetry
.where((task) => taskIds.contains(task.taskId))
.toList(growable: false);
Expand All @@ -422,8 +422,7 @@ abstract base class BaseDownloader {
final pausedTasks = await getPausedTasks();
final pausedTaskIdsToCancel = pausedTasks
.where((task) => remainingTaskIds.contains(task.taskId))
.map((e) => e.taskId)
.toList(growable: false);
.map((e) => e.taskId);
await cancelPausedPlatformTasksWithIds(pausedTasks, pausedTaskIdsToCancel);
// cancel remaining taskIds on the platform
final platformTaskIds = remainingTaskIds
Expand All @@ -435,14 +434,26 @@ abstract base class BaseDownloader {
return cancelPlatformTasksWithIds(platformTaskIds);
}

/// Cancels all tasks, or those in [tasks], or all tasks in group [group]
///
/// 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),
(null, null) => await FileDownloader().allTasks(),
};
return cancelTasksWithIds(tasksToCancel.map((task) => task.taskId));
}

/// Cancel these tasks on the platform
Future<bool> cancelPlatformTasksWithIds(List<String> taskIds);

/// Cancel paused tasks
///
/// Deletes the associated temp file and emits [TaskStatus.cancel]
Future<void> cancelPausedPlatformTasksWithIds(
List<Task> pausedTasks, List<String> taskIds) async {
List<Task> pausedTasks, Iterable<String> taskIds) async {
for (final taskId in taskIds) {
final task =
pausedTasks.firstWhereOrNull((element) => element.taskId == taskId);
Expand Down Expand Up @@ -545,11 +556,12 @@ abstract base class BaseDownloader {
///
/// Returns list of tasks that were paused
Future<List<DownloadTask>> pauseAll(
{List<DownloadTask>? tasks, String? group}) async {
{Iterable<DownloadTask>? tasks, String? group}) async {
final tasksToPause = switch ((tasks, group)) {
(List<DownloadTask> tasks, _) => tasks,
(_, String group) => await FileDownloader().allTasks(group: group),
(null, null) => await FileDownloader().allTasks(),
(Iterable<DownloadTask> tasks, _) => tasks,
(_, String group) =>
(await FileDownloader().allTasks(group: group)) as Iterable<Task>,
(null, null) => (await FileDownloader().allTasks()) as Iterable<Task>,
}
.whereType<DownloadTask>()
.where((task) => task.allowPause && task.post == null)
Expand Down
2 changes: 1 addition & 1 deletion lib/src/desktop/desktop_downloader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ final class DesktopDownloader extends BaseDownloader {
}

@override
Future<List<bool>> enqueueAll(List<Task> tasks) async {
Future<List<bool>> enqueueAll(Iterable<Task> tasks) async {
final results = <bool>[];
for (final task in tasks) {
results.add(await enqueue(task));
Expand Down
14 changes: 10 additions & 4 deletions lib/src/file_downloader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ interface class FileDownloader {
/// each task was successfully enqueued
///
/// See [enqueue] for details
Future<List<bool>> enqueueAll(List<Task> tasks) =>
Future<List<bool>> enqueueAll(Iterable<Task> tasks) =>
_downloader.enqueueAll(tasks);

/// Download a file and return the final [TaskStatusUpdate]
Expand Down Expand Up @@ -491,7 +491,7 @@ interface class FileDownloader {
///
/// Every canceled task wil emit a [TaskStatus.canceled] update to
/// the registered callback, if requested
Future<bool> cancelTasksWithIds(List<String> taskIds) =>
Future<bool> cancelTasksWithIds(Iterable<String> taskIds) =>
_downloader.cancelTasksWithIds(taskIds);

/// Cancel this task
Expand All @@ -500,6 +500,12 @@ interface class FileDownloader {
/// the registered callback, if requested
Future<bool> cancelTaskWithId(String taskId) => cancelTasksWithIds([taskId]);

/// Cancels all tasks, or those in [tasks], or all tasks in group [group]
///
/// Returns true if all cancellations were successful
Future<bool> cancelAll({Iterable<Task>? tasks, String? group}) =>
_downloader.cancelAll(tasks: tasks, group: group);

/// Return [Task] for the given [taskId], or null
/// if not found.
///
Expand Down Expand Up @@ -672,7 +678,7 @@ interface class FileDownloader {
///
/// Returns list of tasks that were paused
Future<List<DownloadTask>> pauseAll(
{List<DownloadTask>? tasks, String? group}) =>
{Iterable<DownloadTask>? tasks, String? group}) =>
_downloader.pauseAll(tasks: tasks, group: group);

/// Resume the task
Expand All @@ -690,7 +696,7 @@ interface class FileDownloader {
///
/// Calls to resume will be spaced out over time by [interval], defaults to 50ms
Future<List<Task>> resumeAll(
{List<DownloadTask>? tasks,
{Iterable<DownloadTask>? tasks,
Duration interval = const Duration(milliseconds: 50)}) async {
final results = <Task>[];
final tasksToResume =
Expand Down
9 changes: 6 additions & 3 deletions lib/src/native_downloader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ abstract base class NativeDownloader extends BaseDownloader {
}

@override
Future<List<bool>> enqueueAll(List<Task> tasks) async {
Future<List<bool>> enqueueAll(Iterable<Task> tasks) async {
for (final task in tasks.where((task) => task.allowPause)) {
canResumeTask[task] = Completer();
}
Expand All @@ -253,7 +253,10 @@ abstract base class NativeDownloader extends BaseDownloader {
}

static (String, String) _taskAndNotificationConfigJsonStrings(
(List<Task>, Set<TaskNotificationConfig>) tasksAndNotificationConfigs) {
(
Iterable<Task>,
Set<TaskNotificationConfig>
) tasksAndNotificationConfigs) {
final (tasks, notificationConfigs) = tasksAndNotificationConfigs;
final tasksJsonString = jsonEncode(tasks);
final configs = tasks
Expand Down Expand Up @@ -514,7 +517,7 @@ final class AndroidDownloader extends NativeDownloader {
}

@override
Future<List<bool>> enqueueAll(List<Task> tasks) async {
Future<List<bool>> enqueueAll(Iterable<Task> tasks) async {
for (final task in tasks) {
await _registerCallbackDispatcher(task);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/web_downloader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class DesktopDownloader extends BaseDownloader {

@override
Future<List<DownloadTask>> pauseAll(
{List<DownloadTask>? tasks, String? group}) {
{Iterable<DownloadTask>? tasks, String? group}) {
throw UnimplementedError();
}

Expand Down Expand Up @@ -85,7 +85,7 @@ final class DesktopDownloader extends BaseDownloader {
}

@override
Future<List<bool>> enqueueAll(List<Task> tasks) {
Future<List<bool>> enqueueAll(Iterable<Task> tasks) {
throw UnimplementedError();
}
}

0 comments on commit 79cfa8b

Please sign in to comment.