From 9372ca36021f46ad326d9ce03fc55abf38370480 Mon Sep 17 00:00:00 2001 From: Greg Wilson Date: Thu, 30 Jun 2016 18:23:28 -0400 Subject: [PATCH] implemented new HK query and exported RCT function getDailyStepSamples --- .../RCTAppleHealthKit+Methods_Fitness.h | 2 + .../RCTAppleHealthKit+Methods_Fitness.m | 48 +++++++ RCTAppleHealthKit/RCTAppleHealthKit+Queries.h | 12 ++ RCTAppleHealthKit/RCTAppleHealthKit+Queries.m | 134 +++++++++++++++--- RCTAppleHealthKit/RCTAppleHealthKit+Utils.h | 2 + RCTAppleHealthKit/RCTAppleHealthKit+Utils.m | 19 +++ RCTAppleHealthKit/RCTAppleHealthKit.m | 5 + 7 files changed, 205 insertions(+), 17 deletions(-) diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h index e6fce05..8cc81ec 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h @@ -15,4 +15,6 @@ - (void)fitness_getDailyStepCounts:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)fitness_getDailyStepSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; + @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m index 4769a73..39cea12 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m @@ -78,4 +78,52 @@ + +- (void)fitness_getDailyStepSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit countUnit]]; + NSUInteger limit = [RCTAppleHealthKit uintFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit]; + BOOL ascending = [RCTAppleHealthKit boolFromOptions:input key:@"ascending" withDefault:false]; + NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil]; + NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]]; + if(startDate == nil){ + callback(@[RCTMakeError(@"startDate is required in options", nil, nil)]); + return; + } + + +// NSDate *startDate = [RCTAppleHealthKit startDateFromOptions:input]; +// NSDate *endDate = [RCTAppleHealthKit endDateFromOptionsDefaultNow:input]; + +// if(startDate == nil) { +// callback(@[RCTMakeError(@"could not parse required startDate from options.startDate", nil, nil)]); +// return; +// } + + HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; +// HKUnit *stepsUnit = [HKUnit countUnit]; + +// [self fetchCumulativeSumStatisticsCollection:stepCountType unit:stepsUnit startDate:startDate endDate:endDate completion:^(NSArray *arr, NSError *err){ +// if (err != nil) { +// NSLog(@"error with fetchCumulativeSumStatisticsCollection: %@", err); +// callback(@[RCTMakeError(@"error with fetchCumulativeSumStatisticsCollection", err, nil)]); +// return; +// } +// callback(@[[NSNull null], arr]); +// }]; + + [self fetchCumulativeSumStatisticsCollection:stepCountType unit:unit startDate:startDate endDate:endDate ascending:ascending limit:limit completion:^(NSArray *arr, NSError *err){ + if (err != nil) { + NSLog(@"error with fetchCumulativeSumStatisticsCollection: %@", err); + callback(@[RCTMakeError(@"error with fetchCumulativeSumStatisticsCollection", err, nil)]); + return; + } + callback(@[[NSNull null], arr]); + }]; + +} + + + + @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h index 74b441e..bb7da93 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h @@ -29,4 +29,16 @@ limit:(NSUInteger)lim completion:(void (^)(NSArray *, NSError *))completion; + + + +- (void)fetchCumulativeSumStatisticsCollection:(HKQuantityType *)quantityType + unit:(HKUnit *)unit + startDate:(NSDate *)startDate + endDate:(NSDate *)endDate + ascending:(BOOL)asc + limit:(NSUInteger)lim + completion:(void (^)(NSArray *, NSError *))completionHandler; + + @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m index 6970b0c..8975c88 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m @@ -22,6 +22,7 @@ limit:1 sortDescriptors:@[timeSortDescriptor] resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) { + if (!results) { if (completion) { completion(nil, nil, nil, error); @@ -37,8 +38,8 @@ NSDate *endDate = quantitySample.endDate; completion(quantity, startDate, endDate, error); } - }]; + }]; [self.healthStore executeQuery:query]; } @@ -72,8 +73,6 @@ 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]; @@ -104,38 +103,46 @@ -- (void)fetchSumOfSamplesTodayForType:(HKQuantityType *)quantityType unit:(HKUnit *)unit completion:(void (^)(double, NSError *))completionHandler { +- (void)fetchSumOfSamplesTodayForType:(HKQuantityType *)quantityType + unit:(HKUnit *)unit + completion:(void (^)(double, NSError *))completionHandler { + NSPredicate *predicate = [RCTAppleHealthKit predicateForSamplesToday]; - 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); - } - }]; + + HKQuantity *sum = [result sumQuantity]; + if (completionHandler) { + double value = [sum doubleValueForUnit:unit]; + completionHandler(value, error); + } + + }]; [self.healthStore executeQuery:query]; } -- (void)fetchSumOfSamplesOnDayForType:(HKQuantityType *)quantityType unit:(HKUnit *)unit day:(NSDate *)day completion:(void (^)(double, NSError *))completionHandler { - NSPredicate *predicate = [RCTAppleHealthKit predicateForSamplesOnDay:day]; +- (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]; @@ -147,8 +154,7 @@ unit:(HKUnit *)unit startDate:(NSDate *)startDate endDate:(NSDate *)endDate - completion:(void (^)(NSArray *, NSError *))completionHandler -{ + completion:(void (^)(NSArray *, NSError *))completionHandler { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *interval = [[NSDateComponents alloc] init]; @@ -174,7 +180,6 @@ } NSMutableArray *data = [NSMutableArray arrayWithCapacity:1]; - [results enumerateStatisticsFromDate:startDate toDate:endDate withBlock:^(HKStatistics *result, BOOL *stop) { @@ -207,4 +212,99 @@ + + + + + +- (void)fetchCumulativeSumStatisticsCollection:(HKQuantityType *)quantityType + unit:(HKUnit *)unit + startDate:(NSDate *)startDate + endDate:(NSDate *)endDate + ascending:(BOOL)asc + limit:(NSUInteger)lim + completion:(void (^)(NSArray *, NSError *))completionHandler { + + NSCalendar *calendar = [NSCalendar currentCalendar]; + NSDateComponents *interval = [[NSDateComponents alloc] init]; + interval.day = 1; + + NSDateComponents *anchorComponents = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear + fromDate:[NSDate date]]; + anchorComponents.hour = 0; + NSDate *anchorDate = [calendar dateFromComponents:anchorComponents]; + + // Create the query + HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:quantityType + quantitySamplePredicate:nil + options:HKStatisticsOptionCumulativeSum + anchorDate:anchorDate + intervalComponents:interval]; + + // 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); + } + + NSMutableArray *data = [NSMutableArray arrayWithCapacity:1]; + + [results enumerateStatisticsFromDate:startDate + toDate:endDate + withBlock:^(HKStatistics *result, BOOL *stop) { + + HKQuantity *quantity = result.sumQuantity; + if (quantity) { + NSDate *startDate = result.startDate; + NSDate *endDate = result.endDate; + double value = [quantity doubleValueForUnit:unit]; + +// NSLog(@"%@: %f", date, value); + + NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:startDate]; + NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:endDate]; + + NSDictionary *elem = @{ + @"value" : @(value), + @"startDate" : startDateString, + @"endDate" : endDateString, + }; + +// NSArray *elem = @[dateString, @(value)]; + + [data addObject:elem]; + } + + }]; + // is ascending by default + if(asc == false) { + [self reverseNSMutableArray:data]; + } + + if(lim > 0) { + NSArray* slicedArray = [data subarrayWithRange:NSMakeRange(0, lim)]; + NSError *err; + completionHandler(slicedArray, err); + } else { + NSError *err; + completionHandler(data, err); + } + }; + + [self.healthStore executeQuery:query]; +} + + + + + + + + + + + + + @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h index c4aa7eb..08fe6e3 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h @@ -31,4 +31,6 @@ + (NSString *)stringFromOptions:(NSDictionary *)options key:(NSString *)key withDefault:(NSString *)defaultValue; + (bool)boolFromOptions:(NSDictionary *)options key:(NSString *)key withDefault:(bool)defaultValue; +- (NSMutableArray *)reverseNSMutableArray:(NSMutableArray *)array; + @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m index 01005e7..ac9b9f5 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m @@ -278,4 +278,23 @@ + + +- (NSMutableArray *)reverseNSMutableArray:(NSMutableArray *)array { + if ([array count] <= 1) + return array; + NSUInteger i = 0; + NSUInteger j = [array count] - 1; + while (i < j) { + [array exchangeObjectAtIndex:i + withObjectAtIndex:j]; + i++; + j--; + } + return array; +} + + + + @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index a7705db..cc8c957 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -109,6 +109,11 @@ RCT_EXPORT_METHOD(getMultiDayStepCounts:(NSDictionary *)input callback:(RCTRespo [self fitness_getDailyStepCounts:input callback:callback]; } +RCT_EXPORT_METHOD(getDailyStepSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self fitness_getDailyStepSamples:input callback:callback]; +} + RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) -- 2.26.2