feat: full local assets / album sync

This commit is contained in:
shenlong-tanwen
2024-10-17 23:33:00 +05:30
parent a09710ec7b
commit c91a2878dc
87 changed files with 2417 additions and 366 deletions
+26 -18
View File
@@ -12,7 +12,7 @@ enum AssetType {
}
class Asset {
final int id;
final int? id;
final String name;
final String hash;
final int? height;
@@ -32,9 +32,10 @@ class Asset {
bool get isRemote => remoteId != null;
bool get isLocal => localId != null;
bool get isMerged => isRemote && isLocal;
bool get isImage => type == AssetType.image;
const Asset({
required this.id,
this.id,
required this.name,
required this.hash,
this.height,
@@ -49,7 +50,6 @@ class Asset {
});
factory Asset.remote(AssetResponseDto dto) => Asset(
id: 0, // assign a temporary auto gen ID
remoteId: dto.id,
createdTime: dto.fileCreatedAt,
duration: dto.duration.tryParseInt() ?? 0,
@@ -93,29 +93,38 @@ class Asset {
}
Asset merge(Asset newAsset) {
if (newAsset.modifiedTime.isAfter(modifiedTime)) {
final existingAsset = this;
assert(existingAsset.id != null, "Existing asset must be from the db");
final oldestCreationTime =
existingAsset.createdTime.isBefore(newAsset.createdTime)
? existingAsset.createdTime
: newAsset.createdTime;
if (newAsset.modifiedTime.isAfter(existingAsset.modifiedTime)) {
return newAsset.copyWith(
height: newAsset.height ?? height,
width: newAsset.width ?? width,
localId: () => newAsset.localId ?? localId,
remoteId: () => newAsset.remoteId ?? remoteId,
livePhotoVideoId: newAsset.livePhotoVideoId ?? livePhotoVideoId,
id: newAsset.id ?? existingAsset.id,
localId: () => existingAsset.localId ?? newAsset.localId,
remoteId: () => existingAsset.remoteId ?? newAsset.remoteId,
width: newAsset.width ?? existingAsset.width,
height: newAsset.height ?? existingAsset.height,
createdTime: oldestCreationTime,
);
}
return copyWith(
height: height ?? newAsset.height,
width: width ?? newAsset.width,
localId: () => localId ?? newAsset.localId,
remoteId: () => remoteId ?? newAsset.remoteId,
livePhotoVideoId: livePhotoVideoId ?? newAsset.livePhotoVideoId,
return existingAsset.copyWith(
localId: () => existingAsset.localId ?? newAsset.localId,
remoteId: () => existingAsset.remoteId ?? newAsset.remoteId,
width: existingAsset.width ?? newAsset.width,
height: existingAsset.height ?? newAsset.height,
createdTime: oldestCreationTime,
);
}
@override
String toString() => """
{
"id": "$id",
"id": "${id ?? "-"}",
"remoteId": "${remoteId ?? "-"}",
"localId": "${localId ?? "-"}",
"name": "$name",
@@ -163,8 +172,7 @@ class Asset {
livePhotoVideoId.hashCode;
}
static int compareByRemoteId(Asset a, Asset b) =>
CollectionUtil.compareToNullable(a.remoteId, b.remoteId);
static int compareByHash(Asset a, Asset b) => a.hash.compareTo(b.hash);
static int compareByLocalId(Asset a, Asset b) =>
CollectionUtil.compareToNullable(a.localId, b.localId);