From c67a147110bcb2fe09a98432e972474fe662f25a Mon Sep 17 00:00:00 2001 From: Peter Ombodi Date: Thu, 9 Oct 2025 11:52:40 +0300 Subject: [PATCH] optimize sync trashed assets call in full sync mode refactor code --- mobile/lib/domain/services/local_sync.service.dart | 13 +++++-------- .../trashed_local_asset.repository.dart | 12 ++++++++++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/mobile/lib/domain/services/local_sync.service.dart b/mobile/lib/domain/services/local_sync.service.dart index 320e01eaa3..fe6c73d8a1 100644 --- a/mobile/lib/domain/services/local_sync.service.dart +++ b/mobile/lib/domain/services/local_sync.service.dart @@ -96,6 +96,10 @@ class LocalSyncService { try { final Stopwatch stopwatch = Stopwatch()..start(); + if (CurrentPlatform.isAndroid) { + await _syncTrashedAssets(sinceLastCheckpoint: false); + } + final deviceAlbums = await _nativeSyncApi.getAlbums(); final dbAlbums = await _localAlbumRepository.getAll(sortBy: {SortLocalAlbumsBy.id}); @@ -107,9 +111,6 @@ class LocalSyncService { onlyFirst: removeAlbum, onlySecond: addAlbum, ); - if (CurrentPlatform.isAndroid) { - await _syncTrashedAssets(sinceLastCheckpoint: false); - } await _nativeSyncApi.checkpointSync(); stopwatch.stop(); @@ -309,11 +310,7 @@ class LocalSyncService { ); _log.fine("syncTrashedAssets, trashedAssets: ${trashedAssets.map((e) => e.asset.id)}"); - if (sinceLastCheckpoint) { - await _trashedLocalAssetRepository.applyDelta(trashedAssets); - } else { - await _trashedLocalAssetRepository.applySnapshot(trashedAssets); - } + await _trashedLocalAssetRepository.applyTrashedAssets(trashedAssets, sinceLastCheckpoint); final remoteAssetsToRestore = await _trashedLocalAssetRepository.getToRestore(); if (remoteAssetsToRestore.isNotEmpty) { diff --git a/mobile/lib/infrastructure/repositories/trashed_local_asset.repository.dart b/mobile/lib/infrastructure/repositories/trashed_local_asset.repository.dart index 5e794a3f49..d29d638b96 100644 --- a/mobile/lib/infrastructure/repositories/trashed_local_asset.repository.dart +++ b/mobile/lib/infrastructure/repositories/trashed_local_asset.repository.dart @@ -56,10 +56,18 @@ class DriftTrashedLocalAssetRepository extends DriftDatabaseRepository { return rows.map((result) => result.readTable(_db.trashedLocalAssetEntity).toLocalAsset()); } + Future applyTrashedAssets(Iterable trashedAssets, bool asDelta) async { + if (asDelta){ + return _applyDelta(trashedAssets); + } else { + return _applySnapshot(trashedAssets); + } + } + /// Applies resulted snapshot of trashed assets: /// - upserts incoming rows /// - deletes rows that are not present in the snapshot - Future applySnapshot(Iterable trashedAssets) async { + Future _applySnapshot(Iterable trashedAssets) async { if (trashedAssets.isEmpty) { await _db.delete(_db.trashedLocalAssetEntity).go(); return; @@ -110,7 +118,7 @@ class DriftTrashedLocalAssetRepository extends DriftDatabaseRepository { }); } - Future applyDelta(Iterable trashedAssets) async { + Future _applyDelta(Iterable trashedAssets) async { if (trashedAssets.isEmpty) { return; }