2023-02-05 05:25:11 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
import 'package:immich_mobile/shared/models/asset.dart';
|
|
|
|
import 'package:immich_mobile/shared/providers/asset.provider.dart';
|
|
|
|
|
2023-03-04 00:38:30 +02:00
|
|
|
class FavoriteSelectionNotifier extends StateNotifier<Set<int>> {
|
2023-02-27 22:15:25 +02:00
|
|
|
FavoriteSelectionNotifier(this.assetsState, this.assetNotifier) : super({}) {
|
|
|
|
state = assetsState.allAssets
|
2023-02-05 05:25:11 +02:00
|
|
|
.where((asset) => asset.isFavorite)
|
|
|
|
.map((asset) => asset.id)
|
|
|
|
.toSet();
|
|
|
|
}
|
|
|
|
|
2023-02-27 22:15:25 +02:00
|
|
|
final AssetsState assetsState;
|
|
|
|
final AssetNotifier assetNotifier;
|
2023-02-05 05:25:11 +02:00
|
|
|
|
2023-03-04 00:38:30 +02:00
|
|
|
void _setFavoriteForAssetId(int id, bool favorite) {
|
2023-02-05 05:25:11 +02:00
|
|
|
if (!favorite) {
|
|
|
|
state = state.difference({id});
|
|
|
|
} else {
|
|
|
|
state = state.union({id});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-04 00:38:30 +02:00
|
|
|
bool _isFavorite(int id) {
|
2023-02-05 05:25:11 +02:00
|
|
|
return state.contains(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> toggleFavorite(Asset asset) async {
|
|
|
|
if (!asset.isRemote) return; // TODO support local favorite assets
|
|
|
|
|
|
|
|
_setFavoriteForAssetId(asset.id, !_isFavorite(asset.id));
|
|
|
|
|
2023-02-27 22:15:25 +02:00
|
|
|
await assetNotifier.toggleFavorite(
|
2023-02-05 05:25:11 +02:00
|
|
|
asset,
|
|
|
|
state.contains(asset.id),
|
|
|
|
);
|
|
|
|
}
|
2023-02-06 14:59:56 +02:00
|
|
|
|
|
|
|
Future<void> addToFavorites(Iterable<Asset> assets) {
|
|
|
|
state = state.union(assets.map((a) => a.id).toSet());
|
2023-03-04 00:38:30 +02:00
|
|
|
final futures = assets.map(
|
|
|
|
(a) => assetNotifier.toggleFavorite(
|
|
|
|
a,
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
);
|
2023-02-06 14:59:56 +02:00
|
|
|
|
|
|
|
return Future.wait(futures);
|
|
|
|
}
|
2023-02-05 05:25:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
final favoriteProvider =
|
2023-03-04 00:38:30 +02:00
|
|
|
StateNotifierProvider<FavoriteSelectionNotifier, Set<int>>((ref) {
|
2023-02-27 22:15:25 +02:00
|
|
|
return FavoriteSelectionNotifier(
|
2023-03-04 00:38:30 +02:00
|
|
|
ref.watch(assetProvider),
|
|
|
|
ref.watch(assetProvider.notifier),
|
2023-02-27 22:15:25 +02:00
|
|
|
);
|
2023-02-05 05:25:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
final favoriteAssetProvider = StateProvider((ref) {
|
|
|
|
final favorites = ref.watch(favoriteProvider);
|
|
|
|
|
|
|
|
return ref
|
|
|
|
.watch(assetProvider)
|
|
|
|
.allAssets
|
|
|
|
.where((element) => favorites.contains(element.id))
|
|
|
|
.toList();
|
|
|
|
});
|