Added documentation and feedback in snackbar
This commit is contained in:
@@ -77,23 +77,37 @@ class _SettingsViewState extends State<SettingsView> {
|
|||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
final String json =
|
final String json =
|
||||||
await DataTransferService.getAppDataAsJson(context);
|
await DataTransferService.getAppDataAsJson(context);
|
||||||
await DataTransferService.exportData(
|
final result = await DataTransferService.exportData(
|
||||||
json,
|
json,
|
||||||
'exported_data',
|
'exported_data',
|
||||||
);
|
);
|
||||||
|
if (!context.mounted) return;
|
||||||
|
showExportSnackBar(context: context, result: result);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
SettingsListTile(
|
SettingsListTile(
|
||||||
title: 'Import data',
|
title: 'Import data',
|
||||||
icon: Icons.download_outlined,
|
icon: Icons.download_outlined,
|
||||||
suffixWidget: const Icon(Icons.arrow_forward_ios, size: 16),
|
suffixWidget: const Icon(Icons.arrow_forward_ios, size: 16),
|
||||||
onPressed: () => DataTransferService.importData(context),
|
onPressed: () async {
|
||||||
|
final result = await DataTransferService.importData(
|
||||||
|
context,
|
||||||
|
);
|
||||||
|
if (!context.mounted) return;
|
||||||
|
showImportSnackBar(context: context, result: result);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
SettingsListTile(
|
SettingsListTile(
|
||||||
title: 'Delete all data',
|
title: 'Delete all data',
|
||||||
icon: Icons.download_outlined,
|
icon: Icons.download_outlined,
|
||||||
suffixWidget: const Icon(Icons.arrow_forward_ios, size: 16),
|
suffixWidget: const Icon(Icons.arrow_forward_ios, size: 16),
|
||||||
onPressed: () => DataTransferService.deleteAllData(context),
|
onPressed: () {
|
||||||
|
DataTransferService.deleteAllData(context);
|
||||||
|
showSnackbar(
|
||||||
|
context: context,
|
||||||
|
message: 'Data successfully deleted',
|
||||||
|
);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
@@ -101,4 +115,81 @@ class _SettingsViewState extends State<SettingsView> {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Displays a snackbar based on the import result.
|
||||||
|
///
|
||||||
|
/// [context] The BuildContext to show the snackbar in.
|
||||||
|
/// [result] The result of the import operation.
|
||||||
|
void showImportSnackBar({
|
||||||
|
required BuildContext context,
|
||||||
|
required ImportResult result,
|
||||||
|
}) {
|
||||||
|
switch (result) {
|
||||||
|
case ImportResult.success:
|
||||||
|
showSnackbar(context: context, message: 'Data successfully imported');
|
||||||
|
case ImportResult.invalidSchema:
|
||||||
|
showSnackbar(context: context, message: 'Invalid Schema');
|
||||||
|
case ImportResult.fileReadError:
|
||||||
|
showSnackbar(context: context, message: 'Error reading file');
|
||||||
|
case ImportResult.canceled:
|
||||||
|
showSnackbar(context: context, message: 'Import canceled');
|
||||||
|
case ImportResult.formatException:
|
||||||
|
showSnackbar(
|
||||||
|
context: context,
|
||||||
|
message: 'Format Exception (see console)',
|
||||||
|
);
|
||||||
|
case ImportResult.unknownException:
|
||||||
|
showSnackbar(
|
||||||
|
context: context,
|
||||||
|
message: 'Unknown Exception (see console)',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Displays a snackbar based on the export result.
|
||||||
|
///
|
||||||
|
/// [context] The BuildContext to show the snackbar in.
|
||||||
|
/// [result] The result of the export operation.
|
||||||
|
void showExportSnackBar({
|
||||||
|
required BuildContext context,
|
||||||
|
required ExportResult result,
|
||||||
|
}) {
|
||||||
|
switch (result) {
|
||||||
|
case ExportResult.success:
|
||||||
|
showSnackbar(context: context, message: 'Data successfully exported');
|
||||||
|
case ExportResult.canceled:
|
||||||
|
showSnackbar(context: context, message: 'Export canceled');
|
||||||
|
case ExportResult.unknownException:
|
||||||
|
showSnackbar(
|
||||||
|
context: context,
|
||||||
|
message: 'Unknown Exception (see console)',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Displays a snackbar with the given message and optional action.
|
||||||
|
///
|
||||||
|
/// [context] The BuildContext to show the snackbar in.
|
||||||
|
/// [message] The message to display in the snackbar.
|
||||||
|
/// [duration] The duration for which the snackbar is displayed.
|
||||||
|
/// [action] An optional callback function to execute when the action button is pressed.
|
||||||
|
void showSnackbar({
|
||||||
|
required BuildContext context,
|
||||||
|
required String message,
|
||||||
|
Duration duration = const Duration(seconds: 3),
|
||||||
|
VoidCallback? action,
|
||||||
|
}) {
|
||||||
|
final messenger = ScaffoldMessenger.of(context);
|
||||||
|
messenger.hideCurrentSnackBar();
|
||||||
|
messenger.showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(message, style: const TextStyle(color: Colors.white)),
|
||||||
|
backgroundColor: CustomTheme.onBoxColor,
|
||||||
|
duration: duration,
|
||||||
|
action: action != null
|
||||||
|
? SnackBarAction(label: 'Rückgängig', onPressed: action)
|
||||||
|
: null,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,17 @@ import 'package:game_tracker/data/dto/player.dart';
|
|||||||
import 'package:game_tracker/presentation/views/main_menu/settings_view.dart';
|
import 'package:game_tracker/presentation/views/main_menu/settings_view.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
|
enum ImportResult {
|
||||||
|
success,
|
||||||
|
canceled,
|
||||||
|
fileReadError,
|
||||||
|
invalidSchema,
|
||||||
|
formatException,
|
||||||
|
unknownException,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum ExportResult { success, canceled, unknownException }
|
||||||
|
|
||||||
class DataTransferService {
|
class DataTransferService {
|
||||||
/// Deletes all data from the database.
|
/// Deletes all data from the database.
|
||||||
static Future<void> deleteAllData(BuildContext context) async {
|
static Future<void> deleteAllData(BuildContext context) async {
|
||||||
@@ -18,9 +29,10 @@ class DataTransferService {
|
|||||||
await db.gameDao.deleteAllGames();
|
await db.gameDao.deleteAllGames();
|
||||||
await db.groupDao.deleteAllGroups();
|
await db.groupDao.deleteAllGroups();
|
||||||
await db.playerDao.deleteAllPlayers();
|
await db.playerDao.deleteAllPlayers();
|
||||||
print('[deleteAllData] All data deleted');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Retrieves all application data and converts it to a JSON string.
|
||||||
|
/// Returns the JSON string representation of the data.
|
||||||
static Future<String> getAppDataAsJson(BuildContext context) async {
|
static Future<String> getAppDataAsJson(BuildContext context) async {
|
||||||
final db = Provider.of<AppDatabase>(context, listen: false);
|
final db = Provider.of<AppDatabase>(context, listen: false);
|
||||||
final games = await db.gameDao.getAllGames();
|
final games = await db.gameDao.getAllGames();
|
||||||
@@ -38,23 +50,34 @@ class DataTransferService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Exports the given JSON string to a file with the specified name.
|
/// Exports the given JSON string to a file with the specified name.
|
||||||
static Future<bool> exportData(String jsonString, String fileName) async {
|
/// Returns an [ExportResult] indicating the outcome.
|
||||||
|
///
|
||||||
|
/// [jsonString] The JSON string to be exported.
|
||||||
|
/// [fileName] The desired name for the exported file (without extension).
|
||||||
|
static Future<ExportResult> exportData(
|
||||||
|
String jsonString,
|
||||||
|
String fileName,
|
||||||
|
) async {
|
||||||
try {
|
try {
|
||||||
final bytes = Uint8List.fromList(utf8.encode(jsonString));
|
final bytes = Uint8List.fromList(utf8.encode(jsonString));
|
||||||
await FilePicker.platform.saveFile(
|
final path = await FilePicker.platform.saveFile(
|
||||||
fileName: '$fileName.json',
|
fileName: '$fileName.json',
|
||||||
bytes: bytes,
|
bytes: bytes,
|
||||||
);
|
);
|
||||||
return true;
|
if (path == null) {
|
||||||
|
return ExportResult.canceled;
|
||||||
|
} else {
|
||||||
|
return ExportResult.success;
|
||||||
|
}
|
||||||
} catch (e, stack) {
|
} catch (e, stack) {
|
||||||
print('[exportData] $e');
|
print('[exportData] $e');
|
||||||
print(stack);
|
print(stack);
|
||||||
return false;
|
return ExportResult.unknownException;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Imports data from a selected JSON file into the database.
|
/// Imports data from a selected JSON file into the database.
|
||||||
static Future<void> importData(BuildContext context) async {
|
static Future<ImportResult> importData(BuildContext context) async {
|
||||||
final db = Provider.of<AppDatabase>(context, listen: false);
|
final db = Provider.of<AppDatabase>(context, listen: false);
|
||||||
|
|
||||||
final path = await FilePicker.platform.pickFiles(
|
final path = await FilePicker.platform.pickFiles(
|
||||||
@@ -63,12 +86,14 @@ class DataTransferService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (path == null) {
|
if (path == null) {
|
||||||
print('[importData] No file selected');
|
return ImportResult.canceled;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final jsonString = await _readFileContent(path.files.single);
|
final jsonString = await _readFileContent(path.files.single);
|
||||||
|
if (jsonString == null) {
|
||||||
|
return ImportResult.fileReadError;
|
||||||
|
}
|
||||||
|
|
||||||
if (await SettingsView.validateJsonSchema(jsonString)) {
|
if (await SettingsView.validateJsonSchema(jsonString)) {
|
||||||
final Map<String, dynamic> jsonData =
|
final Map<String, dynamic> jsonData =
|
||||||
@@ -107,29 +132,26 @@ class DataTransferService {
|
|||||||
await db.gameDao.addGame(game: game);
|
await db.gameDao.addGame(game: game);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
print('[importData] Invalid JSON schema');
|
return ImportResult.invalidSchema;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
print('[importData] Data imported successfully');
|
return ImportResult.success;
|
||||||
return;
|
|
||||||
} on FormatException catch (e, stack) {
|
} on FormatException catch (e, stack) {
|
||||||
print('[importData] FormatException');
|
print('[importData] FormatException');
|
||||||
print('[importData] $e');
|
print('[importData] $e');
|
||||||
print(stack);
|
print(stack);
|
||||||
return;
|
return ImportResult.formatException;
|
||||||
} on Exception catch (e, stack) {
|
} on Exception catch (e, stack) {
|
||||||
print('[importData] Exception');
|
print('[importData] Exception');
|
||||||
print('[importData] $e');
|
print('[importData] $e');
|
||||||
print(stack);
|
print(stack);
|
||||||
return;
|
return ImportResult.unknownException;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper method to read file content from either bytes or path
|
/// Helper method to read file content from either bytes or path
|
||||||
static Future<String> _readFileContent(PlatformFile file) async {
|
static Future<String?> _readFileContent(PlatformFile file) async {
|
||||||
if (file.bytes != null) return utf8.decode(file.bytes!);
|
if (file.bytes != null) return utf8.decode(file.bytes!);
|
||||||
if (file.path != null) return await File(file.path!).readAsString();
|
if (file.path != null) return await File(file.path!).readAsString();
|
||||||
|
return null;
|
||||||
throw Exception('Die Datei hat keinen lesbaren Inhalt');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user