فهرست منبع

Fix sync stop functionality

Vishnu Mohandas 4 سال پیش
والد
کامیت
94ca34de4d
2فایلهای تغییر یافته به همراه19 افزوده شده و 12 حذف شده
  1. 6 12
      lib/services/sync_service.dart
  2. 13 0
      lib/utils/file_uploader.dart

+ 6 - 12
lib/services/sync_service.dart

@@ -86,6 +86,10 @@ class SyncService {
         _logger.warning("Not uploading over mobile data");
         _logger.warning("Not uploading over mobile data");
         Bus.instance.fire(
         Bus.instance.fire(
             SyncStatusUpdate(SyncStatus.paused, reason: "Waiting for WiFi..."));
             SyncStatusUpdate(SyncStatus.paused, reason: "Waiting for WiFi..."));
+      } on SyncStopRequestedError {
+        _syncStopRequested = false;
+        Bus.instance
+            .fire(SyncStatusUpdate(SyncStatus.completed, wasStopped: true));
       } catch (e, s) {
       } catch (e, s) {
         _logger.severe(e, s);
         _logger.severe(e, s);
         Bus.instance.fire(SyncStatusUpdate(SyncStatus.error));
         Bus.instance.fire(SyncStatusUpdate(SyncStatus.error));
@@ -240,12 +244,6 @@ class SyncService {
 
 
     final futures = List<Future>();
     final futures = List<Future>();
     for (final uploadedFileID in updatedFileIDs) {
     for (final uploadedFileID in updatedFileIDs) {
-      if (_syncStopRequested) {
-        _syncStopRequested = false;
-        Bus.instance
-            .fire(SyncStatusUpdate(SyncStatus.completed, wasStopped: true));
-        return false;
-      }
       final file = await _db.getUploadedFileInAnyCollection(uploadedFileID);
       final file = await _db.getUploadedFileInAnyCollection(uploadedFileID);
       final future = _uploader.upload(file, file.collectionID).then((value) {
       final future = _uploader.upload(file, file.collectionID).then((value) {
         uploadCounter++;
         uploadCounter++;
@@ -258,12 +256,6 @@ class SyncService {
     }
     }
 
 
     for (final file in filesToBeUploaded) {
     for (final file in filesToBeUploaded) {
-      if (_syncStopRequested) {
-        _syncStopRequested = false;
-        Bus.instance
-            .fire(SyncStatusUpdate(SyncStatus.completed, wasStopped: true));
-        return false;
-      }
       final collectionID = (await CollectionsService.instance
       final collectionID = (await CollectionsService.instance
               .getOrCreateForPath(file.deviceFolder))
               .getOrCreateForPath(file.deviceFolder))
           .id;
           .id;
@@ -282,6 +274,8 @@ class SyncService {
       // Do nothing
       // Do nothing
     } on WiFiUnavailableError {
     } on WiFiUnavailableError {
       throw WiFiUnavailableError();
       throw WiFiUnavailableError();
+    } on SyncStopRequestedError {
+      throw SyncStopRequestedError();
     } catch (e, s) {
     } catch (e, s) {
       _isSyncInProgress = false;
       _isSyncInProgress = false;
       Bus.instance.fire(SyncStatusUpdate(SyncStatus.error));
       Bus.instance.fire(SyncStatusUpdate(SyncStatus.error));

+ 13 - 0
lib/utils/file_uploader.dart

@@ -15,6 +15,7 @@ import 'package:photos/models/file.dart';
 import 'package:photos/models/location.dart';
 import 'package:photos/models/location.dart';
 import 'package:photos/models/upload_url.dart';
 import 'package:photos/models/upload_url.dart';
 import 'package:photos/services/collections_service.dart';
 import 'package:photos/services/collections_service.dart';
+import 'package:photos/services/sync_service.dart';
 import 'package:photos/utils/crypto_util.dart';
 import 'package:photos/utils/crypto_util.dart';
 import 'package:photos/utils/file_util.dart';
 import 'package:photos/utils/file_util.dart';
 
 
@@ -106,6 +107,16 @@ class FileUploader {
   }
   }
 
 
   void _pollQueue() {
   void _pollQueue() {
+    if (SyncService.instance.shouldStopSync()) {
+      _queue.entries
+          .where((entry) => entry.value.status == UploadStatus.not_started)
+          .forEach((pendingUpload) {
+        _queue
+            .remove(pendingUpload.key)
+            .completer
+            .completeError(SyncStopRequestedError());
+      });
+    }
     if (_queue.length > 0 && _currentlyUploading < _maximumConcurrentUploads) {
     if (_queue.length > 0 && _currentlyUploading < _maximumConcurrentUploads) {
       final firstPendingEntry = _queue.entries
       final firstPendingEntry = _queue.entries
           .firstWhere((entry) => entry.value.status == UploadStatus.not_started,
           .firstWhere((entry) => entry.value.status == UploadStatus.not_started,
@@ -431,3 +442,5 @@ enum UploadStatus {
 class InvalidFileError extends Error {}
 class InvalidFileError extends Error {}
 
 
 class WiFiUnavailableError extends Error {}
 class WiFiUnavailableError extends Error {}
+
+class SyncStopRequestedError extends Error {}