From 339431a7f0e1c3abdc74c348741f36a58458930e Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Tue, 31 Jan 2023 15:50:20 +0530 Subject: [PATCH] refactor --- lib/db/file_updation_db.dart | 2 +- lib/services/local_sync_service.dart | 29 ++++++++++++++-------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/db/file_updation_db.dart b/lib/db/file_updation_db.dart index 26e97fb66..554d76150 100644 --- a/lib/db/file_updation_db.dart +++ b/lib/db/file_updation_db.dart @@ -105,7 +105,7 @@ class FileUpdationDB { endTime.microsecondsSinceEpoch - startTime.microsecondsSinceEpoch, ); _logger.info( - "Batch insert of ${fileLocalIDs.length} " + "Batch insert of ${fileLocalIDs.length} updated files due to $reason " "took ${duration.inMilliseconds} ms.", ); } diff --git a/lib/services/local_sync_service.dart b/lib/services/local_sync_service.dart index 790ee3bd1..e7295d82a 100644 --- a/lib/services/local_sync_service.dart +++ b/lib/services/local_sync_service.dart @@ -109,8 +109,7 @@ class LocalSyncService { toTime: syncStartTime, ); } - if (!_prefs.containsKey(kHasCompletedFirstImportKey) || - !(_prefs.getBool(kHasCompletedFirstImportKey)!)) { + if (!hasCompletedFirstImport()) { await _prefs.setBool(kHasCompletedFirstImportKey, true); // mark device collection has imported on first import await _refreshDeviceFolderCountAndCover(isFirstSync: true); @@ -298,10 +297,14 @@ class LocalSyncService { }) async { final Tuple2, List> result = await getLocalPathAssetsAndFiles(fromTime, toTime, _computer); + + // Update the mapping for device path_id to local file id. Also, keep track + // of newly discovered device paths await FilesDB.instance.insertLocalAssets( result.item1, shouldAutoBackup: Configuration.instance.hasSelectedAllFoldersForBackup(), ); + final List files = result.item2; if (files.isNotEmpty) { _logger.info( @@ -311,8 +314,10 @@ class LocalSyncService { DateTime.fromMicrosecondsSinceEpoch(toTime).toString(), ); await _trackUpdatedFiles(files, existingLocalDs); + // keep reference of all Files for firing LocalPhotosUpdatedEvent final List allFiles = []; allFiles.addAll(files); + // remove existing files and insert newly imported files in the table files.removeWhere((file) => existingLocalDs.contains(file.localID)); await _db.insertMultiple( files, @@ -330,19 +335,15 @@ class LocalSyncService { List files, Set existingLocalFileIDs, ) async { - final updatedFiles = files - .where((file) => existingLocalFileIDs.contains(file.localID)) + final List updatedLocalIDs = files + .where( + (file) => + file.localID != null && + existingLocalFileIDs.contains(file.localID), + ) + .map((e) => e.localID!) .toList(); - if (updatedFiles.isNotEmpty) { - _logger.info( - updatedFiles.length.toString() + " local files were updated.", - ); - final List updatedLocalIDs = []; - for (final file in updatedFiles) { - if (file.localID != null) { - updatedLocalIDs.add(file.localID!); - } - } + if (updatedLocalIDs.isNotEmpty) { await FileUpdationDB.instance.insertMultiple( updatedLocalIDs, FileUpdationDB.modificationTimeUpdated,