diff --git a/lib/data/game_manager.dart b/lib/data/game_manager.dart new file mode 100644 index 0000000..578b03b --- /dev/null +++ b/lib/data/game_manager.dart @@ -0,0 +1,30 @@ +import 'package:cabo_counter/data/game_session.dart'; +import 'package:cabo_counter/services/local_storage_service.dart'; +import 'package:flutter/foundation.dart'; + +class GameManager extends ChangeNotifier { + List gameList = []; + + /// Adds a new game session to the list and sorts it by creation date. + /// Takes a [GameSession] object as input. It then adds the session to the `gameList`, + /// sorts the list in descending order based on the creation date, and notifies listeners of the change. + /// It also saves the updated game sessions to local storage. + void addGameSession(GameSession session) { + gameList.add(session); + gameList.sort((a, b) => b.createdAt.compareTo(a.createdAt)); + notifyListeners(); + LocalStorageService.saveGameSessions(); + } + + /// Removes a game session from the list and sorts it by creation date. + /// Takes a [index] as input. It then removes the session at the specified index from the `gameList`, + /// sorts the list in descending order based on the creation date, and notifies listeners of the change. + /// It also saves the updated game sessions to local storage. + void removeGameSession(int index) { + gameList.removeAt(index); + notifyListeners(); + LocalStorageService.saveGameSessions(); + } +} + +final globals = GameManager(); diff --git a/lib/main.dart b/lib/main.dart index 26ff4eb..edd7d3c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -8,8 +8,8 @@ import 'package:flutter/cupertino.dart'; Future main() async { WidgetsFlutterBinding.ensureInitialized(); await ConfigService.initConfig(); - globals.pointLimit = await ConfigService.getPointLimit(); - globals.caboPenalty = await ConfigService.getCaboPenalty(); + Globals.pointLimit = await ConfigService.getPointLimit(); + Globals.caboPenalty = await ConfigService.getCaboPenalty(); runApp(const App()); } diff --git a/lib/services/config_service.dart b/lib/services/config_service.dart index a4336e0..1c8275a 100644 --- a/lib/services/config_service.dart +++ b/lib/services/config_service.dart @@ -48,8 +48,8 @@ class ConfigService { /// Resets the configuration to default values. static Future resetConfig() async { - globals.pointLimit = _defaultPointLimit; - globals.caboPenalty = _defaultCaboPenalty; + Globals.pointLimit = _defaultPointLimit; + Globals.caboPenalty = _defaultCaboPenalty; final prefs = await SharedPreferences.getInstance(); await prefs.setInt(_keyPointLimit, _defaultPointLimit); await prefs.setInt(_keyCaboPenalty, _defaultCaboPenalty); diff --git a/lib/services/local_storage_service.dart b/lib/services/local_storage_service.dart index 5ca438e..a6d005c 100644 --- a/lib/services/local_storage_service.dart +++ b/lib/services/local_storage_service.dart @@ -1,8 +1,8 @@ import 'dart:convert'; import 'dart:io'; +import 'package:cabo_counter/data/game_manager.dart'; import 'package:cabo_counter/data/game_session.dart'; -import 'package:cabo_counter/utility/globals.dart'; import 'package:file_picker/file_picker.dart'; import 'package:file_saver/file_saver.dart'; import 'package:flutter/services.dart'; diff --git a/lib/utility/globals.dart b/lib/utility/globals.dart index 7b4d7e0..4244f07 100644 --- a/lib/utility/globals.dart +++ b/lib/utility/globals.dart @@ -1,25 +1,5 @@ -import 'package:cabo_counter/data/game_session.dart'; -import 'package:cabo_counter/services/local_storage_service.dart'; -import 'package:flutter/foundation.dart'; - -class Globals extends ChangeNotifier { - List gameList = []; - int pointLimit = 100; - int caboPenalty = 5; - String appDevPhase = 'Alpha'; - - void addGameSession(GameSession session) { - gameList.add(session); - gameList.sort((a, b) => b.createdAt.compareTo(a.createdAt)); - notifyListeners(); // Wichtig! - LocalStorageService.saveGameSessions(); - } - - void removeGameSession(int index) { - gameList.removeAt(index); - notifyListeners(); // Wichtig! - LocalStorageService.saveGameSessions(); - } +class Globals { + static int pointLimit = 100; + static int caboPenalty = 5; + static String appDevPhase = 'Alpha'; } - -final globals = Globals(); // Globale Instanz diff --git a/lib/views/create_game_view.dart b/lib/views/create_game_view.dart index aa0d40f..92ce4f0 100644 --- a/lib/views/create_game_view.dart +++ b/lib/views/create_game_view.dart @@ -1,3 +1,4 @@ +import 'package:cabo_counter/data/game_manager.dart'; import 'package:cabo_counter/data/game_session.dart'; import 'package:cabo_counter/services/local_storage_service.dart'; import 'package:cabo_counter/utility/custom_theme.dart'; @@ -81,7 +82,7 @@ class _CreateGameState extends State { context, CupertinoPageRoute( builder: (context) => ModeSelectionMenu( - pointLimit: globals.pointLimit, + pointLimit: Globals.pointLimit, ), ), ); @@ -285,8 +286,8 @@ class _CreateGameState extends State { createdAt: DateTime.now(), gameTitle: _gameTitleTextController.text, players: players, - pointLimit: globals.pointLimit, - caboPenalty: globals.caboPenalty, + pointLimit: Globals.pointLimit, + caboPenalty: Globals.caboPenalty, isPointsLimitEnabled: selectedMode!, ); globals.addGameSession(gameSession); diff --git a/lib/views/main_menu_view.dart b/lib/views/main_menu_view.dart index 9572c82..0420a4c 100644 --- a/lib/views/main_menu_view.dart +++ b/lib/views/main_menu_view.dart @@ -1,6 +1,6 @@ +import 'package:cabo_counter/data/game_manager.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'; diff --git a/lib/views/settings_view.dart b/lib/views/settings_view.dart index 36c7099..8f0dcf4 100644 --- a/lib/views/settings_view.dart +++ b/lib/views/settings_view.dart @@ -50,14 +50,14 @@ class _SettingsViewState extends State { subtitle: const Text('... für falsches Cabo sagen'), trailing: Stepper( key: _stepperKey1, - initialValue: globals.caboPenalty, + initialValue: Globals.caboPenalty, minValue: 0, maxValue: 50, step: 1, onChanged: (newCaboPenalty) { setState(() { ConfigService.setCaboPenalty(newCaboPenalty); - globals.caboPenalty = newCaboPenalty; + Globals.caboPenalty = newCaboPenalty; }); }, ), @@ -70,14 +70,14 @@ class _SettingsViewState extends State { subtitle: const Text('... hier ist Schluss'), trailing: Stepper( key: _stepperKey2, - initialValue: globals.pointLimit, + initialValue: Globals.pointLimit, minValue: 30, maxValue: 1000, step: 10, onChanged: (newPointLimit) { setState(() { ConfigService.setPointLimit(newPointLimit); - globals.pointLimit = newPointLimit; + Globals.pointLimit = newPointLimit; }); }, ), @@ -201,7 +201,7 @@ class _SettingsViewState extends State { builder: (context, snapshot) { if (snapshot.hasData) { return Text( - '${globals.appDevPhase} ${snapshot.data!.version} ' + '${Globals.appDevPhase} ${snapshot.data!.version} ' '(Build ${snapshot.data!.buildNumber})', textAlign: TextAlign.center, );