Implemented exportJsonFile & importJsonFile methods
This commit is contained in:
@@ -1,17 +1,34 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
import 'dart:typed_data';
|
||||||
|
|
||||||
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:cabo_counter/utility/globals.dart';
|
||||||
|
import 'package:file_picker/file_picker.dart';
|
||||||
|
import 'package:file_saver/file_saver.dart';
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
|
|
||||||
class LocalStorageService {
|
class LocalStorageService {
|
||||||
static const String _fileName = 'game_data.json';
|
static const String _fileName = 'game_data.json';
|
||||||
|
|
||||||
/// Speichert GameSessions im App-Dokumentenverzeichnis
|
/// Writes the game session list to a JSON file and returns it as string.
|
||||||
|
static String getJsonFile() {
|
||||||
|
final jsonFile =
|
||||||
|
Globals.gameList.map((session) => session.toJson()).toList();
|
||||||
|
return json.encode(jsonFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the path to the local JSON file.
|
||||||
|
static Future<File> _getFilePath() async {
|
||||||
|
final directory = await getApplicationDocumentsDirectory();
|
||||||
|
final path = '${directory.path}/$_fileName';
|
||||||
|
return File(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Saves the game sessions to a local JSON file.
|
||||||
static Future<void> saveGameSessions() async {
|
static Future<void> saveGameSessions() async {
|
||||||
try {
|
try {
|
||||||
final file = await _getLocalFile();
|
final file = await _getFilePath();
|
||||||
final jsonFile = getJsonFile();
|
final jsonFile = getJsonFile();
|
||||||
await file.writeAsString(jsonFile);
|
await file.writeAsString(jsonFile);
|
||||||
print('Daten gespeichert');
|
print('Daten gespeichert');
|
||||||
@@ -20,44 +37,82 @@ class LocalStorageService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Lädt GameSessions aus dem App-Dokumentenverzeichnis
|
/// Loads the game data from a local JSON file.
|
||||||
static Future<void> loadGameSessions() async {
|
static Future<void> loadGameSessions() async {
|
||||||
print('Versuche, Daten zu laden...'); // FIXME Debug-Ausgabe
|
print('Versuche, Daten zu laden...');
|
||||||
try {
|
try {
|
||||||
final file = await _getLocalFile();
|
final file = await _getFilePath();
|
||||||
if (await file.exists()) {
|
if (await file.exists()) {
|
||||||
print('Datei existiert'); // FIXME Debug-Ausgabe
|
print('Es existiert bereits eine Datei mit Spieldaten');
|
||||||
final jsonString = await file.readAsString();
|
final jsonString = await file.readAsString();
|
||||||
if (jsonString.isNotEmpty) {
|
if (jsonString.isNotEmpty) {
|
||||||
print('Datei ist nicht leer'); // FIXME Debug-Ausgabe
|
print('Die Datei ist nicht leer');
|
||||||
final jsonList = json.decode(jsonString) as List<dynamic>;
|
final jsonList = json.decode(jsonString) as List<dynamic>;
|
||||||
print('JSON: $jsonList'); // FIXME Debug-Ausgabe
|
print('JSON: $jsonList');
|
||||||
Globals.gameList =
|
Globals.gameList = jsonList
|
||||||
jsonList.map((json) => GameSession.fromJson(json)).toList();
|
.map((jsonItem) =>
|
||||||
print('Daten erfolgreich geladen');
|
GameSession.fromJson(jsonItem as Map<String, dynamic>))
|
||||||
|
.toList()
|
||||||
|
.cast<GameSession>(); // Explicit cast to List<GameSession>
|
||||||
|
print('Die Daten wurden erfolgreich geladen');
|
||||||
} else {
|
} else {
|
||||||
print('Datei ist leer');
|
print('Die Datei ist leer');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
print('Datei existiert nicht');
|
print('Es existiert bisher noch keine Datei mit Spieldaten');
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('Fehler beim Laden: $e');
|
print('Fehler beim Laden der Spieldaten:\n$e');
|
||||||
// Bei Fehler eine leere Liste setzen
|
|
||||||
Globals.gameList = [];
|
Globals.gameList = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static String getJsonFile() {
|
/// Opens the file picker to save a JSON file with the current game data.
|
||||||
final jsonFile =
|
static Future<void> exportJsonFile() async {
|
||||||
Globals.gameList.map((session) => session.toJson()).toList();
|
final jsonString = getJsonFile();
|
||||||
return json.encode(jsonFile);
|
try {
|
||||||
|
final bytes = Uint8List.fromList(utf8.encode(jsonString));
|
||||||
|
final result = await FileSaver.instance.saveAs(
|
||||||
|
name: 'cabo_counter_data',
|
||||||
|
bytes: bytes,
|
||||||
|
ext: 'json',
|
||||||
|
mimeType: MimeType.json,
|
||||||
|
);
|
||||||
|
print('Datei gespeichert: $result');
|
||||||
|
} catch (e) {
|
||||||
|
print('Fehler beim Speichern: $e');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<File> _getLocalFile() async {
|
static Future<bool> importJsonFile() async {
|
||||||
final directory = await getApplicationDocumentsDirectory();
|
try {
|
||||||
final path = '${directory.path}/$_fileName';
|
final result = await FilePicker.platform.pickFiles(
|
||||||
print('Speicherpfad: $path'); // FIXME Debug-Ausgabe
|
type: FileType.custom,
|
||||||
return File(path);
|
allowedExtensions: ['json'],
|
||||||
|
);
|
||||||
|
String jsonString = '';
|
||||||
|
if (result != null) {
|
||||||
|
if (result.files.single.bytes != null) {
|
||||||
|
final Uint8List fileBytes = result.files.single.bytes!;
|
||||||
|
jsonString = utf8.decode(fileBytes);
|
||||||
|
} else if (result.files.single.path != null) {
|
||||||
|
final file = File(result.files.single.path!);
|
||||||
|
jsonString = await file.readAsString();
|
||||||
|
}
|
||||||
|
final jsonList = json.decode(jsonString) as List<dynamic>;
|
||||||
|
print('JSON Inhalt: $jsonList');
|
||||||
|
Globals.gameList = jsonList
|
||||||
|
.map((jsonItem) =>
|
||||||
|
GameSession.fromJson(jsonItem as Map<String, dynamic>))
|
||||||
|
.toList();
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
print('Der Dialog wurde abgebrochen');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
print('Fehler beim Importieren: $e');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import 'package:cabo_counter/utility/local_storage_service.dart';
|
||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||||
@@ -83,7 +84,10 @@ class InformationView extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
CupertinoButton(
|
CupertinoButton(
|
||||||
child: const Text('Spieldaten exportieren'),
|
child: const Text('Spieldaten exportieren'),
|
||||||
onPressed: () => {saveJsonToDevice()})
|
onPressed: () => LocalStorageService.exportJsonFile()),
|
||||||
|
CupertinoButton(
|
||||||
|
child: const Text('Spieldaten importieren'),
|
||||||
|
onPressed: () => LocalStorageService.importJsonFile()),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
Positioned(
|
Positioned(
|
||||||
@@ -115,8 +119,4 @@ class InformationView extends StatelessWidget {
|
|||||||
],
|
],
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void saveJsonToDevice() async {
|
|
||||||
//todo: implement
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.1.3+103
|
version: 0.1.3+107
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ^3.5.4
|
sdk: ^3.5.4
|
||||||
@@ -17,6 +17,9 @@ dependencies:
|
|||||||
flutter_keyboard_visibility: ^6.0.0
|
flutter_keyboard_visibility: ^6.0.0
|
||||||
path_provider: ^2.1.1
|
path_provider: ^2.1.1
|
||||||
file_picker: any
|
file_picker: any
|
||||||
|
file_saver: ^0.2.6
|
||||||
|
typed_data: ^1.3.2
|
||||||
|
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|||||||
Reference in New Issue
Block a user