feat(presentation): add pokedex AsyncNotifier provider

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Maxiwere45 2026-06-09 11:30:55 +02:00
parent f6a6ba2cd1
commit d531fcb2c8

View File

@ -0,0 +1,34 @@
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,
);
});