diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h index d24b1e763764eaaff16f872f4fdcbea90d488eeb..d6ad8ae9f98975f356370e72037e30aed5cfaaec 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 9fd11295d8078e6d42d00a99c7322ba77b2fd4de..7e0752ef14257a56cedec33e4f7a002b7364edde 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