From 8453d910cce15cca3e1f19a7a87b7b2b28046d06 Mon Sep 17 00:00:00 2001 From: Greg Wilson Date: Tue, 28 Jun 2016 18:32:12 -0400 Subject: [PATCH] added body_saveBodyMassIndex method and exported RCT saveBmi --- .../RCTAppleHealthKit+Methods_Body.h | 1 + .../RCTAppleHealthKit+Methods_Body.m | 29 +++++++++++++++---- RCTAppleHealthKit/RCTAppleHealthKit.m | 5 ++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h index 0dae9bb..0503822 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h @@ -14,6 +14,7 @@ - (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_saveHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m index 59596ce..e8520d3 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m @@ -40,19 +40,15 @@ - (void)body_saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { double weight= [[input objectForKey:@"weight"] doubleValue]; + NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input]; -// HKUnit *poundUnit = [HKUnit poundUnit]; HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input]; if(unit == nil){ unit = [HKUnit poundUnit]; } HKQuantity *weightQuantity = [HKQuantity quantityWithUnit:unit doubleValue:weight]; - HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; -// NSDate *now = [NSDate date]; - NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input]; - HKQuantitySample *weightSample = [HKQuantitySample quantitySampleWithType:weightType quantity:weightQuantity startDate:sampleDate endDate:sampleDate]; [self.healthStore saveObject:weightSample withCompletion:^(BOOL success, NSError *error) { @@ -93,6 +89,29 @@ } + +- (void)body_saveBodyMassIndex:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + double bmi = [RCTAppleHealthKit doubleValueFromOptions:input]; + NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input]; + HKUnit *unit = [HKUnit countUnit]; + + HKQuantity *bmiQuantity = [HKQuantity quantityWithUnit:unit doubleValue:bmi]; + HKQuantityType *bmiType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex]; + HKQuantitySample *bmiSample = [HKQuantitySample quantitySampleWithType:bmiType quantity:bmiQuantity startDate:sampleDate endDate:sampleDate]; + + [self.healthStore saveObject:bmiSample withCompletion:^(BOOL success, NSError *error) { + if (!success) { + NSLog(@"An error occured saving the bmi sample %@. In your app, try to handle this gracefully. The error was: %@.", bmiSample, error); + callback(@[RCTMakeError(@"An error occured saving the bmi sample", nil, nil)]); + return; + } + callback(@[[NSNull null], @(bmi)]); + }]; +} + + + - (void)body_getMostRecentHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { HKQuantityType *heightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight]; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index 628b16a..db7b2fd 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -56,6 +56,11 @@ RCT_EXPORT_METHOD(getLatestBmi:(NSDictionary *)input callback:(RCTResponseSender [self body_getLatestBodyMassIndex:input callback:callback]; } +RCT_EXPORT_METHOD(saveBmi:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self body_saveBodyMassIndex:input callback:callback]; +} + RCT_EXPORT_METHOD(getLatestBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) { -- 2.26.2