Commit 6e8ebb62 authored by Greg Wilson's avatar Greg Wilson

updated body_saveWeight to use unit and date from options, if available

parent edf44bcf
...@@ -30,9 +30,7 @@ ...@@ -30,9 +30,7 @@
} }
else { else {
// Determine the weight in the required unit. // Determine the weight in the required unit.
// HKUnit *weightUnit = [HKUnit poundUnit];
double usersWeight = [mostRecentQuantity doubleValueForUnit:unit]; double usersWeight = [mostRecentQuantity doubleValueForUnit:unit];
callback(@[[NSNull null], @(usersWeight)]); callback(@[[NSNull null], @(usersWeight)]);
} }
}]; }];
...@@ -43,13 +41,19 @@ ...@@ -43,13 +41,19 @@
{ {
double weight= [[input objectForKey:@"weight"] doubleValue]; double weight= [[input objectForKey:@"weight"] doubleValue];
HKUnit *poundUnit = [HKUnit poundUnit]; // HKUnit *poundUnit = [HKUnit poundUnit];
HKQuantity *weightQuantity = [HKQuantity quantityWithUnit:poundUnit doubleValue:weight]; HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input];
if(unit == nil){
unit = [HKUnit poundUnit];
}
HKQuantity *weightQuantity = [HKQuantity quantityWithUnit:unit doubleValue:weight];
HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
NSDate *now = [NSDate date]; // NSDate *now = [NSDate date];
NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input];
HKQuantitySample *weightSample = [HKQuantitySample quantitySampleWithType:weightType quantity:weightQuantity startDate:now endDate:now]; 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) {
if (!success) { if (!success) {
...@@ -104,10 +108,8 @@ ...@@ -104,10 +108,8 @@
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)]); 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 { else {
// Determine the weight in the required unit. // Determine the height in the required unit.
// HKUnit *heightUnit = [HKUnit inchUnit];
double usersHeight = [mostRecentQuantity doubleValueForUnit:unit]; double usersHeight = [mostRecentQuantity doubleValueForUnit:unit];
callback(@[[NSNull null], @(usersHeight)]); callback(@[[NSNull null], @(usersHeight)]);
} }
}]; }];
...@@ -116,24 +118,16 @@ ...@@ -116,24 +118,16 @@
- (void)body_saveHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback - (void)body_saveHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{ {
// NSNumber *valueFromOptions = [RCTAppleHealthKit numericValueFromOptions:input];
// double height = [[input objectForKey:@"value"] doubleValue];
double height = [RCTAppleHealthKit doubleValueFromOptions:input]; double height = [RCTAppleHealthKit doubleValueFromOptions:input];
NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input]; NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input];
// HKUnit *heightUnit = [HKUnit inchUnit];
HKUnit *heightUnit = [RCTAppleHealthKit hkUnitFromOptions:input]; HKUnit *heightUnit = [RCTAppleHealthKit hkUnitFromOptions:input];
if(heightUnit == nil){ if(heightUnit == nil){
heightUnit = [HKUnit inchUnit]; heightUnit = [HKUnit inchUnit];
} }
HKQuantity *heightQuantity = [HKQuantity quantityWithUnit:heightUnit doubleValue:height]; HKQuantity *heightQuantity = [HKQuantity quantityWithUnit:heightUnit doubleValue:height];
HKQuantityType *heightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight]; HKQuantityType *heightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];
// NSDate *now = [NSDate date];
// HKQuantitySample *heightSample = [HKQuantitySample quantitySampleWithType:heightType quantity:heightQuantity startDate:now endDate:now];
HKQuantitySample *heightSample = [HKQuantitySample quantitySampleWithType:heightType quantity:heightQuantity startDate:sampleDate endDate:sampleDate]; HKQuantitySample *heightSample = [HKQuantitySample quantitySampleWithType:heightType quantity:heightQuantity startDate:sampleDate endDate:sampleDate];
[self.healthStore saveObject:heightSample withCompletion:^(BOOL success, NSError *error) { [self.healthStore saveObject:heightSample withCompletion:^(BOOL success, NSError *error) {
......
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