diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h index 22e5bd2842b2e2e67c6a33072e87baa0edf7b8d5..780f37221dccb03ad3efb3ad58a106279133a7b1 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h @@ -15,4 +15,9 @@ - (void)body_getLatestBodyMassIndex:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)body_getMostRecentHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)body_getMostRecentBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)body_getMostRecentLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; + + @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m index 539d301f7f757485f56ebcf14ceb647775c86710..30f64fd9b9887f4bc88975260d40538d19d8ea66 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m @@ -84,4 +84,65 @@ } +- (void)body_getMostRecentHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + HKQuantityType *heightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight]; + + [self fetchMostRecentQuantitySampleOfType:heightType predicate:nil completion:^(HKQuantity *mostRecentQuantity, NSDate *startDate, NSDate *endDate, NSError *error) { + if (!mostRecentQuantity) { + NSLog(@"Either an error occured fetching the user's height information or none has been stored yet. In your app, try to handle this gracefully."); + callback(@[RCTMakeError(@"Either an error occured fetching the user's height information or none has been stored yet. In your app, try to handle this gracefully.", nil, nil)]); + } + else { + // Determine the weight in the required unit. + HKUnit *heightUnit = [HKUnit inchUnit]; + double usersHeight = [mostRecentQuantity doubleValueForUnit:heightUnit]; + + callback(@[[NSNull null], @(usersHeight)]); + } + }]; +} + + +- (void)body_getMostRecentBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + HKQuantityType *bodyFatPercentType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyFatPercentage]; + + [self fetchMostRecentQuantitySampleOfType:bodyFatPercentType predicate:nil completion:^(HKQuantity *mostRecentQuantity, NSDate *startDate, NSDate *endDate, NSError *error) { + if (!mostRecentQuantity) { + NSLog(@"Either an error occured fetching the user's BodyFatPercentage information or none has been stored yet. In your app, try to handle this gracefully."); + callback(@[RCTMakeError(@"Either an error occured fetching the user's BodyFatPercentage information or none has been stored yet. In your app, try to handle this gracefully.", nil, nil)]); + } + else { + // Determine the weight in the required unit. + HKUnit *percentUnit = [HKUnit percentUnit]; + double percentage = [mostRecentQuantity doubleValueForUnit:percentUnit]; + + percentage = percentage * 100; + + callback(@[[NSNull null], @(percentage)]); + } + }]; +} + + +- (void)body_getMostRecentLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + HKQuantityType *leanBodyMassType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierLeanBodyMass]; + + [self fetchMostRecentQuantitySampleOfType:leanBodyMassType predicate:nil completion:^(HKQuantity *mostRecentQuantity, NSDate *startDate, NSDate *endDate, NSError *error) { + if (!mostRecentQuantity) { + NSLog(@"Either an error occured fetching the user's LeanBodyMass information or none has been stored yet. In your app, try to handle this gracefully."); + callback(@[RCTMakeError(@"Either an error occured fetching the user's LeanBodyMass information or none has been stored yet. In your app, try to handle this gracefully.", nil, nil)]); + } + else { + HKUnit *weightUnit = [HKUnit poundUnit]; + double leanBodyMass = [mostRecentQuantity doubleValueForUnit:weightUnit]; + + callback(@[[NSNull null], @(leanBodyMass)]); + } + }]; +} + + @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index 36bcd5848497ad0dec9b015a41aaa8d69b11f7dc..09f8a6c268a318f90ff65b60b0ece6a83564798d 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -39,16 +39,39 @@ RCT_EXPORT_METHOD(saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBl [self body_saveWeight:input callback:callback]; } + +RCT_EXPORT_METHOD(getMostRecentHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self body_getMostRecentHeight:input callback:callback]; +} + + + RCT_EXPORT_METHOD(getLatestBmi:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) { [self body_getLatestBodyMassIndex:input callback:callback]; } + +RCT_EXPORT_METHOD(getMostRecentBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self body_getMostRecentBodyFatPercentage:input callback:callback]; +} + +RCT_EXPORT_METHOD(getMostRecentLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self body_getMostRecentLeanBodyMass:input callback:callback]; +} + + RCT_EXPORT_METHOD(getStepCountForToday:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) { [self fitness_getStepCountForToday:input callback:callback]; } + + + RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) { [self getModuleInfo:input callback:callback]; @@ -94,7 +117,7 @@ RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock // make sure at least 1 read or write permission is provided if(!writeDataTypes && !readDataTypes){ - callback(@[RCTMakeError(@"at least 1 write or read permission must be set in options.permissions", nil, nil)]); + callback(@[RCTMakeError(@"at least 1 read or write permission must be set in options.permissions", nil, nil)]); return; }