server things

This commit is contained in:
nico
2024-05-15 19:59:10 +02:00
parent 3528e2ee6c
commit 1656ffb00c
7 changed files with 200 additions and 145 deletions

View File

@@ -15,13 +15,15 @@ import 'package:shared_preferences/shared_preferences.dart';
class AddCamMain extends StatefulWidget {
final bool isTemplate;
final bool isFinished;
final bool isSent;
final Map<String, dynamic>? existingData;
const AddCamMain(
{super.key,
this.isTemplate = false,
this.existingData,
this.isFinished = false});
this.isFinished = false,
this.isSent = false});
@override
State<AddCamMain> createState() => _AddCamMainState();
@@ -121,9 +123,6 @@ class _AddCamMainState extends State<AddCamMain> {
// determine live position with checks for denied permission and turned off location service
Future<Position> _deteterminePosition() async {
bool locationEnabled;
LocationPermission permissionGiven;
@@ -182,10 +181,10 @@ class _AddCamMainState extends State<AddCamMain> {
selectedPlatzung = widget.existingData!['Platzung'] ?? "";
kSchloNrC.text = widget.existingData!['KSchloNr'] ?? "";
datum = DateTime.parse(widget.existingData!['Datum']);
kontDat = widget.existingData!['KontDat'] == null
kontDat = widget.existingData!['KontDat'] == ""
? null
: DateTime.parse(widget.existingData!['KontDat']);
abbauDat = widget.existingData!['AbbauDat'] == null
abbauDat = widget.existingData!['AbbauDat'] == ""
? null
: DateTime.parse(widget.existingData!['AbbauDat']);
auftragC.text = widget.existingData!['Auftrag'] ?? "";
@@ -235,114 +234,155 @@ class _AddCamMainState extends State<AddCamMain> {
}
Future<dynamic> _showServerErrorDialog() {
bool isLoading = false;
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text(AppLocalizations.of(context)!.servererrortitle),
actions: [
TextButton(
onPressed: () async {
int errorCode = _httpRequest();
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);
if (errorCode != 201) {
Navigator.pop(context);
_showServerErrorDialog();
}
},
child: Text(AppLocalizations.of(context)!.sendagain)),
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> showSaveOptionsDialog() async {
bool isLoading = false;
return showDialog(
context: context,
barrierDismissible:
false, // Verhindert das Schließen des Dialogs durch den Benutzer
builder: (BuildContext context) {
return AlertDialog(
title: Text(AppLocalizations.of(context)!.savemethod),
actions: [
TextButton(
onPressed: () async {
saveTemplate();
Navigator.pushNamedAndRemoveUntil(
context,
'/home',
(route) => false);
},
child: Text(AppLocalizations.of(context)!.template)),
TextButton(
onPressed: () async {
int errorCode = _httpRequest();
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();
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) {
saveData();
_showServerErrorDialog();
} else {
saveData();
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))
],
);
});
}
},
child: Text(AppLocalizations.of(context)!.sendtoserver)),
TextButton(
onPressed: () async {
saveData();
saveFile();
},
child: Text(AppLocalizations.of(context)!.saveasfile)),
TextButton(
onPressed: () {
saveData();
Navigator.pushNamedAndRemoveUntil(
context,
'/home',
(route) => false);
},
child: Text(AppLocalizations.of(context)!.justsave)),
TextButton(
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)!.cancel)),
child: Text(AppLocalizations.of(context)!.back)), */
TextButton(
onPressed: () {
Navigator.pushNamedAndRemoveUntil(
context, '/home', (route) => false);
},
child: Text(AppLocalizations.of(context)!.continueB))
],
);
});
}
int _httpRequest() {
Future<int> _httpRequest() async {
Map<String, dynamic> place = getPlace();
Methods method = Methods();
HttpRequest method = HttpRequest();
method.httpRequest(jsonEncode(place));
await method.httpRequest(jsonEncode(place));
return method.errorCode;
}
@@ -366,21 +406,19 @@ class _AddCamMainState extends State<AddCamMain> {
await file.writeAsString(jsonPlace);
} catch (e) {
if (mounted) {
Navigator.pop(context);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(AppLocalizations.of(context)!.savefilefailed))); }
Navigator.pop(context);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(AppLocalizations.of(context)!.savefilefailed)));
}
return;
}
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Datei gespeichert in $selectedDirectory')));
SnackBar(content: Text('Datei gespeichert in $selectedDirectory')));
}
if (mounted) {
Navigator.pushNamedAndRemoveUntil(
context,
'/home',
(route) => false);
Navigator.pushNamedAndRemoveUntil(context, '/home', (route) => false);
}
}
@@ -432,12 +470,16 @@ class _AddCamMainState extends State<AddCamMain> {
// 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() async {
void saveData([bool sent = false]) async {
var placeDB = DBHelper();
Map<String, dynamic> place = getPlace();
await placeDB.addPlace(place);
// Get the ID of the newly added or updated place
int newPlaceId = await placeDB.addPlace(place);
if (sent == true) {
placeDB.updateSent(newPlaceId); // Update 'Sent' using the correct ID
}
if (widget.isTemplate) {
await placeDB.deleteTemplate(cid.text);
@@ -721,7 +763,7 @@ class _AddCamMainState extends State<AddCamMain> {
onDateChanged: (value) {
abbauDat = value;
},
),
),
],
),
const SizedBox(
@@ -832,7 +874,9 @@ class _AddCamMainState extends State<AddCamMain> {
List<String> emptyFields = validateData();
// ! always filled out
empty = false;
if (empty == true) {
if (widget.isSent) {
Navigator.pushNamedAndRemoveUntil(context, '/home', (route) => false);
} else if (empty == true) {
showTemplateDialog(emptyFields);
return;
} else if (empty == false) {