Organized LocalStorageService
This commit is contained in:
@@ -161,23 +161,23 @@ class LocalStorageService {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
final jsonString = await _readFileContent(path.files.single);
|
final jsonString = await _readFileContent(path.files.single);
|
||||||
final jsonData = json.decode(jsonString) as List<dynamic>;
|
|
||||||
|
|
||||||
if (await validateJsonSchema(jsonString, true)) {
|
if (await validateJsonSchema(jsonString, true)) {
|
||||||
print(
|
// Checks if the JSON String is in the gameList format
|
||||||
'[local_storage_service.dart] Die Datei wurde erfolgreich als Liste validiert');
|
|
||||||
List<GameSession> tempList = jsonData
|
final jsonData = json.decode(jsonString) as List<dynamic>;
|
||||||
|
List<GameSession> importedList = jsonData
|
||||||
.map((jsonItem) =>
|
.map((jsonItem) =>
|
||||||
GameSession.fromJson(jsonItem as Map<String, dynamic>))
|
GameSession.fromJson(jsonItem as Map<String, dynamic>))
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
for (GameSession s in tempList) {
|
for (GameSession s in importedList) {
|
||||||
importSession(s);
|
importSession(s);
|
||||||
}
|
}
|
||||||
} else if (await validateJsonSchema(jsonString, false)) {
|
} else if (await validateJsonSchema(jsonString, false)) {
|
||||||
print(
|
// Checks if the JSON String is in the single game format
|
||||||
'[local_storage_service.dart] Die Datei wurde erfolgreich als einzelnes Spiel validiert');
|
final jsonData = json.decode(jsonString) as Map<String, dynamic>;
|
||||||
importSession(GameSession.fromJson(jsonData as Map<String, dynamic>));
|
importSession(GameSession.fromJson(jsonData));
|
||||||
} else {
|
} else {
|
||||||
return ImportStatus.validationError;
|
return ImportStatus.validationError;
|
||||||
}
|
}
|
||||||
@@ -199,12 +199,12 @@ class LocalStorageService {
|
|||||||
|
|
||||||
/// Imports a single game session into the gameList.
|
/// Imports a single game session into the gameList.
|
||||||
static Future<void> importSession(GameSession session) async {
|
static Future<void> importSession(GameSession session) async {
|
||||||
if (gameManager.gameList.any((s) => s.id == session.id)) {
|
if (gameManager.gameExistsInGameList(session.id)) {
|
||||||
print(
|
print(
|
||||||
'[local_storage_service.dart] Die Session mit der ID ${session.id} existiert bereits. Sie wird aktualisiert.');
|
'[local_storage_service.dart] Die Session mit der ID ${session.id} existiert bereits. Sie wird überschrieben.');
|
||||||
gameManager.gameList.removeWhere((s) => s.id == session.id);
|
gameManager.removeGameSessionById(session.id);
|
||||||
}
|
}
|
||||||
gameManager.gameList.add(session);
|
gameManager.addGameSession(session);
|
||||||
print(
|
print(
|
||||||
'[local_storage_service.dart] Die Session mit der ID ${session.id} wurde erfolgreich importiert.');
|
'[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.
|
/// 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<bool> validateJsonSchema(
|
static Future<bool> validateJsonSchema(
|
||||||
String jsonString, bool isGameList) async {
|
String jsonString, bool isGameList) async {
|
||||||
final String schemaString;
|
final String schemaString;
|
||||||
@@ -235,7 +238,8 @@ class LocalStorageService {
|
|||||||
final result = schema.validate(jsonData);
|
final result = schema.validate(jsonData);
|
||||||
|
|
||||||
if (result.isValid) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
print(
|
print(
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ name: cabo_counter
|
|||||||
description: "Mobile app for the card game Cabo"
|
description: "Mobile app for the card game Cabo"
|
||||||
publish_to: 'none'
|
publish_to: 'none'
|
||||||
|
|
||||||
version: 0.3.7+326
|
version: 0.3.8+327
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ^3.5.4
|
sdk: ^3.5.4
|
||||||
@@ -40,3 +40,4 @@ flutter:
|
|||||||
assets:
|
assets:
|
||||||
- assets/cabo_counter-logo_rounded.png
|
- assets/cabo_counter-logo_rounded.png
|
||||||
- assets/game_list-schema.json
|
- assets/game_list-schema.json
|
||||||
|
- assets/game-schema.json
|
||||||
|
|||||||
Reference in New Issue
Block a user