Update photo when it was updated on remote

This commit is contained in:
Vishnu Mohandas 2020-06-15 05:02:37 +05:30
parent 464829e650
commit d196284dfd
2 changed files with 30 additions and 3 deletions

View file

@ -149,6 +149,20 @@ class PhotoDB {
}
}
Future<Photo> 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<int> updatePhoto(
int generatedId, int uploadedId, String remotePath, int updateTimestamp,
[String thumbnailPath]) async {

View file

@ -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);
}
}