Refactoring

This commit is contained in:
2025-06-08 20:39:36 +02:00
parent bcca541f3a
commit 0e1a6cdeaf
4 changed files with 16 additions and 16 deletions

View File

@@ -27,4 +27,4 @@ class GameManager extends ChangeNotifier {
} }
} }
final globals = GameManager(); final gameManager = GameManager();

View File

@@ -19,7 +19,7 @@ class LocalStorageService {
/// Writes the game session list to a JSON file and returns it as string. /// Writes the game session list to a JSON file and returns it as string.
static String getJsonFile() { static String getJsonFile() {
final jsonFile = final jsonFile =
globals.gameList.map((session) => session.toJson()).toList(); gameManager.gameList.map((session) => session.toJson()).toList();
return json.encode(jsonFile); return json.encode(jsonFile);
} }
@@ -63,14 +63,14 @@ class LocalStorageService {
if (!await validateJsonSchema(jsonString)) { if (!await validateJsonSchema(jsonString)) {
logger.w('Die Datei konnte nicht validiert werden'); logger.w('Die Datei konnte nicht validiert werden');
globals.gameList = []; gameManager.gameList = [];
return false; return false;
} }
logger.d('Die gefundene Datei hat Inhalt'); logger.d('Die gefundene Datei hat Inhalt');
logger.d('Die gefundene Datei wurde erfolgreich validiert'); logger.d('Die gefundene Datei wurde erfolgreich validiert');
final jsonList = json.decode(jsonString) as List<dynamic>; final jsonList = json.decode(jsonString) as List<dynamic>;
globals.gameList = jsonList gameManager.gameList = jsonList
.map((jsonItem) => .map((jsonItem) =>
GameSession.fromJson(jsonItem as Map<String, dynamic>)) GameSession.fromJson(jsonItem as Map<String, dynamic>))
.toList(); .toList();
@@ -80,7 +80,7 @@ class LocalStorageService {
} catch (e) { } catch (e) {
logger.e('Fehler beim Laden der Spieldaten:\n$e', logger.e('Fehler beim Laden der Spieldaten:\n$e',
error: 'JSON nicht geladen'); error: 'JSON nicht geladen');
globals.gameList = []; gameManager.gameList = [];
return false; return false;
} }
} }
@@ -125,7 +125,7 @@ class LocalStorageService {
return false; return false;
} }
final jsonData = json.decode(jsonString) as List<dynamic>; final jsonData = json.decode(jsonString) as List<dynamic>;
globals.gameList = jsonData gameManager.gameList = jsonData
.map((jsonItem) => .map((jsonItem) =>
GameSession.fromJson(jsonItem as Map<String, dynamic>)) GameSession.fromJson(jsonItem as Map<String, dynamic>))
.toList(); .toList();
@@ -172,7 +172,7 @@ class LocalStorageService {
static Future<bool> deleteAllGames() async { static Future<bool> deleteAllGames() async {
try { try {
globals.gameList.clear(); gameManager.gameList.clear();
await saveGameSessions(); await saveGameSessions();
logger.i('Alle Runden wurden erfolgreich gelöscht.'); logger.i('Alle Runden wurden erfolgreich gelöscht.');
return true; return true;

View File

@@ -290,7 +290,7 @@ class _CreateGameState extends State<CreateGame> {
caboPenalty: Globals.caboPenalty, caboPenalty: Globals.caboPenalty,
isPointsLimitEnabled: selectedMode!, isPointsLimitEnabled: selectedMode!,
); );
globals.addGameSession(gameSession); gameManager.addGameSession(gameSession);
LocalStorageService.saveGameSessions(); LocalStorageService.saveGameSessions();
if (context.mounted) { if (context.mounted) {
Navigator.pushReplacement( Navigator.pushReplacement(

View File

@@ -26,7 +26,7 @@ class _MainMenuViewState extends State<MainMenuView> {
_isLoading = false; _isLoading = false;
}); });
}); });
globals.addListener(_updateView); gameManager.addListener(_updateView);
} }
void _updateView() { void _updateView() {
@@ -39,7 +39,7 @@ class _MainMenuViewState extends State<MainMenuView> {
LocalStorageService.loadGameSessions(); LocalStorageService.loadGameSessions();
return ListenableBuilder( return ListenableBuilder(
listenable: globals, listenable: gameManager,
builder: (context, _) { builder: (context, _) {
return CupertinoPageScaffold( return CupertinoPageScaffold(
resizeToAvoidBottomInset: false, resizeToAvoidBottomInset: false,
@@ -70,7 +70,7 @@ class _MainMenuViewState extends State<MainMenuView> {
child: SafeArea( child: SafeArea(
child: _isLoading child: _isLoading
? const Center(child: CupertinoActivityIndicator()) ? const Center(child: CupertinoActivityIndicator())
: globals.gameList.isEmpty : gameManager.gameList.isEmpty
? Column( ? Column(
mainAxisAlignment: mainAxisAlignment:
MainAxisAlignment.center, // Oben ausrichten MainAxisAlignment.center, // Oben ausrichten
@@ -97,9 +97,9 @@ class _MainMenuViewState extends State<MainMenuView> {
], ],
) )
: ListView.builder( : ListView.builder(
itemCount: globals.gameList.length, itemCount: gameManager.gameList.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
final session = globals.gameList[index]; final session = gameManager.gameList[index];
return Dismissible( return Dismissible(
key: Key(session.gameTitle), key: Key(session.gameTitle),
background: Container( background: Container(
@@ -114,7 +114,7 @@ class _MainMenuViewState extends State<MainMenuView> {
direction: DismissDirection.startToEnd, direction: DismissDirection.startToEnd,
confirmDismiss: (direction) async { confirmDismiss: (direction) async {
final String gameTitle = final String gameTitle =
globals.gameList[index].gameTitle; gameManager.gameList[index].gameTitle;
return await _showDeleteGamePopup(gameTitle); return await _showDeleteGamePopup(gameTitle);
}, },
onDismissed: (direction) { onDismissed: (direction) {
@@ -161,7 +161,7 @@ class _MainMenuViewState extends State<MainMenuView> {
CupertinoPageRoute( CupertinoPageRoute(
builder: (context) => ActiveGameView( builder: (context) => ActiveGameView(
gameSession: gameSession:
globals.gameList[index]), gameManager.gameList[index]),
), ),
); );
setState(() {}); setState(() {});
@@ -220,7 +220,7 @@ class _MainMenuViewState extends State<MainMenuView> {
/// This function takes an [index] as parameter and removes the game session at /// This function takes an [index] as parameter and removes the game session at
/// that index from the global game list, /// that index from the global game list,
void _deleteSpecificGame(int index) { void _deleteSpecificGame(int index) {
globals.gameList.removeAt(index); gameManager.gameList.removeAt(index);
LocalStorageService.saveGameSessions(); LocalStorageService.saveGameSessions();
} }
} }