Moved config values to ConfigService

This commit is contained in:
2025-07-07 23:14:11 +02:00
parent a13a3ac0c9
commit ace157dc47
6 changed files with 21 additions and 21 deletions

View File

@@ -2,7 +2,6 @@ import 'package:cabo_counter/l10n/app_localizations.dart';
import 'package:cabo_counter/services/config_service.dart';
import 'package:cabo_counter/services/local_storage_service.dart';
import 'package:cabo_counter/utility/custom_theme.dart';
import 'package:cabo_counter/utility/globals.dart';
import 'package:cabo_counter/views/tab_view.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
@@ -12,8 +11,8 @@ Future<void> main() async {
await SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
await ConfigService.initConfig();
Globals.pointLimit = await ConfigService.getPointLimit();
Globals.caboPenalty = await ConfigService.getCaboPenalty();
ConfigService.pointLimit = await ConfigService.getPointLimit();
ConfigService.caboPenalty = await ConfigService.getCaboPenalty();
runApp(const App());
}

View File

@@ -1,4 +1,3 @@
import 'package:cabo_counter/utility/globals.dart';
import 'package:shared_preferences/shared_preferences.dart';
/// This class handles the configuration settings for the app.
@@ -7,8 +6,12 @@ import 'package:shared_preferences/shared_preferences.dart';
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
// Actual values used in the app
static int pointLimit = 100;
static int caboPenalty = 5;
// Default values
static const int _defaultPointLimit = 100;
static const int _defaultCaboPenalty = 5;
static Future<void> initConfig() async {
final prefs = await SharedPreferences.getInstance();
@@ -48,8 +51,8 @@ class ConfigService {
/// Resets the configuration to default values.
static Future<void> resetConfig() async {
Globals.pointLimit = _defaultPointLimit;
Globals.caboPenalty = _defaultCaboPenalty;
ConfigService.pointLimit = _defaultPointLimit;
ConfigService.caboPenalty = _defaultCaboPenalty;
final prefs = await SharedPreferences.getInstance();
await prefs.setInt(_keyPointLimit, _defaultPointLimit);
await prefs.setInt(_keyCaboPenalty, _defaultCaboPenalty);

View File

@@ -1,5 +1,3 @@
class Globals {
static int pointLimit = 100;
static int caboPenalty = 5;
static String appDevPhase = 'Beta';
}

View File

@@ -1,8 +1,8 @@
import 'package:cabo_counter/data/game_manager.dart';
import 'package:cabo_counter/data/game_session.dart';
import 'package:cabo_counter/l10n/app_localizations.dart';
import 'package:cabo_counter/services/config_service.dart';
import 'package:cabo_counter/utility/custom_theme.dart';
import 'package:cabo_counter/utility/globals.dart';
import 'package:cabo_counter/views/active_game_view.dart';
import 'package:cabo_counter/views/mode_selection_view.dart';
import 'package:flutter/cupertino.dart';
@@ -96,7 +96,7 @@ class _CreateGameViewState extends State<CreateGameView> {
_isPointsLimitEnabled == null
? AppLocalizations.of(context).select_mode
: (_isPointsLimitEnabled!
? '${Globals.pointLimit} ${AppLocalizations.of(context).points}'
? '${ConfigService.pointLimit} ${AppLocalizations.of(context).points}'
: AppLocalizations.of(context).unlimited),
),
const SizedBox(width: 3),
@@ -108,7 +108,7 @@ class _CreateGameViewState extends State<CreateGameView> {
context,
CupertinoPageRoute(
builder: (context) => ModeSelectionMenu(
pointLimit: Globals.pointLimit,
pointLimit: ConfigService.pointLimit,
),
),
);
@@ -315,8 +315,8 @@ class _CreateGameViewState extends State<CreateGameView> {
createdAt: DateTime.now(),
gameTitle: _gameTitleTextController.text,
players: players,
pointLimit: Globals.pointLimit,
caboPenalty: Globals.caboPenalty,
pointLimit: ConfigService.pointLimit,
caboPenalty: ConfigService.caboPenalty,
isPointsLimitEnabled: _isPointsLimitEnabled!,
);
final index = await gameManager.addGameSession(gameSession);

View File

@@ -1,8 +1,8 @@
import 'package:cabo_counter/data/game_manager.dart';
import 'package:cabo_counter/l10n/app_localizations.dart';
import 'package:cabo_counter/services/config_service.dart';
import 'package:cabo_counter/services/local_storage_service.dart';
import 'package:cabo_counter/utility/custom_theme.dart';
import 'package:cabo_counter/utility/globals.dart';
import 'package:cabo_counter/views/active_game_view.dart';
import 'package:cabo_counter/views/create_game_view.dart';
import 'package:cabo_counter/views/settings_view.dart';
@@ -199,7 +199,7 @@ class _MainMenuViewState extends State<MainMenuView> {
/// If [pointLimit] is true, it returns '101 Punkte', otherwise it returns 'Unbegrenzt'.
String _translateGameMode(bool pointLimit) {
if (pointLimit) {
return '${Globals.pointLimit} ${AppLocalizations.of(context).points}';
return '${ConfigService.pointLimit} ${AppLocalizations.of(context).points}';
}
return AppLocalizations.of(context).unlimited;
}

View File

@@ -52,14 +52,14 @@ class _SettingsViewState extends State<SettingsView> {
AppLocalizations.of(context).cabo_penalty_subtitle),
trailing: Stepper(
key: _stepperKey1,
initialValue: Globals.caboPenalty,
initialValue: ConfigService.caboPenalty,
minValue: 0,
maxValue: 50,
step: 1,
onChanged: (newCaboPenalty) {
setState(() {
ConfigService.setCaboPenalty(newCaboPenalty);
Globals.caboPenalty = newCaboPenalty;
ConfigService.caboPenalty = newCaboPenalty;
});
},
),
@@ -73,14 +73,14 @@ class _SettingsViewState extends State<SettingsView> {
Text(AppLocalizations.of(context).point_limit_subtitle),
trailing: Stepper(
key: _stepperKey2,
initialValue: Globals.pointLimit,
initialValue: ConfigService.pointLimit,
minValue: 30,
maxValue: 1000,
step: 10,
onChanged: (newPointLimit) {
setState(() {
ConfigService.setPointLimit(newPointLimit);
Globals.pointLimit = newPointLimit;
ConfigService.pointLimit = newPointLimit;
});
},
),