Fixed bug that game session would reset afer creating it
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user