Skip to content

Instantly share code, notes, and snippets.

@flexih
Last active February 25, 2023 23:14
Show Gist options
  • Save flexih/dd0ab9d22a0707ab9360b217efdc1a05 to your computer and use it in GitHub Desktop.
Save flexih/dd0ab9d22a0707ab9360b217efdc1a05 to your computer and use it in GitHub Desktop.
Add weather conditions from WeatherKit to workout, displayed in Health App
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