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-07-13 14:23:48 +02:00
|
|
|
import 'package:openapi/api.dart';
|
2022-04-24 04:08:45 +02:00
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
class SharedAlbumNotifier extends StateNotifier<List<AlbumResponseDto>> {
|
2022-06-25 20:46:51 +02:00
|
|
|
SharedAlbumNotifier(this._sharedAlbumService) : super([]);
|
2022-04-24 04:08:45 +02:00
|
|
|
|
2022-08-03 07:04:34 +02:00
|
|
|
final AlbumService _sharedAlbumService;
|
|
|
|
|
|
|
|
Future<AlbumResponseDto?> createSharedAlbum(
|
|
|
|
String albumName,
|
|
|
|
Set<AssetResponseDto> assets,
|
|
|
|
List<String> sharedUserIds,
|
|
|
|
) async {
|
|
|
|
try {
|
|
|
|
var newAlbum = await _sharedAlbumService.createAlbum(
|
|
|
|
albumName,
|
|
|
|
assets,
|
|
|
|
sharedUserIds,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (newAlbum != null) {
|
|
|
|
state = [...state, newAlbum];
|
|
|
|
}
|
|
|
|
|
|
|
|
return newAlbum;
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error createSharedAlbum ${e.toString()}");
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2022-04-24 04:08:45 +02:00
|
|
|
|
|
|
|
getAllSharedAlbums() async {
|
2022-07-13 14:23:48 +02:00
|
|
|
List<AlbumResponseDto>? sharedAlbums =
|
2022-08-03 07:04:34 +02:00
|
|
|
await _sharedAlbumService.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-04-24 04:08:45 +02:00
|
|
|
}
|
|
|
|
|
2022-08-03 07:04:34 +02:00
|
|
|
deleteAlbum(String albumId) async {
|
|
|
|
state = state.where((album) => album.id != albumId).toList();
|
2022-04-24 04:08:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> leaveAlbum(String albumId) async {
|
|
|
|
var res = await _sharedAlbumService.leaveAlbum(albumId);
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
state = state.where((album) => album.id != albumId).toList();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
Future<bool> removeAssetFromAlbum(
|
2022-07-13 14:23:48 +02:00
|
|
|
String albumId,
|
|
|
|
List<String> assetIds,
|
|
|
|
) async {
|
2022-04-24 04:08:45 +02:00
|
|
|
var res = await _sharedAlbumService.removeAssetFromAlbum(albumId, assetIds);
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
final sharedAlbumProvider =
|
2022-07-13 14:23:48 +02:00
|
|
|
StateNotifierProvider<SharedAlbumNotifier, List<AlbumResponseDto>>((ref) {
|
2022-08-03 07:04:34 +02:00
|
|
|
return SharedAlbumNotifier(ref.watch(albumServiceProvider));
|
2022-04-24 04:08:45 +02:00
|
|
|
});
|
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
final sharedAlbumDetailProvider = FutureProvider.autoDispose
|
2022-07-13 14:23:48 +02:00
|
|
|
.family<AlbumResponseDto?, 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
|
|
|
});
|