outsourced all methods and dialogs as far as possible (I think)

This commit is contained in:
Nico
2025-05-07 21:58:24 +02:00
parent 86ffd77888
commit c86d3503fb
16 changed files with 479 additions and 257 deletions

View File

@@ -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);
}
}
},