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';
|
2022-10-18 14:06:35 +02:00
|
|
|
import 'package:immich_mobile/modules/album/services/album_cache.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';
|
2022-04-24 04:08:45 +02:00
|
|
|
|
2023-02-06 09:13:32 +02:00
|
|
|
class SharedAlbumNotifier extends StateNotifier<List<Album>> {
|
2023-01-29 05:57:13 +02:00
|
|
|
SharedAlbumNotifier(this._albumService, this._sharedAlbumCacheService)
|
2022-11-08 19:00:24 +02:00
|
|
|
: super([]);
|
2022-04-24 04:08:45 +02:00
|
|
|
|
2023-01-29 05:57:13 +02:00
|
|
|
final AlbumService _albumService;
|
2022-10-18 14:06:35 +02:00
|
|
|
final SharedAlbumCacheService _sharedAlbumCacheService;
|
|
|
|
|
2023-02-06 09:13:32 +02:00
|
|
|
void _cacheState() {
|
2022-10-18 14:06:35 +02:00
|
|
|
_sharedAlbumCacheService.put(state);
|
|
|
|
}
|
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-01-29 05:57:13 +02:00
|
|
|
var newAlbum = 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
|
|
|
);
|
|
|
|
|
|
|
|
if (newAlbum != null) {
|
|
|
|
state = [...state, newAlbum];
|
2022-10-18 14:06:35 +02:00
|
|
|
_cacheState();
|
2022-08-03 07:04:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return newAlbum;
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error createSharedAlbum ${e.toString()}");
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2022-04-24 04:08:45 +02:00
|
|
|
|
2023-02-06 09:13:32 +02:00
|
|
|
Future<void> getAllSharedAlbums() async {
|
2022-10-18 14:06:35 +02:00
|
|
|
if (await _sharedAlbumCacheService.isValid() && state.isEmpty) {
|
2023-02-06 09:13:32 +02:00
|
|
|
final albums = await _sharedAlbumCacheService.get();
|
|
|
|
if (albums != null) {
|
|
|
|
state = albums;
|
|
|
|
}
|
2022-10-18 14:06:35 +02:00
|
|
|
}
|
|
|
|
|
2023-02-06 09:13:32 +02:00
|
|
|
List<Album>? sharedAlbums = await _albumService.getAlbums(isShared: true);
|
2022-04-24 04:08:45 +02:00
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
if (sharedAlbums != null) {
|
|
|
|
state = sharedAlbums;
|
2022-10-18 14:06:35 +02:00
|
|
|
_cacheState();
|
2022-07-13 14:23:48 +02:00
|
|
|
}
|
2022-04-24 04:08:45 +02:00
|
|
|
}
|
|
|
|
|
2023-02-06 09:13:32 +02:00
|
|
|
void deleteAlbum(Album album) {
|
|
|
|
state = state.where((a) => a.id != album.id).toList();
|
2022-10-18 14:06:35 +02:00
|
|
|
_cacheState();
|
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-02-06 09:13:32 +02:00
|
|
|
state = state.where((a) => a.id != album.id).toList();
|
2022-10-18 14:06:35 +02:00
|
|
|
_cacheState();
|
2022-04-24 04:08:45 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
Future<bool> removeAssetFromAlbum(
|
2023-02-06 09:13:32 +02:00
|
|
|
Album album,
|
|
|
|
Iterable<Asset> assets,
|
2022-07-13 14:23:48 +02:00
|
|
|
) async {
|
2023-02-06 09:13:32 +02:00
|
|
|
var res = await _albumService.removeAssetFromAlbum(album, assets);
|
2022-04-24 04:08:45 +02:00
|
|
|
|
|
|
|
if (res) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
final sharedAlbumProvider =
|
2023-02-06 09:13:32 +02:00
|
|
|
StateNotifierProvider<SharedAlbumNotifier, List<Album>>((ref) {
|
2022-10-18 14:06:35 +02:00
|
|
|
return SharedAlbumNotifier(
|
|
|
|
ref.watch(albumServiceProvider),
|
|
|
|
ref.watch(sharedAlbumCacheServiceProvider),
|
|
|
|
);
|
2022-04-24 04:08:45 +02:00
|
|
|
});
|
|
|
|
|
2023-02-06 09:13:32 +02:00
|
|
|
final sharedAlbumDetailProvider =
|
|
|
|
FutureProvider.autoDispose.family<Album?, String>((ref, albumId) async {
|
2022-08-03 07:04:34 +02:00
|
|
|
final AlbumService sharedAlbumService = ref.watch(albumServiceProvider);
|
2022-04-24 04:08:45 +02:00
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
return await sharedAlbumService.getAlbumDetail(albumId);
|
2022-04-24 04:08:45 +02:00
|
|
|
});
|