2024-09-20 15:32:37 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
import 'package:immich_mobile/entities/album.entity.dart';
|
2025-03-12 19:26:56 +05:30
|
|
|
import 'package:immich_mobile/infrastructure/entities/user.entity.dart';
|
2025-06-19 18:25:18 -05:00
|
|
|
import 'package:immich_mobile/infrastructure/repositories/user.repository.dart';
|
2025-03-12 19:26:56 +05:30
|
|
|
import 'package:immich_mobile/providers/infrastructure/user.provider.dart';
|
2024-09-20 15:32:37 +02:00
|
|
|
import 'package:immich_mobile/repositories/asset.repository.dart';
|
|
|
|
|
|
|
|
class EntityService {
|
2025-06-21 17:41:02 -05:00
|
|
|
final AssetRepository _assetRepository;
|
2025-06-19 18:25:18 -05:00
|
|
|
final IsarUserRepository _isarUserRepository;
|
2025-06-25 13:06:24 +05:30
|
|
|
const EntityService(
|
2024-09-20 15:32:37 +02:00
|
|
|
this._assetRepository,
|
2025-06-19 18:25:18 -05:00
|
|
|
this._isarUserRepository,
|
2024-09-20 15:32:37 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
Future<Album> fillAlbumWithDatabaseEntities(Album album) async {
|
|
|
|
final ownerId = album.ownerId;
|
|
|
|
if (ownerId != null) {
|
|
|
|
// replace owner with user from database
|
2025-06-19 18:25:18 -05:00
|
|
|
final user = await _isarUserRepository.getByUserId(ownerId);
|
2025-03-12 19:26:56 +05:30
|
|
|
album.owner.value = user == null ? null : User.fromDto(user);
|
2024-09-20 15:32:37 +02:00
|
|
|
}
|
|
|
|
final thumbnailAssetId =
|
|
|
|
album.remoteThumbnailAssetId ?? album.thumbnail.value?.remoteId;
|
|
|
|
if (thumbnailAssetId != null) {
|
|
|
|
// set thumbnail with asset from database
|
|
|
|
album.thumbnail.value =
|
|
|
|
await _assetRepository.getByRemoteId(thumbnailAssetId);
|
|
|
|
}
|
|
|
|
if (album.remoteUsers.isNotEmpty) {
|
|
|
|
// replace all users with users from database
|
2025-06-19 18:25:18 -05:00
|
|
|
final users = await _isarUserRepository
|
2025-03-12 19:26:56 +05:30
|
|
|
.getByUserIds(album.remoteUsers.map((user) => user.id).toList());
|
2024-09-20 15:32:37 +02:00
|
|
|
album.sharedUsers.clear();
|
2025-03-12 19:26:56 +05:30
|
|
|
album.sharedUsers.addAll(users.nonNulls.map(User.fromDto));
|
2024-10-10 15:44:14 +07:00
|
|
|
album.shared = true;
|
2024-09-20 15:32:37 +02:00
|
|
|
}
|
|
|
|
if (album.remoteAssets.isNotEmpty) {
|
|
|
|
// replace all assets with assets from database
|
|
|
|
final assets = await _assetRepository
|
|
|
|
.getAllByRemoteId(album.remoteAssets.map((asset) => asset.remoteId!));
|
|
|
|
album.assets.clear();
|
|
|
|
album.assets.addAll(assets);
|
|
|
|
}
|
|
|
|
return album;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final entityServiceProvider = Provider(
|
|
|
|
(ref) => EntityService(
|
|
|
|
ref.watch(assetRepositoryProvider),
|
|
|
|
ref.watch(userRepositoryProvider),
|
|
|
|
),
|
|
|
|
);
|