Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Rename & move downloadQueue

Neeraj Guptaaef50f3f959e
diff --git a/mobile/lib/ui/viewer/actions/file_selection_actions_widget.dart b/mobile/lib/ui/viewer/actions/file_selection_actions_widget.dart
index 5f59aa73cd..548395a9c4 100644
--- a/mobile/lib/ui/viewer/actions/file_selection_actions_widget.dart
+++ b/mobile/lib/ui/viewer/actions/file_selection_actions_widget.dart
@@ -48,6 +48,7 @@ import "package:photos/utils/file_download_util.dart";
import 'package:photos/utils/magic_util.dart';
import 'package:photos/utils/navigation_util.dart';
import "package:photos/utils/share_util.dart";
+import "package:photos/utils/standalone/simple_task_queue.dart";
import "package:screenshot/screenshot.dart";
class FileSelectionActionsWidget extends StatefulWidget {
@@ -887,12 +888,12 @@ class _FileSelectionActionsWidgetState
);
await dialog.show();
try {
- final downloadQueue = DownloadQueue(maxConcurrent: 5);
+ final taskQueue = SimpleTaskQueue(maxConcurrent: 5);
final futures = <Future>[];
for (final file in files) {
if (file.localID == null) {
futures.add(
- downloadQueue.add(() async {
+ taskQueue.add(() async {
await downloadToGallery(file);
downloadedFiles++;
dialog.update(
diff --git a/mobile/lib/utils/file_download_util.dart b/mobile/lib/utils/file_download_util.dart
index 420ad34713..15d73f5f7f 100644
--- a/mobile/lib/utils/file_download_util.dart
+++ b/mobile/lib/utils/file_download_util.dart
@@ -277,37 +277,4 @@ Future<void> _saveLivePhotoOnDroid(
);
await IgnoredFilesService.instance.cacheAndInsert([ignoreVideoFile]);
}
-
-class DownloadQueue {
- final int maxConcurrent;
- final Queue<Future<void> Function()> _queue = Queue();
- int _runningTasks = 0;
-
- DownloadQueue({this.maxConcurrent = 5});
-
- Future<void> add(Future<void> Function() task) async {
- final completer = Completer<void>();
- _queue.add(() async {
- try {
- await task();
- completer.complete();
- } catch (e) {
- completer.completeError(e);
- } finally {
- _runningTasks--;
- _processQueue();
- }
- return completer.future;
- });
- _processQueue();
- return completer.future;
- }
-
- void _processQueue() {
- while (_runningTasks < maxConcurrent && _queue.isNotEmpty) {
- final task = _queue.removeFirst();
- _runningTasks++;
- task();
- }
- }
}
diff --git a/mobile/lib/utils/standalone/simple_task_queue.dart b/mobile/lib/utils/standalone/simple_task_queue.dart
new file mode 100644
index 0000000000..1f11c2da2a
--- /dev/null
+++ b/mobile/lib/utils/standalone/simple_task_queue.dart
@@ -0,0 +1,41 @@
+import "dart:async";
+import "dart:collection";
+
+// SimpleTaskQueue is a simple task queue that allows you to add tasks
+// and run them concurrently up to a specified limit. It doesn't support
+// task cancellation or timeout, but it can be used for simple
+// asynchronous task management.
+// See [TaskQueue] for a more advanced implementation with
+class SimpleTaskQueue {
+ final int maxConcurrent;
+ final Queue<Future<void> Function()> _queue = Queue();
+ int _runningTasks = 0;
+
+ SimpleTaskQueue({this.maxConcurrent = 5});
+
+ Future<void> add(Future<void> Function() task) async {
+ final completer = Completer<void>();
+ _queue.add(() async {
+ try {
+ await task();
+ completer.complete();
+ } catch (e) {
+ completer.completeError(e);
+ } finally {
+ _runningTasks--;
+ _processQueue();
+ }
+ return completer.future;
+ });
+ _processQueue();
+ return completer.future;
+ }
+
+ void _processQueue() {
+ while (_runningTasks < maxConcurrent && _queue.isNotEmpty) {
+ final task = _queue.removeFirst();
+ _runningTasks++;
+ task();
+ }
+ }
+}