Skip to content

Commit

Permalink
Expose known correlation associations
Browse files Browse the repository at this point in the history
  • Loading branch information
christophhagen committed Mar 12, 2024
1 parent e86c751 commit a04812e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Sources/HealthKitExtensions/Correlations/BloodPressure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,15 @@ extension BloodPressure {
public var foodType: String? {
metadata?[.foodType]
}

/// The associated systolic blood pressure sample
public var systolic: BloodPressureSystolic? {
first()
}

/// The associated diastolic blood pressure sample
public var diastolic: BloodPressureDiastolic? {
first()
}
}

5 changes: 5 additions & 0 deletions Sources/HealthKitExtensions/Correlations/Food.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,10 @@ extension Food {
public var foodType: String? {
metadata?[.foodType]
}

/// The associated sample of consumed dietary energy
public var dietaryEnergyConsumed: DietaryEnergyConsumed? {
first()
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ extension HKCorrelationContainer {
public func samples<T>(of type: T.Type = T.self) -> [T] where T: HKQuantitySampleContainer {
correlation.objects(for: T.quantitySampleType).map(T.init)
}

func first<T>() -> T? where T: HKQuantitySampleContainer {
objects.first { ($0 as? HKQuantitySample)?.quantityType == T.quantitySampleType }
.map { T.init(sample: $0) }
}
}

0 comments on commit a04812e

Please sign in to comment.