2023-10-22 02:38:07 +00:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2024-05-01 02:36:40 +00:00
|
|
|
import 'package:immich_mobile/entities/asset.entity.dart';
|
2024-05-02 20:59:14 +00:00
|
|
|
import 'package:immich_mobile/providers/db.provider.dart';
|
2023-10-22 02:38:07 +00:00
|
|
|
import 'package:isar/isar.dart';
|
2024-03-06 03:42:22 +00:00
|
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
|
|
|
|
|
part 'asset_stack.provider.g.dart';
|
2023-10-22 02:38:07 +00:00
|
|
|
|
|
|
|
class AssetStackNotifier extends StateNotifier<List<Asset>> {
|
2024-12-04 21:03:46 +00:00
|
|
|
final String _stackId;
|
2023-10-22 02:38:07 +00:00
|
|
|
final Ref _ref;
|
|
|
|
|
2024-12-04 21:03:46 +00:00
|
|
|
AssetStackNotifier(this._stackId, this._ref) : super([]) {
|
|
|
|
_fetchStack(_stackId);
|
2023-10-22 02:38:07 +00:00
|
|
|
}
|
|
|
|
|
2024-12-04 21:03:46 +00: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 02:38:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-22 20:15:34 +00:00
|
|
|
void removeChild(int index) {
|
2023-10-22 02:38:07 +00:00
|
|
|
if (index < state.length) {
|
|
|
|
state.removeAt(index);
|
2024-12-04 21:03:46 +00:00
|
|
|
state = List<Asset>.from(state);
|
2023-10-22 02:38:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final assetStackStateProvider = StateNotifierProvider.autoDispose
|
2024-12-04 21:03:46 +00:00
|
|
|
.family<AssetStackNotifier, List<Asset>, String>(
|
|
|
|
(ref, stackId) => AssetStackNotifier(stackId, ref),
|
2023-10-22 02:38:07 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
final assetStackProvider =
|
2024-12-04 21:03:46 +00:00
|
|
|
FutureProvider.autoDispose.family<List<Asset>, String>((ref, stackId) {
|
|
|
|
return ref
|
2023-10-22 02:38:07 +00:00
|
|
|
.watch(dbProvider)
|
|
|
|
.assets
|
|
|
|
.filter()
|
|
|
|
.isArchivedEqualTo(false)
|
|
|
|
.isTrashedEqualTo(false)
|
2024-12-04 21:03:46 +00:00
|
|
|
.stackIdEqualTo(stackId)
|
|
|
|
// orders primary asset first as its ID is null
|
|
|
|
.sortByStackPrimaryAssetId()
|
|
|
|
.thenByFileCreatedAtDesc()
|
2023-10-22 02:38:07 +00:00
|
|
|
.findAll();
|
|
|
|
});
|
2024-03-06 03:42:22 +00:00
|
|
|
|
|
|
|
@riverpod
|
|
|
|
int assetStackIndex(AssetStackIndexRef ref, Asset asset) {
|
|
|
|
return -1;
|
|
|
|
}
|