fix(presentation): resilient pokemon loading, web-safe invalidate, correct best-score persist

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Maxiwere45 2026-06-09 11:38:40 +02:00
parent 4bca4b1ed5
commit 29d2c92b37

View File

@ -1,9 +1,11 @@
import 'dart:math';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../core/config/app_constants.dart';
import '../../domain/game/game_engine.dart';
import '../../domain/game/game_state.dart';
import '../../core/logger.dart';
import 'pokedex_provider.dart';
import 'repository_provider.dart';
@ -33,13 +35,17 @@ class GameNotifier extends Notifier<GameState> {
final isShiny = _random.nextInt(AppConstants.shinyOdds) == 0;
final id = _random.nextInt(AppConstants.totalPokemon) + 1;
final pokemon = await repo.getById(id);
if (pokemon == null) {
try {
final pokemon = await repo.getById(id);
if (pokemon == null) {
state = state.copyWith(status: GameStatus.error);
return;
}
state = _engine.startRound(state, pokemon, isShiny: isShiny);
} catch (e, st) {
AppLogger.error('loadNextPokemon a échoué', e, st);
state = state.copyWith(status: GameStatus.error);
return;
}
state = _engine.startRound(state, pokemon, isShiny: isShiny);
}
Future<GuessResult> submitGuess(String guess) async {
@ -51,7 +57,7 @@ class GameNotifier extends Notifier<GameState> {
final caught = state.currentPokemon;
if (caught != null) await repo.update(caught);
await _persistBestScore();
ref.invalidate(pokedexProvider); // rafraîchit la liste
if (!kIsWeb) ref.invalidate(pokedexProvider); // rafraîchit la liste (pas de persistance sur web)
}
return outcome.result;
}
@ -59,8 +65,8 @@ class GameNotifier extends Notifier<GameState> {
Future<void> _persistBestScore() async {
final prefs = await SharedPreferences.getInstance();
final stored = prefs.getInt(AppConstants.prefsBestScore) ?? 0;
if (state.currentScore > stored) {
await prefs.setInt(AppConstants.prefsBestScore, state.currentScore);
if (state.bestScore > stored) {
await prefs.setInt(AppConstants.prefsBestScore, state.bestScore);
}
}