Last active
February 25, 2023 23:14
-
-
Save flexih/dd0ab9d22a0707ab9360b217efdc1a05 to your computer and use it in GitHub Desktop.
Add weather conditions from WeatherKit to workout, displayed in Health App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension WeatherCondition { | |
var hkWeatherCondition: HKWeatherCondition { | |
switch (self) { | |
case .clear: | |
return .clear | |
case .partlyCloudy: | |
return .partlyCloudy | |
case .mostlyCloudy: | |
return .mostlyCloudy | |
case .cloudy: | |
return .cloudy | |
case .foggy: | |
return .foggy | |
case .haze: | |
return .haze | |
case .windy: | |
return .windy | |
case .smoky: | |
return .smoky | |
case .blowingDust: | |
return .dust | |
case .snow: | |
return .snow | |
case .hail: | |
return .hail | |
case .sleet: | |
return .sleet | |
case .freezingDrizzle: | |
return .freezingDrizzle | |
case .freezingRain: | |
return .freezingRain | |
case .drizzle: | |
return .drizzle | |
case .sunShowers: | |
return .showers | |
case .thunderstorms: | |
return .thunderstorms | |
case .tropicalStorm: | |
return .tropicalStorm | |
case .hurricane: | |
return .hurricane | |
default: | |
return .none | |
} | |
} | |
} | |
///do this before builder?.finishWorkout() | |
func addWeatherConditionsMetadata(current: CurrentWeather) { | |
func weatherConditionsMetadata(current: CurrentWeather) -> [String: Any]? { | |
var metadata: [String: Any]? | |
metadata = [ | |
HKMetadataKeyWeatherTemperature: HKQuantity(unit: HKUnit.degreeCelsius(), doubleValue: current.temperature.value), | |
HKMetadataKeyWeatherHumidity: HKQuantity(unit: HKUnit.percent(), doubleValue: current.humidity * 100), // this is wrong, but it works | |
HKMetadataKeyWeatherCondition: current.condition.hkWeatherCondition.rawValue] | |
return metadata | |
} | |
if let metadata = weatherConditionsMetadata() { | |
builder?.addMetadata(metadata) { success, error in | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment