Procházet zdrojové kódy

Clear upload queue with a reason

Vishnu Mohandas před 4 roky
rodič
revize
a0614ad114
2 změnil soubory, kde provedl 12 přidání a 10 odebrání
  1. 1 1
      lib/services/sync_service.dart
  2. 11 9
      lib/utils/file_uploader.dart

+ 1 - 1
lib/services/sync_service.dart

@@ -44,7 +44,7 @@ class SyncService {
 
 
   SyncService._privateConstructor() {
   SyncService._privateConstructor() {
     Bus.instance.on<SubscriptionPurchasedEvent>().listen((event) {
     Bus.instance.on<SubscriptionPurchasedEvent>().listen((event) {
-      _uploader.clearQueue();
+      _uploader.clearQueue(SyncStopRequestedError());
       sync();
       sync();
     });
     });
 
 

+ 11 - 9
lib/utils/file_uploader.dart

@@ -116,7 +116,7 @@ class FileUploader {
     }
     }
   }
   }
 
 
-  void clearQueue() {
+  void clearQueue(final Error reason) {
     final uploadsToBeRemoved = List<int>();
     final uploadsToBeRemoved = List<int>();
     _queue.entries
     _queue.entries
         .where((entry) => entry.value.status == UploadStatus.not_started)
         .where((entry) => entry.value.status == UploadStatus.not_started)
@@ -124,13 +124,13 @@ class FileUploader {
       uploadsToBeRemoved.add(pendingUpload.key);
       uploadsToBeRemoved.add(pendingUpload.key);
     });
     });
     for (final id in uploadsToBeRemoved) {
     for (final id in uploadsToBeRemoved) {
-      _queue.remove(id).completer.completeError(SyncStopRequestedError());
+      _queue.remove(id).completer.completeError(reason);
     }
     }
   }
   }
 
 
   void _pollQueue() {
   void _pollQueue() {
     if (SyncService.instance.shouldStopSync()) {
     if (SyncService.instance.shouldStopSync()) {
-      clearQueue();
+      clearQueue(SyncStopRequestedError());
     }
     }
     if (_queue.length > 0 && _currentlyUploading < kMaximumConcurrentUploads) {
     if (_queue.length > 0 && _currentlyUploading < kMaximumConcurrentUploads) {
       final firstPendingEntry = _queue.entries
       final firstPendingEntry = _queue.entries
@@ -434,10 +434,12 @@ class FileUploader {
             .toList();
             .toList();
         _uploadURLs.addAll(urls);
         _uploadURLs.addAll(urls);
       } on DioError catch (e) {
       } on DioError catch (e) {
-        if (e.response.statusCode == 402) {
-          _onExpiredSubscription();
-        } else if (e.response.statusCode == 426) {
-          _onStorageLimitExceeded();
+        if (e.response != null) {
+          if (e.response.statusCode == 402) {
+            _onExpiredSubscription();
+          } else if (e.response.statusCode == 426) {
+            _onStorageLimitExceeded();
+          }
         }
         }
         throw e;
         throw e;
       }
       }
@@ -448,12 +450,12 @@ class FileUploader {
   }
   }
 
 
   void _onStorageLimitExceeded() {
   void _onStorageLimitExceeded() {
-    clearQueue();
+    clearQueue(StorageLimitExceededError());
     throw StorageLimitExceededError();
     throw StorageLimitExceededError();
   }
   }
 
 
   void _onExpiredSubscription() {
   void _onExpiredSubscription() {
-    clearQueue();
+    clearQueue(NoActiveSubscriptionError());
     throw NoActiveSubscriptionError();
     throw NoActiveSubscriptionError();
   }
   }