Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(mobile): repositories for album service #12701

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mobile/lib/entities/album.entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,13 @@ class Album {
}

extension AssetsHelper on IsarCollection<Album> {
Future<void> store(Album a) async {
Future<Album> store(Album a) async {
await put(a);
await a.owner.save();
await a.thumbnail.save();
await a.sharedUsers.save();
await a.assets.save();
return a;
}
}

Expand Down
23 changes: 23 additions & 0 deletions mobile/lib/interfaces/album.interface.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:immich_mobile/entities/album.entity.dart';
import 'package:immich_mobile/entities/asset.entity.dart';
import 'package:immich_mobile/entities/user.entity.dart';

abstract interface class IAlbumRepository {
Future<int> countLocal();
fyfrey marked this conversation as resolved.
Show resolved Hide resolved
Future<Album> create(Album album);
Future<Album?> getById(int id);
Future<Album?> getByName(
String name, {
bool? shared,
bool? remote,
});
Future<Album> update(Album album);
Future<void> delete(Album album);
Future<List<Album>> getAll({bool? shared});
Future<void> removeUsers(Album album, List<User> users);
Future<void> addAssets(Album album, List<Asset> assets);
Future<void> removeAssets(Album album, List<Asset> assets);
fyfrey marked this conversation as resolved.
Show resolved Hide resolved
Future<DateTime?> getStartDate(Album album);
Future<DateTime?> getEndDate(Album album);
Future<DateTime?> getLastModified(Album album);
}
8 changes: 8 additions & 0 deletions mobile/lib/interfaces/asset.interface.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'package:immich_mobile/entities/album.entity.dart';
import 'package:immich_mobile/entities/asset.entity.dart';
import 'package:immich_mobile/entities/user.entity.dart';

abstract interface class IAssetRepository {
Future<List<Asset>> getByAlbumWithOwnerUnequal(Album album, User user);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name is a bit unwieldy, maybe just getAlbums({ notOwnedBy}). Also why do you need to get albums not owned by the user? Is that essentially shared with user? Would albumRepo.getSharedWithMe work as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should actually return the Assets belonging to a specific album but not owned by user x. I've modified the signature; it's now easier to read

Future<void> deleteById(List<int> ids);
}
5 changes: 5 additions & 0 deletions mobile/lib/interfaces/backupalbum.interface.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:immich_mobile/entities/backup_album.entity.dart';

abstract interface class IBackupAlbumRepository {
Future<List<String>> getIdsBySelection(BackupSelection backup);
}
5 changes: 5 additions & 0 deletions mobile/lib/interfaces/user.interface.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:immich_mobile/entities/user.entity.dart';

abstract interface class IUserRepository {
Future<List<User>> getByIds(List<String> ids);
}
83 changes: 83 additions & 0 deletions mobile/lib/repositories/album.repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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<int> countLocal() => _db.albums.where().localIdIsNotNull().count();

@override
Future<Album> create(Album album) =>
_db.writeTxn(() => _db.albums.store(album));

@override
Future<Album?> 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<Album> update(Album album) =>
_db.writeTxn(() => _db.albums.store(album));

@override
Future<void> delete(Album album) =>
_db.writeTxn(() => _db.albums.delete(album.id));
fyfrey marked this conversation as resolved.
Show resolved Hide resolved

@override
Future<List<Album>> getAll({bool? shared}) {
final baseQuery = _db.albums.filter();
QueryBuilder<Album, Album, QAfterFilterCondition>? query;
if (shared != null) {
query = baseQuery.sharedEqualTo(true);
}
return query?.findAll() ?? _db.albums.where().findAll();
}

@override
Future<Album?> getById(int id) => _db.albums.get(id);

@override
Future<void> removeUsers(Album album, List<User> users) =>
_db.writeTxn(() => album.sharedUsers.update(unlink: users));

@override
Future<void> addAssets(Album album, List<Asset> assets) =>
_db.writeTxn(() => album.assets.update(link: assets));

@override
Future<void> removeAssets(Album album, List<Asset> assets) =>
_db.writeTxn(() => album.assets.update(unlink: assets));

@override
Future<DateTime?> getStartDate(Album album) =>
album.assets.filter().fileCreatedAtProperty().min();
@override
Future<DateTime?> getEndDate(Album album) =>
album.assets.filter().fileCreatedAtProperty().max();

@override
Future<DateTime?> getLastModified(Album album) =>
album.assets.filter().updatedAtProperty().max();
}
26 changes: 26 additions & 0 deletions mobile/lib/repositories/asset.repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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/asset.interface.dart';
import 'package:immich_mobile/providers/db.provider.dart';
import 'package:isar/isar.dart';

final assetRepositoryProvider =
Provider((ref) => AssetRepository(ref.watch(dbProvider)));

class AssetRepository implements IAssetRepository {
final Isar _db;

AssetRepository(
this._db,
);

@override
Future<List<Asset>> getByAlbumWithOwnerUnequal(Album album, User user) =>
album.assets.filter().not().ownerIdEqualTo(user.isarId).findAll();

@override
Future<void> deleteById(List<int> ids) =>
_db.writeTxn(() => _db.assets.deleteAll(ids));
}
20 changes: 20 additions & 0 deletions mobile/lib/repositories/backupalbum.repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/entities/backup_album.entity.dart';
import 'package:immich_mobile/interfaces/backupalbum.interface.dart';
import 'package:immich_mobile/providers/db.provider.dart';
import 'package:isar/isar.dart';

final backupAlbumRepositoryProvider =
Provider((ref) => BackupAlbumRepository(ref.watch(dbProvider)));

class BackupAlbumRepository implements IBackupAlbumRepository {
fyfrey marked this conversation as resolved.
Show resolved Hide resolved
final Isar _db;

BackupAlbumRepository(
this._db,
);

@override
Future<List<String>> getIdsBySelection(BackupSelection backup) =>
_db.backupAlbums.filter().selectionEqualTo(backup).idProperty().findAll();
}
20 changes: 20 additions & 0 deletions mobile/lib/repositories/user.repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/entities/user.entity.dart';
import 'package:immich_mobile/interfaces/user.interface.dart';
import 'package:immich_mobile/providers/db.provider.dart';
import 'package:isar/isar.dart';

final userRepositoryProvider =
Provider((ref) => UserRepository(ref.watch(dbProvider)));

class UserRepository implements IUserRepository {
final Isar _db;

UserRepository(
this._db,
);

@override
Future<List<User>> getByIds(List<String> ids) async =>
(await _db.users.getAllById(ids)).cast();
}
Loading
Loading