diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h index e6fce0565f4e507a198946d0eee7e228ca0e82ef..8cc81ec73a0d471e35e83455bae246e8122f186e 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 4769a73b0622b3c362e43ca7f8588b79af786b9e..39cea129fb328b530f2d90c3dc7aaf63187824bb 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 74b441e4d45d6be966c0707db0d57dbb8cb20787..bb7da93bd7a9ca5f22c3dbe8bb3314e68a187e7a 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 6970b0cd4105c09d4fc38f90113c2bda7592d1b2..8975c88ddaa874b5d9bc397718d454928d8c2f56 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 c4aa7ebcf16b796204b56f463d7d66dcd24bad61..08fe6e37a1fd7a4d59c99d8b56892cef5867d5f8 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 01005e7973ab3a9a1a798ec9c2ffa738535d080f..ac9b9f59c0582017bf3b9f7f063bb617ef3cc8f0 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 a7705db03988b4e005b87e9304df02f2ad0f0aaf..cc8c9570874e7b020676b47b837c32e5b3646f2b 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)