From 11f87bc198f6bbdb786002cd958a26a9dd9bd48d Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Wed, 30 Apr 2025 17:17:01 +0200 Subject: [PATCH] Implemented save & load method --- .../xcshareddata/xcschemes/Runner.xcscheme | 4 +- lib/main.dart | 87 +++++++------ lib/views/main_menu_view.dart | 121 ++++++++++++------ lib/views/round_view.dart | 18 ++- pubspec.yaml | 3 +- 5 files changed, 140 insertions(+), 93 deletions(-) diff --git a/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme b/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme index 1728d10..4502f08 100644 --- a/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme +++ b/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme @@ -52,8 +52,8 @@ { + @override + initState() { + super.initState(); + LocalStorageService.loadGameSessions().then((_) { + setState(() {}); + }); + } + @override Widget build(BuildContext context) { + LocalStorageService.loadGameSessions(); return CupertinoPageScaffold( + resizeToAvoidBottomInset: false, navigationBar: CupertinoNavigationBar( leading: IconButton( onPressed: () { @@ -45,49 +57,74 @@ class _MainMenuViewState extends State { ), child: CupertinoPageScaffold( child: SafeArea( - child: ListView.builder( - itemCount: Globals.gameList.length, - itemBuilder: (context, index) { - final session = Globals.gameList[index]; - return Padding( - padding: const EdgeInsets.symmetric(vertical: 10.0), - child: CupertinoListTile( - title: Text(session.gameTitle), - subtitle: session.isGameFinished == true - ? Text( - '\u{1F947} ${session.winner}', - style: const TextStyle(fontSize: 14), - ) - : Text( - 'Modus: ${_translateGameMode(session.gameHasPointLimit)}', - style: const TextStyle(fontSize: 14), - ), - trailing: Row( - children: [ - Text('${session.roundNumber}'), - const SizedBox(width: 3), - const Icon( - CupertinoIcons.arrow_2_circlepath_circle_fill), - const SizedBox(width: 15), - Text('${session.players.length}'), - const SizedBox(width: 3), - const Icon(CupertinoIcons.person_2_fill), - ], + child: Globals.gameList.isEmpty + ? Column( + mainAxisAlignment: + MainAxisAlignment.center, // Oben ausrichten + children: [ + const SizedBox(height: 30), // Abstand von oben + Center( + child: GestureDetector( + onTap: () => setState(() {}), + child: Icon( + CupertinoIcons.plus, + size: 60, + color: AppTheme.primaryColor, + ), + )), + const SizedBox(height: 10), // Abstand von oben + const Padding( + padding: EdgeInsets.symmetric(horizontal: 70), + child: Text( + 'Ganz schön leer hier...\nFüge über den Button oben rechts eine neue Runde hinzu.', + textAlign: TextAlign.center, + style: TextStyle(fontSize: 16), + ), ), - onTap: () async { - //ignore: unused_local_variable - final val = await Navigator.push( - context, - CupertinoPageRoute( - builder: (context) => ActiveGameView( - gameSession: Globals.gameList[index]), - ), - ); - setState(() {}); - }, - )); - }, - ), + ], + ) + : ListView.builder( + itemCount: Globals.gameList.length, + itemBuilder: (context, index) { + final session = Globals.gameList[index]; + return Padding( + padding: const EdgeInsets.symmetric(vertical: 10.0), + child: CupertinoListTile( + title: Text(session.gameTitle), + subtitle: session.isGameFinished == true + ? Text( + '\u{1F947} ${session.winner}', + style: const TextStyle(fontSize: 14), + ) + : Text( + 'Modus: ${_translateGameMode(session.gameHasPointLimit)}', + style: const TextStyle(fontSize: 14), + ), + trailing: Row( + children: [ + Text('${session.roundNumber}'), + const SizedBox(width: 3), + const Icon(CupertinoIcons + .arrow_2_circlepath_circle_fill), + const SizedBox(width: 15), + Text('${session.players.length}'), + const SizedBox(width: 3), + const Icon(CupertinoIcons.person_2_fill), + ], + ), + onTap: () async { + //ignore: unused_local_variable + final val = await Navigator.push( + context, + CupertinoPageRoute( + builder: (context) => ActiveGameView( + gameSession: Globals.gameList[index]), + ), + ); + setState(() {}); + }, + )); + }), ), ), ); diff --git a/lib/views/round_view.dart b/lib/views/round_view.dart index ec6a3c8..9a5a035 100644 --- a/lib/views/round_view.dart +++ b/lib/views/round_view.dart @@ -1,5 +1,6 @@ import 'package:cabo_counter/data/game_session.dart'; -import 'package:cabo_counter/utility/theme.dart'; +import 'package:cabo_counter/utility/apptheme.dart'; +import 'package:cabo_counter/utility/local_storage_service.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/services.dart'; import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart'; @@ -71,7 +72,10 @@ class _RoundViewState extends State { previousPageTitle: 'Übersicht', leading: CupertinoButton( padding: EdgeInsets.zero, - onPressed: () => Navigator.pop(context, widget.gameSession), + onPressed: () => { + LocalStorageService.saveGameSessions(), + Navigator.pop(context, widget.gameSession) + }, child: const Text('Abbrechen'), ), ), @@ -86,7 +90,7 @@ class _RoundViewState extends State { children: [ const SizedBox(height: 40), Text('Runde ${widget.roundNumber}', - style: Theme.roundTitle), + style: AppTheme.roundTitle), const SizedBox(height: 10), const Text( 'Wer hat CABO gesagt?', @@ -101,8 +105,8 @@ class _RoundViewState extends State { child: SizedBox( height: 40, child: CupertinoSegmentedControl( - unselectedColor: Theme.backgroundTintColor, - selectedColor: Theme.primaryColor, + unselectedColor: AppTheme.backgroundTintColor, + selectedColor: AppTheme.primaryColor, groupValue: _caboPlayerIndex, children: Map.fromEntries(widget.gameSession.players .asMap() @@ -267,7 +271,7 @@ class _RoundViewState extends State { return Container( height: 80, padding: const EdgeInsets.only(bottom: 20), - color: Theme.backgroundTintColor, + color: AppTheme.backgroundTintColor, child: Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ @@ -275,6 +279,7 @@ class _RoundViewState extends State { onPressed: _areRoundInputsValid() ? () { _finishRound(); + LocalStorageService.saveGameSessions(); Navigator.pop(context, widget.gameSession); } : null, @@ -284,6 +289,7 @@ class _RoundViewState extends State { onPressed: _areRoundInputsValid() ? () { _finishRound(); + LocalStorageService.saveGameSessions(); if (widget.gameSession.isGameFinished == true) { Navigator.pop(context, widget.gameSession); } else { diff --git a/pubspec.yaml b/pubspec.yaml index 326e7ee..95bae58 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: cabo_counter description: "Mobile app for the card game Cabo" publish_to: 'none' -version: 0.1.3+65 +version: 0.1.3+101 environment: sdk: ^3.5.4 @@ -15,6 +15,7 @@ dependencies: url_launcher: any package_info_plus: any flutter_keyboard_visibility: ^6.0.0 + path_provider: ^2.1.1 dev_dependencies: flutter_test: