Skip to content

Commit

Permalink
Add air quality index
Browse files Browse the repository at this point in the history
  • Loading branch information
inderdhir committed Jul 2, 2024
1 parent 1c4a3eb commit 17eb6a8
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 4 deletions.
4 changes: 4 additions & 0 deletions DatWeatherDoe.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
20D8571F2A832AC6005727BB /* TemperatureUnit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20D8571E2A832AC6005727BB /* TemperatureUnit.swift */; };
20E8A1A62C2B3C5A007E8733 /* Localizable.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = 20E8A1A52C2B3C5A007E8733 /* Localizable.xcstrings */; };
20E8A1AB2C2B3FE6007E8733 /* TestData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20E8A1AA2C2B3FE6007E8733 /* TestData.swift */; };
20F0E5F82C33500900434C3A /* AirQuality.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20F0E5F72C33500900434C3A /* AirQuality.swift */; };
20F17D3A26597A02003A164E /* WeatherData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20F17D3926597A02003A164E /* WeatherData.swift */; };
E3105BA62818805A00FB4C55 /* SunriseAndSunsetTextBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3105BA52818805A00FB4C55 /* SunriseAndSunsetTextBuilder.swift */; };
E3BE2C91292C505A00C4F468 /* DropdownIcon.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3BE2C90292C505A00C4F468 /* DropdownIcon.swift */; };
Expand Down Expand Up @@ -156,6 +157,7 @@
20D8571E2A832AC6005727BB /* TemperatureUnit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TemperatureUnit.swift; sourceTree = "<group>"; };
20E8A1A52C2B3C5A007E8733 /* Localizable.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = Localizable.xcstrings; sourceTree = "<group>"; };
20E8A1AA2C2B3FE6007E8733 /* TestData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestData.swift; sourceTree = "<group>"; };
20F0E5F72C33500900434C3A /* AirQuality.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AirQuality.swift; sourceTree = "<group>"; };
20F17D3926597A02003A164E /* WeatherData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeatherData.swift; sourceTree = "<group>"; };
20FF84E92456326400FC9DAA /* DatWeatherDoe.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = DatWeatherDoe.entitlements; sourceTree = "<group>"; };
61D04A2F1C7B7BCF00CBE6AE /* DatWeatherDoeTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = DatWeatherDoeTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -377,6 +379,7 @@
2039B3E62C2896BD006A6B6D /* WindData.swift */,
2039B3E82C2896CF006A6B6D /* ForecastData.swift */,
2039B3EA2C2896D7006A6B6D /* ForecastTemperatureData.swift */,
20F0E5F72C33500900434C3A /* AirQuality.swift */,
);
path = Response;
sourceTree = "<group>";
Expand Down Expand Up @@ -662,6 +665,7 @@
209482C629934BFF00AF39D4 /* MeasurementUnit.swift in Sources */,
2005C057278CB5640067BBD1 /* LocationParser.swift in Sources */,
2005C05D278CE0350067BBD1 /* WeatherAPIResponseParser.swift in Sources */,
20F0E5F82C33500900434C3A /* AirQuality.swift in Sources */,
2000D4082AD86DAA0052EDA6 /* WindSpeedFormatter.swift in Sources */,
2039B3FF2C28D1D3006A6B6D /* NonInteractiveMenuOptionView.swift in Sources */,
20459C641C5C50DA004D0DC1 /* ConfigManager.swift in Sources */,
Expand Down
54 changes: 54 additions & 0 deletions DatWeatherDoe/API/Response/AirQuality.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// AirQualityIndex.swift
// DatWeatherDoe
//
// Created by Inder Dhir on 7/1/24.
// Copyright © 2024 Inder Dhir. All rights reserved.
//

import Foundation

