From 1a1c4c71433d142f160145c36ea004567b95d064 Mon Sep 17 00:00:00 2001 From: Greg Wilson Date: Wed, 29 Jun 2016 18:26:23 -0400 Subject: [PATCH] added getWeightSamples and getHeightSamples to Methods_Body category --- .../RCTAppleHealthKit+Methods_Body.h | 3 +- .../RCTAppleHealthKit+Methods_Body.m | 77 +++++++++++++++++-- 2 files changed, 73 insertions(+), 7 deletions(-) diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h index 0503822..789bcc2 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h @@ -11,15 +11,16 @@ @interface RCTAppleHealthKit (Methods_Body) - (void)body_getCurrentWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)body_getWeightSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_getLatestBodyMassIndex:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_saveBodyMassIndex:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_getMostRecentHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)body_getHeightSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_saveHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - - (void)body_getMostRecentBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_getMostRecentLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m index 7cef81f..a2fcd51 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m @@ -37,16 +37,46 @@ } +- (void)body_getWeightSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; + + HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit poundUnit]]; + 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 fetchQuantitySamplesOfType:weightType + unit:unit + predicate:predicate + ascending:ascending + limit:limit + completion:^(NSArray *results, NSError *error) { + if(results){ + callback(@[[NSNull null], results]); + return; + } else { + NSLog(@"error getting weight samples: %@", error); + callback(@[RCTMakeError(@"error getting weight samples", nil, nil)]); + return; + } + }]; +} + + + + - (void)body_saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { -// double weight= [[input objectForKey:@"weight"] doubleValue]; double weight = [RCTAppleHealthKit doubleValueFromOptions:input]; NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input]; - - HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input]; - if(unit == nil){ - unit = [HKUnit poundUnit]; - } + HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit poundUnit]]; HKQuantity *weightQuantity = [HKQuantity quantityWithUnit:unit doubleValue:weight]; HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; @@ -136,6 +166,41 @@ } + +- (void)body_getHeightSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + HKQuantityType *heightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight]; + + HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit inchUnit]]; + 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 fetchQuantitySamplesOfType:heightType + unit:unit + predicate:predicate + ascending:ascending + limit:limit + completion:^(NSArray *results, NSError *error) { + if(results){ + callback(@[[NSNull null], results]); + return; + } else { + NSLog(@"error getting height samples: %@", error); + callback(@[RCTMakeError(@"error getting height samples", nil, nil)]); + return; + } + }]; +} + + + - (void)body_saveHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { double height = [RCTAppleHealthKit doubleValueFromOptions:input]; -- 2.26.2