diff --git a/lib/l10n/app_de.arb b/lib/l10n/app_de.arb index ccebefc..3e2acdf 100644 --- a/lib/l10n/app_de.arb +++ b/lib/l10n/app_de.arb @@ -30,8 +30,14 @@ "import": "Importieren", "activateJsonExport": "Immer als JSON speichern", + "@@comment": "Info", + "exportSuccess": "Daten exportiert", + "importSuccess": "Daten importiert", + "@@comment": "Errors", "errorStoragePermisson": "Zugriff auf Speicher verwehrt", "errorCouldNotLaunchUrl": "Konnte Url nicht öffnen", - "errorInvalidUrl": "Fehlerhafte Url" + "errorInvalidUrl": "Fehlerhafte Url", + "exportFailed": "Export fehlgeschlagen", + "importFailed": "Import fehlgeschlagen" } \ No newline at end of file diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index f5f64c3..41652c7 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -29,9 +29,16 @@ "export": "Export", "import": "Import", "activateJsonExport": "Always save to JSON", + + + "@@comment": "Info", + "exportSuccess": "Exported data", + "importSuccess": "Imported data", "@@comment": "Errors", "errorStoragePermisson": "Storage permissions denied", "errorCouldNotLaunchUrl": "Could not launch Url", - "errorInvalidUrl": "Invalid Url" + "errorInvalidUrl": "Invalid Url", + "exportFailed": "Export failed", + "importFailed": "Import failed" } \ No newline at end of file diff --git a/lib/pages/settings_page.dart b/lib/pages/settings_page.dart index 606c56c..babf159 100644 --- a/lib/pages/settings_page.dart +++ b/lib/pages/settings_page.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:flutter/material.dart'; import 'package:permission_handler/permission_handler.dart'; @@ -43,20 +45,42 @@ class _SettingsPageState extends State { } void onActivateJsonExportPressed() async { - if (await checkStoragePermission) Storage.exportToJsonFile(); + if (!await checkStoragePermission) return; + + Storage.exportToJsonFile().then(showExportInfo); } void onActivateJsonImportPressed() async { - if (await checkStoragePermission) Storage.importFromJsonFile(); + if (!await checkStoragePermission) return; + Storage.importFromJsonFile().then(showImportInfo); } Future get checkStoragePermission async { if (!(await PermissionService.requestStoragePermission).isGranted) { if (mounted) { - Notifying.showStoragePermissionErrorSnackbar(context); + Notifying.showErrorSnackbar( + context, + AppLocalizations.of(context)!.errorStoragePermisson, + ); return false; } } return true; } + + void showExportInfo(bool success) => Notifying.showSnackbar( + context, + text: success + ? AppLocalizations.of(context)!.exportSuccess + : AppLocalizations.of(context)!.exportFailed, + isError: success, + ); + + void showImportInfo(bool success) => Notifying.showSnackbar( + context, + text: success + ? AppLocalizations.of(context)!.importSuccess + : AppLocalizations.of(context)!.importFailed, + isError: success, + ); } diff --git a/lib/service/json_file_service.dart b/lib/service/json_file_service.dart index 56c2159..95d1162 100644 --- a/lib/service/json_file_service.dart +++ b/lib/service/json_file_service.dart @@ -13,48 +13,56 @@ class JsonFileService { required List collections, required List bookmarks, }) async { - final dir = await _directoryPath; + try { + final dir = await _directoryPath; + if (dir.isEmpty) return false; - final data = { - 'collections': collections.map((c) => c.toJson()).toList(), - 'bookmarks': bookmarks.map((b) => b.toJson()).toList(), - }; - final json = jsonEncode(data).codeUnits; - final file = XFile.fromData( - Uint8List.fromList(json), - mimeType: 'application/json', - name: constants.jsonFileName, - ); - - file.saveTo('$dir/${constants.jsonFileName}'); + final data = { + 'collections': collections.map((c) => c.toJson()).toList(), + 'bookmarks': bookmarks.map((b) => b.toJson()).toList(), + }; + final json = jsonEncode(data).codeUnits; + final file = XFile.fromData( + Uint8List.fromList(json), + mimeType: 'application/json', + name: constants.jsonFileName, + ); + file.saveTo('$dir/${constants.jsonFileName}'); + } catch (e) { + return false; + } return false; } static Future<({List collections, List bookmarks})> importFromJson() async { - const typeGroup = XTypeGroup(label: 'json', extensions: ['json']); - final XFile? file = await openFile( - acceptedTypeGroups: [typeGroup], - ); + try { + const typeGroup = XTypeGroup(label: 'json', extensions: ['json']); + final XFile? file = await openFile( + acceptedTypeGroups: [typeGroup], + ); - if (file == null) { + if (file == null) { + return (collections: [], bookmarks: []); + } + + final jsonString = await file.readAsString(); + + final data = jsonDecode(jsonString) as Map; + + final collections = (data['collections'] as List? ?? []) + .map((json) => Collection.fromJson(json as Map)) + .toList(); + + final bookmarks = (data['bookmarks'] as List? ?? []) + .map((json) => Bookmark.fromJson(json as Map)) + .toList(); + + return (collections: collections, bookmarks: bookmarks); + } catch (e) { return (collections: [], bookmarks: []); } - - final jsonString = await file.readAsString(); - - final data = jsonDecode(jsonString) as Map; - - final collections = (data['collections'] as List? ?? []) - .map((json) => Collection.fromJson(json as Map)) - .toList(); - - final bookmarks = (data['bookmarks'] as List? ?? []) - .map((json) => Bookmark.fromJson(json as Map)) - .toList(); - - return (collections: collections, bookmarks: bookmarks); } static Future get _directoryPath async { diff --git a/lib/service/notifying.dart b/lib/service/notifying.dart index 8e12162..4870c2b 100644 --- a/lib/service/notifying.dart +++ b/lib/service/notifying.dart @@ -54,11 +54,11 @@ class Notifying { showSnackbar(context, text: errorText, isError: true); } - static void showStoragePermissionErrorSnackbar(BuildContext context) { - showSnackbar( - context, - text: AppLocalizations.of(context)!.errorStoragePermisson, - isError: true, - ); + static void showErrorSnackbar(BuildContext context, String message) { + showSnackbar(context, text: message, isError: true); + } + + static void showMessageSnackbar(BuildContext context, String message) { + showSnackbar(context, text: message, isError: false); } }