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';
|
2022-11-08 19:00:24 +02:00
|
|
|
import 'package:immich_mobile/shared/models/asset.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-11-08 19:00:24 +02:00
|
|
|
SharedAlbumNotifier(this._sharedAlbumService, this._sharedAlbumCacheService)
|
|
|
|
: super([]);
|
2022-04-24 04:08:45 +02:00
|
|
|
|
2022-08-03 07:04:34 +02:00
|
|
|
final AlbumService _sharedAlbumService;
|
2022-10-18 14:06:35 +02:00
|
|
|
final SharedAlbumCacheService _sharedAlbumCacheService;
|
|
|
|
|
|
|
|
_cacheState() {
|
|
|
|
_sharedAlbumCacheService.put(state);
|
|
|
|
}
|
2022-08-03 07:04:34 +02:00
|
|
|
|
|
|
|
Future<AlbumResponseDto?> createSharedAlbum(
|
|
|
|
String albumName,
|
2022-11-08 19:00:24 +02:00
|
|
|
Set<Asset> assets,
|
2022-08-03 07:04:34 +02:00
|
|
|
List<String> sharedUserIds,
|
|
|
|
) async {
|
|
|
|
try {
|
|
|
|
var newAlbum = await _sharedAlbumService.createAlbum(
|
|
|
|
albumName,
|
|
|
|
assets,
|
|
|
|
sharedUserIds,
|
|
|
|
);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
getAllSharedAlbums() async {
|
2022-10-18 14:06:35 +02:00
|
|
|
if (await _sharedAlbumCacheService.isValid() && state.isEmpty) {
|
|
|
|
state = await _sharedAlbumCacheService.get();
|
|
|
|
}
|
|
|
|
|
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-10-18 14:06:35 +02:00
|
|
|
_cacheState();
|
2022-07-13 14:23:48 +02:00
|
|
|
}
|
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-10-18 14:06:35 +02:00
|
|
|
_cacheState();
|
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();
|
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(
|
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-10-18 14:06:35 +02:00
|
|
|
return SharedAlbumNotifier(
|
|
|
|
ref.watch(albumServiceProvider),
|
|
|
|
ref.watch(sharedAlbumCacheServiceProvider),
|
|
|
|
);
|
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
|
|
|
});
|