Fixed bug that game session would reset afer creating it

This commit is contained in:
2025-06-11 14:45:20 +02:00
parent c8b9e71581
commit c7c48d6022
3 changed files with 15 additions and 6 deletions

View File

@@ -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<int> 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.

View File

@@ -28,6 +28,7 @@ class LocalStorageService {
/// Saves the game sessions to a local JSON file.
static Future<void> saveGameSessions() async {
print('[local_storage_service.dart] Versuche, Daten zu speichern...');
try {
final file = await _getFilePath();
final jsonFile = getJsonFile();

View File

@@ -206,7 +206,7 @@ class _CreateGameState extends State<CreateGame> {
),
],
),
onPressed: () {
onPressed: () async {
if (_gameTitleTextController.text == '') {
showCupertinoDialog(
context: context,
@@ -289,13 +289,14 @@ class _CreateGameState extends State<CreateGame> {
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');
}