Compare commits

..

No commits in common. "a7d8bebe1ffaadacd7c857924d03a043e7df145c" and "4d3533eb8ac0b2d6d700a735afdc5764e9203c20" have entirely different histories.

2 changed files with 45 additions and 77 deletions

View File

@ -1,4 +1,3 @@
import 'package:covas_mobile/classes/alert.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:flutter_dotenv/flutter_dotenv.dart'; // Import dotenv import 'package:flutter_dotenv/flutter_dotenv.dart'; // Import dotenv
@ -51,63 +50,46 @@ class _MyHomePageState extends State<ListItemMenu> {
// Fetching events from API // Fetching events from API
static Future<List<Events>> getPosts() async { static Future<List<Events>> getPosts() async {
PermissionStatus status = await Permission.location.status; PermissionStatus status = await Permission.location.status;
final List<Events> body = [];
var url = Uri.parse("${globals.api}/events"); var url = Uri.parse("${globals.api}/events");
if (status.isGranted) { if (status.isGranted) {
print("Location permission granted"); print("Location permission granted");
// Get the current position with high accuracy // Get the current position with high accuracy
LocationSettings locationSettings = LocationSettings(
const LocationSettings locationSettings = LocationSettings( accuracy: LocationAccuracy.high,
accuracy: LocationAccuracy.medium, distanceFilter:
timeLimit: Duration(seconds: 5), 10, // Optional: Minimum distance (in meters) to trigger location update
); );
Position? position;
try {
position = await Geolocator.getCurrentPosition(
locationSettings: locationSettings);
} on LocationServiceDisabledException {
// Handle location services disabled
print('Location services are disabled.');
position = await Geolocator.getLastKnownPosition();
if (position == null) {
print('No last known position available.');
}
} catch (e) {
// Handle other errors
print('Failed to get location: $e');
position = await Geolocator.getLastKnownPosition();
if (position == null) {
print('No last known position available.');
}
}
if (position != null) {
// Calculate the boundaries
double radiusInKm = 50;
double latDistance = radiusInKm / 111.0;
double lonDistance =
radiusInKm / (111.0 * cos(position.latitude * pi / 180));
double minLat = position.latitude - latDistance; Position position = await Geolocator.getCurrentPosition(
double maxLat = position.latitude + latDistance; locationSettings: locationSettings,
double minLon = position.longitude - lonDistance; );
double maxLon = position.longitude + lonDistance; // Calculate the boundaries
DateTime currentDatetime = DateTime.now(); double radiusInKm = 50;
url = Uri.parse("${globals.api}/events/search" double latDistance = radiusInKm / 111.0;
"?min_lat=$minLat&max_lat=$maxLat" double lonDistance =
"&min_lon=$minLon&max_lon=$maxLon&current_datetime=${currentDatetime.toString()}"); radiusInKm / (111.0 * cos(position.latitude * pi / 180));
}
SharedPreferences prefs = await SharedPreferences.getInstance();
var accessToken = prefs.getString("access_token") ?? "";
if (accessToken.isNotEmpty) { double minLat = position.latitude - latDistance;
final response = await http.get(url, headers: { double maxLat = position.latitude + latDistance;
"Content-Type": "application/json", double minLon = position.longitude - lonDistance;
HttpHeaders.cookieHeader: "access_token=${accessToken}" double maxLon = position.longitude + lonDistance;
}); DateTime currentDatetime = DateTime.now();
final List body = json.decode(utf8.decode(response.bodyBytes)); url = Uri.parse("${globals.api}/events/search"
return body.map((e) => Events.fromJson(e)).toList(); "?min_lat=$minLat&max_lat=$maxLat"
} "&min_lon=$minLon&max_lon=$maxLon&current_datetime=${currentDatetime.toString()}");
}
SharedPreferences prefs = await SharedPreferences.getInstance();
var accessToken = prefs.getString("access_token") ?? "";
final List<Events> body = [];
if (accessToken.isNotEmpty) {
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; return body;
} }

View File

@ -141,33 +141,19 @@ class _MapboxPagesState extends State<MapboxPages> with ShowErrorDialog {
"Location permissions are permanently denied. Enable them in settings."); "Location permissions are permanently denied. Enable them in settings.");
return; return;
} }
const LocationSettings locationSettings = LocationSettings( LocationSettings locationSettings = LocationSettings(
accuracy: LocationAccuracy.medium, timeLimit: Duration(seconds: 5)); accuracy: LocationAccuracy.high,
Position? position; distanceFilter:
try { 10, // Optional: Minimum distance (in meters) to trigger location update
position = await Geolocator.getCurrentPosition( );
locationSettings: locationSettings);
} on LocationServiceDisabledException { Position position = await Geolocator.getCurrentPosition(
// Handle location services disabled locationSettings: locationSettings,
print('Location services are disabled.'); );
position = await Geolocator.getLastKnownPosition(); setState(() {
if (position == null) { userPosition = LatLng(position.latitude, position.longitude);
print('No last known position available.'); isUserPositionInitialized = true;
} });
} catch (e) {
// Handle other errors
print('Failed to get location: $e');
position = await Geolocator.getLastKnownPosition();
if (position == null) {
print('No last known position available.');
}
}
if (position != null) {
setState(() {
userPosition = LatLng(position!.latitude, position!.longitude);
isUserPositionInitialized = true;
});
}
_initToken(); _initToken();
_getEventInfo(); _getEventInfo();
} catch (e) { } catch (e) {