feat: full local assets / album sync
This commit is contained in:
@@ -5,24 +5,31 @@ import 'package:immich_mobile/domain/entities/asset.entity.drift.dart';
|
||||
import 'package:immich_mobile/domain/interfaces/asset.interface.dart';
|
||||
import 'package:immich_mobile/domain/models/asset.model.dart';
|
||||
import 'package:immich_mobile/domain/repositories/database.repository.dart';
|
||||
import 'package:immich_mobile/domain/utils/drift_model_converters.dart';
|
||||
import 'package:immich_mobile/utils/mixins/log.mixin.dart';
|
||||
|
||||
class AssetDriftRepository with LogMixin implements IAssetRepository {
|
||||
class AssetRepository with LogMixin implements IAssetRepository {
|
||||
final DriftDatabaseRepository _db;
|
||||
|
||||
const AssetDriftRepository(this._db);
|
||||
const AssetRepository(this._db);
|
||||
|
||||
@override
|
||||
Future<bool> upsertAll(Iterable<Asset> assets) async {
|
||||
try {
|
||||
await _db.batch((batch) => batch.insertAllOnConflictUpdate(
|
||||
await _db.batch((batch) {
|
||||
final rows = assets.map(_toEntity);
|
||||
for (final row in rows) {
|
||||
batch.insert(
|
||||
_db.asset,
|
||||
assets.map(_toEntity),
|
||||
));
|
||||
row,
|
||||
onConflict: DoUpdate((_) => row, target: [_db.asset.hash]),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
} catch (e, s) {
|
||||
log.e("Cannot insert remote assets into table", e, s);
|
||||
log.e("Cannot insert assets into table", e, s);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -33,7 +40,7 @@ class AssetDriftRepository with LogMixin implements IAssetRepository {
|
||||
await _db.asset.deleteAll();
|
||||
return true;
|
||||
} catch (e, s) {
|
||||
log.e("Cannot clear remote assets", e, s);
|
||||
log.e("Cannot clear assets", e, s);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -47,35 +54,45 @@ class AssetDriftRepository with LogMixin implements IAssetRepository {
|
||||
query.limit(limit, offset: offset);
|
||||
}
|
||||
|
||||
return (await query.get()).map(_toModel).toList();
|
||||
return (await query.map(DriftModelConverters.toAssetModel).get()).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<Asset>> getForLocalIds(List<String> localIds) async {
|
||||
Future<List<Asset>> getForLocalIds(Iterable<String> localIds) async {
|
||||
final query = _db.asset.select()
|
||||
..where((row) => row.localId.isIn(localIds))
|
||||
..orderBy([(asset) => OrderingTerm.asc(asset.localId)]);
|
||||
..orderBy([(asset) => OrderingTerm.asc(asset.hash)]);
|
||||
|
||||
return (await query.get()).map(_toModel).toList();
|
||||
return (await query.get()).map(DriftModelConverters.toAssetModel).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<Asset>> getForRemoteIds(List<String> remoteIds) async {
|
||||
Future<List<Asset>> getForRemoteIds(Iterable<String> remoteIds) async {
|
||||
final query = _db.asset.select()
|
||||
..where((row) => row.remoteId.isIn(remoteIds))
|
||||
..orderBy([(asset) => OrderingTerm.asc(asset.remoteId)]);
|
||||
..orderBy([(asset) => OrderingTerm.asc(asset.hash)]);
|
||||
|
||||
return (await query.get()).map(_toModel).toList();
|
||||
return (await query.get()).map(DriftModelConverters.toAssetModel).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
FutureOr<void> deleteIds(List<int> ids) async {
|
||||
Future<List<Asset>> getForHashes(Iterable<String> hashes) async {
|
||||
final query = _db.asset.select()
|
||||
..where((row) => row.hash.isIn(hashes))
|
||||
..orderBy([(asset) => OrderingTerm.asc(asset.hash)]);
|
||||
|
||||
return (await query.get()).map(DriftModelConverters.toAssetModel).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
FutureOr<void> deleteIds(Iterable<int> ids) async {
|
||||
await _db.asset.deleteWhere((row) => row.id.isIn(ids));
|
||||
}
|
||||
}
|
||||
|
||||
AssetCompanion _toEntity(Asset asset) {
|
||||
return AssetCompanion.insert(
|
||||
id: Value.absentIfNull(asset.id),
|
||||
localId: Value(asset.localId),
|
||||
remoteId: Value(asset.remoteId),
|
||||
name: asset.name,
|
||||
@@ -89,20 +106,3 @@ AssetCompanion _toEntity(Asset asset) {
|
||||
livePhotoVideoId: Value(asset.livePhotoVideoId),
|
||||
);
|
||||
}
|
||||
|
||||
Asset _toModel(AssetData asset) {
|
||||
return Asset(
|
||||
id: asset.id,
|
||||
localId: asset.localId,
|
||||
remoteId: asset.remoteId,
|
||||
name: asset.name,
|
||||
type: asset.type,
|
||||
hash: asset.hash,
|
||||
createdTime: asset.createdTime,
|
||||
modifiedTime: asset.modifiedTime,
|
||||
height: asset.height,
|
||||
width: asset.width,
|
||||
livePhotoVideoId: asset.livePhotoVideoId,
|
||||
duration: asset.duration,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user