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 {
|
Future<void> main() async {
|
||||||
WidgetsFlutterBinding.ensureInitialized();
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
await ConfigService.initConfig();
|
await ConfigService.initConfig();
|
||||||
globals.pointLimit = await ConfigService.getPointLimit();
|
Globals.pointLimit = await ConfigService.getPointLimit();
|
||||||
globals.caboPenalty = await ConfigService.getCaboPenalty();
|
Globals.caboPenalty = await ConfigService.getCaboPenalty();
|
||||||
runApp(const App());
|
runApp(const App());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,8 +48,8 @@ class ConfigService {
|
|||||||
|
|
||||||
/// Resets the configuration to default values.
|
/// Resets the configuration to default values.
|
||||||
static Future<void> resetConfig() async {
|
static Future<void> resetConfig() async {
|
||||||
globals.pointLimit = _defaultPointLimit;
|
Globals.pointLimit = _defaultPointLimit;
|
||||||
globals.caboPenalty = _defaultCaboPenalty;
|
Globals.caboPenalty = _defaultCaboPenalty;
|
||||||
final prefs = await SharedPreferences.getInstance();
|
final prefs = await SharedPreferences.getInstance();
|
||||||
await prefs.setInt(_keyPointLimit, _defaultPointLimit);
|
await prefs.setInt(_keyPointLimit, _defaultPointLimit);
|
||||||
await prefs.setInt(_keyCaboPenalty, _defaultCaboPenalty);
|
await prefs.setInt(_keyCaboPenalty, _defaultCaboPenalty);
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:cabo_counter/data/game_manager.dart';
|
||||||
import 'package:cabo_counter/data/game_session.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_picker/file_picker.dart';
|
||||||
import 'package:file_saver/file_saver.dart';
|
import 'package:file_saver/file_saver.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
|||||||
@@ -1,25 +1,5 @@
|
|||||||
import 'package:cabo_counter/data/game_session.dart';
|
class Globals {
|
||||||
import 'package:cabo_counter/services/local_storage_service.dart';
|
static int pointLimit = 100;
|
||||||
import 'package:flutter/foundation.dart';
|
static int caboPenalty = 5;
|
||||||
|
static String appDevPhase = 'Alpha';
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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/data/game_session.dart';
|
||||||
import 'package:cabo_counter/services/local_storage_service.dart';
|
import 'package:cabo_counter/services/local_storage_service.dart';
|
||||||
import 'package:cabo_counter/utility/custom_theme.dart';
|
import 'package:cabo_counter/utility/custom_theme.dart';
|
||||||
@@ -81,7 +82,7 @@ class _CreateGameState extends State<CreateGame> {
|
|||||||
context,
|
context,
|
||||||
CupertinoPageRoute(
|
CupertinoPageRoute(
|
||||||
builder: (context) => ModeSelectionMenu(
|
builder: (context) => ModeSelectionMenu(
|
||||||
pointLimit: globals.pointLimit,
|
pointLimit: Globals.pointLimit,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -285,8 +286,8 @@ class _CreateGameState extends State<CreateGame> {
|
|||||||
createdAt: DateTime.now(),
|
createdAt: DateTime.now(),
|
||||||
gameTitle: _gameTitleTextController.text,
|
gameTitle: _gameTitleTextController.text,
|
||||||
players: players,
|
players: players,
|
||||||
pointLimit: globals.pointLimit,
|
pointLimit: Globals.pointLimit,
|
||||||
caboPenalty: globals.caboPenalty,
|
caboPenalty: Globals.caboPenalty,
|
||||||
isPointsLimitEnabled: selectedMode!,
|
isPointsLimitEnabled: selectedMode!,
|
||||||
);
|
);
|
||||||
globals.addGameSession(gameSession);
|
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/services/local_storage_service.dart';
|
||||||
import 'package:cabo_counter/utility/custom_theme.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/active_game_view.dart';
|
||||||
import 'package:cabo_counter/views/create_game_view.dart';
|
import 'package:cabo_counter/views/create_game_view.dart';
|
||||||
import 'package:cabo_counter/views/settings_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'),
|
subtitle: const Text('... für falsches Cabo sagen'),
|
||||||
trailing: Stepper(
|
trailing: Stepper(
|
||||||
key: _stepperKey1,
|
key: _stepperKey1,
|
||||||
initialValue: globals.caboPenalty,
|
initialValue: Globals.caboPenalty,
|
||||||
minValue: 0,
|
minValue: 0,
|
||||||
maxValue: 50,
|
maxValue: 50,
|
||||||
step: 1,
|
step: 1,
|
||||||
onChanged: (newCaboPenalty) {
|
onChanged: (newCaboPenalty) {
|
||||||
setState(() {
|
setState(() {
|
||||||
ConfigService.setCaboPenalty(newCaboPenalty);
|
ConfigService.setCaboPenalty(newCaboPenalty);
|
||||||
globals.caboPenalty = newCaboPenalty;
|
Globals.caboPenalty = newCaboPenalty;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@@ -70,14 +70,14 @@ class _SettingsViewState extends State<SettingsView> {
|
|||||||
subtitle: const Text('... hier ist Schluss'),
|
subtitle: const Text('... hier ist Schluss'),
|
||||||
trailing: Stepper(
|
trailing: Stepper(
|
||||||
key: _stepperKey2,
|
key: _stepperKey2,
|
||||||
initialValue: globals.pointLimit,
|
initialValue: Globals.pointLimit,
|
||||||
minValue: 30,
|
minValue: 30,
|
||||||
maxValue: 1000,
|
maxValue: 1000,
|
||||||
step: 10,
|
step: 10,
|
||||||
onChanged: (newPointLimit) {
|
onChanged: (newPointLimit) {
|
||||||
setState(() {
|
setState(() {
|
||||||
ConfigService.setPointLimit(newPointLimit);
|
ConfigService.setPointLimit(newPointLimit);
|
||||||
globals.pointLimit = newPointLimit;
|
Globals.pointLimit = newPointLimit;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@@ -201,7 +201,7 @@ class _SettingsViewState extends State<SettingsView> {
|
|||||||
builder: (context, snapshot) {
|
builder: (context, snapshot) {
|
||||||
if (snapshot.hasData) {
|
if (snapshot.hasData) {
|
||||||
return Text(
|
return Text(
|
||||||
'${globals.appDevPhase} ${snapshot.data!.version} '
|
'${Globals.appDevPhase} ${snapshot.data!.version} '
|
||||||
'(Build ${snapshot.data!.buildNumber})',
|
'(Build ${snapshot.data!.buildNumber})',
|
||||||
textAlign: TextAlign.center,
|
textAlign: TextAlign.center,
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user