Browse Source

Refactor and honor select all folder sync

Neeraj Gupta 2 years ago
parent
commit
0502cd20da
3 changed files with 79 additions and 38 deletions
  1. 35 4
      lib/db/device_files_db.dart
  2. 2 0
      lib/models/device_folder.dart
  3. 42 34
      lib/services/local_sync_service.dart

+ 35 - 4
lib/db/device_files_db.dart

@@ -10,6 +10,8 @@ import 'package:tuple/tuple.dart';
 
 extension DeviceFiles on FilesDB {
   static final Logger _logger = Logger("DeviceFilesDB");
+  static const _sqlBoolTrue = 1;
+  static const _sqlBoolFalse = 0;
 
   Future<void> insertDeviceFiles(
     List<File> files, {
@@ -165,8 +167,9 @@ extension DeviceFiles on FilesDB {
 
   // todo: covert it to batch
   Future<void> insertOrUpdatePathName(
-    List<AssetPathEntity> pathEntities,
-  ) async {
+    List<AssetPathEntity> pathEntities, {
+    bool autoSync = false,
+  }) async {
     try {
       final Set<String> existingPathIds = await getDevicePathIDs();
       final Database db = await database;
@@ -183,6 +186,7 @@ extension DeviceFiles on FilesDB {
             {
               "id": pathEntity.id,
               "name": pathEntity.name,
+              "sync": autoSync ? _sqlBoolTrue : _sqlBoolFalse
             },
           );
         }
@@ -194,8 +198,9 @@ extension DeviceFiles on FilesDB {
   }
 
   Future<bool> updateDeviceCoverWithCount(
-    List<Tuple2<AssetPathEntity, File>> devicePathInfo,
-  ) async {
+    List<Tuple2<AssetPathEntity, File>> devicePathInfo, {
+    bool autoSync = false,
+  }) async {
     bool hasUpdated = false;
     try {
       final Database db = await database;
@@ -219,6 +224,7 @@ extension DeviceFiles on FilesDB {
               "name": pathEntity.name,
               "count": pathEntity.assetCount,
               "cover_id": localID,
+              "sync": autoSync ? _sqlBoolTrue : _sqlBoolFalse
             },
           );
         }
@@ -248,6 +254,30 @@ extension DeviceFiles on FilesDB {
     }
   }
 
+  Future<void> updateDevicePathSyncStatus(Map<String, bool> syncStatus) async {
+    final db = await database;
+    var batch = db.batch();
+    int batchCounter = 0;
+    for (MapEntry e in syncStatus.entries) {
+      String pathID = e.key;
+      if (batchCounter == 400) {
+        await batch.commit(noResult: true);
+        batch = db.batch();
+        batchCounter = 0;
+      }
+      batch.update(
+        "device_path_collections",
+        {
+          "sync": e.value ? _sqlBoolTrue : _sqlBoolFalse,
+        },
+        where: 'id = ?',
+        whereArgs: [pathID],
+      );
+      batchCounter++;
+    }
+    await batch.commit(noResult: true);
+  }
+
   Future<FileLoadResult> getFilesInDevicePathCollection(
     DevicePathCollection devicePathCollection,
     int startTime,
@@ -292,6 +322,7 @@ extension DeviceFiles on FilesDB {
           count: row['count'],
           collectionID: row["collection_id"],
           coverId: row["cover_id"],
+          sync: (row["sync"] ?? _sqlBoolFalse) == _sqlBoolTrue,
         );
         devicePathCollection.thumbnail = files.firstWhere(
           (element) => element.localID == devicePathCollection.coverId,

+ 2 - 0
lib/models/device_folder.dart

@@ -18,6 +18,7 @@ class DevicePathCollection {
   final String path;
   final String coverId;
   final int count;
+  final bool sync;
   final int collectionID;
   File thumbnail;
 
@@ -29,5 +30,6 @@ class DevicePathCollection {
     this.count,
     this.collectionID,
     this.thumbnail,
+    this.sync = false,
   });
 }

+ 42 - 34
lib/services/local_sync_service.dart

@@ -133,7 +133,10 @@ class LocalSyncService {
   Future<bool> refreshDeviceFolderCountAndCover() async {
     List<Tuple2<AssetPathEntity, File>> result =
         await getDeviceFolderWithCountAndCoverFile();
-    return await _db.updateDeviceCoverWithCount(result);
+    return await _db.updateDeviceCoverWithCount(
+      result,
+      autoSync: Configuration.instance.hasSelectedAllFoldersForBackup(),
+    );
   }
 
   Future<bool> syncAll() async {
@@ -164,7 +167,8 @@ class LocalSyncService {
       await _db.insertPathIDToLocalIDMapping(unsyncedFiles.newPathToLocalIDs);
     }
     if (unsyncedFiles.deletePathToLocalIDs.isNotEmpty) {
-      _db.deletePathIDToLocalIDMapping(unsyncedFiles.deletePathToLocalIDs);
+      await _db
+          .deletePathIDToLocalIDMapping(unsyncedFiles.deletePathToLocalIDs);
     }
     if (unsyncedFiles.uniqueLocalFiles.isNotEmpty) {
       await _db.insertMultiple(unsyncedFiles.uniqueLocalFiles);
@@ -173,9 +177,8 @@ class LocalSyncService {
             unsyncedFiles.uniqueLocalFiles.toString() +
             " unsynced files.",
       );
-      // todo: review
-      // _updatePathsToBackup(unsyncedFiles);
-      // Bus.instance.fire(LocalPhotosUpdatedEvent(unsyncedFiles));
+      Bus.instance.fire(LocalPhotosUpdatedEvent(unsyncedFiles));
+
       return true;
     }
     return false;
@@ -274,21 +277,43 @@ class LocalSyncService {
     final deviceFiles = await getDeviceFiles(fromTime, toTime, _computer);
     final List<File> files = deviceFiles.item2;
     unawaited(FilesDB.instance.insertDeviceFiles(files));
-    unawaited(FilesDB.instance.insertOrUpdatePathName(deviceFiles.item1));
+    unawaited(
+      FilesDB.instance.insertOrUpdatePathName(
+        deviceFiles.item1,
+        autoSync: Configuration.instance.hasSelectedAllFoldersForBackup(),
+      ),
+    );
     if (files.isNotEmpty) {
       _logger.info("Fetched " + files.length.toString() + " files.");
-      final updatedFiles = files
-          .where((file) => existingLocalFileIDs.contains(file.localID))
-          .toList();
-      updatedFiles.removeWhere((file) => editedFileIDs.contains(file.localID));
-      updatedFiles
-          .removeWhere((file) => downloadedFileIDs.contains(file.localID));
-      if (updatedFiles.isNotEmpty) {
-        _logger.info(
-          updatedFiles.length.toString() + " local files were updated.",
-        );
-      }
+      await _trackUpdatedFiles(
+          files, existingLocalFileIDs, editedFileIDs, downloadedFileIDs);
+      final List<File> allFiles = [];
+      allFiles.addAll(files);
+      files.removeWhere((file) => existingLocalFileIDs.contains(file.localID));
+      await _db.insertMultiple(files);
+      _logger.info("Inserted " + files.length.toString() + " files.");
+      // _updatePathsToBackup(files);
+      Bus.instance.fire(LocalPhotosUpdatedEvent(allFiles));
+    }
+    await _prefs.setInt(kDbUpdationTimeKey, toTime);
+  }
 
+  Future<void> _trackUpdatedFiles(
+    List<File> files,
+    Set<String> existingLocalFileIDs,
+    Set<String> editedFileIDs,
+    Set<String> downloadedFileIDs,
+  ) async {
+    final updatedFiles = files
+        .where((file) => existingLocalFileIDs.contains(file.localID))
+        .toList();
+    updatedFiles.removeWhere((file) => editedFileIDs.contains(file.localID));
+    updatedFiles
+        .removeWhere((file) => downloadedFileIDs.contains(file.localID));
+    if (updatedFiles.isNotEmpty) {
+      _logger.info(
+        updatedFiles.length.toString() + " local files were updated.",
+      );
       List<String> updatedLocalIDs = [];
       for (final file in updatedFiles) {
         if (file.localID != null) {
@@ -299,23 +324,6 @@ class LocalSyncService {
         updatedLocalIDs,
         FilesMigrationDB.modificationTimeUpdated,
       );
-      final List<File> allFiles = [];
-      allFiles.addAll(files);
-      files.removeWhere((file) => existingLocalFileIDs.contains(file.localID));
-      await _db.insertMultiple(files);
-      _logger.info("Inserted " + files.length.toString() + " files.");
-      _updatePathsToBackup(files);
-      Bus.instance.fire(LocalPhotosUpdatedEvent(allFiles));
-    }
-    await _prefs.setInt(kDbUpdationTimeKey, toTime);
-  }
-
-  void _updatePathsToBackup(List<File> files) {
-    if (Configuration.instance.hasSelectedAllFoldersForBackup()) {
-      final pathsToBackup = Configuration.instance.getPathsToBackUp();
-      final newFilePaths = files.map((file) => file.deviceFolder).toList();
-      pathsToBackup.addAll(newFilePaths);
-      Configuration.instance.setPathsToBackUp(pathsToBackup);
     }
   }