store cloudId in sqlite

This commit is contained in:
shenlong-tanwen 2025-07-30 02:54:53 +05:30
parent ecbaca3cee
commit 61e079a63e
5 changed files with 36 additions and 1 deletions

View File

@ -3,11 +3,13 @@ part of 'base_asset.model.dart';
class LocalAsset extends BaseAsset {
final String id;
final String? remoteId;
final String? cloudId;
final int orientation;
const LocalAsset({
required this.id,
this.remoteId,
this.cloudId,
required super.name,
super.checksum,
required super.type,
@ -31,6 +33,8 @@ class LocalAsset extends BaseAsset {
String toString() {
return '''LocalAsset {
id: $id,
remoteId: ${remoteId ?? "<NA>"},
cloudId: ${cloudId ?? "<NA>"},
name: $name,
type: $type,
createdAt: $createdAt,
@ -49,7 +53,7 @@ class LocalAsset extends BaseAsset {
bool operator ==(Object other) {
if (other is! LocalAsset) return false;
if (identical(this, other)) return true;
return super == other && id == other.id && orientation == other.orientation;
return super == other && id == other.id && orientation == other.orientation && cloudId == other.cloudId;
}
@override
@ -58,6 +62,7 @@ class LocalAsset extends BaseAsset {
LocalAsset copyWith({
String? id,
String? remoteId,
String? cloudId,
String? name,
String? checksum,
AssetType? type,
@ -72,6 +77,7 @@ class LocalAsset extends BaseAsset {
return LocalAsset(
id: id ?? this.id,
remoteId: remoteId ?? this.remoteId,
cloudId: cloudId ?? this.cloudId,
name: name ?? this.name,
checksum: checksum ?? this.checksum,
type: type ?? this.type,

View File

@ -73,6 +73,12 @@ class LocalSyncService {
}
await updateAlbum(dbAlbum, album);
}
final newAssetIds = delta.updates.map((e) => e.id).toList();
if (newAssetIds.isNotEmpty) {
final cloudMapping = await _nativeSyncApi.getCloudIdForAssetIds(newAssetIds);
await _localAlbumRepository.updateCloudMapping(cloudMapping);
}
}
await _nativeSyncApi.checkpointSync();

View File

@ -16,6 +16,8 @@ class LocalAssetEntity extends Table with DriftDefaultsMixin, AssetEntityMixin {
IntColumn get orientation => integer().withDefault(const Constant(0))();
TextColumn get cloudId => text().nullable()();
@override
Set<Column> get primaryKey => {id};
}

View File

@ -97,6 +97,8 @@ class Drift extends $Drift implements IDatabaseRepository {
await m.create(v4.assetFaceEntity);
},
from4To5: (m, v5) async {
// Add cloudId column to local_asset_entity
await m.addColumn(v5.localAssetEntity, v5.localAssetEntity.cloudId);
await m.alterTable(
TableMigration(
v5.userEntity,

View File

@ -231,6 +231,25 @@ class DriftLocalAlbumRepository extends DriftDatabaseRepository {
return query.map((row) => row.readTable(_db.localAssetEntity).toDto()).get();
}
Future<void> updateCloudMapping(Map<String, String?> cloudMapping) {
if (cloudMapping.isEmpty) {
return Future.value();
}
return _db.batch((batch) {
for (final entry in cloudMapping.entries) {
final assetId = entry.key;
final cloudId = entry.value;
batch.update(
_db.localAssetEntity,
LocalAssetEntityCompanion(cloudId: Value(cloudId)),
where: (f) => f.id.equals(assetId),
);
}
});
}
Future<void> _upsertAssets(Iterable<LocalAsset> localAssets) {
if (localAssets.isEmpty) {
return Future.value();