204 lines
8.3 KiB
Dart
204 lines
8.3 KiB
Dart
import 'package:fforte/enums/databases.dart';
|
|
import 'package:fforte/screens/helper/snack_bar_helper.dart';
|
|
import 'package:fforte/screens/sharedMethods/http_request.dart';
|
|
import 'package:fforte/screens/sharedMethods/save_file.dart';
|
|
import 'package:fforte/screens/sharedMethods/save_template.dart';
|
|
import 'package:fforte/screens/sharedMethods/save_main_entry.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:fforte/l10n/app_localizations.dart';
|
|
|
|
class DialogHelper {
|
|
// Function to show the dialog where the user has to choose if he want to safe his values as a template
|
|
static Future<void> showTemplateDialog(BuildContext context,
|
|
Map<String, String> saveData, bool update) async {
|
|
return showDialog(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
title: Text(AppLocalizations.of(context)!.fieldEmpty),
|
|
actions: <Widget>[
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
},
|
|
child: Text(AppLocalizations.of(context)!.cancel)),
|
|
TextButton(
|
|
onPressed: () {
|
|
saveTemplate(saveData, DatabasesEnum.place, update);
|
|
Navigator.pushNamedAndRemoveUntil(
|
|
context, '/home', (route) => false);
|
|
},
|
|
child: Text(AppLocalizations.of(context)!.template))
|
|
],
|
|
);
|
|
});
|
|
}
|
|
|
|
static Future<dynamic> showServerErrorDialog(
|
|
BuildContext context, Map<String, String> saveData, bool isTemplate) {
|
|
bool isLoading = false;
|
|
|
|
return showDialog(
|
|
context: context,
|
|
builder: (context) {
|
|
return StatefulBuilder(
|
|
builder: (BuildContext context, StateSetter setState) {
|
|
return AlertDialog(
|
|
title: Text(AppLocalizations.of(context)!.servererrortitle),
|
|
content: isLoading
|
|
? const SizedBox(
|
|
height: 100,
|
|
child: Center(child: CircularProgressIndicator()))
|
|
: null,
|
|
actions: [
|
|
if (!isLoading)
|
|
TextButton(
|
|
onPressed: () async {
|
|
setState(() => isLoading = true);
|
|
int errorCode = await HttpRequest.httpRequest(
|
|
saveDataMap: saveData);
|
|
setState(() => isLoading = false);
|
|
|
|
if (errorCode != 201 && context.mounted) {
|
|
showServerErrorDialog(
|
|
context, saveData, isTemplate);
|
|
} else {
|
|
if (context.mounted) Navigator.pop(context);
|
|
// saveData(true);
|
|
SaveMainEntryMethod.saveEntry(
|
|
entryData: saveData, isTemplate: isTemplate);
|
|
if (context.mounted) showSuccessDialog(context);
|
|
}
|
|
},
|
|
child: Text(AppLocalizations.of(context)!.sendagain)),
|
|
if (!isLoading)
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.pop(context);
|
|
},
|
|
child: Text(AppLocalizations.of(context)!.cancel))
|
|
],
|
|
);
|
|
},
|
|
);
|
|
});
|
|
}
|
|
|
|
static Future<void> showSaveOptionsDialog(BuildContext context,
|
|
Map<String, String> saveData, bool isTemplate) async {
|
|
bool isLoading = false;
|
|
|
|
return showDialog(
|
|
context: context,
|
|
barrierDismissible:
|
|
false, // Verhindert das Schließen des Dialogs durch den Benutzer
|
|
builder: (BuildContext context) {
|
|
return StatefulBuilder(
|
|
builder: (context, setState) {
|
|
return AlertDialog(
|
|
title: isLoading
|
|
? Text(AppLocalizations.of(context)!.loading)
|
|
: Text(AppLocalizations.of(context)!.savemethod),
|
|
content: isLoading
|
|
? const SizedBox(
|
|
height: 100,
|
|
child: Center(child: CircularProgressIndicator()))
|
|
: null,
|
|
actions: [
|
|
if (!isLoading)
|
|
TextButton(
|
|
onPressed: () async {
|
|
setState(() => isLoading = true);
|
|
saveTemplate(
|
|
saveData, DatabasesEnum.place, isTemplate);
|
|
Navigator.pushNamedAndRemoveUntil(
|
|
context, '/home', (route) => false);
|
|
},
|
|
child: Text(AppLocalizations.of(context)!.template)),
|
|
if (!isLoading)
|
|
TextButton(
|
|
onPressed: () async {
|
|
setState(() => isLoading = true);
|
|
int errorCode = await HttpRequest.httpRequest(
|
|
saveDataMap: saveData);
|
|
setState(() => isLoading = false);
|
|
|
|
if (errorCode != 201 || !context.mounted) {
|
|
SaveMainEntryMethod.saveEntry(
|
|
entryData: saveData, isTemplate: isTemplate);
|
|
if (context.mounted) {
|
|
DialogHelper.showServerErrorDialog(
|
|
context, saveData, isTemplate);
|
|
}
|
|
} else {
|
|
SaveMainEntryMethod.saveEntry(
|
|
entryData: saveData,
|
|
isTemplate: isTemplate,
|
|
sent: true);
|
|
showSuccessDialog(context);
|
|
}
|
|
},
|
|
child:
|
|
Text(AppLocalizations.of(context)!.sendtoserver)),
|
|
if (!isLoading)
|
|
TextButton(
|
|
onPressed: () async {
|
|
setState(() => isLoading = true);
|
|
|
|
try {
|
|
SaveMainEntryMethod.saveEntry(
|
|
entryData: saveData, isTemplate: isTemplate);
|
|
SaveFileMethod.saveFile(
|
|
saveData,
|
|
AppLocalizations.of(context)!.savefilefailed,
|
|
saveData["CID"]!);
|
|
setState(() => isLoading = false);
|
|
|
|
} catch (e) {
|
|
SnackBarHelper.showSnackBarMessage(context, AppLocalizations.of(context)!.savefilefailed);
|
|
}
|
|
|
|
},
|
|
child: Text(AppLocalizations.of(context)!.saveasfile)),
|
|
if (!isLoading)
|
|
TextButton(
|
|
onPressed: () {
|
|
SaveMainEntryMethod.saveEntry(
|
|
entryData: saveData, isTemplate: isTemplate);
|
|
Navigator.pushNamedAndRemoveUntil(
|
|
context, '/home', (route) => false);
|
|
},
|
|
child: Text(AppLocalizations.of(context)!.justsave)),
|
|
if (!isLoading)
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.pop(context);
|
|
},
|
|
child: Text(AppLocalizations.of(context)!.cancel)),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
});
|
|
}
|
|
|
|
static Future<void> showSuccessDialog(BuildContext context) async {
|
|
return showDialog(
|
|
context: context,
|
|
builder: (context) {
|
|
return AlertDialog(
|
|
title: Text(AppLocalizations.of(context)!.successful),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.pushNamedAndRemoveUntil(
|
|
context, '/home', (route) => false);
|
|
},
|
|
child: Text(AppLocalizations.of(context)!.continueB))
|
|
],
|
|
);
|
|
});
|
|
}
|
|
}
|