2025-07-17 00:25:41 +03:00
|
|
|
import 'package:collection/collection.dart';
|
2025-06-06 11:23:05 +05:30
|
|
|
import 'package:drift/drift.dart';
|
|
|
|
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
|
2025-07-05 00:38:06 +05:30
|
|
|
import 'package:immich_mobile/infrastructure/entities/local_asset.entity.dart';
|
2025-06-06 11:23:05 +05:30
|
|
|
import 'package:immich_mobile/infrastructure/entities/local_asset.entity.drift.dart';
|
|
|
|
import 'package:immich_mobile/infrastructure/repositories/db.repository.dart';
|
|
|
|
|
2025-06-23 11:27:44 -05:00
|
|
|
class DriftLocalAssetRepository extends DriftDatabaseRepository {
|
2025-06-06 11:23:05 +05:30
|
|
|
final Drift _db;
|
|
|
|
const DriftLocalAssetRepository(this._db) : super(_db);
|
|
|
|
|
2025-07-05 00:38:06 +05:30
|
|
|
Stream<LocalAsset?> watchAsset(String id) {
|
|
|
|
final query = _db.localAssetEntity
|
|
|
|
.select()
|
2025-07-09 20:04:25 +05:30
|
|
|
.addColumns([_db.remoteAssetEntity.id]).join([
|
2025-07-05 00:38:06 +05:30
|
|
|
leftOuterJoin(
|
|
|
|
_db.remoteAssetEntity,
|
|
|
|
_db.localAssetEntity.checksum.equalsExp(_db.remoteAssetEntity.checksum),
|
|
|
|
useColumns: false,
|
|
|
|
),
|
|
|
|
])
|
|
|
|
..where(_db.localAssetEntity.id.equals(id));
|
|
|
|
|
|
|
|
return query.map((row) {
|
|
|
|
final asset = row.readTable(_db.localAssetEntity).toDto();
|
|
|
|
return asset.copyWith(
|
|
|
|
remoteId: row.read(_db.remoteAssetEntity.id),
|
|
|
|
);
|
|
|
|
}).watchSingleOrNull();
|
|
|
|
}
|
|
|
|
|
2025-06-06 11:23:05 +05:30
|
|
|
Future<void> updateHashes(Iterable<LocalAsset> hashes) {
|
|
|
|
if (hashes.isEmpty) {
|
|
|
|
return Future.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
return _db.batch((batch) async {
|
|
|
|
for (final asset in hashes) {
|
|
|
|
batch.update(
|
|
|
|
_db.localAssetEntity,
|
|
|
|
LocalAssetEntityCompanion(checksum: Value(asset.checksum)),
|
|
|
|
where: (e) => e.id.equals(asset.id),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2025-07-17 00:25:41 +03:00
|
|
|
|
|
|
|
Future<void> delete(List<String> ids) {
|
|
|
|
if (ids.isEmpty) {
|
|
|
|
return Future.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
return _db.batch((batch) {
|
|
|
|
for (final slice in ids.slices(32000)) {
|
|
|
|
batch.deleteWhere(_db.localAssetEntity, (e) => e.id.isIn(slice));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2025-06-06 11:23:05 +05:30
|
|
|
}
|