import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:immich_mobile/entities/album.entity.dart'; import 'package:immich_mobile/entities/asset.entity.dart'; import 'package:immich_mobile/entities/user.entity.dart'; import 'package:immich_mobile/interfaces/album.interface.dart'; import 'package:immich_mobile/providers/db.provider.dart'; import 'package:isar/isar.dart'; final albumRepositoryProvider = Provider((ref) => AlbumRepository(ref.watch(dbProvider))); class AlbumRepository implements IAlbumRepository { final Isar _db; AlbumRepository( this._db, ); @override Future count({bool? local}) { if (local == true) return _db.albums.where().localIdIsNotNull().count(); if (local == false) return _db.albums.where().remoteIdIsNotNull().count(); return _db.albums.count(); } @override Future create(Album album) => _db.writeTxn(() => _db.albums.store(album)); @override Future getByName(String name, {bool? shared, bool? remote}) { var query = _db.albums.filter().nameEqualTo(name); if (shared != null) { query = query.sharedEqualTo(shared); } if (remote == true) { query = query.localIdIsNull(); } else if (remote == false) { query = query.remoteIdIsNull(); } return query.findFirst(); } @override Future update(Album album) => _db.writeTxn(() => _db.albums.store(album)); @override Future delete(int albumId) => _db.writeTxn(() => _db.albums.delete(albumId)); @override Future> getAll({bool? shared}) { final baseQuery = _db.albums.filter(); QueryBuilder? query; if (shared != null) { query = baseQuery.sharedEqualTo(true); } return query?.findAll() ?? _db.albums.where().findAll(); } @override Future getById(int id) => _db.albums.get(id); @override Future removeUsers(Album album, List users) => _db.writeTxn(() => album.sharedUsers.update(unlink: users)); @override Future addAssets(Album album, List assets) => _db.writeTxn(() => album.assets.update(link: assets)); @override Future removeAssets(Album album, List assets) => _db.writeTxn(() => album.assets.update(unlink: assets)); @override Future recalculateMetadata(Album album) async { album.startDate = await album.assets.filter().fileCreatedAtProperty().min(); album.endDate = await album.assets.filter().fileCreatedAtProperty().max(); album.lastModifiedAssetTimestamp = await album.assets.filter().updatedAtProperty().max(); return album; } }