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