From 08ea5bef51a0cbb47d99d0a976ef7c35d4f0d697 Mon Sep 17 00:00:00 2001 From: Andrew Allen Date: Sun, 14 Apr 2019 11:02:40 -0700 Subject: [PATCH] add saveBodyFatPercentage --- .../RCTAppleHealthKit+Methods_Body.h | 2 ++ .../RCTAppleHealthKit+Methods_Body.m | 24 +++++++++++++++++++ RCTAppleHealthKit/RCTAppleHealthKit.m | 5 ++++ README.md | 2 ++ docs/saveBodyFatPercentage().md | 17 +++++++++++++ 5 files changed, 50 insertions(+) create mode 100644 docs/saveBodyFatPercentage().md diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h index 0867974..d932312 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h @@ -23,6 +23,8 @@ - (void)body_saveHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_getLatestBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)body_saveBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; + - (void)body_getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m index 34fd328..cdf0563 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m @@ -257,6 +257,30 @@ } +- (void)body_saveBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + + double percentage = [RCTAppleHealthKit doubleValueFromOptions:input]; + NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input]; + HKUnit *unit = [HKUnit percentUnit]; + + percentage = percentage / 100; + + HKQuantity *bodyFatPercentQuantity = [HKQuantity quantityWithUnit:unit doubleValue:percentage]; + HKQuantityType *bodyFatPercentType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyFatPercentage]; + HKQuantitySample *bodyFatPercentSample = [HKQuantitySample quantitySampleWithType:bodyFatPercentType quantity:bodyFatPercentQuantity startDate:sampleDate endDate:sampleDate]; + + [self.healthStore saveObject:bodyFatPercentSample withCompletion:^(BOOL success, NSError *error) { + if (!success) { + NSLog(@"error saving body fat percent sample: %@", error); + callback(@[RCTMakeError(@"error saving body fat percent sample", error, nil)]); + return; + } + callback(@[[NSNull null], @(percentage)]); + }]; +} + + - (void)body_getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { HKQuantityType *leanBodyMassType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierLeanBodyMass]; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index c91c471..1d8486c 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -99,6 +99,11 @@ RCT_EXPORT_METHOD(getLatestBodyFatPercentage:(NSDictionary *)input callback:(RCT [self body_getLatestBodyFatPercentage:input callback:callback]; } +RCT_EXPORT_METHOD(saveBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self body_saveBodyFatPercentage:input callback:callback]; +} + RCT_EXPORT_METHOD(getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) { [self body_getLatestLeanBodyMass:input callback:callback]; diff --git a/README.md b/README.md index b5e2e20..3232abe 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,7 @@ AppleHealthKit.initHealthKit(options: Object, (err: string, results: Object) => * [saveMindfulSession](/docs/saveMindfulSession().md) * [saveWeight](/docs/saveWeight().md) * [saveSteps](/docs/saveSteps().md) + * [saveBodyFatPercentage](/docs/saveBodyFatPercentage().md) * [References](#references) ## Supported Apple Permissions @@ -171,6 +172,7 @@ The available Healthkit permissions to use with `initHealthKit` | StepCount | [HKQuantityTypeIdentifierStepCount](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierstepcount?language=objc) | ✓ | ✓ | | Steps | [HKQuantityTypeIdentifierSteps](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifiersteps?language=objc) | ✓ | ✓ | | Weight | [HKQuantityTypeIdentifierBodyMass](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierbodymass?language=objc) | ✓ | ✓ | +| BodyFatPercentage | [HKQuantityTypeIdentifierBodyFatPercentage](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierbodyfatpercentage?language=objc) | ✓ | ✓ | These permissions are exported as constants of the `rn-apple-healthkit` module. diff --git a/docs/saveBodyFatPercentage().md b/docs/saveBodyFatPercentage().md new file mode 100644 index 0000000..e5f2bc3 --- /dev/null +++ b/docs/saveBodyFatPercentage().md @@ -0,0 +1,17 @@ +save a percentage body fat value to Healthkit + +`saveBodyFatPercentage` accepts an options object containing a percent value: +```javascript +let options = { + value: 16.7 // 16.7% +} +``` + +```javascript +AppleHealthKit.saveBodyFatPercentage(options: Object, (err: Object, results: Object) => { + if (err) { + return; + } + // body fat percentage successfully saved +}); +``` -- 2.26.2