1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-07 20:36:48 +01:00
immich/mobile/lib/providers/asset_viewer/asset_stack.provider.dart

60 lines
1.5 KiB
Dart
Raw Normal View History

2023-10-22 04:38:07 +02:00
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/entities/asset.entity.dart';
import 'package:immich_mobile/providers/db.provider.dart';
2023-10-22 04:38:07 +02:00
import 'package:isar/isar.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'asset_stack.provider.g.dart';
2023-10-22 04:38:07 +02:00
class AssetStackNotifier extends StateNotifier<List<Asset>> {
2024-11-17 00:05:22 +01:00
final String _stackId;
2023-10-22 04:38:07 +02:00
final Ref _ref;
2024-11-17 00:05:22 +01:00
AssetStackNotifier(this._stackId, this._ref) : super([]) {
_fetchStack(_stackId);
2023-10-22 04:38:07 +02:00
}
2024-11-17 00:05:22 +01:00
void _fetchStack(String stackId) async {
if (!mounted) {
return;
}
final stack = await _ref.read(assetStackProvider(stackId).future);
if (stack.isNotEmpty) {
state = stack;
2023-10-22 04:38:07 +02:00
}
}
void removeChild(int index) {
2023-10-22 04:38:07 +02:00
if (index < state.length) {
state.removeAt(index);
2024-11-17 00:05:22 +01:00
state = List<Asset>.from(state);
2023-10-22 04:38:07 +02:00
}
}
}
final assetStackStateProvider = StateNotifierProvider.autoDispose
2024-11-17 00:05:22 +01:00
.family<AssetStackNotifier, List<Asset>, String>(
(ref, stackId) => AssetStackNotifier(stackId, ref),
2023-10-22 04:38:07 +02:00
);
final assetStackProvider =
2024-11-17 00:05:22 +01:00
FutureProvider.autoDispose.family<List<Asset>, String>((ref, stackId) {
return ref
2023-10-22 04:38:07 +02:00
.watch(dbProvider)
.assets
.filter()
.isArchivedEqualTo(false)
.isTrashedEqualTo(false)
2024-11-17 00:05:22 +01:00
.stackIdEqualTo(stackId)
// orders primary asset first as its ID is null
.sortByStackPrimaryAssetId()
.thenByFileCreatedAtDesc()
2023-10-22 04:38:07 +02:00
.findAll();
});
@riverpod
int assetStackIndex(AssetStackIndexRef ref, Asset asset) {
return -1;
}