diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m index 84f4eeb9444c7347bc0b3e1b2d9a078de7da9fd7..539d301f7f757485f56ebcf14ceb647775c86710 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m @@ -8,6 +8,7 @@ #import "RCTAppleHealthKit+Methods_Body.h" #import "RCTAppleHealthKit+Queries.h" +#import "RCTAppleHealthKit+Utils.h" @implementation RCTAppleHealthKit (Methods_Body) @@ -17,7 +18,7 @@ // Query to get the user's latest weight, if it exists. HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; - [self fetchMostRecentQuantitySampleOfType:weightType predicate:nil completion:^(HKQuantity *mostRecentQuantity, NSError *error) { + [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."); callback(@[RCTMakeError(@"Either an error occured fetching the user's weight information or none has been stored yet. In your app, try to handle this gracefully.", nil, nil)]); @@ -50,7 +51,6 @@ NSLog(@"An error occured saving the weight sample %@. In your app, try to handle this gracefully. The error was: %@.", weightSample, error); callback(@[RCTMakeError(@"An error occured saving the weight sample", nil, nil)]); return; - // abort(); } callback(@[[NSNull null], @(weight)]); }]; @@ -62,7 +62,7 @@ // Query to get the user's latest BMI, if it exists. HKQuantityType *bmiType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex]; - [self fetchMostRecentQuantitySampleOfType:bmiType predicate:nil completion:^(HKQuantity *mostRecentQuantity, NSError *error) { + [self fetchMostRecentQuantitySampleOfType:bmiType predicate:nil completion:^(HKQuantity *mostRecentQuantity, NSDate *startDate, NSDate *endDate, NSError *error) { if (!mostRecentQuantity) { NSLog(@"Either an error occured fetching the user's bmi information or none has been stored yet. In your app, try to handle this gracefully."); callback(@[RCTMakeError(@"Either an error occured fetching the user's bmi information or none has been stored yet. In your app, try to handle this gracefully.", nil, nil)]); @@ -72,7 +72,13 @@ HKUnit *countUnit = [HKUnit countUnit]; double bmi = [mostRecentQuantity doubleValueForUnit:countUnit]; - callback(@[[NSNull null], @(bmi)]); + NSDictionary *response = @{ + @"value" : @(bmi), + @"startDate" : [RCTAppleHealthKit buildISO8601StringFromDate:startDate], + @"endDate" : [RCTAppleHealthKit buildISO8601StringFromDate:endDate], + }; + + callback(@[[NSNull null], response]); } }]; } diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h index 45139dffd11e5874818af91da77dbb3099e67743..d24b1e763764eaaff16f872f4fdcbea90d488eeb 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h @@ -10,7 +10,7 @@ @interface RCTAppleHealthKit (Queries) -- (void)fetchMostRecentQuantitySampleOfType:(HKQuantityType *)quantityType predicate:(NSPredicate *)predicate completion:(void (^)(HKQuantity *mostRecentQuantity, NSError *error))completion; +- (void)fetchMostRecentQuantitySampleOfType:(HKQuantityType *)quantityType predicate:(NSPredicate *)predicate completion:(void (^)(HKQuantity *mostRecentQuantity, NSDate *startDate, NSDate *endDate, NSError *error))completion; - (void)fetchSumOfSamplesTodayForType:(HKQuantityType *)quantityType unit:(HKUnit *)unit completion:(void (^)(double, NSError *))completionHandler; @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m index 172615ed29bcb0e8c6fe11b9f44888e219ea5c57..9fd11295d8078e6d42d00a99c7322ba77b2fd4de 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m @@ -12,7 +12,7 @@ @implementation RCTAppleHealthKit (Queries) -- (void)fetchMostRecentQuantitySampleOfType:(HKQuantityType *)quantityType predicate:(NSPredicate *)predicate completion:(void (^)(HKQuantity *, NSError *))completion { NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate ascending:NO]; +- (void)fetchMostRecentQuantitySampleOfType:(HKQuantityType *)quantityType predicate:(NSPredicate *)predicate completion:(void (^)(HKQuantity *, NSDate *, NSDate *, NSError *))completion { NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate ascending:NO]; HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType predicate:predicate @@ -21,7 +21,7 @@ resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) { if (!results) { if (completion) { - completion(nil, error); + completion(nil, nil, nil, error); } return; } @@ -30,7 +30,9 @@ // If quantity isn't in the database, return nil in the completion block. HKQuantitySample *quantitySample = results.firstObject; HKQuantity *quantity = quantitySample.quantity; - completion(quantity, error); + NSDate *startDate = quantitySample.startDate; + NSDate *endDate = quantitySample.endDate; + completion(quantity, startDate, endDate, error); } }];