From c86d3503fbfdfc6f29b9b94e8bb46f79c5a620e3 Mon Sep 17 00:00:00 2001 From: Nico Date: Wed, 7 May 2025 21:58:24 +0200 Subject: [PATCH] outsourced all methods and dialogs as far as possible (I think) --- lib/home.dart | 2 +- lib/main.dart | 2 +- lib/screens/addCam/add_cam_main.dart | 427 +++++++++--------- .../excursion_main.dart | 14 +- .../widgets/anzahlen.dart | 0 .../widgets/bima_nutzer.dart | 0 .../widgets/hinweise.dart | 0 .../widgets/hund_u_leine.dart | 0 .../widgets/letzter_niederschlag.dart | 0 .../widgets/spur_gefunden.dart | 0 .../widgets/strecke_u_spurbedingungen.dart | 0 lib/screens/helper/dialog_helper.dart | 206 +++++++++ lib/screens/helper/snack_bar_helper.dart | 8 + lib/screens/sharedMethods/http_request.dart | 31 +- lib/screens/sharedMethods/save_file.dart | 25 +- .../sharedMethods/save_main_entry.dart | 21 + 16 files changed, 479 insertions(+), 257 deletions(-) rename lib/screens/{Excursion => excursion}/excursion_main.dart (97%) rename lib/screens/{Excursion => excursion}/widgets/anzahlen.dart (100%) rename lib/screens/{Excursion => excursion}/widgets/bima_nutzer.dart (100%) rename lib/screens/{Excursion => excursion}/widgets/hinweise.dart (100%) rename lib/screens/{Excursion => excursion}/widgets/hund_u_leine.dart (100%) rename lib/screens/{Excursion => excursion}/widgets/letzter_niederschlag.dart (100%) rename lib/screens/{Excursion => excursion}/widgets/spur_gefunden.dart (100%) rename lib/screens/{Excursion => excursion}/widgets/strecke_u_spurbedingungen.dart (100%) create mode 100644 lib/screens/helper/dialog_helper.dart create mode 100644 lib/screens/helper/snack_bar_helper.dart create mode 100644 lib/screens/sharedMethods/save_main_entry.dart diff --git a/lib/home.dart b/lib/home.dart index 9df1fdc..d30ac91 100644 --- a/lib/home.dart +++ b/lib/home.dart @@ -18,7 +18,7 @@ class HomePage extends StatelessWidget { File file = File(result.files.single.path!); String content = await file.readAsString(); - HttpRequest().httpRequest(content); + HttpRequest.httpRequest(saveDataString: content); } } diff --git a/lib/main.dart b/lib/main.dart index 4277bd9..d4ce89c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,4 @@ -import 'package:fforte/screens/Excursion/excursion_main.dart'; +import 'package:fforte/screens/excursion/excursion_main.dart'; import 'package:fforte/screens/addCam/add_cam_main.dart'; import 'package:fforte/screens/intro_screen.dart'; import 'package:fforte/screens/settings.dart'; diff --git a/lib/screens/addCam/add_cam_main.dart b/lib/screens/addCam/add_cam_main.dart index 4a669e6..d8957ff 100644 --- a/lib/screens/addCam/add_cam_main.dart +++ b/lib/screens/addCam/add_cam_main.dart @@ -1,13 +1,14 @@ import 'dart:async'; -import 'dart:convert'; import 'package:fforte/enums/databases.dart'; import 'package:fforte/screens/addCam/cam_widgets.dart'; -import 'package:fforte/methods/place_db_helper.dart'; +import 'package:fforte/screens/helper/dialog_helper.dart'; +import 'package:fforte/screens/helper/snack_bar_helper.dart'; import 'package:fforte/screens/sharedMethods/http_request.dart'; import 'package:fforte/screens/addCam/exceptions/location_disabled_exception.dart'; import 'package:fforte/screens/addCam/exceptions/location_forbidden_exception.dart'; import 'package:fforte/screens/addCam/services/geolocator_service.dart'; import 'package:fforte/screens/sharedMethods/save_file.dart'; +import 'package:fforte/screens/sharedMethods/save_main_entry.dart'; import 'package:fforte/screens/sharedMethods/save_template.dart'; import 'package:fforte/screens/sharedWidgets/datum.dart'; import 'package:fforte/screens/sharedWidgets/var_text_field.dart'; @@ -166,9 +167,9 @@ class _AddCamMainState extends State { GeolocatorService.deteterminePosition() .then((result) => currentPosition = result); } on LocationDisabledException { - showSnackBarMessage(AppLocalizations.of(context)!.locationDisabled); + SnackBarHelper.showSnackBarMessage(context, AppLocalizations.of(context)!.locationDisabled); } on LocationForbiddenException { - showSnackBarMessage(AppLocalizations.of(context)!.locationForbidden); + SnackBarHelper.showSnackBarMessage(context, AppLocalizations.of(context)!.locationForbidden); } // If a template is edited this fills in the existing values @@ -212,207 +213,203 @@ class _AddCamMainState extends State { } } - void showSnackBarMessage(String message) { - ScaffoldMessenger.of(context) - .showSnackBar(SnackBar(content: Text(message))); - } + // void showSnackBarMessage(String message) { + // ScaffoldMessenger.of(context) + // .showSnackBar(SnackBar(content: Text(message))); + // } // Function to show the dialog where the user has to choose if he want to safe his values as a template - Future showTemplateDialog(List emptyField) async { - return showDialog( - context: context, - barrierDismissible: false, - builder: (BuildContext context) { - return AlertDialog( - title: Text(AppLocalizations.of(context)!.fieldEmpty), - content: SingleChildScrollView( - child: ListBody(children: [Text(emptyField.join("; "))]), - ), - actions: [ - TextButton( - onPressed: () { - Navigator.of(context).pop(); - }, - child: Text(AppLocalizations.of(context)!.cancel)), - TextButton( - onPressed: () { - saveTemplate( - getPlace(), DatabasesEnum.place, widget.isTemplate); - Navigator.pushNamedAndRemoveUntil( - context, '/home', (route) => false); - }, - child: Text(AppLocalizations.of(context)!.template)) - ], - ); - }); - } + // Future showTemplateDialog(List emptyField) async { + // return showDialog( + // context: context, + // barrierDismissible: false, + // builder: (BuildContext context) { + // return AlertDialog( + // title: Text(AppLocalizations.of(context)!.fieldEmpty), + // content: SingleChildScrollView( + // child: ListBody(children: [Text(emptyField.join("; "))]), + // ), + // actions: [ + // TextButton( + // onPressed: () { + // Navigator.of(context).pop(); + // }, + // child: Text(AppLocalizations.of(context)!.cancel)), + // TextButton( + // onPressed: () { + // saveTemplate( + // getPlace(), DatabasesEnum.place, widget.isTemplate); + // Navigator.pushNamedAndRemoveUntil( + // context, '/home', (route) => false); + // }, + // child: Text(AppLocalizations.of(context)!.template)) + // ], + // ); + // }); + // } - Future _showServerErrorDialog() { - bool isLoading = false; + // Future _showServerErrorDialog() { + // 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: getPlace()); + // setState(() => isLoading = false); + // + // if (errorCode != 201 && context.mounted) { + // _showServerErrorDialog(); + // } else { + // if (context.mounted) Navigator.pop(context); + // // saveData(true); + // SaveMainEntryMethod.saveEntry(entryData: getPlace(), isTemplate: widget.isTemplate); + // _showSuccessDialog(); + // } + // }, + // child: Text(AppLocalizations.of(context)!.sendagain)), + // if (!isLoading) + // TextButton( + // onPressed: () { + // Navigator.pop(context); + // }, + // child: Text(AppLocalizations.of(context)!.cancel)) + // ], + // ); + // }, + // ); + // }); + // } - 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(); - setState(() => isLoading = false); + // Future showSaveOptionsDialog() 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(getPlace(), DatabasesEnum.place, + // widget.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: getPlace()); + // setState(() => isLoading = false); + // + // if (errorCode != 201 || !context.mounted) { + // SaveMainEntryMethod.saveEntry(entryData: getPlace(), isTemplate: widget.isTemplate); + // if (context.mounted) DialogHelper.showServerErrorDialog(context, getPlace(), widget.isTemplate); + // } else { + // SaveMainEntryMethod.saveEntry(entryData: getPlace(), isTemplate: widget.isTemplate, sent: true); + // _showSuccessDialog(); + // } + // }, + // child: + // Text(AppLocalizations.of(context)!.sendtoserver)), + // if (!isLoading) + // TextButton( + // onPressed: () async { + // setState(() => isLoading = true); + // SaveMainEntryMethod.saveEntry(entryData: getPlace(), isTemplate: widget.isTemplate); + // _saveFile(); + // setState(() => isLoading = false); + // }, + // child: Text(AppLocalizations.of(context)!.saveasfile)), + // if (!isLoading) + // TextButton( + // onPressed: () { + // SaveMainEntryMethod.saveEntry(entryData: getPlace(), isTemplate: widget.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)), + // ], + // ); + // }, + // ); + // }); + // } - if (errorCode != 201 && context.mounted) { - _showServerErrorDialog(); - } else { - if (context.mounted) Navigator.pop(context); - saveData(true); - _showSuccessDialog(); - } - }, - child: Text(AppLocalizations.of(context)!.sendagain)), - if (!isLoading) - TextButton( - onPressed: () { - Navigator.pop(context); - }, - child: Text(AppLocalizations.of(context)!.cancel)) - ], - ); - }, - ); - }); - } + // Future _showSuccessDialog() 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)) + // ], + // ); + // }); + // } - Future showSaveOptionsDialog() async { - bool isLoading = false; + // Future _httpRequest() async { + // Map place = getPlace(); + // + // HttpRequest method = HttpRequest(); + // + // await method.httpRequest(jsonEncode(place)); + // + // return method.errorCode; + // } - 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(getPlace(), DatabasesEnum.place, - widget.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(); - setState(() => isLoading = false); - - if (errorCode != 201 || !context.mounted) { - saveData(); - _showServerErrorDialog(); - } else { - saveData(true); - _showSuccessDialog(); - } - }, - child: - Text(AppLocalizations.of(context)!.sendtoserver)), - if (!isLoading) - TextButton( - onPressed: () async { - setState(() => isLoading = true); - saveData(); - _saveFile(); - setState(() => isLoading = false); - }, - child: Text(AppLocalizations.of(context)!.saveasfile)), - if (!isLoading) - TextButton( - onPressed: () { - saveData(); - 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)), - ], - ); - }, - ); - }); - } - - Future _showSuccessDialog() async { - return showDialog( - context: context, - builder: (context) { - return AlertDialog( - title: Text(AppLocalizations.of(context)!.successful), - actions: [ - /* TextButton( -onPressed: () { -Navigator.pop(context); -}, -child: Text(AppLocalizations.of(context)!.back)), */ - TextButton( - onPressed: () { - Navigator.pushNamedAndRemoveUntil( - context, '/home', (route) => false); - }, - child: Text(AppLocalizations.of(context)!.continueB)) - ], - ); - }); - } - - Future _httpRequest() async { - Map place = getPlace(); - - HttpRequest method = HttpRequest(); - - await method.httpRequest(jsonEncode(place)); - - return method.errorCode; - } - - void _saveFile() { - try { - saveFile( - getPlace(), AppLocalizations.of(context)!.justplace, standortC.text); - // TODO change to fileSaved - showSnackBarMessage(AppLocalizations.of(context)!.savefilefailed); - Navigator.pushNamedAndRemoveUntil(context, '/home', (route) => false); - } catch (e) { - showSnackBarMessage(AppLocalizations.of(context)!.savefilefailed); - } - } + // void _saveFile() { + // try { + // SaveMainEntryMethod.saveEntry( + // getPlace(), AppLocalizations.of(context)!.justplace, standortC.text); + // // TODO change to fileSaved. Also in SaveFileMethod class + // SnackBarHelper.showSnackBarMessage(context, AppLocalizations.of(context)!.savefilefailed); + // Navigator.pushNamedAndRemoveUntil(context, '/home', (route) => false); + // } catch (e) { + // SnackBarHelper.showSnackBarMessage(context, AppLocalizations.of(context)!.savefilefailed); + // } + // } // checks if required fields are not empty. If one is the name will be returned // TODO rewrite @@ -469,21 +466,21 @@ child: Text(AppLocalizations.of(context)!.back)), */ // TODO FINISHED HERE // If the user has filled all needed values this function will be called to safe them in the database // * also creates a json string to send it to the server later - void saveData([bool sent = false]) async { - var placeDB = PlaceDBHelper(); - Map place = getPlace(); - - // Get the ID of the newly added or updated place - int newPlaceId = await placeDB.addMainEntry(place); - - if (sent == true) { - placeDB.updateSent(newPlaceId); // Update 'Sent' using the correct ID - } - - if (widget.isTemplate) { - await placeDB.deleteTemplateById(cid.text); - } - } + // void saveData([bool sent = false]) async { + // var placeDB = PlaceDBHelper(); + // Map place = getPlace(); + // + // // Get the ID of the newly added or updated place + // int newPlaceId = await placeDB.addMainEntry(place); + // + // if (sent == true) { + // placeDB.updateSent(newPlaceId); // Update 'Sent' using the correct ID + // } + // + // if (widget.isTemplate) { + // await placeDB.deleteTemplateById(cid.text); + // } + // } // The widget tree which gets the shown widget from the ./cam_widgets.dart file // The names of the widgets should be self-explaining @@ -922,10 +919,10 @@ child: Text(AppLocalizations.of(context)!.back)), */ Navigator.pushNamedAndRemoveUntil( context, '/home', (route) => false); } else if (empty == true) { - showTemplateDialog(emptyFields); + DialogHelper.showTemplateDialog(context, emptyFields, getPlace() ); return; } else if (empty == false) { - await showSaveOptionsDialog(); + await DialogHelper.showSaveOptionsDialog(context, getPlace(), widget.isTemplate); } } }, diff --git a/lib/screens/Excursion/excursion_main.dart b/lib/screens/excursion/excursion_main.dart similarity index 97% rename from lib/screens/Excursion/excursion_main.dart rename to lib/screens/excursion/excursion_main.dart index 1b3ab0d..f300fff 100644 --- a/lib/screens/Excursion/excursion_main.dart +++ b/lib/screens/excursion/excursion_main.dart @@ -1,12 +1,12 @@ import 'package:animations/animations.dart'; import 'package:fforte/enums/databases.dart'; -import 'package:fforte/screens/Excursion/widgets/anzahlen.dart'; -import 'package:fforte/screens/Excursion/widgets/bima_nutzer.dart'; -import 'package:fforte/screens/Excursion/widgets/hinweise.dart'; -import 'package:fforte/screens/Excursion/widgets/hund_u_leine.dart'; -import 'package:fforte/screens/Excursion/widgets/letzter_niederschlag.dart'; -import 'package:fforte/screens/Excursion/widgets/spur_gefunden.dart'; -import 'package:fforte/screens/Excursion/widgets/strecke_u_spurbedingungen.dart'; +import 'package:fforte/screens/excursion/widgets/anzahlen.dart'; +import 'package:fforte/screens/excursion/widgets/bima_nutzer.dart'; +import 'package:fforte/screens/excursion/widgets/hinweise.dart'; +import 'package:fforte/screens/excursion/widgets/hund_u_leine.dart'; +import 'package:fforte/screens/excursion/widgets/letzter_niederschlag.dart'; +import 'package:fforte/screens/excursion/widgets/spur_gefunden.dart'; +import 'package:fforte/screens/excursion/widgets/strecke_u_spurbedingungen.dart'; import 'package:fforte/screens/sharedWidgets/datum.dart'; import 'package:fforte/screens/sharedWidgets/var_text_field.dart'; import 'package:flutter/material.dart'; diff --git a/lib/screens/Excursion/widgets/anzahlen.dart b/lib/screens/excursion/widgets/anzahlen.dart similarity index 100% rename from lib/screens/Excursion/widgets/anzahlen.dart rename to lib/screens/excursion/widgets/anzahlen.dart diff --git a/lib/screens/Excursion/widgets/bima_nutzer.dart b/lib/screens/excursion/widgets/bima_nutzer.dart similarity index 100% rename from lib/screens/Excursion/widgets/bima_nutzer.dart rename to lib/screens/excursion/widgets/bima_nutzer.dart diff --git a/lib/screens/Excursion/widgets/hinweise.dart b/lib/screens/excursion/widgets/hinweise.dart similarity index 100% rename from lib/screens/Excursion/widgets/hinweise.dart rename to lib/screens/excursion/widgets/hinweise.dart diff --git a/lib/screens/Excursion/widgets/hund_u_leine.dart b/lib/screens/excursion/widgets/hund_u_leine.dart similarity index 100% rename from lib/screens/Excursion/widgets/hund_u_leine.dart rename to lib/screens/excursion/widgets/hund_u_leine.dart diff --git a/lib/screens/Excursion/widgets/letzter_niederschlag.dart b/lib/screens/excursion/widgets/letzter_niederschlag.dart similarity index 100% rename from lib/screens/Excursion/widgets/letzter_niederschlag.dart rename to lib/screens/excursion/widgets/letzter_niederschlag.dart diff --git a/lib/screens/Excursion/widgets/spur_gefunden.dart b/lib/screens/excursion/widgets/spur_gefunden.dart similarity index 100% rename from lib/screens/Excursion/widgets/spur_gefunden.dart rename to lib/screens/excursion/widgets/spur_gefunden.dart diff --git a/lib/screens/Excursion/widgets/strecke_u_spurbedingungen.dart b/lib/screens/excursion/widgets/strecke_u_spurbedingungen.dart similarity index 100% rename from lib/screens/Excursion/widgets/strecke_u_spurbedingungen.dart rename to lib/screens/excursion/widgets/strecke_u_spurbedingungen.dart diff --git a/lib/screens/helper/dialog_helper.dart b/lib/screens/helper/dialog_helper.dart new file mode 100644 index 0000000..da4401a --- /dev/null +++ b/lib/screens/helper/dialog_helper.dart @@ -0,0 +1,206 @@ +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:flutter_gen/gen_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 showTemplateDialog(BuildContext context, + List emptyField, Map saveData) async { + return showDialog( + context: context, + barrierDismissible: false, + builder: (BuildContext context) { + return AlertDialog( + title: Text(AppLocalizations.of(context)!.fieldEmpty), + content: SingleChildScrollView( + child: ListBody(children: [Text(emptyField.join("; "))]), + ), + actions: [ + TextButton( + onPressed: () { + Navigator.of(context).pop(); + }, + child: Text(AppLocalizations.of(context)!.cancel)), + TextButton( + onPressed: () { + saveTemplate(saveData, DatabasesEnum.place, true); + Navigator.pushNamedAndRemoveUntil( + context, '/home', (route) => false); + }, + child: Text(AppLocalizations.of(context)!.template)) + ], + ); + }); + } + + static Future showServerErrorDialog( + BuildContext context, Map 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 showSaveOptionsDialog(BuildContext context, + Map 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 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)) + ], + ); + }); + } +} diff --git a/lib/screens/helper/snack_bar_helper.dart b/lib/screens/helper/snack_bar_helper.dart new file mode 100644 index 0000000..ef5e5e9 --- /dev/null +++ b/lib/screens/helper/snack_bar_helper.dart @@ -0,0 +1,8 @@ +import 'package:flutter/material.dart'; + +class SnackBarHelper { + static void showSnackBarMessage(BuildContext context, String message) { + ScaffoldMessenger.of(context) + .showSnackBar(SnackBar(content: Text(message))); + } +} diff --git a/lib/screens/sharedMethods/http_request.dart b/lib/screens/sharedMethods/http_request.dart index 9560107..5969422 100644 --- a/lib/screens/sharedMethods/http_request.dart +++ b/lib/screens/sharedMethods/http_request.dart @@ -6,37 +6,26 @@ import 'package:dio/dio.dart'; import 'package:shared_preferences/shared_preferences.dart'; class HttpRequest { - int? _errorCode; - - - int get errorCode => _errorCode ?? 0; - - - Future httpRequest(String httpData) async { + static Future httpRequest({Map? saveDataMap, String? saveDataString}) async { // print(jsonEncode(place)); final dio = Dio(); final SharedPreferences prefs = await SharedPreferences.getInstance(); dio.options - ..connectTimeout = const Duration(seconds: 5) - ..receiveTimeout = const Duration(seconds: 5) - ..responseType = ResponseType.plain; - Response response = Response(requestOptions: RequestOptions(path: ''), statusCode: 400); + ..connectTimeout = const Duration(seconds: 5) + ..receiveTimeout = const Duration(seconds: 5) + ..responseType = ResponseType.plain; + Response response = + Response(requestOptions: RequestOptions(path: ''), statusCode: 400); try { response = await dio.post(prefs.getString('apiAddress') ?? "", - data: jsonEncode(httpData)); + data: saveDataMap == null ? saveDataString : jsonEncode(saveDataMap)); - // ignore: unused_catch_clause - } on DioException catch (e) { - _errorCode = response.statusCode; - print('is hier. var: $_errorCode'); - return; + return response.statusCode!; + } on DioException { + return response.statusCode ?? 400; } - _errorCode = response.statusCode; - print('is hier 2. var: $_errorCode'); } } - - diff --git a/lib/screens/sharedMethods/save_file.dart b/lib/screens/sharedMethods/save_file.dart index e71bc31..ea56c6b 100644 --- a/lib/screens/sharedMethods/save_file.dart +++ b/lib/screens/sharedMethods/save_file.dart @@ -4,18 +4,19 @@ import 'dart:io'; import 'package:file_picker/file_picker.dart'; import 'package:shared_preferences/shared_preferences.dart'; -Future saveFile( - Map place, String fileNameLocalization, String placeID) async { - String? selectedDirectory = await FilePicker.platform.getDirectoryPath(); - SharedPreferences prefs = await SharedPreferences.getInstance(); - String jsonPlace = jsonEncode(place); +class SaveFileMethod { + static Future saveFile(Map place, String fileNameLocalization, String placeID) async { + String? selectedDirectory = await FilePicker.platform.getDirectoryPath(); + SharedPreferences prefs = await SharedPreferences.getInstance(); + String jsonPlace = jsonEncode(place); - if (selectedDirectory == null) { - return; + if (selectedDirectory == null) { + return; + } + await prefs.setString('saveDir', selectedDirectory); + + File file = File('$selectedDirectory/$fileNameLocalization-$placeID.txt'); + + await file.writeAsString(jsonPlace); } - await prefs.setString('saveDir', selectedDirectory); - - File file = File('$selectedDirectory/$fileNameLocalization-$placeID.txt'); - - await file.writeAsString(jsonPlace); } diff --git a/lib/screens/sharedMethods/save_main_entry.dart b/lib/screens/sharedMethods/save_main_entry.dart new file mode 100644 index 0000000..c4bfaa5 --- /dev/null +++ b/lib/screens/sharedMethods/save_main_entry.dart @@ -0,0 +1,21 @@ +import 'package:fforte/methods/place_db_helper.dart'; + +class SaveMainEntryMethod { + static void saveEntry( + {required Map entryData, + required bool isTemplate, + bool sent = false}) async { + var placeDB = PlaceDBHelper(); + + // Get the ID of the newly added or updated place + int newPlaceId = await placeDB.addMainEntry(entryData); + + if (sent == true) { + placeDB.updateSent(newPlaceId); // Update 'Sent' using the correct ID + } + + if (isTemplate) { + await placeDB.deleteTemplateById(entryData["CID"]); + } + } +}