From d196284dfd861252c876a1fb344d647e036ecbc4 Mon Sep 17 00:00:00 2001 From: Vishnu Mohandas Date: Mon, 15 Jun 2020 05:02:37 +0530 Subject: [PATCH] Update photo when it was updated on remote --- lib/db/photo_db.dart | 14 ++++++++++++++ lib/folder_service.dart | 19 ++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/db/photo_db.dart b/lib/db/photo_db.dart index 269b44aba..6a73c3a1a 100644 --- a/lib/db/photo_db.dart +++ b/lib/db/photo_db.dart @@ -149,6 +149,20 @@ class PhotoDB { } } + Future getMatchingRemotePhoto(int uploadedFileId) async { + final db = await instance.database; + final rows = await db.query( + table, + where: '$columnUploadedFileId=?', + whereArgs: [uploadedFileId], + ); + if (rows.isNotEmpty) { + return _getPhotoFromRow(rows[0]); + } else { + throw ("No matching photo found"); + } + } + Future updatePhoto( int generatedId, int uploadedId, String remotePath, int updateTimestamp, [String thumbnailPath]) async { diff --git a/lib/folder_service.dart b/lib/folder_service.dart index d3e38fb25..e50e209ae 100644 --- a/lib/folder_service.dart +++ b/lib/folder_service.dart @@ -57,9 +57,22 @@ class FolderSharingService { } catch (e) { // Folder has never been synced } - var photos = await getDiff(folder.id, lastSyncTimestamp, _diffLimit); - await PhotoDB.instance.insertPhotos(photos); - if (photos.length == _diffLimit) { + var diff = await getDiff(folder.id, lastSyncTimestamp, _diffLimit); + for (Photo photo in diff) { + try { + var existingPhoto = + await PhotoDB.instance.getMatchingRemotePhoto(photo.uploadedFileId); + await PhotoDB.instance.updatePhoto( + existingPhoto.generatedId, + photo.uploadedFileId, + photo.remotePath, + photo.updateTimestamp, + photo.thumbnailPath); + } catch (e) { + await PhotoDB.instance.insertPhoto(photo); + } + } + if (diff.length == _diffLimit) { await syncDiff(folder); } }