Overview
The Location Search feature allows users to find and select any city worldwide to view its air quality data. It includes real-time search with autocomplete, current location detection via GPS, and a curated list of popular cities.Implementation
SearchView Component
The search interface is built as a modal sheet with a navigation stack:SearchView.swift
Features
1. Real-Time Search
The search implements debounced autocomplete to avoid excessive API calls:DashboardViewModel.swift
Debouncing: The search waits 300ms after the user stops typing before making an API request. This reduces server load and provides a smoother user experience.
2. Current Location
The “Use Current Location” button triggers GPS location detection:DashboardViewModel.swift
Location Manager Delegate
The ViewModel conforms toCLLocationManagerDelegate to handle location updates:
DashboardViewModel.swift
3. Popular Cities
When no search is active, a list of popular cities is displayed:SearchView.swift
City Model
Cities are represented with comprehensive location data:City.swift
Display Name Format
ThedisplayName property creates a hierarchical location string:
- With admin1: “Los Angeles, California, USA”
- Without admin1: “Paris, France”
Top Cities List
A curated list of major cities worldwide:City.swift
Geocoding Service
City search is powered by the Open-Meteo Geocoding API viaGeocodingService.swift:
GeocodingService.swift
API Response Format
City.swift
User Experience Features
Auto-Focus
The search field automatically gains focus when the sheet opens:Clear Button
The search field includes a clear button that appears when text is entered:Search Validation
Search only triggers when at least 2 characters are entered:- Empty query: Shows popular cities
- Query < 2 chars: Shows popular cities
- Query >= 2 chars, no results: Shows “No results found”
- Query >= 2 chars, has results: Shows search results list
City Selection Flow
- User taps a city (from search results or popular cities)
viewModel.selectCity(city)is called- Location name is updated
- Search state is cleared
- Modal is dismissed
- Data is fetched for the new location
DashboardViewModel.swift
Location Permissions
The app handles various location permission states:1
Not Determined
Request permission from the user
2
Authorized
Request current location
3
Denied/Restricted
Show error message directing user to Settings
File Locations
- Component:
BreezeApp/Views/Search/SearchView.swift - Model:
BreezeApp/Models/City.swift - Service:
BreezeApp/Services/GeocodingService.swift - ViewModel:
BreezeApp/ViewModels/DashboardViewModel.swift