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';
|
|
|
|
import 'package:immich_mobile/modules/album/services/album.service.dart';
|
2022-11-08 19:00:24 +02:00
|
|
|
import 'package:immich_mobile/shared/models/asset.dart';
|
2023-02-06 09:13:32 +02:00
|
|
|
import 'package:immich_mobile/shared/models/album.dart';
|
2023-03-04 00:38:30 +02:00
|
|
|
import 'package:immich_mobile/shared/models/store.dart';
|
|
|
|
import 'package:immich_mobile/shared/models/user.dart';
|
|
|
|
import 'package:immich_mobile/shared/providers/db.provider.dart';
|
|
|
|
import 'package:isar/isar.dart';
|
2022-08-03 07:04:34 +02:00
|
|
|
|
2023-02-06 09:13:32 +02: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-04 00:38:30 +02:00
|
|
|
.filter()
|
2023-05-17 19:36:02 +02:00
|
|
|
.owner((q) => q.isarIdEqualTo(Store.get(StoreKey.currentUser).isarId));
|
|
|
|
query.findAll().then((value) => state = value);
|
|
|
|
_streamSub = query.watch().listen((data) => state = data);
|
2022-08-03 07:04:34 +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),
|
|
|
|
]);
|
|
|
|
|
|
|
|
Future<bool> deleteAlbum(Album album) => _albumService.deleteAlbum(album);
|
2022-08-03 07:04:34 +02:00
|
|
|
|
2023-02-06 09:13:32 +02:00
|
|
|
Future<Album?> createAlbum(
|
2022-08-03 22:36:12 +02:00
|
|
|
String albumTitle,
|
2022-11-08 19:00:24 +02:00
|
|
|
Set<Asset> assets,
|
2023-05-17 19:36:02 +02:00
|
|
|
) =>
|
|
|
|
_albumService.createAlbum(albumTitle, assets, []);
|
|
|
|
|
|
|
|
@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-04 00:38:30 +02:00
|
|
|
ref.watch(dbProvider),
|
2022-10-17 14:53:27 +02:00
|
|
|
);
|
2022-08-03 07:04:34 +02:00
|
|
|
});
|