diff --git a/mobile/lib/domain/models/asset/local_asset.model.dart b/mobile/lib/domain/models/asset/local_asset.model.dart index 9cd20acb0a..0e7bfc7330 100644 --- a/mobile/lib/domain/models/asset/local_asset.model.dart +++ b/mobile/lib/domain/models/asset/local_asset.model.dart @@ -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 ?? ""}, + cloudId: ${cloudId ?? ""}, 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, diff --git a/mobile/lib/domain/services/local_sync.service.dart b/mobile/lib/domain/services/local_sync.service.dart index 5ab10bdf09..ebb4adb382 100644 --- a/mobile/lib/domain/services/local_sync.service.dart +++ b/mobile/lib/domain/services/local_sync.service.dart @@ -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(); diff --git a/mobile/lib/infrastructure/entities/local_asset.entity.dart b/mobile/lib/infrastructure/entities/local_asset.entity.dart index 337a6d728d..29e1fb4629 100644 --- a/mobile/lib/infrastructure/entities/local_asset.entity.dart +++ b/mobile/lib/infrastructure/entities/local_asset.entity.dart @@ -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 get primaryKey => {id}; } diff --git a/mobile/lib/infrastructure/repositories/db.repository.dart b/mobile/lib/infrastructure/repositories/db.repository.dart index f8de114f85..a6d80032e7 100644 --- a/mobile/lib/infrastructure/repositories/db.repository.dart +++ b/mobile/lib/infrastructure/repositories/db.repository.dart @@ -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, diff --git a/mobile/lib/infrastructure/repositories/local_album.repository.dart b/mobile/lib/infrastructure/repositories/local_album.repository.dart index 923d6e0a68..8d9a48992b 100644 --- a/mobile/lib/infrastructure/repositories/local_album.repository.dart +++ b/mobile/lib/infrastructure/repositories/local_album.repository.dart @@ -231,6 +231,25 @@ class DriftLocalAlbumRepository extends DriftDatabaseRepository { return query.map((row) => row.readTable(_db.localAssetEntity).toDto()).get(); } + Future updateCloudMapping(Map 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 _upsertAssets(Iterable localAssets) { if (localAssets.isEmpty) { return Future.value();