From a5533aa0d3e9e2c197d703a869b889bb2dcf7586 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 24 Nov 2024 22:44:35 +0100 Subject: [PATCH] change place input + fix endpoint --- covas_mobile/lib/pages/ListItemMenu.dart | 69 +++++++++++++++--------- 1 file changed, 44 insertions(+), 25 deletions(-) diff --git a/covas_mobile/lib/pages/ListItemMenu.dart b/covas_mobile/lib/pages/ListItemMenu.dart index dec8726..44dc232 100644 --- a/covas_mobile/lib/pages/ListItemMenu.dart +++ b/covas_mobile/lib/pages/ListItemMenu.dart @@ -50,7 +50,7 @@ class _MyHomePageState extends State { TextEditingController inputItem = TextEditingController(); bool showDateFields = false; // State to toggle date fields - + bool showArrow = true; // Fetching events from API static Future> getPosts() async { PermissionStatus status = await Permission.location.status; @@ -258,6 +258,9 @@ class _MyHomePageState extends State { 'geometry'], // Include geometry for latitude/longitude }) .toList(); + if (suggestions.isNotEmpty) { + showArrow = false; + } }); } else { throw Exception('Failed to load suggestions'); @@ -272,7 +275,7 @@ class _MyHomePageState extends State { double latitude = prefs.getDouble("city_lat") ?? 0.0; double longitude = prefs.getDouble("city_long") ?? 0.0; String stringParameter = ""; - String dateParameter = "current_datetime"; + String endpoint = "events"; if ((latitude != 0.0) && (longitude != 0.0)) { // Calculate the boundaries double radiusInKm = 50; @@ -283,19 +286,22 @@ class _MyHomePageState extends State { double maxLat = latitude + latDistance; double minLon = longitude - lonDistance; double maxLon = longitude + lonDistance; + endpoint = "events/search"; stringParameter = "min_lat=$minLat&max_lat=$maxLat" "&min_lon=$minLon&max_lon=$maxLon"; } DateTime currentDate = DateTime.now(); - dateParameter = "¤t_datetime=${currentDate.toString()}"; + String dateParameter = "current_datetime=${currentDate.toString()}"; if (startDatepicker.text.isNotEmpty) { var date = DateTime.parse(formatDate(startDatepicker.text)); - dateParameter = "&date_event=" + date.toString(); + dateParameter = "date_event=" + date.toString(); + endpoint = "events/search"; } if (endDatepicker.text.isNotEmpty) { var date = DateTime.parse(formatDate(endDatepicker.text)); - dateParameter = "&date_event=" + date.toString(); + dateParameter = "date_event=" + date.toString(); + endpoint = "events/search"; } if ((startDatepicker.text.isNotEmpty) && (endDatepicker.text.isNotEmpty)) { @@ -305,13 +311,20 @@ class _MyHomePageState extends State { startDate.toString() + "&end_date=" + endDate.toString(); + endpoint = "events/search"; } - stringParameter = stringParameter + dateParameter; + if (inputItem.text.isNotEmpty) { stringParameter = stringParameter + "&item=${inputItem.text}"; + endpoint = "events/search"; + } + if (stringParameter.isNotEmpty) { + stringParameter = "$stringParameter&$dateParameter"; + } else { + stringParameter = dateParameter; } print("stringParameter : ${stringParameter}"); - var url = Uri.parse("${globals.api}/events/search?${stringParameter}"); + var url = Uri.parse("${globals.api}/${endpoint}?${stringParameter}"); final response = await http.get(url, headers: { "Content-Type": "application/json", HttpHeaders.cookieHeader: "access_token=$accessToken" @@ -408,10 +421,9 @@ class _MyHomePageState extends State { inputGeo.clear(); // Clear the text field geographicalZone = ''; // Reset the geographical zone state suggestions.clear(); // Optionally clear suggestions - fetchPostsByLocation(); - /// Clear the filtered posts }); + fetchPostsByLocation(); }, ), ), @@ -424,7 +436,7 @@ class _MyHomePageState extends State { ), if (suggestions.isNotEmpty) Container( - height: 200, + height: 175, decoration: BoxDecoration( border: Border.all(color: Colors.blue), borderRadius: BorderRadius.circular(8), @@ -445,6 +457,7 @@ class _MyHomePageState extends State { geographicalZone = suggestions[index]['place_name']; inputGeo.text = geographicalZone; suggestions.clear(); + showArrow = true; }); SharedPreferences prefs = await SharedPreferences.getInstance(); @@ -506,23 +519,29 @@ class _MyHomePageState extends State { body: Column( children: [ _buildItemZoneSearchField(), + if (showDateFields) + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Flexible(child: _buildDateField("start")), + Flexible(child: _buildDateField("end")) + ]), if (showDateFields) _buildGeographicalZoneSearchField(), - if (showDateFields) _buildDateField("start"), - if (showDateFields) _buildDateField("end"), - IconButton( - onPressed: () { - setState(() { - showDateFields = !showDateFields; // Toggle visibility - }); - }, - icon: Icon( - showDateFields - ? Icons.keyboard_arrow_up - : Icons.keyboard_arrow_down, - color: Colors.blue, + if (showArrow) + IconButton( + onPressed: () { + setState(() { + showDateFields = !showDateFields; // Toggle visibility + }); + }, + icon: Icon( + showDateFields + ? Icons.keyboard_arrow_up + : Icons.keyboard_arrow_down, + color: Colors.blue, + ), + tooltip: showDateFields ? 'Show Date Fields' : 'Hide Date Fields', ), - tooltip: showDateFields ? 'Show Date Fields' : 'Hide Date Fields', - ), Expanded( child: FutureBuilder>( future: postsFuture,