From d4f8ee2182ed875bcd91f6e51eb3297c85d61532 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 23 Nov 2024 17:24:46 +0100 Subject: [PATCH] add parameter --- covas_mobile/lib/pages/ListItemMenu.dart | 34 +++++++++++++++++----- covas_mobile/lib/pages/SearchDelegate.dart | 21 +++++++++++-- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/covas_mobile/lib/pages/ListItemMenu.dart b/covas_mobile/lib/pages/ListItemMenu.dart index 3ba5571..b6a92ef 100644 --- a/covas_mobile/lib/pages/ListItemMenu.dart +++ b/covas_mobile/lib/pages/ListItemMenu.dart @@ -112,6 +112,15 @@ class _MyHomePageState extends State { return body; } + String formatDate(String date) { + var splitedDate = date.split("-"); + var day = splitedDate[0]; + var month = splitedDate[1]; + var year = splitedDate[2]; + + return "${year}-${month}-${day}"; + } + // Fetching events from API Future> getAllPosts() async { SharedPreferences prefs = await SharedPreferences.getInstance(); @@ -122,8 +131,9 @@ class _MyHomePageState extends State { var url = Uri.parse( "${globals.api}/events?current_datetime=${currentDateTime.toString()}"); if (Datepicker.text.isNotEmpty) { - url = Uri.parse( - "${globals.api}/events?current_datetime=${currentDateTime.toString()}"); + TimeOfDay time = TimeOfDay.now(); + var date = formatDate(Datepicker.text) + "T" + time.toString(); + url = Uri.parse("${globals.api}/events?date_event=${date}"); } final response = await http.get(url, headers: { "Content-Type": "application/json", @@ -311,9 +321,12 @@ class _MyHomePageState extends State { "?min_lat=$minLat&max_lat=$maxLat" "&min_lon=$minLon&max_lon=$maxLon¤t_datetime=${currentDate.toString()}"); if (Datepicker.text.isNotEmpty) { + TimeOfDay time = TimeOfDay.now(); + var date = formatDate(Datepicker.text) + "T" + time.toString(); url = Uri.parse("${globals.api}/events/search" - "?min_lat=$minLat&max_lat=$maxLat" - "&min_lon=$minLon&max_lon=$maxLon"); + "?min_lat=$minLat&max_lat=$maxLat" + "&min_lon=$minLon&max_lon=$maxLon&date_event=" + + date); } final response = await http.get(url, headers: { @@ -349,6 +362,8 @@ class _MyHomePageState extends State { context: context, firstDate: dateEvent, lastDate: DateTime(2104)); if (pickedDate == null) return; Datepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate); + SharedPreferences prefs = await SharedPreferences.getInstance(); + prefs.setString("date_event", Datepicker.text); if (inputGeo.text.isEmpty) { _fetchInitialData(); } else { @@ -370,8 +385,8 @@ class _MyHomePageState extends State { onPressed: () async { SharedPreferences prefs = await SharedPreferences.getInstance(); - prefs.remove("city_lat"); - prefs.remove("city_long"); + + prefs.remove("date_event"); setState(() { Datepicker.text = ''; }); @@ -394,7 +409,11 @@ class _MyHomePageState extends State { border: OutlineInputBorder(), suffixIcon: IconButton( icon: const Icon(Icons.clear), - onPressed: () { + onPressed: () async { + SharedPreferences prefs = + await SharedPreferences.getInstance(); + prefs.remove("city_lat"); + prefs.remove("city_long"); setState(() { inputGeo.clear(); // Clear the text field geographicalZone = ''; // Reset the geographical zone state @@ -527,7 +546,6 @@ class _MyHomePageState extends State { final startDate = DateTime.parse(post.startDate!); final date = DateFormat.yMd().format(startDate); final time = DateFormat.Hm().format(startDate); - return ListTile( title: Text('${post.name!}'), subtitle: Text('${post.place!}\n${date} ${time}'), diff --git a/covas_mobile/lib/pages/SearchDelegate.dart b/covas_mobile/lib/pages/SearchDelegate.dart index c534e79..40c8f88 100644 --- a/covas_mobile/lib/pages/SearchDelegate.dart +++ b/covas_mobile/lib/pages/SearchDelegate.dart @@ -75,6 +75,15 @@ class SearchDelegateExample extends SearchDelegate { return Container(); // Implement suggestions if needed } + String formatDate(String date) { + var splitedDate = date.split("-"); + var day = splitedDate[0]; + var month = splitedDate[1]; + var year = splitedDate[2]; + + return "${year}-${month}-${day}"; + } + Future> searchPosts(String query) async { SharedPreferences prefs = await SharedPreferences.getInstance(); var accessToken = prefs.getString("access_token") ?? ""; @@ -82,9 +91,15 @@ class SearchDelegateExample extends SearchDelegate { var longitude = prefs.getDouble("city_long") ?? 0.0; final List body = []; if (accessToken.isNotEmpty) { - DateTime currentDate = DateTime.now(); + String parameter = "current_datetime"; + String currentDate = DateTime.now().toString(); + String date = prefs.getString("date_event") ?? ""; + if (date.isNotEmpty) { + currentDate = formatDate(date) + "T" + TimeOfDay.now().toString(); + parameter = "date_event"; + } var url = Uri.parse( - "${globals.api}/events/search?item=${query}¤t_dateime=${currentDate.toString()}"); + "${globals.api}/events/search?item=${query}&${parameter}=${currentDate}"); if ((latitude != 0.0) && (longitude != 0.0)) { // Now use the latitude and longitude to get events within a 50km radius double radiusInKm = 50; @@ -96,7 +111,7 @@ class SearchDelegateExample extends SearchDelegate { double maxLon = longitude + lonDistance; // Construct the search URL with the item query and latitude/longitude bounds url = Uri.parse( - "${globals.api}/events/search?item=${query}&min_lat=${minLat}&max_lat=${maxLat}&min_lon=${minLon}&max_lon=${maxLon}¤t_dateime=${currentDate.toString()}"); + "${globals.api}/events/search?item=${query}&min_lat=${minLat}&max_lat=${maxLat}&min_lon=${minLon}&max_lon=${maxLon}&${parameter}=${currentDate}"); } final response = await http.get(url, headers: { "Content-Type": "application/json",