DashboardViewModel serves as the central state manager for the app.
DashboardViewModel
Main view model implementingObservableObject for SwiftUI data binding.
BreezeApp/ViewModels/DashboardViewModel.swift
Published Properties
Data Properties
AirQuality?
Current air quality data including AQI and pollutant concentrations.
nil when no data is loaded.[PollutantReading]
Array of individual pollutant readings (PM2.5, PM10, NO₂, SO₂, O₃, CO) with status calculations.
[PollenItem]
Pollen data including general types (Grass, Tree, Weed) and specific plants.
[ClimateDataPoint]
Historical temperature data for climate trend visualization.
UI State Properties
String
Display name for current location (e.g., “San Francisco, USA” or “Your Location”)
Bool
Loading state indicator for UI feedback
String?
Error message to display to user if data fetch fails
Search Properties
String
Current search input text
[City]
Array of city search results from geocoding API
Settings Properties
Bool
Temperature unit preference (true = Fahrenheit, false = Celsius)
Computed Properties
aqiStatus
Computes human-readable status from current AQI value.BreezeApp/ViewModels/DashboardViewModel.swift
AQIStatus?
Status object containing text, emoji, description, color, and health tips
aqiColor
Maps AQI status color name to SwiftUI Color.BreezeApp/ViewModels/DashboardViewModel.swift
Location Methods
requestLocation
Requests user’s current location with authorization handling.BreezeApp/ViewModels/DashboardViewModel.swift
This method handles all authorization states and provides user-friendly error messages for denied access.
CLLocationManagerDelegate
Implements location delegate methods for receiving location updates.BreezeApp/ViewModels/DashboardViewModel.swift
Search Methods
searchCities
Searches for cities with debouncing to reduce API calls.BreezeApp/ViewModels/DashboardViewModel.swift
String
required
Search term (minimum 2 characters)
The method implements 300ms debouncing to avoid making API calls on every keystroke, improving performance and reducing server load.
selectCity
Handles city selection from search results.BreezeApp/ViewModels/DashboardViewModel.swift
City
required
Selected city from search results
Data Fetching Methods
fetchAllData
Fetches all environmental data for a location (air quality, pollen, climate).BreezeApp/ViewModels/DashboardViewModel.swift
Double
required
Latitude coordinate
Double
required
Longitude coordinate
Air quality data is fetched first (blocking) since it’s essential for the main UI. Pollen and climate data are fetched in parallel non-blocking tasks to improve perceived performance.
Temperature Formatting Methods
formatTemperature
Formats temperature based on user’s unit preference.BreezeApp/ViewModels/DashboardViewModel.swift
Double
required
Temperature in Celsius
String
Formatted temperature string (e.g., “72.0°F” or “22.2°C”)
formatTemperatureDiff
Formats temperature difference with sign indicator.BreezeApp/ViewModels/DashboardViewModel.swift
Double
required
Temperature difference in Celsius
String
Formatted difference with sign (e.g., “+3.6°F” or “-1.2°C”)
Usage Example
Thread Safety
The@MainActor attribute ensures all view model operations run on the main thread:
- Safe UI updates from published properties
- No data races on shared state
- Automatic thread synchronization