2023-05-17 19:36:02 +02:00
|
|
|
import 'dart:async';
|
|
|
|
|
2022-08-03 07:04:34 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-04-24 04:08:45 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2022-08-03 07:04:34 +02:00
|
|
|
import 'package:immich_mobile/modules/album/services/album.service.dart';
|
2023-02-06 09:13:32 +02:00
|
|
|
import 'package:immich_mobile/shared/models/album.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/user.dart';
|
2023-03-04 00:38:30 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/db.provider.dart';
|
|
|
|
import 'package:isar/isar.dart';
|
2022-04-24 04:08:45 +02:00
|
|
|
|
2023-02-06 09:13:32 +02:00
|
|
|
class SharedAlbumNotifier extends StateNotifier<List<Album>> {
|
2023-12-07 17:38:22 +02:00
|
|
|
SharedAlbumNotifier(this._albumService, Isar db) : super([]) {
|
2023-05-17 19:36:02 +02:00
|
|
|
final query = db.albums.filter().sharedEqualTo(true).sortByCreatedAtDesc();
|
2024-01-10 17:31:54 +02: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-04-24 04:08:45 +02:00
|
|
|
|
2023-01-29 05:57:13 +02:00
|
|
|
final AlbumService _albumService;
|
2023-05-17 19:36:02 +02:00
|
|
|
late final StreamSubscription<List<Album>> _streamSub;
|
2022-08-03 07:04:34 +02:00
|
|
|
|
2023-02-06 09:13:32 +02:00
|
|
|
Future<Album?> createSharedAlbum(
|
2022-08-03 07:04:34 +02:00
|
|
|
String albumName,
|
2023-02-06 09:13:32 +02:00
|
|
|
Iterable<Asset> assets,
|
|
|
|
Iterable<User> sharedUsers,
|
2022-08-03 07:04:34 +02:00
|
|
|
) async {
|
|
|
|
try {
|
2023-05-17 19:36:02 +02:00
|
|
|
return await _albumService.createAlbum(
|
2022-08-03 07:04:34 +02:00
|
|
|
albumName,
|
|
|
|
assets,
|
2023-02-06 09:13:32 +02:00
|
|
|
sharedUsers,
|
2022-08-03 07:04:34 +02:00
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error createSharedAlbum ${e.toString()}");
|
|
|
|
}
|
2023-03-04 00:38:30 +02:00
|
|
|
return null;
|
2022-08-03 07:04:34 +02:00
|
|
|
}
|
2022-04-24 04:08:45 +02:00
|
|
|
|
2023-05-17 19:36:02 +02:00
|
|
|
Future<void> getAllSharedAlbums() =>
|
|
|
|
_albumService.refreshRemoteAlbums(isShared: true);
|
2022-04-24 04:08:45 +02:00
|
|
|
|
2023-05-17 19:36:02 +02:00
|
|
|
Future<bool> deleteAlbum(Album album) => _albumService.deleteAlbum(album);
|
2022-04-24 04:08:45 +02:00
|
|
|
|
2023-02-06 09:13:32 +02:00
|
|
|
Future<bool> leaveAlbum(Album album) async {
|
|
|
|
var res = await _albumService.leaveAlbum(album);
|
2022-04-24 04:08:45 +02:00
|
|
|
|
|
|
|
if (res) {
|
2023-03-04 00:38:30 +02:00
|
|
|
await deleteAlbum(album);
|
2022-04-24 04:08:45 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-04 00:38:30 +02:00
|
|
|
Future<bool> removeAssetFromAlbum(Album album, Iterable<Asset> assets) {
|
|
|
|
return _albumService.removeAssetFromAlbum(album, assets);
|
2022-04-24 04:08:45 +02:00
|
|
|
}
|
2023-05-17 19:36:02 +02:00
|
|
|
|
2023-08-18 06:26:12 +02:00
|
|
|
Future<bool> removeUserFromAlbum(Album album, User user) async {
|
|
|
|
final result = await _albumService.removeUserFromAlbum(album, user);
|
|
|
|
|
|
|
|
if (result && album.sharedUsers.isEmpty) {
|
|
|
|
state = state.where((element) => element.id != album.id).toList();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-12-07 17:38:22 +02:00
|
|
|
Future<bool> setActivityEnabled(Album album, bool activityEnabled) {
|
|
|
|
return _albumService.setActivityEnabled(album, activityEnabled);
|
2023-11-08 05:07:43 +02:00
|
|
|
}
|
|
|
|
|
2023-05-17 19:36:02 +02:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_streamSub.cancel();
|
|
|
|
super.dispose();
|
|
|
|
}
|
2022-04-24 04:08:45 +02:00
|
|
|
}
|
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
final sharedAlbumProvider =
|
2023-05-17 19:36:02 +02:00
|
|
|
StateNotifierProvider.autoDispose<SharedAlbumNotifier, List<Album>>((ref) {
|
2022-10-18 14:06:35 +02:00
|
|
|
return SharedAlbumNotifier(
|
|
|
|
ref.watch(albumServiceProvider),
|
2023-03-04 00:38:30 +02:00
|
|
|
ref.watch(dbProvider),
|
2022-10-18 14:06:35 +02:00
|
|
|
);
|
2022-04-24 04:08:45 +02:00
|
|
|
});
|