Files
cabo-counter/lib/services/config_service.dart
Felix Kirchner d627f33579 Beta-Version 0.5.3 (#136)
* Updated createGameView ListBuilder

* Added ReorderableListView

* Increment build no

* Fixed bug with wrong medal icon

* change not equal to greater than

* Updated bool var

* Fixed deletion error

* Small translation improvements

* Implemented first version of point overview

* Visual improvements on table

* Added details and sum row

* Updated strings

* Implemented new strings

* Refactoring

* Updated graph displayment

* Moved new views to statistics section

* Added seperator in main menu

* Renaming

* Updated sign

* Updated colors & class name

* Removed empty line

* Updated round index

* Updated types

* Added new kamikaze button and bundles navigation functionality

* Updated lock icon

* Updated button position and design

* Removed title row and changed segmendetControl Padding

* Refactored logic and added comments

* Updated comment

* Chaned icon

* Added comment

* Removed print

* Updated colors

* Changed var name

* Removed unused strings

* Added gameMode

* Changed creation variable

* Updated mode selection

* Updated strings

* Changed mode order

* Implemented default mode selection

* Updated initState

* Removed print

* Removed print

* Removed comments

* Updated config service

* Changed create game view

* Changed icon

* Updated strings

* Updated config

* Updated mode selection logic

* Deleted getter

* Removed not used code

* Implemented reset logic for default game mode

* Updated to 0.5.0

* Hotfix: Pixel Overflow

* Changed the overall return type for gamemodes

* Updated documentation

* Fixed merge issues

* Added Custom button

* Updated strings

* Updated buttons, implemented animatedOpacity

* Keyboard still doesnt works

* Fixed keyboard behaviour

* Changed keyboard height

* Added method getGameSessionById()

* Updated gameSession class

* id gets added to gameSession class at creation

* Cleaned up file

* Added docs and dependency

* Removed toString

* Implemented null safety

* Added named parameter

* Replaced button with custom button

* Updated key

* Updated addGameSessionMethod

* Update README.md

* Added Strings for popup

* Implemented popup & confetti

* Extracted code to method _playFinishAnimation()

* Replaced tenary operator with Visibility Widget

* Replaced tenary operator with Visibility Widget

* Used variable again

* Added delays in constants.dart

* Removed confetti button

* Updated strings

* Removed print

* Added dispose for confettiController

* Implemented missing constant in code

* Updated gameSession logic so more than one player can be winner

* Updated strings

* Updated winner popup

* game names now can have up to 20 chars

* Updated strings

* Added sized box for visual enhancement

* Centered the add player button and made it wider

* New created player textfields get automatically focused

* Added focus nodes for autofocus and navigation between textfields

* Updated version number

* Updated game title textfield with focus node and textaction

* Added focusnodes to dispose

* Update README.md

* Fixed bug with no popup shown

* Fixed bug with out of range error

* Updated listener notification
2025-07-21 13:29:25 +02:00

110 lines
3.9 KiB
Dart

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.
/// 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 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();
// 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);
}
/// 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;
}
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 {
ConfigService._pointLimit = _defaultPointLimit;
ConfigService._caboPenalty = _defaultCaboPenalty;
ConfigService._gameMode = _defaultGameMode;
final prefs = await SharedPreferences.getInstance();
await prefs.setInt(_keyPointLimit, _defaultPointLimit);
await prefs.setInt(_keyCaboPenalty, _defaultCaboPenalty);
}
}