From f3f15d4b7e9930c13eda21d32deedcc72bdf947d Mon Sep 17 00:00:00 2001 From: Greg Wilson Date: Tue, 28 Jun 2016 20:03:19 -0400 Subject: [PATCH] added fetchSumOfSamplesOnDayForType method to Queries --- RCTAppleHealthKit/RCTAppleHealthKit+Queries.h | 1 + RCTAppleHealthKit/RCTAppleHealthKit+Queries.m | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h index d24b1e7..d6ad8ae 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h @@ -12,5 +12,6 @@ - (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)fetchSumOfSamplesOnDayForType:(HKQuantityType *)quantityType unit:(HKUnit *)unit day:(NSDate *)day completion:(void (^)(double, NSError *))completionHandler; @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m index 9fd1129..7e0752e 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m @@ -59,4 +59,26 @@ [self.healthStore executeQuery:query]; } + + +- (void)fetchSumOfSamplesOnDayForType:(HKQuantityType *)quantityType unit:(HKUnit *)unit day:(NSDate *)day completion:(void (^)(double, NSError *))completionHandler { + NSPredicate *predicate = [RCTAppleHealthKit predicateForSamplesOnDay:day]; + + HKStatisticsQuery *query = [[HKStatisticsQuery alloc] initWithQuantityType:quantityType + quantitySamplePredicate:predicate + options:HKStatisticsOptionCumulativeSum + completionHandler:^(HKStatisticsQuery *query, HKStatistics *result, NSError *error) { + HKQuantity *sum = [result sumQuantity]; + + if (completionHandler) { + double value = [sum doubleValueForUnit:unit]; + completionHandler(value, error); + } + }]; + + [self.healthStore executeQuery:query]; +} + + + @end -- 2.26.2