From aca094bfe3d335af2f7a3885410f4c7fa7874c36 Mon Sep 17 00:00:00 2001 From: Andrew Allen Date: Sun, 14 Apr 2019 11:14:42 -0700 Subject: [PATCH] add saveLeanBodyMass --- .../RCTAppleHealthKit+Methods_Body.h | 1 + .../RCTAppleHealthKit+Methods_Body.m | 22 ++++++++++++++++++- RCTAppleHealthKit/RCTAppleHealthKit.m | 5 +++++ docs/saveLeanBodyMass().md | 18 +++++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 docs/saveLeanBodyMass().md diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h index d932312..c3a3027 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h @@ -26,5 +26,6 @@ - (void)body_saveBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)body_saveLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m index cdf0563..c8992c1 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m @@ -259,7 +259,6 @@ - (void)body_saveBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { - double percentage = [RCTAppleHealthKit doubleValueFromOptions:input]; NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input]; HKUnit *unit = [HKUnit percentUnit]; @@ -307,4 +306,25 @@ }]; } + +- (void)body_saveLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + double mass = [RCTAppleHealthKit doubleValueFromOptions:input]; + NSDate *sampleDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:[NSDate date]]; + HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit poundUnit]]; + + HKQuantity *massQuantity = [HKQuantity quantityWithUnit:unit doubleValue:mass]; + HKQuantityType *massType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierLeanBodyMass]; + HKQuantitySample *massSample = [HKQuantitySample quantitySampleWithType:massType quantity:massQuantity startDate:sampleDate endDate:sampleDate]; + + [self.healthStore saveObject:massSample withCompletion:^(BOOL success, NSError *error) { + if (!success) { + NSLog(@"error saving lean body mass sample: %@", error); + callback(@[RCTMakeError(@"error saving lean body mass sample", error, nil)]); + return; + } + callback(@[[NSNull null], @(mass)]); + }]; +} + @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index 1d8486c..f459afa 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -109,6 +109,11 @@ RCT_EXPORT_METHOD(getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTRespo [self body_getLatestLeanBodyMass:input callback:callback]; } +RCT_EXPORT_METHOD(saveLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self body_saveLeanBodyMass:input callback:callback]; +} + RCT_EXPORT_METHOD(getStepCount:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) { [self fitness_getStepCountOnDay:input callback:callback]; diff --git a/docs/saveLeanBodyMass().md b/docs/saveLeanBodyMass().md new file mode 100644 index 0000000..44b7a31 --- /dev/null +++ b/docs/saveLeanBodyMass().md @@ -0,0 +1,18 @@ +save a numeric lean body mass value to Healthkit + +`saveLeanBodyMass` accepts an options object containing a numeric weight value: +```javascript +let options = { + value: 155.6 // lbs +} +``` + +```javascript +AppleHealthKit.saveLeanBodyMass(options: Object, (err: Object, results: Object) => { + if (err) { + console.log("error saving lean body mass to Healthkit: ", err); + return; + } + // Done +}); +``` -- 2.26.2