diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Vitals.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Vitals.h index 985d3a451a91f3aa308b8dacb55b9e89cf06b683..885f7bed94a394909fac63c43a145cfa9b156a07 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Vitals.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Vitals.h @@ -6,9 +6,7 @@ - (void)vitals_getBodyTemperatureSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; -- (void)vitals_getBloodPressureSystolicSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - -- (void)vitals_getBloodPressureDiastolicSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)vitals_getBloodPressureSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)vitals_getRespiratoryRateSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Vitals.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Vitals.m index 45b091b9f1b1b5c6388b5dbaf7cd4bfb0ad4021a..3016d2737d19f4a7606c6cb3f199bd088941f471 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Vitals.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Vitals.m @@ -72,4 +72,60 @@ }]; } + +- (void)vitals_getBloodPressureSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + HKCorrelationType *bloodPressureCorrelationType = [HKCorrelationType correlationTypeForIdentifier:HKCorrelationTypeIdentifierBloodPressure]; + HKQuantityType *systolicType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodPressureSystolic]; + HKQuantityType *diastolicType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodPressureDiastolic]; + + + HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit millimeterOfMercuryUnit]]; + NSUInteger limit = [RCTAppleHealthKit uintFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit]; + BOOL ascending = [RCTAppleHealthKit boolFromOptions:input key:@"ascending" withDefault:false]; + NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil]; + NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]]; + if(startDate == nil){ + callback(@[RCTMakeError(@"startDate is required in options", nil, nil)]); + return; + } + NSPredicate * predicate = [RCTAppleHealthKit predicateForSamplesBetweenDates:startDate endDate:endDate]; + + [self fetchCorrelationSamplesOfType:bloodPressureCorrelationType + unit:unit + predicate:predicate + ascending:ascending + limit:limit + completion:^(NSArray *results, NSError *error) { + if(results){ + NSMutableArray *data = [NSMutableArray arrayWithCapacity:1]; + + for (NSDictionary *sample in results) { + HKCorrelation *bloodPressureValues = [sample valueForKey:@"correlation"]; + + HKQuantitySample *bloodPressureSystolicValue = [bloodPressureValues objectsForType:systolicType].anyObject; + HKQuantitySample *bloodPressureDiastolicValue = [bloodPressureValues objectsForType:diastolicType].anyObject; + + NSDictionary *elem = @{ + @"bloodPressureSystolicValue" : @([bloodPressureSystolicValue.quantity doubleValueForUnit:unit]), + @"bloodPressureDiastolicValue" : @([bloodPressureDiastolicValue.quantity doubleValueForUnit:unit]), + @"startDate" : [sample valueForKey:@"startDate"], + @"endDate" : [sample valueForKey:@"endDate"], + }; + + [data addObject:elem]; + } + + + callback(@[[NSNull null], data]); + return; + } else { + NSLog(@"error getting blood pressure samples: %@", error); + callback(@[RCTMakeError(@"error getting blood pressure samples", nil, nil)]); + return; + } + }]; +} + + @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m index 491fe62b96681a5449716ff97fc10bc24378068c..41e6398d22931c69ad83f6d14b490c9454b781d2 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m @@ -108,7 +108,60 @@ } +- (void)fetchCorrelationSamplesOfType:(HKQuantityType *)quantityType + unit:(HKUnit *)unit + predicate:(NSPredicate *)predicate + ascending:(BOOL)asc + limit:(NSUInteger)lim + completion:(void (^)(NSArray *, NSError *))completion { + + NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate + ascending:asc]; + + // declare the block + void (^handlerBlock)(HKSampleQuery *query, NSArray *results, NSError *error); + // create and assign the block + handlerBlock = ^(HKSampleQuery *query, NSArray *results, NSError *error) { + if (!results) { + if (completion) { + completion(nil, error); + } + return; + } + + if (completion) { + NSMutableArray *data = [NSMutableArray arrayWithCapacity:1]; + + dispatch_async(dispatch_get_main_queue(), ^{ + + for (HKCorrelation *sample in results) { + + NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate]; + NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate]; + + NSDictionary *elem = @{ + @"correlation" : sample, + @"startDate" : startDateString, + @"endDate" : endDateString, + }; + + [data addObject:elem]; + } + + completion(data, error); + }); + } + }; + + HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType + predicate:predicate + limit:lim + sortDescriptors:@[timeSortDescriptor] + resultsHandler:handlerBlock]; + + [self.healthStore executeQuery:query]; +} diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m index 699bdb81e042efd0c255d695c3c0ad51ed7013a0..cd7a5b6e5f0fc70ebc22354cd049423fb1d29ac1 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m @@ -237,6 +237,9 @@ if([unitString isEqualToString:@"celsius"]){ theUnit = [HKUnit degreeCelsiusUnit]; } + if([unitString isEqualToString:@"mmhg"]){ + theUnit = [HKUnit millimeterOfMercuryUnit]; + } if(theUnit == nil){ theUnit = defaultValue; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index 367bff732ae38415bb383e8e2436029cf478a98a..435272258a11cc45cc1f910e8fe4d9f52cb81c5b 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -145,6 +145,10 @@ RCT_EXPORT_METHOD(getBodyTemperatureSamples:(NSDictionary *)input callback:(RCTR [self vitals_getBodyTemperatureSamples:input callback:callback]; } +RCT_EXPORT_METHOD(getBloodPressureSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self vitals_getBloodPressureSamples:input callback:callback]; +} RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) diff --git a/constants.js b/constants.js index 0b04bcfe9432d39c98a9e24d6746a4ee2fa4abf9..b36f13f27d991769114d417190687ed64c5518a1 100644 --- a/constants.js +++ b/constants.js @@ -46,6 +46,7 @@ const UNITS = { bpm: 'bpm', fahrenheit: 'fahrenheit', celsius: 'celsius', + mmhg: 'mmhg', };