Implemented enum for import status
This commit is contained in:
@@ -9,6 +9,14 @@ import 'package:flutter/services.dart';
|
||||
import 'package:json_schema/json_schema.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
enum ImportStatus {
|
||||
success,
|
||||
canceled,
|
||||
validationError,
|
||||
formatError,
|
||||
genericError
|
||||
}
|
||||
|
||||
class LocalStorageService {
|
||||
static const String _fileName = 'game_data.json';
|
||||
|
||||
@@ -111,7 +119,7 @@ class LocalStorageService {
|
||||
}
|
||||
|
||||
/// Opens the file picker to import a JSON file and loads the game data from it.
|
||||
static Future<int> importJsonFile() async {
|
||||
static Future<ImportStatus> importJsonFile() async {
|
||||
final result = await FilePicker.platform.pickFiles(
|
||||
dialogTitle: 'Wähle eine Datei mit Spieldaten aus',
|
||||
type: FileType.custom,
|
||||
@@ -121,14 +129,14 @@ class LocalStorageService {
|
||||
if (result == null) {
|
||||
print(
|
||||
'[local_storage_service.dart] Der Filepicker-Dialog wurde abgebrochen');
|
||||
return 0;
|
||||
return ImportStatus.canceled;
|
||||
}
|
||||
|
||||
try {
|
||||
final jsonString = await _readFileContent(result.files.single);
|
||||
|
||||
if (!await validateJsonSchema(jsonString)) {
|
||||
return -1;
|
||||
return ImportStatus.validationError;
|
||||
}
|
||||
final jsonData = json.decode(jsonString) as List<dynamic>;
|
||||
gameManager.gameList = jsonData
|
||||
@@ -138,15 +146,15 @@ class LocalStorageService {
|
||||
print(
|
||||
'[local_storage_service.dart] Die Datei wurde erfolgreich Importiertn');
|
||||
await saveGameSessions();
|
||||
return 1;
|
||||
return ImportStatus.success;
|
||||
} on FormatException catch (e) {
|
||||
print(
|
||||
'[local_storage_service.dart] Ungültiges JSON-Format. Exception: $e');
|
||||
return -2;
|
||||
return ImportStatus.formatError;
|
||||
} on Exception catch (e) {
|
||||
print(
|
||||
'[local_storage_service.dart] Fehler beim Dateizugriff. Exception: $e');
|
||||
return -3;
|
||||
return ImportStatus.genericError;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -217,7 +217,7 @@ class _SettingsViewState extends State<SettingsView> {
|
||||
return await PackageInfo.fromPlatform();
|
||||
}
|
||||
|
||||
void showFeedbackDialog(int success) {
|
||||
void showFeedbackDialog(ImportStatus success) {
|
||||
if (success == 0) return;
|
||||
final (title, message) = _getDialogContent(success);
|
||||
|
||||
@@ -237,27 +237,31 @@ class _SettingsViewState extends State<SettingsView> {
|
||||
});
|
||||
}
|
||||
|
||||
(String, String) _getDialogContent(int success) {
|
||||
if (success == 1) {
|
||||
return (
|
||||
AppLocalizations.of(context).import_success_title,
|
||||
AppLocalizations.of(context).import_success_message
|
||||
);
|
||||
} else if (success == -1) {
|
||||
return (
|
||||
AppLocalizations.of(context).import_validation_error_title,
|
||||
AppLocalizations.of(context).import_validation_error_message
|
||||
);
|
||||
} else if (success == -2) {
|
||||
return (
|
||||
AppLocalizations.of(context).import_format_error_title,
|
||||
AppLocalizations.of(context).import_format_error_message
|
||||
);
|
||||
} else {
|
||||
return (
|
||||
AppLocalizations.of(context).import_generic_error_title,
|
||||
AppLocalizations.of(context).import_generic_error_message
|
||||
);
|
||||
(String, String) _getDialogContent(ImportStatus success) {
|
||||
switch (success) {
|
||||
case ImportStatus.success:
|
||||
return (
|
||||
AppLocalizations.of(context).import_success_title,
|
||||
AppLocalizations.of(context).import_success_message
|
||||
);
|
||||
case ImportStatus.validationError:
|
||||
return (
|
||||
AppLocalizations.of(context).import_validation_error_title,
|
||||
AppLocalizations.of(context).import_validation_error_message
|
||||
);
|
||||
|
||||
case ImportStatus.formatError:
|
||||
return (
|
||||
AppLocalizations.of(context).import_format_error_title,
|
||||
AppLocalizations.of(context).import_format_error_message
|
||||
);
|
||||
case ImportStatus.genericError:
|
||||
return (
|
||||
AppLocalizations.of(context).import_generic_error_title,
|
||||
AppLocalizations.of(context).import_generic_error_message
|
||||
);
|
||||
case ImportStatus.canceled:
|
||||
return ('', '');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user