369 lines
12 KiB
Dart
369 lines
12 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';
|
|
import 'package:geolocator/geolocator.dart';
|
|
|
|
class AddEntriesDialogHelper {
|
|
// 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,
|
|
DatabasesEnum dbType,
|
|
) 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, dbType);
|
|
Navigator.pushNamedAndRemoveUntil(
|
|
context,
|
|
'/home',
|
|
(route) => false,
|
|
);
|
|
},
|
|
child: Text(AppLocalizations.of(context)!.template),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
static Future<dynamic> showServerErrorDialog(
|
|
BuildContext context,
|
|
Map<String, String> saveData,
|
|
bool isTemplate,
|
|
DatabasesEnum dbType,
|
|
) {
|
|
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) {
|
|
Navigator.pop(context);
|
|
// saveData(true);
|
|
SaveMainEntryMethod.saveEntry(
|
|
entryData: saveData,
|
|
isTemplate: isTemplate,
|
|
dbType: dbType,
|
|
);
|
|
showSuccessDialog(context);
|
|
}
|
|
},
|
|
child: Text(AppLocalizations.of(context)!.sendagain),
|
|
),
|
|
if (!isLoading)
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.pop(context);
|
|
},
|
|
child: Text(AppLocalizations.of(context)!.cancel),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
static Future<bool> showSaveOptionsDialog(
|
|
BuildContext context,
|
|
Map<String, String> saveData,
|
|
bool isTemplate,
|
|
DatabasesEnum dbType,
|
|
) async {
|
|
bool isLoading = false;
|
|
bool pop = false;
|
|
|
|
await showDialog(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
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 {
|
|
pop = true;
|
|
setState(() => isLoading = true);
|
|
saveTemplate(saveData, dbType);
|
|
Navigator.pushNamedAndRemoveUntil(
|
|
context,
|
|
'/home',
|
|
(route) => false,
|
|
);
|
|
|
|
if (context.mounted) {
|
|
SnackBarHelper.showSnackBarMessage(
|
|
context,
|
|
AppLocalizations.of(context)!.erfolgreich,
|
|
);
|
|
}
|
|
},
|
|
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,
|
|
dbType: dbType,
|
|
);
|
|
if (context.mounted) {
|
|
AddEntriesDialogHelper.showServerErrorDialog(
|
|
context,
|
|
saveData,
|
|
isTemplate,
|
|
dbType,
|
|
);
|
|
}
|
|
} else {
|
|
SaveMainEntryMethod.saveEntry(
|
|
entryData: saveData,
|
|
isTemplate: isTemplate,
|
|
dbType: dbType,
|
|
sent: true,
|
|
);
|
|
showSuccessDialog(context);
|
|
pop = true;
|
|
}
|
|
},
|
|
child: Text(AppLocalizations.of(context)!.sendtoserver),
|
|
),
|
|
if (!isLoading)
|
|
TextButton(
|
|
onPressed: () async {
|
|
setState(() => isLoading = true);
|
|
|
|
try {
|
|
int id = await SaveMainEntryMethod.saveEntry(
|
|
entryData: saveData,
|
|
isTemplate: isTemplate,
|
|
dbType: dbType,
|
|
);
|
|
try {
|
|
if (context.mounted) {
|
|
await SaveFileMethod.saveFile(
|
|
saveData,
|
|
id,
|
|
dbType == DatabasesEnum.place
|
|
? AppLocalizations.of(context)!.justplace
|
|
: AppLocalizations.of(context)!.excursion,
|
|
dbType,
|
|
);
|
|
if (context.mounted) {
|
|
Navigator.of(context).pop();
|
|
SnackBarHelper.showSnackBarMessage(
|
|
context,
|
|
AppLocalizations.of(context)!.erfolgreich,
|
|
);
|
|
}
|
|
pop = true;
|
|
}
|
|
} catch (_) {
|
|
// User cancelled the dialog
|
|
}
|
|
setState(() => isLoading = false);
|
|
} catch (e) {
|
|
if (context.mounted) {
|
|
Navigator.pop(context);
|
|
SnackBarHelper.showSnackBarMessage(
|
|
context,
|
|
AppLocalizations.of(context)!.savefilefailed,
|
|
);
|
|
}
|
|
}
|
|
},
|
|
child: Text(AppLocalizations.of(context)!.saveasfile),
|
|
),
|
|
if (!isLoading)
|
|
TextButton(
|
|
onPressed: () {
|
|
try {
|
|
SaveMainEntryMethod.saveEntry(
|
|
entryData: saveData,
|
|
isTemplate: isTemplate,
|
|
dbType: dbType,
|
|
);
|
|
Navigator.pushNamedAndRemoveUntil(
|
|
context,
|
|
'/home',
|
|
(route) => false,
|
|
);
|
|
pop = true;
|
|
if (context.mounted) {
|
|
SnackBarHelper.showSnackBarMessage(
|
|
context,
|
|
AppLocalizations.of(context)!.erfolgreich,
|
|
);
|
|
}
|
|
} catch (e) {
|
|
debugPrint(e.toString());
|
|
}
|
|
},
|
|
child: Text(AppLocalizations.of(context)!.justsave),
|
|
),
|
|
if (!isLoading)
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.pop(context);
|
|
},
|
|
child: Text(AppLocalizations.of(context)!.cancel),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
|
|
return pop;
|
|
}
|
|
|
|
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),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
static void locationSettingsDialog(BuildContext context) async {
|
|
return showDialog(
|
|
context: context,
|
|
barrierDismissible: true,
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
content: Text(AppLocalizations.of(context)!.needsAlwaysLocation),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () {
|
|
Geolocator.openAppSettings();
|
|
Navigator.pop(context);
|
|
},
|
|
child: Text("Ok"),
|
|
),
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
},
|
|
child: Text(AppLocalizations.of(context)!.cancel),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
static Future<bool> deleteCompleteRouteDialog(BuildContext context) async {
|
|
bool confirmed = false;
|
|
|
|
await showDialog(
|
|
context: context,
|
|
barrierDismissible: true,
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
title: Text(AppLocalizations.of(context)!.deleteEverything),
|
|
content: SingleChildScrollView(
|
|
child: ListBody(
|
|
children: <Widget>[
|
|
Text(AppLocalizations.of(context)!.deleteWholeRouteBody),
|
|
],
|
|
),
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () {
|
|
confirmed = true;
|
|
Navigator.of(context).pop();
|
|
},
|
|
child: Text("Ok"),
|
|
),
|
|
TextButton(
|
|
onPressed: () => {Navigator.of(context).pop()},
|
|
child: Text(AppLocalizations.of(context)!.cancel),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
|
|
return confirmed;
|
|
}
|
|
}
|