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

@@ -37,6 +37,8 @@ class _HomeScreenState extends State<HomeScreen> with SingleTickerProviderStateM
String _username = '';
String _location = '';
String _pincode = 'all';
double? _userLat;
double? _userLng;
final EventsService _eventsService = EventsService();
@@ -104,12 +106,17 @@ class _HomeScreenState extends State<HomeScreen> with SingleTickerProviderStateM
_location = storedLocation;
}
_pincode = prefs.getString('pincode') ?? 'all';
_userLat = prefs.getDouble('user_lat');
_userLng = prefs.getDouble('user_lng');
try {
// Fetch types and events in parallel for faster loading
// Fetch types and events in parallel for faster loading.
// Prefer haversine (lat/lng) when GPS coords are available; fall back to pincode.
final results = await Future.wait([
_events_service_getEventTypesSafe(),
_events_service_getEventsSafe(_pincode),
(_userLat != null && _userLng != null)
? _events_service_getEventsByLocationSafe(_userLat!, _userLng!)
: _events_service_getEventsSafe(_pincode),
]);
final types = results[0] as List<EventTypeModel>;
final events = results[1] as List<EventModel>;
@@ -171,6 +178,15 @@ class _HomeScreenState extends State<HomeScreen> with SingleTickerProviderStateM
}
}
Future<List<EventModel>> _events_service_getEventsByLocationSafe(double lat, double lng) async {
try {
return await _eventsService.getEventsByLocation(lat, lng);
} catch (_) {
// Fallback to all events if location-based fetch fails
return _events_service_getEventsSafe('all');
}
}
Future<void> _refresh() async {
await _loadUserDataAndEvents();
}
@@ -277,12 +293,31 @@ class _HomeScreenState extends State<HomeScreen> with SingleTickerProviderStateM
transitionDuration: const Duration(milliseconds: 220),
));
if (selected != null && selected is String) {
if (selected != null && selected is Map) {
final label = (selected['label'] as String?) ?? 'Current Location';
final pincode = (selected['pincode'] as String?) ?? 'all';
final lat = selected['lat'] as double?;
final lng = selected['lng'] as double?;
final prefs = await SharedPreferences.getInstance();
await prefs.setString('location', selected);
setState(() {
_location = selected;
});
await prefs.setString('location', label);
await prefs.setString('pincode', pincode);
if (lat != null && lng != null) {
await prefs.setDouble('user_lat', lat);
await prefs.setDouble('user_lng', lng);
} else {
await prefs.remove('user_lat');
await prefs.remove('user_lng');
}
if (mounted) {
setState(() {
_location = label;
_pincode = pincode;
_userLat = lat;
_userLng = lng;
});
}
await _refresh();
}
}