feat(presentation): add pokedex AsyncNotifier provider
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
f6a6ba2cd1
commit
d531fcb2c8
34
lib/presentation/providers/pokedex_provider.dart
Normal file
34
lib/presentation/providers/pokedex_provider.dart
Normal 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,
|
||||
);
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user