import 'package:immich_mobile/domain/entities/asset_isar.entity.dart' as entity; import 'package:immich_mobile/domain/interfaces/asset.interface.dart'; import 'package:immich_mobile/domain/models/asset.model.dart'; import 'package:isar/isar.dart'; class AssetIsarRepository implements IAssetRepository { final Isar db; const AssetIsarRepository({required this.db}); @override Future deleteAll() async { await db.writeTxn(() async { await db.assets.clear(); }); return true; } @override Future deleteIds(Iterable ids) async { await db.writeTxn(() async { await db.assets.deleteAll(ids.toList()); }); } @override Future> getAll({int? offset, int? limit}) async { return await db.assets .where() .offset(offset ?? 0) .limit(limit ?? 100) .findAll() .then((value) => value.map(_toModel).toList()); } @override Future> getForHashes(Iterable hashes) async { return await db.assets .where() .filter() .anyOf(hashes, (asset, hash) => asset.hashEqualTo(hash)) .findAll() .then((value) => value.map(_toModel).toList()); } @override Future> getForLocalIds(Iterable localIds) async { return await db.assets .where() .filter() .anyOf(localIds, (asset, localId) => asset.localIdEqualTo(localId)) .findAll() .then((value) => value.map(_toModel).toList()); } @override Future> getForRemoteIds(Iterable remoteIds) async { return await db.assets .where() .filter() .anyOf(remoteIds, (asset, remoteId) => asset.remoteIdEqualTo(remoteId)) .findAll() .then((value) => value.map(_toModel).toList()); } @override Future upsertAll(Iterable assets) async { await db.writeTxn(() async { await db.assets.putAll(assets.toEntity()); }); return true; } @override Future upsert(Asset assets) async { await db.writeTxn(() async { await db.assets.put(assets.toEntity()); }); return true; } } Asset _toModel(entity.Asset entity) { return Asset( id: entity.id, name: entity.name, hash: entity.hash, height: entity.height, width: entity.width, type: entity.type, createdTime: entity.createdTime, modifiedTime: entity.modifiedTime, duration: entity.duration, localId: entity.localId, remoteId: entity.remoteId, livePhotoVideoId: entity.livePhotoVideoId, ); } extension on Asset { entity.Asset toEntity() { return entity.Asset( id: id, name: name, hash: hash, height: height, width: width, type: type, createdTime: createdTime, modifiedTime: modifiedTime, duration: duration, localId: localId, remoteId: remoteId, livePhotoVideoId: livePhotoVideoId, ); } } extension on Iterable { List toEntity() { return map((asset) => entity.Asset( id: asset.id, name: asset.name, hash: asset.hash, height: asset.height, width: asset.width, type: asset.type, createdTime: asset.createdTime, modifiedTime: asset.modifiedTime, duration: asset.duration, localId: asset.localId, remoteId: asset.remoteId, livePhotoVideoId: asset.livePhotoVideoId, )).toList(); } }