outsourced all methods and dialogs as far as possible (I think)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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<AddCamMain> {
|
||||
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<AddCamMain> {
|
||||
}
|
||||
}
|
||||
|
||||
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<void> showTemplateDialog(List<String> emptyField) async {
|
||||
return showDialog(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Text(AppLocalizations.of(context)!.fieldEmpty),
|
||||
content: SingleChildScrollView(
|
||||
child: ListBody(children: <Widget>[Text(emptyField.join("; "))]),
|
||||
),
|
||||
actions: <Widget>[
|
||||
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<void> showTemplateDialog(List<String> emptyField) async {
|
||||
// return showDialog(
|
||||
// context: context,
|
||||
// barrierDismissible: false,
|
||||
// builder: (BuildContext context) {
|
||||
// return AlertDialog(
|
||||
// title: Text(AppLocalizations.of(context)!.fieldEmpty),
|
||||
// content: SingleChildScrollView(
|
||||
// child: ListBody(children: <Widget>[Text(emptyField.join("; "))]),
|
||||
// ),
|
||||
// actions: <Widget>[
|
||||
// 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<dynamic> _showServerErrorDialog() {
|
||||
bool isLoading = false;
|
||||
// Future<dynamic> _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<void> 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<void> _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<void> showSaveOptionsDialog() async {
|
||||
bool isLoading = false;
|
||||
// Future<int> _httpRequest() async {
|
||||
// Map<String, dynamic> 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<void> _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<int> _httpRequest() async {
|
||||
Map<String, dynamic> 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<String, dynamic> 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<String, dynamic> 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);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -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';
|
||||
206
lib/screens/helper/dialog_helper.dart
Normal file
206
lib/screens/helper/dialog_helper.dart
Normal file
@@ -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<void> showTemplateDialog(BuildContext context,
|
||||
List<String> emptyField, Map<String, dynamic> saveData) async {
|
||||
return showDialog(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Text(AppLocalizations.of(context)!.fieldEmpty),
|
||||
content: SingleChildScrollView(
|
||||
child: ListBody(children: <Widget>[Text(emptyField.join("; "))]),
|
||||
),
|
||||
actions: <Widget>[
|
||||
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<dynamic> showServerErrorDialog(
|
||||
BuildContext context, Map<String, dynamic> 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, dynamic> 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))
|
||||
],
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
8
lib/screens/helper/snack_bar_helper.dart
Normal file
8
lib/screens/helper/snack_bar_helper.dart
Normal file
@@ -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)));
|
||||
}
|
||||
}
|
||||
@@ -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<void> httpRequest(String httpData) async {
|
||||
static Future<int> httpRequest({Map<String, dynamic>? 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');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -4,18 +4,19 @@ import 'dart:io';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
Future<void> 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<void> saveFile(Map<String, dynamic> 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);
|
||||
}
|
||||
|
||||
21
lib/screens/sharedMethods/save_main_entry.dart
Normal file
21
lib/screens/sharedMethods/save_main_entry.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
import 'package:fforte/methods/place_db_helper.dart';
|
||||
|
||||
class SaveMainEntryMethod {
|
||||
static void saveEntry(
|
||||
{required Map<String, dynamic> 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"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user