Commit 295e4b0a authored by Greg Wilson's avatar Greg Wilson

added new query fetchQuantitySamplesOfType to Queries

parent 1a1c4c71
...@@ -19,4 +19,14 @@ ...@@ -19,4 +19,14 @@
startDate:(NSDate *)startDate startDate:(NSDate *)startDate
endDate:(NSDate *)endDate endDate:(NSDate *)endDate
completion:(void (^)(NSArray *, NSError *))completionHandler; 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 @end
...@@ -12,8 +12,11 @@ ...@@ -12,8 +12,11 @@
@implementation RCTAppleHealthKit (Queries) @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 HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType
predicate:predicate predicate:predicate
limit:1 limit:1
...@@ -41,6 +44,63 @@ ...@@ -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 @@ ...@@ -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 - (void)fetchCumulativeSumStatisticsCollection:(HKQuantityType *)quantityType
unit:(HKUnit *)unit unit:(HKUnit *)unit
startDate:(NSDate *)startDate startDate:(NSDate *)startDate
...@@ -119,30 +158,21 @@ ...@@ -119,30 +158,21 @@
fromDate:[NSDate date]]; fromDate:[NSDate date]];
anchorComponents.hour = 0; anchorComponents.hour = 0;
NSDate *anchorDate = [calendar dateFromComponents:anchorComponents]; NSDate *anchorDate = [calendar dateFromComponents:anchorComponents];
// HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
// Create the query // Create the query
HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:quantityType HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:quantityType
quantitySamplePredicate:nil quantitySamplePredicate:nil
options:HKStatisticsOptionCumulativeSum options:HKStatisticsOptionCumulativeSum
anchorDate:anchorDate anchorDate:anchorDate
intervalComponents:interval]; intervalComponents:interval];
// Set the results handler // Set the results handler
query.initialResultsHandler = ^(HKStatisticsCollectionQuery *query, HKStatisticsCollection *results, NSError *error) { query.initialResultsHandler = ^(HKStatisticsCollectionQuery *query, HKStatisticsCollection *results, NSError *error) {
if (error) { if (error) {
// Perform proper error handling here // Perform proper error handling here
NSLog(@"*** An error occurred while calculating the statistics: %@ ***",error.localizedDescription); 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]; NSMutableArray *data = [NSMutableArray arrayWithCapacity:1];
[results enumerateStatisticsFromDate:startDate [results enumerateStatisticsFromDate:startDate
......
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