refactor code

This commit is contained in:
Valentin CZERYBA 2025-01-20 21:42:14 +01:00
parent 56ae77137c
commit b7fe1da681

View File

@ -281,62 +281,58 @@ class _MyHomePageState extends State<ListItemMenu> {
} }
Future<Uri> getUrlForEvents() async { Future<Uri> getUrlForEvents() async {
SharedPreferences prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
double latitude = prefs.getDouble("city_lat") ?? 0.0; final latitude = prefs.getDouble("city_lat") ?? 0.0;
double longitude = prefs.getDouble("city_long") ?? 0.0; final longitude = prefs.getDouble("city_long") ?? 0.0;
String stringParameter = "";
final radiusInKm = 50;
String endpoint = "events"; String endpoint = "events";
if ((latitude != 0.0) && (longitude != 0.0)) { String queryParameters = "";
// Calculate the boundaries
double radiusInKm = 50; if (latitude != 0.0 && longitude != 0.0) {
double latDistance = radiusInKm / 111.0; final latDistance = radiusInKm / 111.0;
double lonDistance = radiusInKm / (111.0 * cos(latitude * pi / 180)); final lonDistance = radiusInKm / (111.0 * cos(latitude * pi / 180));
final minLat = latitude - latDistance;
final maxLat = latitude + latDistance;
final minLon = longitude - lonDistance;
final maxLon = longitude + lonDistance;
double minLat = latitude - latDistance;
double maxLat = latitude + latDistance;
double minLon = longitude - lonDistance;
double maxLon = longitude + lonDistance;
endpoint = "events/search"; endpoint = "events/search";
queryParameters =
stringParameter = "min_lat=$minLat&max_lat=$maxLat" "min_lat=$minLat&max_lat=$maxLat&min_lon=$minLon&max_lon=$maxLon";
"&min_lon=$minLon&max_lon=$maxLon";
} }
DateTime currentDate = DateTime.now();
String dateParameter = "current_datetime=${currentDate.toString()}"; final currentDate = DateTime.now();
if (startDatepicker.text.isNotEmpty) { String dateParameter = "current_datetime=${currentDate.toIso8601String()}";
var date = DateTime.parse(formatDate(startDatepicker.text));
dateParameter = "date_event=" + date.toString(); if (startDatepicker.text.isNotEmpty || endDatepicker.text.isNotEmpty) {
endpoint = "events/search"; endpoint = "events/search";
if (startDatepicker.text.isNotEmpty) {
final startDate = DateTime.parse(formatDate(startDatepicker.text));
dateParameter = "start_date=${startDate.toIso8601String()}";
} }
if (endDatepicker.text.isNotEmpty) { if (endDatepicker.text.isNotEmpty) {
var date = DateTime.parse(formatDate(endDatepicker.text)); final endDate = DateTime.parse(formatDate(endDatepicker.text));
dateParameter = "date_event=" + date.toString(); dateParameter += "&end_date=${endDate.toIso8601String()}";
endpoint = "events/search";
} }
if ((startDatepicker.text.isNotEmpty) && (endDatepicker.text.isNotEmpty)) {
var startDate = DateTime.parse(formatDate(startDatepicker.text));
var endDate = DateTime.parse(formatDate(endDatepicker.text));
dateParameter = "&start_date=" +
startDate.toString() +
"&end_date=" +
endDate.toString();
endpoint = "events/search";
} }
if (inputItem.text.isNotEmpty) { if (inputItem.text.isNotEmpty) {
stringParameter = stringParameter + "&item=${inputItem.text}"; queryParameters += "&item=${inputItem.text}";
endpoint = "events/search";
} }
if (inputTags.text.isNotEmpty) { if (inputTags.text.isNotEmpty) {
stringParameter = stringParameter + "&tags=${inputTags.text}"; queryParameters += "&tags=${inputTags.text}";
endpoint = "events/search";
} }
if (stringParameter.isNotEmpty) {
stringParameter = "$stringParameter&$dateParameter"; if (queryParameters.isNotEmpty) {
queryParameters = "$queryParameters&$dateParameter";
} else { } else {
stringParameter = dateParameter; queryParameters = dateParameter;
} }
return Uri.parse("${globals.api}/${endpoint}?${stringParameter}");
return Uri.parse("${globals.api}/$endpoint?$queryParameters");
} }
Future<void> searchSuggestionsByItem(String input) async { Future<void> searchSuggestionsByItem(String input) async {