diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h index 608f961c8c573a1d1606056236bd40c170cc0e22..74b441e4d45d6be966c0707db0d57dbb8cb20787 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h @@ -19,4 +19,14 @@ startDate:(NSDate *)startDate endDate:(NSDate *)endDate completion:(void (^)(NSArray *, NSError *))completionHandler; + + + +- (void)fetchQuantitySamplesOfType:(HKQuantityType *)quantityType + unit:(HKUnit *)unit + predicate:(NSPredicate *)predicate + ascending:(BOOL)asc + limit:(NSUInteger)lim + completion:(void (^)(NSArray *, NSError *))completion; + @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m index 9ef010c056f46c9c099e861b9d66ec67162e3c91..6970b0cd4105c09d4fc38f90113c2bda7592d1b2 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m @@ -12,8 +12,11 @@ @implementation RCTAppleHealthKit (Queries) -- (void)fetchMostRecentQuantitySampleOfType:(HKQuantityType *)quantityType predicate:(NSPredicate *)predicate completion:(void (^)(HKQuantity *, NSDate *, NSDate *, 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 limit:1 @@ -41,6 +44,63 @@ +- (void)fetchQuantitySamplesOfType:(HKQuantityType *)quantityType + unit:(HKUnit *)unit + predicate:(NSPredicate *)predicate + ascending:(BOOL)asc + limit:(NSUInteger)lim + completion:(void (^)(NSArray *, NSError *))completion { + + NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate ascending:asc]; + HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType + predicate:predicate + limit:lim + sortDescriptors:@[timeSortDescriptor] + resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) { + + if (!results) { + if (completion) { + completion(nil, error); + } + return; + } + + if (completion) { + NSMutableArray *data = [NSMutableArray arrayWithCapacity:1]; + + dispatch_async(dispatch_get_main_queue(), ^{ + + for (HKQuantitySample *sample in results) { + HKQuantity *quantity = sample.quantity; +// NSDate *startDate = sample.startDate; +// NSDate *endDate = sample.endDate; + double value = [quantity doubleValueForUnit:unit]; + + NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate]; + NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate]; + + NSDictionary *elem = @{ + @"value" : @(value), + @"startDate" : startDateString, + @"endDate" : endDateString, + }; + + [data addObject:elem]; + } + + completion(data, error); + }); + } + + }]; + [self.healthStore executeQuery:query]; +} + + + + + + @@ -83,27 +143,6 @@ -//- (void)fetchSumOfSamplesBetweenDatesForType:(HKQuantityType *)quantityType unit:(HKUnit *)unit startDate:(NSDate *)startDate endDate:(NSDate *)endDate completion:(void (^)(NSArray *, NSError *))completionHandler { -// NSPredicate *predicate = [RCTAppleHealthKit predicateForSamplesBetweenDates:startDate endDate:endDate]; -// -// 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]; -//} - - - - - - (void)fetchCumulativeSumStatisticsCollection:(HKQuantityType *)quantityType unit:(HKUnit *)unit startDate:(NSDate *)startDate @@ -119,30 +158,21 @@ fromDate:[NSDate date]]; anchorComponents.hour = 0; NSDate *anchorDate = [calendar dateFromComponents:anchorComponents]; -// HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; -// Create the query + // Create the query HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:quantityType quantitySamplePredicate:nil options:HKStatisticsOptionCumulativeSum anchorDate:anchorDate intervalComponents:interval]; -// Set the results handler + // Set the results handler query.initialResultsHandler = ^(HKStatisticsCollectionQuery *query, HKStatisticsCollection *results, NSError *error) { if (error) { // Perform proper error handling here NSLog(@"*** An error occurred while calculating the statistics: %@ ***",error.localizedDescription); } -// NSDate *endDate = [NSDate date]; -// NSDate *startDate = [calendar dateByAddingUnit:NSCalendarUnitDay -// value:-7 -// toDate:endDate -// options:0]; - - // Plot the daily step counts over the past 7 days - NSMutableArray *data = [NSMutableArray arrayWithCapacity:1]; [results enumerateStatisticsFromDate:startDate