Refactoring & comments
This commit is contained in:
30
lib/data/game_manager.dart
Normal file
30
lib/data/game_manager.dart
Normal file
@@ -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<GameSession> 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();
|
||||
@@ -8,8 +8,8 @@ import 'package:flutter/cupertino.dart';
|
||||
Future<void> 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());
|
||||
}
|
||||
|
||||
|
||||
@@ -48,8 +48,8 @@ class ConfigService {
|
||||
|
||||
/// Resets the configuration to default values.
|
||||
static Future<void> 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);
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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<GameSession> 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
|
||||
|
||||
@@ -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<CreateGame> {
|
||||
context,
|
||||
CupertinoPageRoute(
|
||||
builder: (context) => ModeSelectionMenu(
|
||||
pointLimit: globals.pointLimit,
|
||||
pointLimit: Globals.pointLimit,
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -285,8 +286,8 @@ class _CreateGameState extends State<CreateGame> {
|
||||
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);
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -50,14 +50,14 @@ class _SettingsViewState extends State<SettingsView> {
|
||||
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<SettingsView> {
|
||||
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<SettingsView> {
|
||||
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,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user