From 1133de61844baef86a5d3ca26068fdd8b672ab2a Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Sat, 3 May 2025 00:52:57 +0200 Subject: [PATCH] Implemented ConfigService --- lib/services/config_service.dart | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 lib/services/config_service.dart diff --git a/lib/services/config_service.dart b/lib/services/config_service.dart new file mode 100644 index 0000000..67c2e9d --- /dev/null +++ b/lib/services/config_service.dart @@ -0,0 +1,47 @@ +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 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 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 setPointLimit(int newPointLimit) async { + final prefs = await SharedPreferences.getInstance(); + await prefs.setInt(_keyPointLimit, newPointLimit); + } + + /// Getter for the cabo penalty. + static Future 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 setCaboPenalty(int newCaboPenalty) async { + final prefs = await SharedPreferences.getInstance(); + await prefs.setInt(_keyCaboPenalty, newCaboPenalty); + } +}