diff --git a/lib/services/local_storage_service.dart b/lib/services/local_storage_service.dart index 71dd332..074caf6 100644 --- a/lib/services/local_storage_service.dart +++ b/lib/services/local_storage_service.dart @@ -103,12 +103,12 @@ class LocalStorageService { } /// Opens the file picker to save a JSON file with the current game data. - static Future exportJsonFile() async { + static Future exportGameData() async { final jsonString = getJsonFile(); try { final bytes = Uint8List.fromList(utf8.encode(jsonString)); final result = await FileSaver.instance.saveAs( - name: 'cabo_counter_data', + name: 'cabo_counter-game_data', bytes: bytes, ext: 'json', mimeType: MimeType.json, @@ -123,6 +123,27 @@ class LocalStorageService { } } + /// Opens the file picker to save a single game session as a JSON file. + static Future exportSingleGameSession(GameSession session) async { + final jsonString = json.encode(session.toJson()); + try { + final bytes = Uint8List.fromList(utf8.encode(jsonString)); + final result = await FileSaver.instance.saveAs( + name: 'cabo_counter-game_${session.id.substring(0, 7)}', + bytes: bytes, + ext: 'json', + mimeType: MimeType.json, + ); + print( + '[local_storage_service.dart] Die Spieldaten der Session wurden exportiert. Dateipfad: $result'); + return true; + } catch (e) { + print( + '[local_storage_service.dart] Fehler beim Exportieren der Spieldaten der Session. Exception: $e'); + return false; + } + } + /// Opens the file picker to import a JSON file and loads the game data from it. static Future importJsonFile() async { final result = await FilePicker.platform.pickFiles( diff --git a/lib/views/active_game_view.dart b/lib/views/active_game_view.dart index 586eab3..2ae9665 100644 --- a/lib/views/active_game_view.dart +++ b/lib/views/active_game_view.dart @@ -1,6 +1,7 @@ import 'package:cabo_counter/data/game_manager.dart'; import 'package:cabo_counter/data/game_session.dart'; import 'package:cabo_counter/l10n/app_localizations.dart'; +import 'package:cabo_counter/services/local_storage_service.dart'; import 'package:cabo_counter/utility/custom_theme.dart'; import 'package:cabo_counter/views/create_game_view.dart'; import 'package:cabo_counter/views/graph_view.dart'; @@ -192,14 +193,15 @@ class _ActiveGameViewState extends State { }, ), CupertinoListTile( - title: - Text(AppLocalizations.of(context).export_game, - style: const TextStyle( - color: Colors.white30, - )), - backgroundColorActivated: - CustomTheme.backgroundColor, - ), + title: Text( + AppLocalizations.of(context).export_game, + ), + backgroundColorActivated: + CustomTheme.backgroundColor, + onTap: () { + LocalStorageService.exportSingleGameSession( + widget.gameSession); + }), ], ) ], diff --git a/lib/views/settings_view.dart b/lib/views/settings_view.dart index b9ae02c..f98c542 100644 --- a/lib/views/settings_view.dart +++ b/lib/views/settings_view.dart @@ -140,7 +140,7 @@ class _SettingsViewState extends State { ), onPressed: () async { final success = - await LocalStorageService.exportJsonFile(); + await LocalStorageService.exportGameData(); if (!success && context.mounted) { showCupertinoDialog( context: context, diff --git a/pubspec.yaml b/pubspec.yaml index 160097c..b786c31 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.3.7+323 +version: 0.3.7+325 environment: sdk: ^3.5.4