Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Health] Add support for saving blood pressure as a correlation(ios) #919

Merged
Merged
8 changes: 5 additions & 3 deletions packages/health/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Note that for Android, the target phone **needs** to have [Google Fit](https://w
## Data Types

| **Data Type** | **Unit** | **iOS** | **Android (Google Fit)** | **Android (Health Connect)** | **Comments** |
| --------------------------- | ----------------------- | ------- | ------------------------ |------------------------------| -------------------------------------- |
|-----------------------------|-------------------------| ------- |--------------------------|------------------------------| -------------------------------------- |
| ACTIVE_ENERGY_BURNED | CALORIES | yes | yes | yes | |
| BASAL_ENERGY_BURNED | CALORIES | yes | | yes | |
| BLOOD_GLUCOSE | MILLIGRAM_PER_DECILITER | yes | yes | yes | |
Expand All @@ -33,12 +33,13 @@ Note that for Android, the target phone **needs** to have [Google Fit](https://w
| BODY_FAT_PERCENTAGE | PERCENTAGE | yes | yes | yes | |
| BODY_MASS_INDEX | NO_UNIT | yes | yes | yes | |
| BODY_TEMPERATURE | DEGREE_CELSIUS | yes | yes | yes | |
| BODY_WATER_MASS | KILOGRAMS | | | yes | |
| ELECTRODERMAL_ACTIVITY | SIEMENS | yes | | | |
| HEART_RATE | BEATS_PER_MINUTE | yes | yes | yes | |
| HEIGHT | METERS | yes | yes | yes | |
| RESTING_HEART_RATE | BEATS_PER_MINUTE | yes | | yes | |
| RESPIRATORY_RATE | RESPIRATIONS_PER_MINUTE | yes | | yes |
| PERIPHERAL_PERFUSION_INDEX | PERCENTAGE | yes | | |
| RESPIRATORY_RATE | RESPIRATIONS_PER_MINUTE | yes | | yes |
| PERIPHERAL_PERFUSION_INDEX | PERCENTAGE | yes | | |
| STEPS | COUNT | yes | yes | yes | |
| WAIST_CIRCUMFERENCE | METERS | yes | | | |
| WALKING_HEART_RATE | BEATS_PER_MINUTE | yes | | | |
Expand Down Expand Up @@ -71,6 +72,7 @@ Note that for Android, the target phone **needs** to have [Google Fit](https://w
| AUDIOGRAM | DECIBEL_HEARING_LEVEL | yes | | | |
| ELECTROCARDIOGRAM | VOLT | yes | | | Requires Apple Watch to write the data |
| NUTRITION | NO_UNIT | yes | yes | yes | |
| STAND_TIME | MINUTES | yes | | | |

## Setup

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class HealthPlugin(private var channel: MethodChannel? = null) :
private var ACTIVE_ENERGY_BURNED = "ACTIVE_ENERGY_BURNED"
private var HEART_RATE = "HEART_RATE"
private var BODY_TEMPERATURE = "BODY_TEMPERATURE"
private var BODY_WATER_MASS = "BODY_WATER_MASS"
private var BLOOD_PRESSURE_SYSTOLIC = "BLOOD_PRESSURE_SYSTOLIC"
private var BLOOD_PRESSURE_DIASTOLIC = "BLOOD_PRESSURE_DIASTOLIC"
private var BLOOD_OXYGEN = "BLOOD_OXYGEN"
Expand Down Expand Up @@ -2198,6 +2199,16 @@ class HealthPlugin(private var channel: MethodChannel? = null) :
),
)

is BodyWaterMassRecord -> return listOf(
mapOf<String, Any>(
"value" to record.mass.inKilograms,
"date_from" to record.time.toEpochMilli(),
"date_to" to record.time.toEpochMilli(),
"source_id" to "",
"source_name" to metadata.dataOrigin.packageName,
),
)

is BloodPressureRecord -> return listOf(
mapOf<String, Any>(
"value" to if (dataType == BLOOD_PRESSURE_DIASTOLIC) record.diastolic.inMillimetersOfMercury else record.systolic.inMillimetersOfMercury,
Expand Down Expand Up @@ -2399,6 +2410,12 @@ class HealthPlugin(private var channel: MethodChannel? = null) :
zoneOffset = null,
)

BODY_WATER_MASS -> BodyWaterMassRecord(
time = Instant.ofEpochMilli(startTime),
mass = Mass.kilograms(value),
zoneOffset = null,
)

BLOOD_OXYGEN -> OxygenSaturationRecord(
time = Instant.ofEpochMilli(startTime),
percentage = Percentage(value),
Expand Down Expand Up @@ -2690,6 +2707,7 @@ class HealthPlugin(private var channel: MethodChannel? = null) :
ACTIVE_ENERGY_BURNED to ActiveCaloriesBurnedRecord::class,
HEART_RATE to HeartRateRecord::class,
BODY_TEMPERATURE to BodyTemperatureRecord::class,
BODY_WATER_MASS to BodyWaterMassRecord::class,
BLOOD_PRESSURE_SYSTOLIC to BloodPressureRecord::class,
BLOOD_PRESSURE_DIASTOLIC to BloodPressureRecord::class,
BLOOD_OXYGEN to OxygenSaturationRecord::class,
Expand Down
14 changes: 10 additions & 4 deletions packages/health/ios/Classes/SwiftHealthPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class SwiftHealthPlugin: NSObject, FlutterPlugin {
let HEADACHE_SEVERE = "HEADACHE_SEVERE"
let ELECTROCARDIOGRAM = "ELECTROCARDIOGRAM"
let NUTRITION = "NUTRITION"
let STAND_TIME = "STAND_TIME"

// Health Unit types
// MOLE_UNIT_WITH_MOLAR_MASS, // requires molar mass input - not supported yet
Expand Down Expand Up @@ -426,9 +427,12 @@ public class SwiftHealthPlugin: NSObject, FlutterPlugin {
type: HKSampleType.quantityType(forIdentifier: .bloodPressureDiastolic)!,
quantity: HKQuantity(unit: HKUnit.millimeterOfMercury(), doubleValue: diastolic),
start: dateFrom, end: dateTo)

let bpCorrelationType = HKCorrelationType.correlationType(forIdentifier: .bloodPressure)!
let bpCorrelation = Set(arrayLiteral: systolic_sample, diastolic_sample)
let blood_pressure_sample = HKCorrelation(type: bpCorrelationType , start: dateFrom, end: dateTo, objects: bpCorrelation)

HKHealthStore().save(
[systolic_sample, diastolic_sample],
[blood_pressure_sample],
withCompletion: { (success, error) in
if let err = error {
print("Error Saving Blood Pressure Sample: \(err.localizedDescription)")
Expand Down Expand Up @@ -610,14 +614,16 @@ public class SwiftHealthPlugin: NSObject, FlutterPlugin {

var predicate = HKQuery.predicateForSamples(
withStart: dateFrom, end: dateTo, options: .strictStartDate)
let watchPredicate = HKQuery.predicateForObjects(withDeviceProperty: HKDevicePropertyKeyModel, allowedValues: ["Watch"])
let combinedPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [predicate, watchPredicate])
if (!includeManualEntry) {
let manualPredicate = NSPredicate(format: "metadata.%K != YES", HKMetadataKeyWasUserEntered)
predicate = NSCompoundPredicate(type: .and, subpredicates: [predicate, manualPredicate])
}
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)

let query = HKSampleQuery(
sampleType: dataType, predicate: predicate, limit: limit, sortDescriptors: [sortDescriptor]
sampleType: dataType, predicate: combinedPredicate, limit: limit, sortDescriptors: [sortDescriptor]
) {
[self]
x, samplesOrNil, error in
Expand Down Expand Up @@ -1194,7 +1200,7 @@ public class SwiftHealthPlugin: NSObject, FlutterPlugin {
dataTypesDict[WORKOUT] = HKSampleType.workoutType()
dataTypesDict[NUTRITION] = HKSampleType.correlationType(
forIdentifier: .food)!

dataTypesDict[STAND_TIME] = HKSampleType.quantityType(forIdentifier: .appleStandTime)
healthDataTypes = Array(dataTypesDict.values)
}

Expand Down
6 changes: 6 additions & 0 deletions packages/health/lib/src/data_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ enum HealthDataType {
BODY_FAT_PERCENTAGE,
BODY_MASS_INDEX,
BODY_TEMPERATURE,
BODY_WATER_MASS,
DIETARY_CARBS_CONSUMED,
DIETARY_CAFFEINE,
DIETARY_ENERGY_CONSUMED,
Expand Down Expand Up @@ -55,6 +56,7 @@ enum HealthDataType {
HEADACHE_SEVERE,
HEADACHE_UNSPECIFIED,
NUTRITION,
STAND_TIME,

// Heart Rate events (specific to Apple Watch)
HIGH_HEART_RATE_EVENT,
Expand Down Expand Up @@ -129,6 +131,7 @@ const List<HealthDataType> _dataTypeKeysIOS = [
HealthDataType.HEADACHE_UNSPECIFIED,
HealthDataType.ELECTROCARDIOGRAM,
HealthDataType.NUTRITION,
HealthDataType.STAND_TIME,
];

/// List of data types available on Android
Expand All @@ -141,6 +144,7 @@ const List<HealthDataType> _dataTypeKeysAndroid = [
HealthDataType.BODY_FAT_PERCENTAGE,
HealthDataType.BODY_MASS_INDEX,
HealthDataType.BODY_TEMPERATURE,
HealthDataType.BODY_WATER_MASS,
HealthDataType.HEART_RATE,
HealthDataType.HEIGHT,
HealthDataType.STEPS,
Expand Down Expand Up @@ -177,6 +181,7 @@ const Map<HealthDataType, HealthDataUnit> _dataTypeToUnit = {
HealthDataType.BODY_FAT_PERCENTAGE: HealthDataUnit.PERCENT,
HealthDataType.BODY_MASS_INDEX: HealthDataUnit.NO_UNIT,
HealthDataType.BODY_TEMPERATURE: HealthDataUnit.DEGREE_CELSIUS,
HealthDataType.BODY_WATER_MASS: HealthDataUnit.KILOGRAM,
HealthDataType.DIETARY_CARBS_CONSUMED: HealthDataUnit.GRAM,
HealthDataType.DIETARY_CAFFEINE: HealthDataUnit.GRAM,
HealthDataType.DIETARY_ENERGY_CONSUMED: HealthDataUnit.KILOCALORIE,
Expand Down Expand Up @@ -231,6 +236,7 @@ const Map<HealthDataType, HealthDataUnit> _dataTypeToUnit = {
HealthDataType.ELECTROCARDIOGRAM: HealthDataUnit.VOLT,

HealthDataType.NUTRITION: HealthDataUnit.NO_UNIT,
HealthDataType.STAND_TIME: HealthDataUnit.MINUTE,

// Health Connect
HealthDataType.TOTAL_CALORIES_BURNED: HealthDataUnit.KILOCALORIE,
Expand Down