Implementing single game export

This commit is contained in:
2025-07-07 22:40:24 +02:00
parent 3d73e5076a
commit 7a3c8b2e80
4 changed files with 35 additions and 12 deletions

View File

@@ -103,12 +103,12 @@ class LocalStorageService {
} }
/// Opens the file picker to save a JSON file with the current game data. /// Opens the file picker to save a JSON file with the current game data.
static Future<bool> exportJsonFile() async { static Future<bool> exportGameData() async {
final jsonString = getJsonFile(); final jsonString = getJsonFile();
try { try {
final bytes = Uint8List.fromList(utf8.encode(jsonString)); final bytes = Uint8List.fromList(utf8.encode(jsonString));
final result = await FileSaver.instance.saveAs( final result = await FileSaver.instance.saveAs(
name: 'cabo_counter_data', name: 'cabo_counter-game_data',
bytes: bytes, bytes: bytes,
ext: 'json', ext: 'json',
mimeType: MimeType.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<bool> 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. /// Opens the file picker to import a JSON file and loads the game data from it.
static Future<ImportStatus> importJsonFile() async { static Future<ImportStatus> importJsonFile() async {
final result = await FilePicker.platform.pickFiles( final result = await FilePicker.platform.pickFiles(

View File

@@ -1,6 +1,7 @@
import 'package:cabo_counter/data/game_manager.dart'; 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/l10n/app_localizations.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/utility/custom_theme.dart';
import 'package:cabo_counter/views/create_game_view.dart'; import 'package:cabo_counter/views/create_game_view.dart';
import 'package:cabo_counter/views/graph_view.dart'; import 'package:cabo_counter/views/graph_view.dart';
@@ -192,14 +193,15 @@ class _ActiveGameViewState extends State<ActiveGameView> {
}, },
), ),
CupertinoListTile( CupertinoListTile(
title: title: Text(
Text(AppLocalizations.of(context).export_game, AppLocalizations.of(context).export_game,
style: const TextStyle( ),
color: Colors.white30, backgroundColorActivated:
)), CustomTheme.backgroundColor,
backgroundColorActivated: onTap: () {
CustomTheme.backgroundColor, LocalStorageService.exportSingleGameSession(
), widget.gameSession);
}),
], ],
) )
], ],

View File

@@ -140,7 +140,7 @@ class _SettingsViewState extends State<SettingsView> {
), ),
onPressed: () async { onPressed: () async {
final success = final success =
await LocalStorageService.exportJsonFile(); await LocalStorageService.exportGameData();
if (!success && context.mounted) { if (!success && context.mounted) {
showCupertinoDialog( showCupertinoDialog(
context: context, context: context,

View File

@@ -2,7 +2,7 @@ name: cabo_counter
description: "Mobile app for the card game Cabo" description: "Mobile app for the card game Cabo"
publish_to: 'none' publish_to: 'none'
version: 0.3.7+323 version: 0.3.7+325
environment: environment:
sdk: ^3.5.4 sdk: ^3.5.4