35 lines
1.1 KiB
Dart
35 lines
1.1 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import '../../domain/entities/pokemon.dart';
|
|
import 'repository_provider.dart';
|
|
|
|
/// Liste complète du Pokédex (triée par id), avec synchro initiale gérée par le repository.
|
|
class PokedexNotifier extends AsyncNotifier<List<Pokemon>> {
|
|
Future<List<Pokemon>> _load() async {
|
|
final repo = ref.read(pokemonRepositoryProvider);
|
|
final list = await repo.getAll();
|
|
list.sort((a, b) => a.id.compareTo(b.id));
|
|
return list;
|
|
}
|
|
|
|
@override
|
|
Future<List<Pokemon>> build() => _load();
|
|
|
|
/// Recharge la liste (ex. après avoir attrapé un Pokémon).
|
|
Future<void> refresh() async {
|
|
state = const AsyncLoading();
|
|
state = await AsyncValue.guard(_load);
|
|
}
|
|
}
|
|
|
|
final pokedexProvider =
|
|
AsyncNotifierProvider<PokedexNotifier, List<Pokemon>>(PokedexNotifier.new);
|
|
|
|
/// Nombre de Pokémon attrapés, dérivé de la liste.
|
|
final caughtCountProvider = Provider<int>((ref) {
|
|
final async = ref.watch(pokedexProvider);
|
|
return async.maybeWhen(
|
|
data: (list) => list.where((p) => p.isCaught).length,
|
|
orElse: () => 0,
|
|
);
|
|
});
|