Browse Source

Ensure that sync interruptions are handled gracefully

Vishnu Mohandas 4 years ago
parent
commit
ca5a30e98a
2 changed files with 24 additions and 5 deletions
  1. 12 0
      lib/services/collections_service.dart
  2. 12 5
      lib/services/sync_service.dart

+ 12 - 0
lib/services/collections_service.dart

@@ -56,6 +56,7 @@ class CollectionsService {
     _logger.info("Syncing");
     final lastCollectionUpdationTime =
         await _db.getLastCollectionUpdationTime();
+    // Might not have synced the collection fully
     final fetchedCollections =
         await _fetchCollections(lastCollectionUpdationTime ?? 0);
     final updatedCollections = List<Collection>();
@@ -78,6 +79,17 @@ class CollectionsService {
       _logger.info("Collections updated");
       Bus.instance.fire(CollectionUpdatedEvent());
     }
+    return collections;
+  }
+
+  Future<List<Collection>> getCollectionsToBeSynced() async {
+    final collections = await _db.getAllCollections();
+    final updatedCollections = List<Collection>();
+    for (final c in collections) {
+      if (c.updationTime > getCollectionSyncTime(c.id)) {
+        updatedCollections.add(c);
+      }
+    }
     return updatedCollections;
   }
 

+ 12 - 5
lib/services/sync_service.dart

@@ -1,5 +1,6 @@
 import 'dart:async';
 import 'dart:io';
+import 'dart:math';
 import 'package:connectivity/connectivity.dart';
 import 'package:flutter/foundation.dart';
 import 'package:flutter_cache_manager/flutter_cache_manager.dart';
@@ -12,6 +13,7 @@ import 'package:photos/db/files_db.dart';
 import 'package:photos/events/collection_updated_event.dart';
 import 'package:photos/events/sync_status_update_event.dart';
 import 'package:photos/events/subscription_purchased_event.dart';
+import 'package:photos/models/collection.dart';
 import 'package:photos/models/file_type.dart';
 import 'package:photos/services/billing_service.dart';
 import 'package:photos/services/collections_service.dart';
@@ -203,12 +205,17 @@ class SyncService {
     if (!Configuration.instance.hasConfiguredAccount()) {
       return Future.error("Account not configured yet");
     }
-    final updatedCollections = await _collectionsService.sync();
+    await _collectionsService.sync();
+    final updatedCollections =
+        await _collectionsService.getCollectionsToBeSynced();
+
     if (updatedCollections.isNotEmpty && !silently) {
       Bus.instance.fire(SyncStatusUpdate(SyncStatus.applying_remote_diff));
     }
-    for (final collection in updatedCollections) {
-      await _fetchEncryptedFilesDiff(collection.id);
+    for (final c in updatedCollections) {
+      await _syncCollectionDiff(c.id);
+      _collectionsService.setCollectionSyncTime(c.id,
+          max(c.updationTime, _collectionsService.getCollectionSyncTime(c.id)));
     }
     await deleteFilesOnServer();
     bool hasUploadedFiles = await _uploadDiff();
@@ -217,7 +224,7 @@ class SyncService {
     }
   }
 
-  Future<void> _fetchEncryptedFilesDiff(int collectionID) async {
+  Future<void> _syncCollectionDiff(int collectionID) async {
     final diff = await _downloader.getEncryptedFilesDiff(
       collectionID,
       _collectionsService.getCollectionSyncTime(collectionID),
@@ -232,7 +239,7 @@ class SyncService {
       FileRepository.instance.reloadFiles();
       Bus.instance.fire(CollectionUpdatedEvent(collectionID: collectionID));
       if (diff.fetchCount == _diffLimit) {
-        return await _fetchEncryptedFilesDiff(collectionID);
+        return await _syncCollectionDiff(collectionID);
       }
     }
   }