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`,
|
/// 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.
|
/// 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.
|
/// 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);
|
gameList.add(session);
|
||||||
|
print(
|
||||||
|
'[game_manager.dart] Added game session: ${session.gameTitle} at ${session.createdAt}');
|
||||||
gameList.sort((a, b) => b.createdAt.compareTo(a.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();
|
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.
|
/// 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.
|
/// Saves the game sessions to a local JSON file.
|
||||||
static Future<void> saveGameSessions() async {
|
static Future<void> saveGameSessions() async {
|
||||||
|
print('[local_storage_service.dart] Versuche, Daten zu speichern...');
|
||||||
try {
|
try {
|
||||||
final file = await _getFilePath();
|
final file = await _getFilePath();
|
||||||
final jsonFile = getJsonFile();
|
final jsonFile = getJsonFile();
|
||||||
|
|||||||
@@ -206,7 +206,7 @@ class _CreateGameState extends State<CreateGame> {
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
onPressed: () {
|
onPressed: () async {
|
||||||
if (_gameTitleTextController.text == '') {
|
if (_gameTitleTextController.text == '') {
|
||||||
showCupertinoDialog(
|
showCupertinoDialog(
|
||||||
context: context,
|
context: context,
|
||||||
@@ -289,13 +289,14 @@ class _CreateGameState extends State<CreateGame> {
|
|||||||
caboPenalty: Globals.caboPenalty,
|
caboPenalty: Globals.caboPenalty,
|
||||||
isPointsLimitEnabled: selectedMode!,
|
isPointsLimitEnabled: selectedMode!,
|
||||||
);
|
);
|
||||||
gameManager.addGameSession(gameSession);
|
final index = await gameManager.addGameSession(gameSession);
|
||||||
|
print('index des spiels: $index');
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
Navigator.pushReplacement(
|
Navigator.pushReplacement(
|
||||||
context,
|
context,
|
||||||
CupertinoPageRoute(
|
CupertinoPageRoute(
|
||||||
builder: (context) =>
|
builder: (context) => ActiveGameView(
|
||||||
ActiveGameView(gameSession: gameSession)));
|
gameSession: gameManager.gameList[index])));
|
||||||
} else {
|
} else {
|
||||||
print('Context is not mounted');
|
print('Context is not mounted');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user