From cd372698ef908309714a7e45bff28cc60a013eab Mon Sep 17 00:00:00 2001 From: Daniele Volpi Date: Wed, 13 Jul 2016 18:08:27 +0200 Subject: [PATCH] added fetchCorrelationSamplesOfType method and blood pressure samples vitals method --- .../RCTAppleHealthKit+Methods_Vitals.h | 4 +- .../RCTAppleHealthKit+Methods_Vitals.m | 56 +++++++++++++++++++ RCTAppleHealthKit/RCTAppleHealthKit+Queries.m | 53 ++++++++++++++++++ RCTAppleHealthKit/RCTAppleHealthKit+Utils.m | 3 + RCTAppleHealthKit/RCTAppleHealthKit.m | 4 ++ constants.js | 1 + 6 files changed, 118 insertions(+), 3 deletions(-) diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Vitals.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Vitals.h index 985d3a4..885f7be 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 45b091b..3016d27 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 491fe62..41e6398 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 699bdb8..cd7a5b6 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 367bff7..4352722 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 0b04bcf..b36f13f 100644 --- a/constants.js +++ b/constants.js @@ -46,6 +46,7 @@ const UNITS = { bpm: 'bpm', fahrenheit: 'fahrenheit', celsius: 'celsius', + mmhg: 'mmhg', }; -- 2.26.2