Fixed bug for games showing round number 1 in main menu after creating

This commit is contained in:
2025-06-11 15:27:34 +02:00
parent c7c48d6022
commit 0374678b91
4 changed files with 184 additions and 156 deletions

View File

@@ -11,6 +11,9 @@ class GameManager extends ChangeNotifier {
/// It also saves the updated game sessions to local storage.
/// Returns the index of the newly added session in the sorted list.
Future<int> addGameSession(GameSession session) async {
session.addListener(() {
notifyListeners(); // Propagate session changes
});
gameList.add(session);
print(
'[game_manager.dart] Added game session: ${session.gameTitle} at ${session.createdAt}');
@@ -28,6 +31,7 @@ class GameManager extends ChangeNotifier {
/// 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 removeGameSession(int index) {
gameList[index].removeListener(notifyListeners);
gameList.removeAt(index);
notifyListeners();
LocalStorageService.saveGameSessions();

View File

@@ -11,7 +11,7 @@ import 'package:flutter/cupertino.dart';
/// [roundNumber] is the current round number.
/// [isGameFinished] is a boolean indicating if the game has ended yet.
/// [winner] is the name of the player who won the game.
class GameSession {
class GameSession extends ChangeNotifier {
final DateTime createdAt;
final String gameTitle;
final List<String> players;
@@ -222,6 +222,7 @@ class GameSession {
} else {
roundList[roundNum - 1] = newRound;
}
notifyListeners();
}
/// This method updates the points of each player after a round.
@@ -248,6 +249,7 @@ class GameSession {
}
}
}
notifyListeners();
}
@visibleForTesting
@@ -262,6 +264,7 @@ class GameSession {
playerScores[i] += roundList[j].scoreUpdates[i];
}
}
notifyListeners();
}
/// Checks if a player has reached 100 points in the current round.
@@ -291,10 +294,14 @@ class GameSession {
}
}
winner = lowestPlayer;
notifyListeners();
}
/// Increases the round number by 1.
void increaseRound() {
roundNumber++;
print('roundNumber erhöht: $roundNumber — Hash: ${identityHashCode(this)}');
notifyListeners();
}
}