Prevent duplication of images within iOS

This commit is contained in:
Vishnu Mohandas 2020-05-27 22:01:12 +05:30
parent 6990faa451
commit 650e2f3161
3 changed files with 34 additions and 29 deletions

View file

@ -91,23 +91,11 @@ class Photo {
return o is Photo &&
o.generatedId == generatedId &&
o.uploadedFileId == uploadedFileId &&
o.localId == localId &&
o.title == title &&
o.deviceFolder == deviceFolder &&
o.remotePath == remotePath &&
o.createTimestamp == createTimestamp &&
o.updateTimestamp == updateTimestamp;
o.localId == localId;
}
@override
int get hashCode {
return generatedId.hashCode ^
uploadedFileId.hashCode ^
localId.hashCode ^
title.hashCode ^
deviceFolder.hashCode ^
remotePath.hashCode ^
createTimestamp.hashCode ^
updateTimestamp.hashCode;
return generatedId.hashCode ^ uploadedFileId.hashCode ^ localId.hashCode;
}
}

View file

@ -14,7 +14,10 @@ class PhotoProvider {
final filterOptionGroup = FilterOptionGroup();
filterOptionGroup.setOption(AssetType.image, FilterOption(needTitle: true));
var galleryList = await PhotoManager.getAssetPathList(
type: RequestType.image, filterOption: filterOptionGroup);
hasAll: true,
type: RequestType.image,
filterOption: filterOptionGroup,
);
galleryList.sort((s1, s2) {
return s2.assetCount.compareTo(s1.assetCount);

View file

@ -54,23 +54,18 @@ class PhotoSyncManager {
await PhotoProvider.instance.refreshGalleryList();
final pathEntities = PhotoProvider.instance.list;
final photos = List<Photo>();
AssetPathEntity recents;
for (AssetPathEntity pathEntity in pathEntities) {
if (Platform.isIOS || pathEntity.name != "Recent") {
// "Recents" contain duplicate information on Android
var assetList = await pathEntity.assetList;
for (AssetEntity entity in assetList) {
if (max(entity.createDateTime.microsecondsSinceEpoch,
entity.modifiedDateTime.microsecondsSinceEpoch) >
lastDBUpdateTimestamp) {
try {
photos.add(await Photo.fromAsset(pathEntity, entity));
} catch (e) {
_logger.severe(e);
}
}
}
if (pathEntity.name == "Recent" || pathEntity.name == "Recents") {
recents = pathEntity;
} else {
await _addToPhotos(pathEntity, lastDBUpdateTimestamp, photos);
}
}
if (recents != null) {
await _addToPhotos(recents, lastDBUpdateTimestamp, photos);
}
if (photos.isEmpty) {
_isSyncInProgress = false;
_syncWithRemote(prefs);
@ -84,6 +79,25 @@ class PhotoSyncManager {
}
}
Future _addToPhotos(AssetPathEntity pathEntity, int lastDBUpdateTimestamp,
List<Photo> photos) async {
final assetList = await pathEntity.assetList;
for (AssetEntity entity in assetList) {
if (max(entity.createDateTime.microsecondsSinceEpoch,
entity.modifiedDateTime.microsecondsSinceEpoch) >
lastDBUpdateTimestamp) {
try {
final photo = await Photo.fromAsset(pathEntity, entity);
if (!photos.contains(photo)) {
photos.add(photo);
}
} catch (e) {
_logger.severe(e);
}
}
}
}
Future<void> _syncWithRemote(SharedPreferences prefs) {
// TODO: Fix race conditions triggered due to concurrent syncs.
// Add device_id/last_sync_timestamp to the upload request?