From e70b6aa5a51fdbaae4444587d04725a9c01dd259 Mon Sep 17 00:00:00 2001 From: Greg Wilson Date: Tue, 28 Jun 2016 18:21:41 -0400 Subject: [PATCH] updated body_getMostRecentHeight and body_getCurrentWeight to use the unit from options if available --- .../RCTAppleHealthKit+Methods_Body.m | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m index 383dd5e..c555368 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m @@ -18,6 +18,11 @@ // Query to get the user's latest weight, if it exists. HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; + HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input]; + if(unit == nil){ + unit = [HKUnit poundUnit]; + } + [self fetchMostRecentQuantitySampleOfType:weightType predicate:nil completion:^(HKQuantity *mostRecentQuantity, NSDate *startDate, NSDate *endDate, NSError *error) { if (!mostRecentQuantity) { NSLog(@"Either an error occured fetching the user's weight information or none has been stored yet. In your app, try to handle this gracefully."); @@ -25,8 +30,8 @@ } else { // Determine the weight in the required unit. - HKUnit *weightUnit = [HKUnit poundUnit]; - double usersWeight = [mostRecentQuantity doubleValueForUnit:weightUnit]; +// HKUnit *weightUnit = [HKUnit poundUnit]; + double usersWeight = [mostRecentQuantity doubleValueForUnit:unit]; callback(@[[NSNull null], @(usersWeight)]); } @@ -88,6 +93,11 @@ { HKQuantityType *heightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight]; + HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input]; + if(unit == nil){ + unit = [HKUnit inchUnit]; + } + [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."); @@ -95,8 +105,8 @@ } else { // Determine the weight in the required unit. - HKUnit *heightUnit = [HKUnit inchUnit]; - double usersHeight = [mostRecentQuantity doubleValueForUnit:heightUnit]; +// HKUnit *heightUnit = [HKUnit inchUnit]; + double usersHeight = [mostRecentQuantity doubleValueForUnit:unit]; callback(@[[NSNull null], @(usersHeight)]); } @@ -111,7 +121,13 @@ double height = [RCTAppleHealthKit doubleValueFromOptions:input]; NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input]; - HKUnit *heightUnit = [HKUnit inchUnit]; +// HKUnit *heightUnit = [HKUnit inchUnit]; + HKUnit *heightUnit = [RCTAppleHealthKit hkUnitFromOptions:input]; + if(heightUnit == nil){ + heightUnit = [HKUnit inchUnit]; + } + + HKQuantity *heightQuantity = [HKQuantity quantityWithUnit:heightUnit doubleValue:height]; HKQuantityType *heightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight]; -- 2.26.2