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