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:
parent
4bca4b1ed5
commit
29d2c92b37
@ -1,9 +1,11 @@
|
|||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
|
import 'package:flutter/foundation.dart' show kIsWeb;
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
import '../../core/config/app_constants.dart';
|
import '../../core/config/app_constants.dart';
|
||||||
import '../../domain/game/game_engine.dart';
|
import '../../domain/game/game_engine.dart';
|
||||||
import '../../domain/game/game_state.dart';
|
import '../../domain/game/game_state.dart';
|
||||||
|
import '../../core/logger.dart';
|
||||||
import 'pokedex_provider.dart';
|
import 'pokedex_provider.dart';
|
||||||
import 'repository_provider.dart';
|
import 'repository_provider.dart';
|
||||||
|
|
||||||
@ -33,13 +35,17 @@ class GameNotifier extends Notifier<GameState> {
|
|||||||
final isShiny = _random.nextInt(AppConstants.shinyOdds) == 0;
|
final isShiny = _random.nextInt(AppConstants.shinyOdds) == 0;
|
||||||
|
|
||||||
final id = _random.nextInt(AppConstants.totalPokemon) + 1;
|
final id = _random.nextInt(AppConstants.totalPokemon) + 1;
|
||||||
|
try {
|
||||||
final pokemon = await repo.getById(id);
|
final pokemon = await repo.getById(id);
|
||||||
|
|
||||||
if (pokemon == null) {
|
if (pokemon == null) {
|
||||||
state = state.copyWith(status: GameStatus.error);
|
state = state.copyWith(status: GameStatus.error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
state = _engine.startRound(state, pokemon, isShiny: isShiny);
|
state = _engine.startRound(state, pokemon, isShiny: isShiny);
|
||||||
|
} catch (e, st) {
|
||||||
|
AppLogger.error('loadNextPokemon a échoué', e, st);
|
||||||
|
state = state.copyWith(status: GameStatus.error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<GuessResult> submitGuess(String guess) async {
|
Future<GuessResult> submitGuess(String guess) async {
|
||||||
@ -51,7 +57,7 @@ class GameNotifier extends Notifier<GameState> {
|
|||||||
final caught = state.currentPokemon;
|
final caught = state.currentPokemon;
|
||||||
if (caught != null) await repo.update(caught);
|
if (caught != null) await repo.update(caught);
|
||||||
await _persistBestScore();
|
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;
|
return outcome.result;
|
||||||
}
|
}
|
||||||
@ -59,8 +65,8 @@ class GameNotifier extends Notifier<GameState> {
|
|||||||
Future<void> _persistBestScore() async {
|
Future<void> _persistBestScore() async {
|
||||||
final prefs = await SharedPreferences.getInstance();
|
final prefs = await SharedPreferences.getInstance();
|
||||||
final stored = prefs.getInt(AppConstants.prefsBestScore) ?? 0;
|
final stored = prefs.getInt(AppConstants.prefsBestScore) ?? 0;
|
||||||
if (state.currentScore > stored) {
|
if (state.bestScore > stored) {
|
||||||
await prefs.setInt(AppConstants.prefsBestScore, state.currentScore);
|
await prefs.setInt(AppConstants.prefsBestScore, state.bestScore);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user