2023-03-04 00:38:30 +02:00
|
|
|
import 'package:collection/collection.dart';
|
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-03-04 00:38:30 +02:00
|
|
|
SharedAlbumNotifier(this._albumService, this._db) : super([]);
|
2022-04-24 04:08:45 +02:00
|
|
|
|
2023-01-29 05:57:13 +02:00
|
|
|
final AlbumService _albumService;
|
2023-03-04 00:38:30 +02:00
|
|
|
final Isar _db;
|
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-03-04 00:38:30 +02:00
|
|
|
final Album? 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];
|
2023-03-04 00:38:30 +02:00
|
|
|
return newAlbum;
|
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-02-06 09:13:32 +02:00
|
|
|
Future<void> getAllSharedAlbums() async {
|
2023-03-12 15:43:09 +02:00
|
|
|
var albums = await _db.albums
|
|
|
|
.filter()
|
|
|
|
.sharedEqualTo(true)
|
|
|
|
.sortByCreatedAtDesc()
|
|
|
|
.findAll();
|
2023-03-04 00:38:30 +02:00
|
|
|
if (!const ListEquality().equals(albums, state)) {
|
|
|
|
state = albums;
|
2022-10-18 14:06:35 +02:00
|
|
|
}
|
2023-03-04 00:38:30 +02:00
|
|
|
await _albumService.refreshRemoteAlbums(isShared: true);
|
2023-03-12 15:43:09 +02:00
|
|
|
albums = await _db.albums
|
|
|
|
.filter()
|
|
|
|
.sharedEqualTo(true)
|
|
|
|
.sortByCreatedAtDesc()
|
|
|
|
.findAll();
|
2023-03-04 00:38:30 +02:00
|
|
|
if (!const ListEquality().equals(albums, state)) {
|
|
|
|
state = albums;
|
2022-07-13 14:23:48 +02:00
|
|
|
}
|
2022-04-24 04:08:45 +02:00
|
|
|
}
|
|
|
|
|
2023-03-04 00:38:30 +02:00
|
|
|
Future<bool> deleteAlbum(Album album) {
|
2023-02-06 09:13:32 +02:00
|
|
|
state = state.where((a) => a.id != album.id).toList();
|
2023-03-04 00:38:30 +02:00
|
|
|
return _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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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),
|
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
|
|
|
});
|
|
|
|
|
2023-02-06 09:13:32 +02:00
|
|
|
final sharedAlbumDetailProvider =
|
2023-03-04 00:38:30 +02:00
|
|
|
FutureProvider.autoDispose.family<Album?, int>((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
|
|
|
|
2023-03-04 00:38:30 +02:00
|
|
|
final Album? a = await sharedAlbumService.getAlbumDetail(albumId);
|
|
|
|
await a?.loadSortedAssets();
|
|
|
|
return a;
|
2022-04-24 04:08:45 +02:00
|
|
|
});
|