Commit 8453d910 authored by Greg Wilson's avatar Greg Wilson

added body_saveBodyMassIndex method and exported RCT saveBmi

parent 6e8ebb62
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
- (void)body_saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_getLatestBodyMassIndex:(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_getMostRecentHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_saveHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_saveHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
......
...@@ -40,19 +40,15 @@ ...@@ -40,19 +40,15 @@
- (void)body_saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback - (void)body_saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{ {
double weight= [[input objectForKey:@"weight"] doubleValue]; double weight= [[input objectForKey:@"weight"] doubleValue];
NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input];
// HKUnit *poundUnit = [HKUnit poundUnit];
HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input]; HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input];
if(unit == nil){ if(unit == nil){
unit = [HKUnit poundUnit]; unit = [HKUnit poundUnit];
} }
HKQuantity *weightQuantity = [HKQuantity quantityWithUnit:unit doubleValue:weight]; HKQuantity *weightQuantity = [HKQuantity quantityWithUnit:unit doubleValue:weight];
HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; 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]; HKQuantitySample *weightSample = [HKQuantitySample quantitySampleWithType:weightType quantity:weightQuantity startDate:sampleDate endDate:sampleDate];
[self.healthStore saveObject:weightSample withCompletion:^(BOOL success, NSError *error) { [self.healthStore saveObject:weightSample withCompletion:^(BOOL success, NSError *error) {
...@@ -93,6 +89,29 @@ ...@@ -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 - (void)body_getMostRecentHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{ {
HKQuantityType *heightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight]; HKQuantityType *heightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];
......
...@@ -56,6 +56,11 @@ RCT_EXPORT_METHOD(getLatestBmi:(NSDictionary *)input callback:(RCTResponseSender ...@@ -56,6 +56,11 @@ RCT_EXPORT_METHOD(getLatestBmi:(NSDictionary *)input callback:(RCTResponseSender
[self body_getLatestBodyMassIndex:input callback:callback]; [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) RCT_EXPORT_METHOD(getLatestBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment