diff --git a/lib/services/local_storage_service.dart b/lib/services/local_storage_service.dart index 68097da..12130a1 100644 --- a/lib/services/local_storage_service.dart +++ b/lib/services/local_storage_service.dart @@ -161,23 +161,23 @@ class LocalStorageService { try { final jsonString = await _readFileContent(path.files.single); - final jsonData = json.decode(jsonString) as List; if (await validateJsonSchema(jsonString, true)) { - print( - '[local_storage_service.dart] Die Datei wurde erfolgreich als Liste validiert'); - List tempList = jsonData + // Checks if the JSON String is in the gameList format + + final jsonData = json.decode(jsonString) as List; + List importedList = jsonData .map((jsonItem) => GameSession.fromJson(jsonItem as Map)) .toList(); - for (GameSession s in tempList) { + for (GameSession s in importedList) { importSession(s); } } else if (await validateJsonSchema(jsonString, false)) { - print( - '[local_storage_service.dart] Die Datei wurde erfolgreich als einzelnes Spiel validiert'); - importSession(GameSession.fromJson(jsonData as Map)); + // Checks if the JSON String is in the single game format + final jsonData = json.decode(jsonString) as Map; + importSession(GameSession.fromJson(jsonData)); } else { return ImportStatus.validationError; } @@ -199,12 +199,12 @@ class LocalStorageService { /// Imports a single game session into the gameList. static Future importSession(GameSession session) async { - if (gameManager.gameList.any((s) => s.id == session.id)) { + if (gameManager.gameExistsInGameList(session.id)) { print( - '[local_storage_service.dart] Die Session mit der ID ${session.id} existiert bereits. Sie wird aktualisiert.'); - gameManager.gameList.removeWhere((s) => s.id == session.id); + '[local_storage_service.dart] Die Session mit der ID ${session.id} existiert bereits. Sie wird überschrieben.'); + gameManager.removeGameSessionById(session.id); } - gameManager.gameList.add(session); + gameManager.addGameSession(session); print( '[local_storage_service.dart] Die Session mit der ID ${session.id} wurde erfolgreich importiert.'); } @@ -218,6 +218,9 @@ class LocalStorageService { } /// Validates the JSON data against the schema. + /// This method checks if the provided [jsonString] is valid against the + /// JSON schema. It takes a boolean [isGameList] to determine + /// which schema to use (game list or single game). static Future validateJsonSchema( String jsonString, bool isGameList) async { final String schemaString; @@ -235,7 +238,8 @@ class LocalStorageService { final result = schema.validate(jsonData); if (result.isValid) { - print('[local_storage_service.dart] JSON ist erfolgreich validiert.'); + print( + '[local_storage_service.dart] JSON ist erfolgreich validiert. Typ: ${isGameList ? 'Game List' : 'Single Game'}'); return true; } print( diff --git a/pubspec.yaml b/pubspec.yaml index 144adf4..7ac98ec 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: cabo_counter description: "Mobile app for the card game Cabo" publish_to: 'none' -version: 0.3.7+326 +version: 0.3.8+327 environment: sdk: ^3.5.4 @@ -40,3 +40,4 @@ flutter: assets: - assets/cabo_counter-logo_rounded.png - assets/game_list-schema.json + - assets/game-schema.json