diff --git a/lib/presentation/providers/game_provider.dart b/lib/presentation/providers/game_provider.dart index 261e978..4adadaf 100644 --- a/lib/presentation/providers/game_provider.dart +++ b/lib/presentation/providers/game_provider.dart @@ -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 { 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 submitGuess(String guess) async { @@ -51,7 +57,7 @@ class GameNotifier extends Notifier { 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 { Future _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); } }