2023-05-17 19:36:02 +02:00
|
|
|
import 'dart:async';
|
|
|
|
|
2022-08-03 07:04:34 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2024-05-02 22:59:14 +02:00
|
|
|
import 'package:immich_mobile/services/album.service.dart';
|
2024-05-07 06:04:21 +02:00
|
|
|
import 'package:immich_mobile/widgets/asset_grid/asset_grid_data_structure.dart';
|
2024-05-01 04:36:40 +02:00
|
|
|
import 'package:immich_mobile/entities/asset.entity.dart';
|
|
|
|
import 'package:immich_mobile/entities/album.entity.dart';
|
|
|
|
import 'package:immich_mobile/entities/store.entity.dart';
|
|
|
|
import 'package:immich_mobile/entities/user.entity.dart';
|
2024-05-02 22:59:14 +02:00
|
|
|
import 'package:immich_mobile/providers/db.provider.dart';
|
2023-12-07 16:38:22 +01:00
|
|
|
import 'package:immich_mobile/utils/renderlist_generator.dart';
|
2023-03-03 23:38:30 +01:00
|
|
|
import 'package:isar/isar.dart';
|
2022-08-03 07:04:34 +02:00
|
|
|
|
2023-02-06 08:13:32 +01:00
|
|
|
class AlbumNotifier extends StateNotifier<List<Album>> {
|
2023-05-17 19:36:02 +02:00
|
|
|
AlbumNotifier(this._albumService, Isar db) : super([]) {
|
|
|
|
final query = db.albums
|
2023-03-03 23:38:30 +01:00
|
|
|
.filter()
|
2023-05-17 19:36:02 +02:00
|
|
|
.owner((q) => q.isarIdEqualTo(Store.get(StoreKey.currentUser).isarId));
|
2024-01-10 16:31:54 +01:00
|
|
|
query.findAll().then((value) {
|
|
|
|
if (mounted) {
|
|
|
|
state = value;
|
|
|
|
}
|
|
|
|
});
|
2023-05-17 19:36:02 +02:00
|
|
|
_streamSub = query.watch().listen((data) => state = data);
|
2022-08-03 07:04:34 +02:00
|
|
|
}
|
2024-08-26 20:21:19 +02:00
|
|
|
|
2023-05-17 19:36:02 +02:00
|
|
|
final AlbumService _albumService;
|
|
|
|
late final StreamSubscription<List<Album>> _streamSub;
|
2022-08-03 07:04:34 +02:00
|
|
|
|
2023-05-17 19:36:02 +02:00
|
|
|
Future<void> getAllAlbums() => Future.wait([
|
|
|
|
_albumService.refreshDeviceAlbums(),
|
|
|
|
_albumService.refreshRemoteAlbums(isShared: false),
|
|
|
|
]);
|
|
|
|
|
2023-11-14 21:30:27 +01:00
|
|
|
Future<void> getDeviceAlbums() => _albumService.refreshDeviceAlbums();
|
|
|
|
|
2023-05-17 19:36:02 +02:00
|
|
|
Future<bool> deleteAlbum(Album album) => _albumService.deleteAlbum(album);
|
2022-08-03 07:04:34 +02:00
|
|
|
|
2023-02-06 08:13:32 +01:00
|
|
|
Future<Album?> createAlbum(
|
2022-08-03 22:36:12 +02:00
|
|
|
String albumTitle,
|
2022-11-08 18:00:24 +01:00
|
|
|
Set<Asset> assets,
|
2023-05-17 19:36:02 +02:00
|
|
|
) =>
|
|
|
|
_albumService.createAlbum(albumTitle, assets, []);
|
|
|
|
|
2024-08-26 20:21:19 +02:00
|
|
|
Future<Album?> getAlbumByName(String albumName, {bool remoteOnly = false}) =>
|
|
|
|
_albumService.getAlbumByName(albumName, remoteOnly);
|
|
|
|
|
|
|
|
/// Create an album on the server with the same name as the selected album for backup
|
|
|
|
/// First this will check if the album already exists on the server with name
|
|
|
|
/// If it does not exist, it will create the album on the server
|
|
|
|
Future<void> createSyncAlbum(
|
|
|
|
String albumName,
|
|
|
|
) async {
|
|
|
|
final album = await getAlbumByName(albumName, remoteOnly: true);
|
|
|
|
if (album != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await createAlbum(albumName, {});
|
|
|
|
}
|
|
|
|
|
2023-05-17 19:36:02 +02:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_streamSub.cancel();
|
|
|
|
super.dispose();
|
2022-08-03 07:04:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-17 19:36:02 +02:00
|
|
|
final albumProvider =
|
|
|
|
StateNotifierProvider.autoDispose<AlbumNotifier, List<Album>>((ref) {
|
2022-10-17 14:53:27 +02:00
|
|
|
return AlbumNotifier(
|
|
|
|
ref.watch(albumServiceProvider),
|
2023-03-03 23:38:30 +01:00
|
|
|
ref.watch(dbProvider),
|
2022-10-17 14:53:27 +02:00
|
|
|
);
|
2022-08-03 07:04:34 +02:00
|
|
|
});
|
2023-12-07 16:38:22 +01:00
|
|
|
|
|
|
|
final albumWatcher =
|
|
|
|
StreamProvider.autoDispose.family<Album, int>((ref, albumId) async* {
|
|
|
|
final db = ref.watch(dbProvider);
|
|
|
|
final a = await db.albums.get(albumId);
|
|
|
|
if (a != null) yield a;
|
|
|
|
await for (final a in db.albums.watchObject(albumId, fireImmediately: true)) {
|
|
|
|
if (a != null) yield a;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
final albumRenderlistProvider =
|
|
|
|
StreamProvider.autoDispose.family<RenderList, int>((ref, albumId) {
|
|
|
|
final album = ref.watch(albumWatcher(albumId)).value;
|
|
|
|
if (album != null) {
|
|
|
|
final query =
|
|
|
|
album.assets.filter().isTrashedEqualTo(false).sortByFileCreatedAtDesc();
|
|
|
|
return renderListGeneratorWithGroupBy(query, GroupAssetsBy.none);
|
|
|
|
}
|
|
|
|
return const Stream.empty();
|
|
|
|
});
|