// US - EPA standard
enum AirQualityIndex: Int, Decodable {
case good = 1
case moderate = 2
case unhealthyForSensitive = 3
case unhealthy = 4
case veryUnhealthy = 5
case hazardous = 6

var description: String {
switch self {
case .good:
return NSLocalizedString(
"Good",
comment: "Air quality index: Good"
)
case .moderate:
return NSLocalizedString(
"Moderate",
comment: "Air quality index: Moderate"
)
case .unhealthyForSensitive:
return NSLocalizedString(
"Unhealthy for sensitive groups",
comment: "Air quality index: Unhealthy for sensitive groups"
)
case .unhealthy:
return NSLocalizedString(
"Unhealthy",
comment: "Air quality index: Unhealthy"
)
case .veryUnhealthy:
return NSLocalizedString(
"Very unhealthy",
comment: "Air quality index: Very unhealthy"
)
case .hazardous:
return NSLocalizedString(
"Hazardous",
comment: "Air quality index: Hazardous"
)
}
}
}
13 changes: 12 additions & 1 deletion DatWeatherDoe/API/Response/WeatherAPIResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct WeatherAPIResponse: Decodable {
let humidity: Int
let windData: WindData
let forecastDayData: ForecastDayData
let airQualityIndex: AirQualityIndex

private enum RootKeys: String, CodingKey {
case location, current, forecast
Expand All @@ -28,6 +29,7 @@ struct WeatherAPIResponse: Decodable {
private enum CurrentKeys: String, CodingKey {
case isDay = "is_day"
case condition, humidity
case airQuality = "air_quality"
}

private enum WeatherConditionKeys: String, CodingKey {
Expand All @@ -42,6 +44,10 @@ struct WeatherAPIResponse: Decodable {
case day, astro
}

private enum AirQualityKeys: String, CodingKey {
case usEpaIndex = "us-epa-index"
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: RootKeys.self)

Expand Down Expand Up @@ -73,6 +79,9 @@ struct WeatherAPIResponse: Decodable {
debugDescription: "Missing forecast day data"
)
}

let airQualityContainer = try currentContainer.nestedContainer(keyedBy: AirQualityKeys.self, forKey: .airQuality)
airQualityIndex = try airQualityContainer.decode(AirQualityIndex.self, forKey: .usEpaIndex)
}

init(
Expand All @@ -82,7 +91,8 @@ struct WeatherAPIResponse: Decodable {
weatherConditionCode: Int,
humidity: Int,
windData: WindData,
forecastDayData: ForecastDayData
forecastDayData: ForecastDayData,
airQualityIndex: AirQualityIndex
) {
self.locationName = locationName
self.temperatureData = temperatureData
Expand All @@ -91,5 +101,6 @@ struct WeatherAPIResponse: Decodable {
self.humidity = humidity
self.windData = windData
self.forecastDayData = forecastDayData
self.airQualityIndex = airQualityIndex
}
}
3 changes: 2 additions & 1 deletion DatWeatherDoe/Resources/DevelopmentAssets/TestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ let response = WeatherAPIResponse(
maxTempC: 32.8, maxTempF: 91.0, minTempC: 20.6, minTempF: 69.2
),
astro: .init(sunrise: "05:26 AM", sunset: "08:31 PM")
)
),
airQualityIndex: .good
)
27 changes: 27 additions & 0 deletions DatWeatherDoe/Resources/Localization/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,14 @@
}
}
},
"Good" : {
"comment" : "Air quality index: Good",
"extractionState" : "manual"
},
"Hazardous" : {
"comment" : "Air quality index: Hazardous",
"extractionState" : "manual"
},
"Heavy rain" : {
"comment" : "Heavy rain weather condition",
"localizations" : {
Expand Down Expand Up @@ -642,6 +650,9 @@
}
}
},
"Key" : {
"extractionState" : "manual"
},
"Lat/Long" : {
"comment" : "Weather based on Lat/Long",
"localizations" : {
Expand Down Expand Up @@ -810,6 +821,10 @@
}
}
},
"Moderate" : {
"comment" : "Air quality index: Moderate",
"extractionState" : "manual"
},
"NSHumanReadableCopyright" : {
"comment" : "Copyright (human-readable)",
"extractionState" : "manual",
Expand Down Expand Up @@ -1264,6 +1279,14 @@
}
}
},
"Unhealthy" : {
"comment" : "Air quality index: Unhealthy",
"extractionState" : "manual"
},
"Unhealthy for sensitive groups" : {
"comment" : "Air quality index: Unhealthy for sensitive groups",
"extractionState" : "manual"
},
"Unit" : {
"comment" : "Temperature unit",
"localizations" : {
Expand Down Expand Up @@ -1383,6 +1406,10 @@
}
}
},
"Very unhealthy" : {
"comment" : "Air quality index: Very unhealthy",
"extractionState" : "manual"
},
"Weather Condition Position" : {
"localizations" : {
"zh-Hans" : {
Expand Down
2 changes: 1 addition & 1 deletion DatWeatherDoe/ViewModel/Repository/WeatherURLBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class WeatherURLBuilder: WeatherURLBuilderType {

let queryItems: [URLQueryItem] = [
URLQueryItem(name: "key", value: appId),
URLQueryItem(name: "aqi", value: String("no")),
URLQueryItem(name: "aqi", value: String("yes")),
URLQueryItem(name: "q", value: latLonString),
URLQueryItem(name: "dt", value: parsedDateToday)
]
Expand Down
6 changes: 5 additions & 1 deletion DatWeatherDoe/ViewModel/WeatherDataFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class WeatherDataFormatter: WeatherDataFormatterType {
}

func getWindSpeedItem(for data: WeatherData) -> String {
if configManager.measurementUnit == MeasurementUnit.all.rawValue {
let windData = if configManager.measurementUnit == MeasurementUnit.all.rawValue {
WindSpeedFormatter()
.getFormattedWindSpeedStringForAllUnits(
windData: data.response.windData,
Expand All @@ -66,5 +66,9 @@ final class WeatherDataFormatter: WeatherDataFormatterType {
windData: data.response.windData
)
}

let airQuality = "AQI: \(data.response.airQualityIndex.description)"

return "\(windData) | \(airQuality)"
}
}

0 comments on commit 17eb6a8

Please sign in to comment.