From c7c48d60220c0e5729fbcede64f5edb6bad82220 Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Wed, 11 Jun 2025 14:45:20 +0200 Subject: [PATCH] Fixed bug that game session would reset afer creating it --- lib/data/game_manager.dart | 11 +++++++++-- lib/services/local_storage_service.dart | 1 + lib/views/create_game_view.dart | 9 +++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/data/game_manager.dart b/lib/data/game_manager.dart index b22ee65..a27adaa 100644 --- a/lib/data/game_manager.dart +++ b/lib/data/game_manager.dart @@ -9,11 +9,18 @@ class GameManager extends ChangeNotifier { /// Takes a [GameSession] object as input. It then adds the session to the `gameList`, /// sorts the list in descending order based on the creation date, and notifies listeners of the change. /// It also saves the updated game sessions to local storage. - void addGameSession(GameSession session) { + /// Returns the index of the newly added session in the sorted list. + Future addGameSession(GameSession session) async { gameList.add(session); + print( + '[game_manager.dart] Added game session: ${session.gameTitle} at ${session.createdAt}'); gameList.sort((a, b) => b.createdAt.compareTo(a.createdAt)); + print( + '[game_manager.dart] Sorted game sessions by creation date. Total sessions: ${gameList.length}'); notifyListeners(); - LocalStorageService.saveGameSessions(); + await LocalStorageService.saveGameSessions(); + print('[game_manager.dart] Saved game sessions to local storage.'); + return gameList.indexOf(session); } /// Removes a game session from the list and sorts it by creation date. diff --git a/lib/services/local_storage_service.dart b/lib/services/local_storage_service.dart index a028127..e3fddcc 100644 --- a/lib/services/local_storage_service.dart +++ b/lib/services/local_storage_service.dart @@ -28,6 +28,7 @@ class LocalStorageService { /// Saves the game sessions to a local JSON file. static Future saveGameSessions() async { + print('[local_storage_service.dart] Versuche, Daten zu speichern...'); try { final file = await _getFilePath(); final jsonFile = getJsonFile(); diff --git a/lib/views/create_game_view.dart b/lib/views/create_game_view.dart index 8b1cf37..565d68a 100644 --- a/lib/views/create_game_view.dart +++ b/lib/views/create_game_view.dart @@ -206,7 +206,7 @@ class _CreateGameState extends State { ), ], ), - onPressed: () { + onPressed: () async { if (_gameTitleTextController.text == '') { showCupertinoDialog( context: context, @@ -289,13 +289,14 @@ class _CreateGameState extends State { caboPenalty: Globals.caboPenalty, isPointsLimitEnabled: selectedMode!, ); - gameManager.addGameSession(gameSession); + final index = await gameManager.addGameSession(gameSession); + print('index des spiels: $index'); if (context.mounted) { Navigator.pushReplacement( context, CupertinoPageRoute( - builder: (context) => - ActiveGameView(gameSession: gameSession))); + builder: (context) => ActiveGameView( + gameSession: gameManager.gameList[index]))); } else { print('Context is not mounted'); }