From bae64ebd359ca07f643e56adcee0b5b56c79ec15 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 23 Nov 2024 21:52:50 +0100 Subject: [PATCH] add start_date and end_date --- covas_mobile/lib/pages/ListItemMenu.dart | 102 +++++++++++++++++---- covas_mobile/lib/pages/SearchDelegate.dart | 12 ++- 2 files changed, 92 insertions(+), 22 deletions(-) diff --git a/covas_mobile/lib/pages/ListItemMenu.dart b/covas_mobile/lib/pages/ListItemMenu.dart index 3afaecb..e9fee28 100644 --- a/covas_mobile/lib/pages/ListItemMenu.dart +++ b/covas_mobile/lib/pages/ListItemMenu.dart @@ -46,7 +46,8 @@ class _MyHomePageState extends State { String query = ''; List> suggestions = []; TextEditingController inputGeo = TextEditingController(); - TextEditingController Datepicker = TextEditingController(); + TextEditingController startDatepicker = TextEditingController(); + TextEditingController endDatepicker = TextEditingController(); // Fetching events from API static Future> getPosts() async { @@ -130,10 +131,21 @@ class _MyHomePageState extends State { DateTime currentDateTime = DateTime.now(); var url = Uri.parse( "${globals.api}/events?current_datetime=${currentDateTime.toString()}"); - if (Datepicker.text.isNotEmpty) { - var date = DateTime.parse(formatDate(Datepicker.text)); + if (startDatepicker.text.isNotEmpty) { + var date = DateTime.parse(formatDate(startDatepicker.text)); url = Uri.parse("${globals.api}/events?date_event=${date.toString()}"); } + if (endDatepicker.text.isNotEmpty) { + var date = DateTime.parse(formatDate(endDatepicker.text)); + url = Uri.parse("${globals.api}/events?date_event=${date.toString()}"); + } + if ((startDatepicker.text.isNotEmpty) && + (endDatepicker.text.isNotEmpty)) { + var start_date = DateTime.parse(formatDate(startDatepicker.text)); + var end_date = DateTime.parse(formatDate(endDatepicker.text)); + url = Uri.parse( + "${globals.api}/events?start_date=${start_date.toString()}&end_date=${end_date.toString()}"); + } final response = await http.get(url, headers: { "Content-Type": "application/json", HttpHeaders.cookieHeader: "access_token=${accessToken}" @@ -320,15 +332,31 @@ class _MyHomePageState extends State { var url = Uri.parse("${globals.api}/events/search" "?min_lat=$minLat&max_lat=$maxLat" "&min_lon=$minLon&max_lon=$maxLon¤t_datetime=${currentDate.toString()}"); - if (Datepicker.text.isNotEmpty) { - var date = DateTime.parse(formatDate(Datepicker.text)); - print("date_event ${date.toString()}"); + if (startDatepicker.text.isNotEmpty) { + var date = DateTime.parse(formatDate(startDatepicker.text)); url = Uri.parse("${globals.api}/events/search" "?min_lat=$minLat&max_lat=$maxLat" "&min_lon=$minLon&max_lon=$maxLon&date_event=" + date.toString()); } - + if (endDatepicker.text.isNotEmpty) { + var date = DateTime.parse(formatDate(endDatepicker.text)); + url = Uri.parse("${globals.api}/events/search" + "?min_lat=$minLat&max_lat=$maxLat" + "&min_lon=$minLon&max_lon=$maxLon&date_event=" + + date.toString()); + } + if ((startDatepicker.text.isNotEmpty) && + (endDatepicker.text.isNotEmpty)) { + var start_date = DateTime.parse(formatDate(startDatepicker.text)); + var end_date = DateTime.parse(formatDate(endDatepicker.text)); + url = Uri.parse("${globals.api}/events/search" + "?min_lat=$minLat&max_lat=$maxLat" + "&min_lon=$minLon&max_lon=$maxLon&start_date=" + + start_date.toString() + + "&end_date=" + + end_date.toString()); + } final response = await http.get(url, headers: { "Content-Type": "application/json", HttpHeaders.cookieHeader: "access_token=$accessToken" @@ -355,15 +383,33 @@ class _MyHomePageState extends State { } } - onTapFunctionDatePicker({required BuildContext context}) async { + onTapFunctionDatePicker( + {required BuildContext context, String position = ""}) async { DateTime dateEvent = DateTime.now(); + if (startDatepicker.text.isNotEmpty) { + dateEvent = DateTime.parse(formatDate(startDatepicker.text)); + } DateTime? pickedDate = await showDatePicker( context: context, firstDate: dateEvent, lastDate: DateTime(2104)); if (pickedDate == null) return; - Datepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate); + if (position == "start") { + startDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate); + } else if (position == "end") { + endDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate); + } SharedPreferences prefs = await SharedPreferences.getInstance(); - prefs.setString("date_event", Datepicker.text); + if (startDatepicker.text.isNotEmpty) { + prefs.setString("date_event", startDatepicker.text); + prefs.setString("start_date", startDatepicker.text); + } + if (endDatepicker.text.isNotEmpty) { + prefs.setString("date_event", endDatepicker.text); + prefs.setString("end_date", endDatepicker.text); + } + if ((startDatepicker.text.isNotEmpty) && (endDatepicker.text.isNotEmpty)) { + prefs.remove("date_event"); + } if (inputGeo.text.isEmpty) { _fetchInitialData(); } else { @@ -371,25 +417,39 @@ class _MyHomePageState extends State { } } - Padding _BuildDateField() { + Padding _buildDateField(String position) { + TextEditingController datePicker = startDatepicker; + String hintText = "Date de début"; + if (position == "end") { + datePicker = endDatepicker; + hintText = "Date de fin"; + } return Padding( padding: const EdgeInsets.all(8.0), //padding: EdgeInsets.symmetric(horizontal: 15), child: TextFormField( - controller: Datepicker, + controller: datePicker, readOnly: true, decoration: InputDecoration( border: OutlineInputBorder(), suffixIcon: IconButton( icon: const Icon(Icons.clear), onPressed: () async { + setState(() { + datePicker.text = ''; + }); SharedPreferences prefs = await SharedPreferences.getInstance(); - - prefs.remove("date_event"); - setState(() { - Datepicker.text = ''; - }); + if (position == "start") { + prefs.remove("start_date"); + } + if (position == "end") { + prefs.remove("end_date"); + } + if ((startDatepicker.text.isEmpty) && + (endDatepicker.text.isEmpty)) { + prefs.remove("date_event"); + } if (inputGeo.text.isNotEmpty) { fetchPostsByLocation(); } else { @@ -397,8 +457,9 @@ class _MyHomePageState extends State { } }, ), - hintText: 'Recherche par date'), - onTap: () => onTapFunctionDatePicker(context: context)), + hintText: hintText), + onTap: () => + onTapFunctionDatePicker(context: context, position: position)), ); } @@ -501,7 +562,8 @@ class _MyHomePageState extends State { body: Column( children: [ _buildGeographicalZoneSearchField(), - _BuildDateField(), + _buildDateField("start"), + _buildDateField("end"), Expanded( child: FutureBuilder>( future: postsFuture, diff --git a/covas_mobile/lib/pages/SearchDelegate.dart b/covas_mobile/lib/pages/SearchDelegate.dart index b996837..c7c2fcb 100644 --- a/covas_mobile/lib/pages/SearchDelegate.dart +++ b/covas_mobile/lib/pages/SearchDelegate.dart @@ -94,12 +94,20 @@ class SearchDelegateExample extends SearchDelegate { String parameter = "current_datetime"; String currentDate = DateTime.now().toString(); String date = prefs.getString("date_event") ?? ""; + String startDate = prefs.getString("start_date") ?? ""; + String endDate = prefs.getString("end_date") ?? ""; if (date.isNotEmpty) { currentDate = DateTime.parse(formatDate(date)).toString(); parameter = "date_event"; } + String parameterString = "${parameter}=${currentDate}"; + if ((startDate.isNotEmpty) && (endDate.isNotEmpty)) { + startDate = DateTime.parse(formatDate(startDate)).toString(); + endDate = DateTime.parse(formatDate(endDate)).toString(); + parameterString = "start_date=${startDate}&end_date=${endDate}"; + } var url = Uri.parse( - "${globals.api}/events/search?item=${query}&${parameter}=${currentDate}"); + "${globals.api}/events/search?item=${query}&${parameterString}"); if ((latitude != 0.0) && (longitude != 0.0)) { // Now use the latitude and longitude to get events within a 50km radius double radiusInKm = 50; @@ -111,7 +119,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}&${parameter}=${currentDate}"); + "${globals.api}/events/search?item=${query}&min_lat=${minLat}&max_lat=${maxLat}&min_lon=${minLon}&max_lon=${maxLon}&${parameterString}"); } final response = await http.get(url, headers: { "Content-Type": "application/json",