Changed the overall return type for gamemodes

This commit is contained in:
2025-07-20 16:34:00 +02:00
parent 96471a5764
commit 261e8f80c1
9 changed files with 103 additions and 100 deletions

View File

@@ -1,3 +1,4 @@
import 'package:cabo_counter/presentation/views/mode_selection_view.dart';
import 'package:shared_preferences/shared_preferences.dart';
/// This class handles the configuration settings for the app.
@@ -8,9 +9,9 @@ class ConfigService {
static const String _keyCaboPenalty = 'caboPenalty';
static const String _keyGameMode = 'gameMode';
// Actual values used in the app
static int pointLimit = 100;
static int caboPenalty = 5;
static int gameMode = -1;
static int _pointLimit = 100;
static int _caboPenalty = 5;
static int _gameMode = -1;
// Default values
static const int _defaultPointLimit = 100;
static const int _defaultCaboPenalty = 5;
@@ -21,43 +22,70 @@ class ConfigService {
// 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;
_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);
prefs.setInt(_keyPointLimit, _pointLimit);
prefs.setInt(_keyCaboPenalty, _caboPenalty);
prefs.setInt(_keyGameMode, _gameMode);
}
static Future<void> setGameMode(int newGameMode) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setInt(_keyGameMode, newGameMode);
gameMode = newGameMode;
static GameMode getGameMode() {
switch (_gameMode) {
case 0:
return GameMode.pointLimit;
case 1:
return GameMode.unlimited;
default:
return GameMode.none;
}
}
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;
_pointLimit = newPointLimit;
}
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;
_caboPenalty = newCaboPenalty;
}
/// Resets the configuration to default values.
static Future<void> resetConfig() async {
ConfigService.pointLimit = _defaultPointLimit;
ConfigService.caboPenalty = _defaultCaboPenalty;
ConfigService.gameMode = _defaultGameMode;
ConfigService._pointLimit = _defaultPointLimit;
ConfigService._caboPenalty = _defaultCaboPenalty;
ConfigService._gameMode = _defaultGameMode;
final prefs = await SharedPreferences.getInstance();
await prefs.setInt(_keyPointLimit, _defaultPointLimit);
await prefs.setInt(_keyCaboPenalty, _defaultCaboPenalty);