Commit 7da1087a authored by Greg Wilson's avatar Greg Wilson

modified fetchMostRecentQuantitySampleOfType query to return startDate and endDate of sample

parent 71ae6eaa
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#import "RCTAppleHealthKit+Methods_Body.h" #import "RCTAppleHealthKit+Methods_Body.h"
#import "RCTAppleHealthKit+Queries.h" #import "RCTAppleHealthKit+Queries.h"
#import "RCTAppleHealthKit+Utils.h"
@implementation RCTAppleHealthKit (Methods_Body) @implementation RCTAppleHealthKit (Methods_Body)
...@@ -17,7 +18,7 @@ ...@@ -17,7 +18,7 @@
// Query to get the user's latest weight, if it exists. // Query to get the user's latest weight, if it exists.
HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; 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) { 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."); 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)]); 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 @@ ...@@ -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); 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)]); callback(@[RCTMakeError(@"An error occured saving the weight sample", nil, nil)]);
return; return;
// abort();
} }
callback(@[[NSNull null], @(weight)]); callback(@[[NSNull null], @(weight)]);
}]; }];
...@@ -62,7 +62,7 @@ ...@@ -62,7 +62,7 @@
// Query to get the user's latest BMI, if it exists. // Query to get the user's latest BMI, if it exists.
HKQuantityType *bmiType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex]; 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) { 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."); 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)]); 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 @@ ...@@ -72,7 +72,13 @@
HKUnit *countUnit = [HKUnit countUnit]; HKUnit *countUnit = [HKUnit countUnit];
double bmi = [mostRecentQuantity doubleValueForUnit: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]);
} }
}]; }];
} }
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
@interface RCTAppleHealthKit (Queries) @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; - (void)fetchSumOfSamplesTodayForType:(HKQuantityType *)quantityType unit:(HKUnit *)unit completion:(void (^)(double, NSError *))completionHandler;
@end @end
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
@implementation RCTAppleHealthKit (Queries) @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 HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType
predicate:predicate predicate:predicate
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) { resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
if (!results) { if (!results) {
if (completion) { if (completion) {
completion(nil, error); completion(nil, nil, nil, error);
} }
return; return;
} }
...@@ -30,7 +30,9 @@ ...@@ -30,7 +30,9 @@
// If quantity isn't in the database, return nil in the completion block. // If quantity isn't in the database, return nil in the completion block.
HKQuantitySample *quantitySample = results.firstObject; HKQuantitySample *quantitySample = results.firstObject;
HKQuantity *quantity = quantitySample.quantity; HKQuantity *quantity = quantitySample.quantity;
completion(quantity, error); NSDate *startDate = quantitySample.startDate;
NSDate *endDate = quantitySample.endDate;
completion(quantity, startDate, endDate, 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