Merge branch 'develop' into feature/60-implement-drift-database

# Conflicts:
#	lib/views/active_game_view.dart
#	lib/views/create_game_view.dart
#	lib/views/graph_view.dart
#	lib/views/main_menu_view.dart
#	lib/views/round_view.dart
#	pubspec.yaml
This commit is contained in:
2025-08-21 19:19:32 +02:00
54 changed files with 11273 additions and 2485 deletions

View File

@@ -1,55 +1,109 @@
import 'package:cabo_counter/utility/globals.dart';
import 'package:cabo_counter/presentation/views/home/active_game/mode_selection_view.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.
/// A service class for managing and persisting app configuration settings using `SharedPreferences`.
///
/// Provides methods to initialize, retrieve, update, and reset configuration values such as point limit,
/// cabo penalty, and game mode. Ensures that user preferences are stored locally and persist across app restarts.
class ConfigService {
// Keys for the stored values
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 const String _keyGameMode = 'gameMode';
// Actual values used in the app
static int _pointLimit = 100;
static int _caboPenalty = 5;
static int _gameMode = -1;
// Default values
static const int _defaultPointLimit = 100;
static const int _defaultCaboPenalty = 5;
static const int _defaultGameMode = -1;
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);
// Initialize pointLimit, caboPenalty, and gameMode from SharedPreferences
// If they are not set, use the default values
_pointLimit = prefs.getInt(_keyPointLimit) ?? _defaultPointLimit;
_caboPenalty = prefs.getInt(_keyCaboPenalty) ?? _defaultCaboPenalty;
_gameMode = prefs.getInt(_keyGameMode) ?? _defaultGameMode;
// Save the initial values to SharedPreferences
prefs.setInt(_keyPointLimit, _pointLimit);
prefs.setInt(_keyCaboPenalty, _caboPenalty);
prefs.setInt(_keyGameMode, _gameMode);
}
/// Getter for the point limit.
static Future<int> getPointLimit() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getInt(_keyPointLimit) ?? _defaultPointLimit;
/// Retrieves the current game mode.
///
/// The game mode is determined based on the stored integer value:
/// - `0`: [GameMode.pointLimit]
/// - `1`: [GameMode.unlimited]
/// - Any other value: [GameMode.none] (-1 is used as a default for no mode)
///
/// Returns the corresponding [GameMode] enum value.
static GameMode getGameMode() {
switch (_gameMode) {
case 0:
return GameMode.pointLimit;
case 1:
return GameMode.unlimited;
default:
return GameMode.none;
}
}
/// Sets the game mode for the application.
///
/// [newGameMode] is the new game mode to be set. It can be one of the following:
/// - `GameMode.pointLimit`: The game ends when a pleayer reaches the point limit.
/// - `GameMode.unlimited`: Every game goes for infinity until you end it.
/// - `GameMode.none`: No default mode set.
///
/// This method updates the `_gameMode` field and persists the value in `SharedPreferences`.
static Future<void> setGameMode(GameMode newGameMode) async {
int gameMode;
switch (newGameMode) {
case GameMode.pointLimit:
gameMode = 0;
break;
case GameMode.unlimited:
gameMode = 1;
break;
default:
gameMode = -1;
}
final prefs = await SharedPreferences.getInstance();
await prefs.setInt(_keyGameMode, gameMode);
_gameMode = gameMode;
}
static int getPointLimit() => _pointLimit;
/// 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);
_pointLimit = newPointLimit;
}
/// Getter for the cabo penalty.
static Future<int> getCaboPenalty() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getInt(_keyCaboPenalty) ?? _defaultCaboPenalty;
}
static int getCaboPenalty() => _caboPenalty;
/// 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);
_caboPenalty = newCaboPenalty;
}
/// Resets the configuration to default values.
static Future<void> resetConfig() async {
Globals.pointLimit = _defaultPointLimit;
Globals.caboPenalty = _defaultCaboPenalty;
ConfigService._pointLimit = _defaultPointLimit;
ConfigService._caboPenalty = _defaultCaboPenalty;
ConfigService._gameMode = _defaultGameMode;
final prefs = await SharedPreferences.getInstance();
await prefs.setInt(_keyPointLimit, _defaultPointLimit);
await prefs.setInt(_keyCaboPenalty, _defaultCaboPenalty);

View File

@@ -9,11 +9,19 @@ import 'package:flutter/services.dart';
import 'package:json_schema/json_schema.dart';
import 'package:path_provider/path_provider.dart';
enum ImportStatus {
success,
canceled,
validationError,
formatError,
genericError
}
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() {
/// Writes the game session list to a JSON file and returns it as string.
static String _getGameDataAsJsonFile() {
final jsonFile =
gameManager.gameList.map((session) => session.toJson()).toList();
return json.encode(jsonFile);
@@ -31,7 +39,7 @@ class LocalStorageService {
print('[local_storage_service.dart] Versuche, Daten zu speichern...');
try {
final file = await _getFilePath();
final jsonFile = getJsonFile();
final jsonFile = _getGameDataAsJsonFile();
await file.writeAsString(jsonFile);
print(
'[local_storage_service.dart] Die Spieldaten wurden zwischengespeichert.');
@@ -47,6 +55,7 @@ class LocalStorageService {
try {
final file = await _getFilePath();
// Check if the file exists
if (!await file.exists()) {
print(
'[local_storage_service.dart] Es existiert noch keine Datei mit Spieldaten');
@@ -57,12 +66,14 @@ class LocalStorageService {
'[local_storage_service.dart] Es existiert bereits eine Datei mit Spieldaten');
final jsonString = await file.readAsString();
// Check if the file is empty
if (jsonString.isEmpty) {
print('[local_storage_service.dart] Die gefundene Datei ist leer');
return false;
}
if (!await validateJsonSchema(jsonString)) {
// Validate the JSON schema
if (!await _validateJsonSchema(jsonString, true)) {
print(
'[local_storage_service.dart] Die Datei konnte nicht validiert werden');
gameManager.gameList = [];
@@ -78,6 +89,11 @@ class LocalStorageService {
GameSession.fromJson(jsonItem as Map<String, dynamic>))
.toList();
for (GameSession session in gameManager.gameList) {
print(
'[local_storage_service.dart] Geladene Session: ${session.gameTitle} - ${session.id}');
}
print(
'[local_storage_service.dart] Die Spieldaten wurden erfolgreich geladen und verarbeitet');
return true;
@@ -89,19 +105,27 @@ class LocalStorageService {
}
}
/// Opens the file picker to save a JSON file with the current game data.
static Future<bool> exportJsonFile() async {
final jsonString = getJsonFile();
/// Opens the file picker to export game data as a JSON file.
/// This method will export the given [jsonString] as a JSON file. It opens
/// the file picker with the choosen [fileName].
static Future<bool> _exportJsonData(
String jsonString,
String fileName,
) async {
try {
final bytes = Uint8List.fromList(utf8.encode(jsonString));
final result = await FileSaver.instance.saveAs(
name: 'cabo_counter_data',
final path = await FileSaver.instance.saveAs(
name: fileName,
bytes: bytes,
ext: 'json',
mimeType: MimeType.json,
);
print(
'[local_storage_service.dart] Die Spieldaten wurden exportiert. Dateipfad: $result');
if (path == null) {
print('[local_storage_service.dart]: Export abgebrochen');
} else {
print(
'[local_storage_service.dart] Die Spieldaten wurden exportiert. Dateipfad: $path');
}
return true;
} catch (e) {
print(
@@ -110,45 +134,82 @@ class LocalStorageService {
}
}
/// Opens the file picker to export all game sessions as a JSON file.
static Future<bool> exportGameData() async {
String jsonString = _getGameDataAsJsonFile();
String fileName = 'cabo_counter-game_data';
return _exportJsonData(jsonString, fileName);
}
/// Opens the file picker to save a single game session as a JSON file.
static Future<bool> exportSingleGameSession(GameSession session) async {
String jsonString = json.encode(session.toJson());
String fileName = 'cabo_counter-game_${session.id.substring(0, 7)}';
return _exportJsonData(jsonString, fileName);
}
/// 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',
static Future<ImportStatus> importJsonFile() async {
final path = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['json'],
);
if (result == null) {
if (path == null) {
print(
'[local_storage_service.dart] Der Filepicker-Dialog wurde abgebrochen');
return false;
return ImportStatus.canceled;
}
try {
final jsonString = await _readFileContent(result.files.single);
final jsonString = await _readFileContent(path.files.single);
if (!await validateJsonSchema(jsonString)) {
return false;
// Checks if the JSON String is in the gameList format
if (await _validateJsonSchema(jsonString, true)) {
final jsonData = json.decode(jsonString) as List<dynamic>;
List<GameSession> importedList = jsonData
.map((jsonItem) =>
GameSession.fromJson(jsonItem as Map<String, dynamic>))
.toList();
for (GameSession s in importedList) {
_importSession(s);
}
} else if (await _validateJsonSchema(jsonString, false)) {
// Checks if the JSON String is in the single game format
final jsonData = json.decode(jsonString) as Map<String, dynamic>;
_importSession(GameSession.fromJson(jsonData));
} else {
return ImportStatus.validationError;
}
final jsonData = json.decode(jsonString) as List<dynamic>;
gameManager.gameList = jsonData
.map((jsonItem) =>
GameSession.fromJson(jsonItem as Map<String, dynamic>))
.toList();
print(
'[local_storage_service.dart] Die Datei wurde erfolgreich Importiertn');
return true;
'[local_storage_service.dart] Die Datei wurde erfolgreich Importiert');
await saveGameSessions();
return ImportStatus.success;
} on FormatException catch (e) {
print(
'[local_storage_service.dart] Ungültiges JSON-Format. Exception: $e');
return false;
return ImportStatus.formatError;
} on Exception catch (e) {
print(
'[local_storage_service.dart] Fehler beim Dateizugriff. Exception: $e');
return false;
return ImportStatus.genericError;
}
}
/// Imports a single game session into the gameList.
static Future<void> _importSession(GameSession session) async {
if (gameManager.gameExistsInGameList(session.id)) {
print(
'[local_storage_service.dart] Die Session mit der ID ${session.id} existiert bereits. Sie wird überschrieben.');
gameManager.removeGameSessionById(session.id);
}
gameManager.addGameSession(session);
print(
'[local_storage_service.dart] Die Session mit der ID ${session.id} wurde erfolgreich importiert.');
}
/// 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!);
@@ -158,15 +219,28 @@ class LocalStorageService {
}
/// Validates the JSON data against the schema.
static Future<bool> validateJsonSchema(String jsonString) async {
/// 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(
String jsonString, bool isGameList) async {
final String schemaString;
if (isGameList) {
schemaString =
await rootBundle.loadString('assets/game_list-schema.json');
} else {
schemaString = await rootBundle.loadString('assets/game-schema.json');
}
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('[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(

View File

@@ -0,0 +1,32 @@
import 'package:cabo_counter/core/constants.dart';
import 'package:package_info_plus/package_info_plus.dart';
class VersionService {
static String _version = '-.-.-';
static String _buildNumber = '-';
static Future<void> init() async {
var packageInfo = await PackageInfo.fromPlatform();
_version = packageInfo.version;
_buildNumber = packageInfo.buildNumber;
}
static String getVersionNumber() {
return _version;
}
static String getVersion() {
if (_version == '-.-.-') {
return getVersionNumber();
}
return '${Constants.appDevPhase} $_version';
}
static String getBuildNumber() {
return _buildNumber;
}
static String getVersionWithBuild() {
return '$_version ($_buildNumber)';
}
}