fix: LOC — location filter never applied to event API calls

Root cause: SearchScreen popped with a plain city label string; the
pincode was available in search results but discarded. home_screen only
saved the display label to prefs and never updated the 'pincode' key,
so every API call always sent {pincode:'all'} regardless of selection.

GPS path had the same issue — lat/lng were obtained but thrown away
after reverse-geocoding; only the label was passed back.

Fix:
- SearchScreen now pops with Map<String,dynamic> {label, pincode,
  lat?, lng?} instead of a plain String
- Pincode results return their pincode; GPS returns actual coordinates;
  popular city chips look up the first matching pincode from the
  Kerala pincodes DB (fallback 'all' if not found)
- home_screen._openLocationSearch() saves pincode + lat/lng to prefs
  and updates _pincode/_userLat/_userLng in state
- home_screen._loadUserDataAndEvents() prefers getEventsByLocation
  (haversine) when GPS coords are saved, falls back to getEventsByPincode
- EventsService gains getEventsByLocation(lat, lng) which sends
  latitude/longitude/radius_km to the existing Django haversine endpoint
  and auto-expands radius 10→25→50→100 km until ≥ 6 events found
This commit is contained in:
2026-04-04 18:43:02 +05:30
parent bb06bd8ac6
commit a32ead31c2
3 changed files with 101 additions and 15 deletions

View File

@@ -108,6 +108,33 @@ class EventsService {
return [];
}
/// Get events by GPS coordinates using haversine distance filtering.
/// Automatically expands radius (10 → 25 → 50 → 100 km) until ≥ 6 events found.
Future<List<EventModel>> getEventsByLocation(double lat, double lng, {double initialRadiusKm = 10}) async {
const radii = [10.0, 25.0, 50.0, 100.0];
for (final radius in radii) {
if (radius < initialRadiusKm) continue;
final body = {
'latitude': lat,
'longitude': lng,
'radius_km': radius,
'page': 1,
'page_size': 50,
'per_type': 5,
};
final res = await _api.post(ApiEndpoints.eventsByPincode, body: body, requiresAuth: false);
final list = <EventModel>[];
final events = res['events'] ?? res['data'] ?? [];
if (events is List) {
for (final e in events) {
if (e is Map<String, dynamic>) list.add(EventModel.fromJson(Map<String, dynamic>.from(e)));
}
}
if (list.length >= 6 || radius >= 100) return list;
}
return [];
}
/// Events by month and year for calendar (POST to /events/events-by-month-year/)
Future<Map<String, dynamic>> getEventsByMonthYear(String month, int year) async {
final res = await _api.post(ApiEndpoints.eventsByMonth, body: {'month': month, 'year': year}, requiresAuth: false);