Merge branch 'develop' into feature/18-json-validation
# Conflicts: # pubspec.yaml
This commit is contained in:
57
lib/services/config_service.dart
Normal file
57
lib/services/config_service.dart
Normal file
@@ -0,0 +1,57 @@
|
||||
import 'package:cabo_counter/utility/globals.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// This class handles the configuration settings for the app.
|
||||
/// It uses SharedPreferences to store and retrieve the personal configuration of the app.
|
||||
/// Currently it provides methods to initialize, get, and set the point limit and cabo penalty.
|
||||
class ConfigService {
|
||||
static const String _keyPointLimit = 'pointLimit';
|
||||
static const String _keyCaboPenalty = 'caboPenalty';
|
||||
static const int _defaultPointLimit = 100; // Default Value
|
||||
static const int _defaultCaboPenalty = 5; // Default Value
|
||||
|
||||
static Future<void> initConfig() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
// Default values only set if they are not already set
|
||||
prefs.setInt(
|
||||
_keyPointLimit, prefs.getInt(_keyPointLimit) ?? _defaultPointLimit);
|
||||
prefs.setInt(
|
||||
_keyCaboPenalty, prefs.getInt(_keyCaboPenalty) ?? _defaultCaboPenalty);
|
||||
}
|
||||
|
||||
/// Getter for the point limit.
|
||||
static Future<int> getPointLimit() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
return prefs.getInt(_keyPointLimit) ?? _defaultPointLimit;
|
||||
}
|
||||
|
||||
/// Setter for the point limit.
|
||||
/// [newPointLimit] is the new point limit to be set.
|
||||
static Future<void> setPointLimit(int newPointLimit) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setInt(_keyPointLimit, newPointLimit);
|
||||
}
|
||||
|
||||
/// Getter for the cabo penalty.
|
||||
static Future<int> getCaboPenalty() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
return prefs.getInt(_keyCaboPenalty) ?? _defaultCaboPenalty;
|
||||
}
|
||||
|
||||
/// Setter for the cabo penalty.
|
||||
/// [newCaboPenalty] is the new cabo penalty to be set.
|
||||
static Future<void> setCaboPenalty(int newCaboPenalty) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setInt(_keyCaboPenalty, newCaboPenalty);
|
||||
}
|
||||
|
||||
/// Resets the configuration to default values.
|
||||
static Future<void> resetConfig() async {
|
||||
Globals.pointLimit = _defaultPointLimit;
|
||||
Globals.caboPenalty = _defaultCaboPenalty;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setInt(_keyPointLimit, _defaultPointLimit);
|
||||
await prefs.setInt(_keyCaboPenalty, _defaultCaboPenalty);
|
||||
}
|
||||
}
|
||||
164
lib/services/local_storage_service.dart
Normal file
164
lib/services/local_storage_service.dart
Normal file
@@ -0,0 +1,164 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:cabo_counter/data/game_session.dart';
|
||||
import 'package:cabo_counter/utility/globals.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:file_saver/file_saver.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:json_schema/json_schema.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
class LocalStorageService {
|
||||
static const String _fileName = 'game_data.json';
|
||||
|
||||
/// Writes the game session list to a JSON file and returns it as string.
|
||||
static String getJsonFile() {
|
||||
final jsonFile =
|
||||
Globals.gameList.map((session) => session.toJson()).toList();
|
||||
return json.encode(jsonFile);
|
||||
}
|
||||
|
||||
/// Returns the path to the local JSON file.
|
||||
static Future<File> _getFilePath() async {
|
||||
final directory = await getApplicationDocumentsDirectory();
|
||||
final path = '${directory.path}/$_fileName';
|
||||
return File(path);
|
||||
}
|
||||
|
||||
/// Saves the game sessions to a local JSON file.
|
||||
static Future<void> saveGameSessions() async {
|
||||
try {
|
||||
final file = await _getFilePath();
|
||||
final jsonFile = getJsonFile();
|
||||
await file.writeAsString(jsonFile);
|
||||
print('Daten gespeichert');
|
||||
} catch (e) {
|
||||
print('Fehler beim Speichern: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Loads the game data from a local JSON file.
|
||||
static Future<bool> loadGameSessions() async {
|
||||
print('Versuche, Daten zu laden...');
|
||||
try {
|
||||
final file = await _getFilePath();
|
||||
|
||||
if (!await file.exists()) {
|
||||
print('Es existiert noch keine Datei mit Spieldaten');
|
||||
return false;
|
||||
}
|
||||
|
||||
print('Es existiert bereits eine Datei mit Spieldaten');
|
||||
final jsonString = await file.readAsString();
|
||||
|
||||
if (jsonString.isEmpty) {
|
||||
print('Die gefundene Datei ist leer');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!await validateJsonSchema(jsonString)) {
|
||||
print('Die Datei konnte nicht validiert werden');
|
||||
Globals.gameList = [];
|
||||
return false;
|
||||
}
|
||||
|
||||
print('Die gefundene Datei ist nicht leer und validiert');
|
||||
final jsonList = json.decode(jsonString) as List<dynamic>;
|
||||
|
||||
Globals.gameList = jsonList
|
||||
.map((jsonItem) =>
|
||||
GameSession.fromJson(jsonItem as Map<String, dynamic>))
|
||||
.toList();
|
||||
|
||||
print('Die Daten wurden erfolgreich geladen und verarbeitet');
|
||||
return true;
|
||||
} catch (e) {
|
||||
print('Fehler beim Laden der Spieldaten:\n$e');
|
||||
Globals.gameList = [];
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Opens the file picker to save a JSON file with the current game data.
|
||||
static Future<bool> exportJsonFile() async {
|
||||
final jsonString = getJsonFile();
|
||||
try {
|
||||
final bytes = Uint8List.fromList(utf8.encode(jsonString));
|
||||
final result = await FileSaver.instance.saveAs(
|
||||
name: 'cabo_counter_data',
|
||||
bytes: bytes,
|
||||
ext: 'json',
|
||||
mimeType: MimeType.json,
|
||||
);
|
||||
print('Datei gespeichert: $result');
|
||||
return true;
|
||||
} catch (e) {
|
||||
print('Fehler beim Speichern: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Opens the file picker to import a JSON file and loads the game data from it.
|
||||
static Future<bool> importJsonFile() async {
|
||||
final result = await FilePicker.platform.pickFiles(
|
||||
dialogTitle: 'Wähle eine Datei mit Spieldaten aus',
|
||||
type: FileType.custom,
|
||||
allowedExtensions: ['json'],
|
||||
);
|
||||
|
||||
if (result == null) {
|
||||
print('Der Dialog wurde abgebrochen');
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
final jsonString = await _readFileContent(result.files.single);
|
||||
|
||||
if (!await validateJsonSchema(jsonString)) {
|
||||
return false;
|
||||
}
|
||||
final jsonData = json.decode(jsonString) as List<dynamic>;
|
||||
print('JSON Inhalt: $jsonData');
|
||||
Globals.gameList = jsonData
|
||||
.map((jsonItem) =>
|
||||
GameSession.fromJson(jsonItem as Map<String, dynamic>))
|
||||
.toList();
|
||||
return true;
|
||||
} on FormatException catch (e) {
|
||||
print('Ungültiges JSON-Format: $e');
|
||||
return false;
|
||||
} on Exception catch (e) {
|
||||
print('Fehler beim Dateizugriff: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper method to read file content from either bytes or path
|
||||
static Future<String> _readFileContent(PlatformFile file) async {
|
||||
if (file.bytes != null) return utf8.decode(file.bytes!);
|
||||
if (file.path != null) return await File(file.path!).readAsString();
|
||||
|
||||
throw Exception('Die Datei hat keinen lesbaren Inhalt');
|
||||
}
|
||||
|
||||
/// Validates the JSON data against the schema.
|
||||
static Future<bool> validateJsonSchema(String jsonString) async {
|
||||
try {
|
||||
final schemaString = await rootBundle.loadString('assets/schema.json');
|
||||
final schema = JsonSchema.create(json.decode(schemaString));
|
||||
final jsonData = json.decode(jsonString);
|
||||
final result = schema.validate(jsonData);
|
||||
|
||||
if (result.isValid) {
|
||||
print('JSON ist erfolgreich validiert.');
|
||||
return true;
|
||||
}
|
||||
print('JSON ist nicht gültig: ${result.errors}');
|
||||
return false;
|
||||
} catch (e) {
|
||||
print('Fehler beim Validieren des JSON-Schemas: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user