2024-11-05 23:29:55 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'ItemMenu.dart';
|
|
|
|
import '../classes/events.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'package:flutter_dotenv/flutter_dotenv.dart'; // Import dotenv
|
|
|
|
import 'dart:math';
|
|
|
|
|
|
|
|
import '../variable/globals.dart' as globals;
|
|
|
|
|
|
|
|
class SearchDelegateExample extends SearchDelegate {
|
2024-11-23 13:29:25 +01:00
|
|
|
SearchDelegateExample();
|
2024-11-05 23:29:55 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
List<Widget> buildActions(BuildContext context) {
|
|
|
|
return [
|
|
|
|
IconButton(
|
|
|
|
icon: const Icon(Icons.clear),
|
|
|
|
onPressed: () {
|
|
|
|
query = '';
|
|
|
|
},
|
|
|
|
),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget buildLeading(BuildContext context) {
|
|
|
|
return IconButton(
|
|
|
|
icon: const Icon(Icons.arrow_back),
|
|
|
|
onPressed: () {
|
|
|
|
close(context, null);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget buildResults(BuildContext context) {
|
|
|
|
// Perform the search and return the results
|
|
|
|
return FutureBuilder<List<Events>>(
|
2024-11-23 13:29:25 +01:00
|
|
|
future: searchPosts(query),
|
2024-11-05 23:29:55 +01:00
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
|
|
return const Center(child: CircularProgressIndicator());
|
|
|
|
} else if (snapshot.hasData) {
|
|
|
|
final posts = snapshot.data!;
|
|
|
|
return ListView.builder(
|
|
|
|
itemCount: posts.length,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
final post = posts[index];
|
|
|
|
return ListTile(
|
|
|
|
title: Text(post.name!),
|
|
|
|
subtitle: Text(post.place!),
|
|
|
|
onTap: () {
|
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (_) => ItemMenu(title: post.id!),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return const Center(child: Text("No results found"));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget buildSuggestions(BuildContext context) {
|
|
|
|
return Container(); // Implement suggestions if needed
|
|
|
|
}
|
|
|
|
|
2024-11-23 17:24:46 +01:00
|
|
|
String formatDate(String date) {
|
|
|
|
var splitedDate = date.split("-");
|
|
|
|
var day = splitedDate[0];
|
|
|
|
var month = splitedDate[1];
|
|
|
|
var year = splitedDate[2];
|
|
|
|
|
|
|
|
return "${year}-${month}-${day}";
|
|
|
|
}
|
|
|
|
|
2024-11-23 13:29:25 +01:00
|
|
|
Future<List<Events>> searchPosts(String query) async {
|
2024-11-05 23:29:55 +01:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
var accessToken = prefs.getString("access_token") ?? "";
|
2024-11-23 13:29:25 +01:00
|
|
|
var latitude = prefs.getDouble("city_lat") ?? 0.0;
|
|
|
|
var longitude = prefs.getDouble("city_long") ?? 0.0;
|
2024-11-05 23:29:55 +01:00
|
|
|
final List<Events> body = [];
|
|
|
|
if (accessToken.isNotEmpty) {
|
2024-11-23 17:24:46 +01:00
|
|
|
String parameter = "current_datetime";
|
|
|
|
String currentDate = DateTime.now().toString();
|
|
|
|
String date = prefs.getString("date_event") ?? "";
|
2024-11-23 21:52:50 +01:00
|
|
|
String startDate = prefs.getString("start_date") ?? "";
|
|
|
|
String endDate = prefs.getString("end_date") ?? "";
|
2024-11-23 17:24:46 +01:00
|
|
|
if (date.isNotEmpty) {
|
2024-11-23 17:59:52 +01:00
|
|
|
currentDate = DateTime.parse(formatDate(date)).toString();
|
2024-11-23 17:24:46 +01:00
|
|
|
parameter = "date_event";
|
|
|
|
}
|
2024-11-23 21:52:50 +01:00
|
|
|
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}";
|
|
|
|
}
|
2024-11-10 18:18:08 +01:00
|
|
|
var url = Uri.parse(
|
2024-11-23 21:52:50 +01:00
|
|
|
"${globals.api}/events/search?item=${query}&${parameterString}");
|
2024-11-23 13:29:25 +01:00
|
|
|
if ((latitude != 0.0) && (longitude != 0.0)) {
|
|
|
|
// Now use the latitude and longitude to get events within a 50km radius
|
|
|
|
double radiusInKm = 50;
|
|
|
|
double latDistance = radiusInKm / 111.0;
|
|
|
|
double lonDistance = radiusInKm / (111.0 * cos(latitude * pi / 180));
|
|
|
|
double minLat = latitude - latDistance;
|
|
|
|
double maxLat = latitude + latDistance;
|
|
|
|
double minLon = longitude - lonDistance;
|
|
|
|
double maxLon = longitude + lonDistance;
|
|
|
|
// Construct the search URL with the item query and latitude/longitude bounds
|
|
|
|
url = Uri.parse(
|
2024-11-23 21:52:50 +01:00
|
|
|
"${globals.api}/events/search?item=${query}&min_lat=${minLat}&max_lat=${maxLat}&min_lon=${minLon}&max_lon=${maxLon}&${parameterString}");
|
2024-11-05 23:29:55 +01:00
|
|
|
}
|
|
|
|
final response = await http.get(url, headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
HttpHeaders.cookieHeader: "access_token=${accessToken}"
|
|
|
|
});
|
|
|
|
final List body = json.decode(utf8.decode(response.bodyBytes));
|
|
|
|
return body.map((e) => Events.fromJson(e)).toList();
|
|
|
|
}
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
}
|