1
0
mirror of https://github.com/immich-app/immich.git synced 2024-12-25 10:43:13 +02:00
immich/mobile/lib/modules/album/providers/shared_album.provider.dart

106 lines
2.7 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
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';
import 'package:immich_mobile/shared/models/album.dart';
import 'package:immich_mobile/shared/models/asset.dart';
import 'package:immich_mobile/shared/models/user.dart';
class SharedAlbumNotifier extends StateNotifier<List<Album>> {
SharedAlbumNotifier(this._albumService, this._sharedAlbumCacheService)
: super([]);
final AlbumService _albumService;
2022-10-18 14:06:35 +02:00
final SharedAlbumCacheService _sharedAlbumCacheService;
void _cacheState() {
2022-10-18 14:06:35 +02:00
_sharedAlbumCacheService.put(state);
}
Future<Album?> createSharedAlbum(
String albumName,
Iterable<Asset> assets,
Iterable<User> sharedUsers,
) async {
try {
var newAlbum = await _albumService.createAlbum(
albumName,
assets,
sharedUsers,
);
if (newAlbum != null) {
state = [...state, newAlbum];
2022-10-18 14:06:35 +02:00
_cacheState();
}
return newAlbum;
} catch (e) {
debugPrint("Error createSharedAlbum ${e.toString()}");
return null;
}
}
Future<void> getAllSharedAlbums() async {
2022-10-18 14:06:35 +02:00
if (await _sharedAlbumCacheService.isValid() && state.isEmpty) {
final albums = await _sharedAlbumCacheService.get();
if (albums != null) {
state = albums;
}
2022-10-18 14:06:35 +02:00
}
List<Album>? sharedAlbums = await _albumService.getAlbums(isShared: true);
if (sharedAlbums != null) {
state = sharedAlbums;
2022-10-18 14:06:35 +02:00
_cacheState();
}
}
void deleteAlbum(Album album) {
state = state.where((a) => a.id != album.id).toList();
2022-10-18 14:06:35 +02:00
_cacheState();
}
Future<bool> leaveAlbum(Album album) async {
var res = await _albumService.leaveAlbum(album);
if (res) {
state = state.where((a) => a.id != album.id).toList();
2022-10-18 14:06:35 +02:00
_cacheState();
return true;
} else {
return false;
}
}
Future<bool> removeAssetFromAlbum(
Album album,
Iterable<Asset> assets,
) async {
var res = await _albumService.removeAssetFromAlbum(album, assets);
if (res) {
return true;
} else {
return false;
}
}
}
final sharedAlbumProvider =
StateNotifierProvider<SharedAlbumNotifier, List<Album>>((ref) {
2022-10-18 14:06:35 +02:00
return SharedAlbumNotifier(
ref.watch(albumServiceProvider),
ref.watch(sharedAlbumCacheServiceProvider),
);
});
final sharedAlbumDetailProvider =
FutureProvider.autoDispose.family<Album?, String>((ref, albumId) async {
final AlbumService sharedAlbumService = ref.watch(albumServiceProvider);
return await sharedAlbumService.getAlbumDetail(albumId);
});