diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m index 316ead66..4b66bd18 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m @@ -174,8 +174,8 @@ - (void)fitness_initializeStepEventObserver:(NSDictionary *)input callback:(RCTR return; } - [self.bridge.eventDispatcher sendAppEventWithName:@"change:steps" - body:@{@"name": @"change:steps"}]; + [self.bridge.eventDispatcher sendAppEventWithName:@"change:steps" + body:@{@"name": @"change:steps"}]; // If you have subscribed for background updates you must call the completion handler here. // completionHandler(); @@ -200,10 +200,10 @@ - (void)fitness_getDistanceWalkingRunningOnDay:(NSDictionary *)input callback:(R } NSDictionary *response = @{ - @"value" : @(distance), - @"startDate" : [RCTAppleHealthKit buildISO8601StringFromDate:startDate], - @"endDate" : [RCTAppleHealthKit buildISO8601StringFromDate:endDate], - }; + @"value" : @(distance), + @"startDate" : [RCTAppleHealthKit buildISO8601StringFromDate:startDate], + @"endDate" : [RCTAppleHealthKit buildISO8601StringFromDate:endDate], + }; callback(@[[NSNull null], response]); @@ -291,10 +291,10 @@ - (void)fitness_getDistanceCyclingOnDay:(NSDictionary *)input callback:(RCTRespo } NSDictionary *response = @{ - @"value" : @(distance), - @"startDate" : [RCTAppleHealthKit buildISO8601StringFromDate:startDate], - @"endDate" : [RCTAppleHealthKit buildISO8601StringFromDate:endDate], - }; + @"value" : @(distance), + @"startDate" : [RCTAppleHealthKit buildISO8601StringFromDate:startDate], + @"endDate" : [RCTAppleHealthKit buildISO8601StringFromDate:endDate], + }; callback(@[[NSNull null], response]); }]; @@ -347,10 +347,10 @@ - (void)fitness_getFlightsClimbedOnDay:(NSDictionary *)input callback:(RCTRespon } NSDictionary *response = @{ - @"value" : @(count), - @"startDate" : [RCTAppleHealthKit buildISO8601StringFromDate:startDate], - @"endDate" : [RCTAppleHealthKit buildISO8601StringFromDate:endDate], - }; + @"value" : @(count), + @"startDate" : [RCTAppleHealthKit buildISO8601StringFromDate:startDate], + @"endDate" : [RCTAppleHealthKit buildISO8601StringFromDate:endDate], + }; callback(@[[NSNull null], response]); }]; @@ -394,6 +394,7 @@ - (void)fitness_getFlightsClimbedBetweenDates:(NSDictionary *)input callback:(RC NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]]; NSUInteger intervalDay = [RCTAppleHealthKit uintFromOptions:input key:@"intervalDay" withDefault:1]; NSUInteger intervalHour = [RCTAppleHealthKit uintFromOptions:input key:@"intervalHour" withDefault:0]; + NSUInteger intervalMinute = [RCTAppleHealthKit uintFromOptions:input key:@"intervalMinute" withDefault:0]; if(startDate == nil){ callback(@[RCTMakeError(@"startDate is required in options", nil, nil)]); return; @@ -409,6 +410,7 @@ - (void)fitness_getFlightsClimbedBetweenDates:(NSDictionary *)input callback:(RC limit:limit intervalDay:intervalDay intervalHour:intervalHour + intervalMinute:intervalMinute completion:^(NSArray *arr, NSError *err) { if (err != nil) { NSLog(@"error with fetchCumulativeSumStatisticsCollection: %@", err); diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h index 0db7f08c..25038cc6 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h @@ -52,6 +52,7 @@ limit:(NSUInteger)lim intervalDay:(NSUInteger)intervalDay intervalHour:(NSUInteger)intervalHour + intervalMinute:(NSUInteger)intervalMinute completion:(void (^)(NSArray *, NSError *))completionHandler; - (void)fetchCumulativeSumStatisticsCollection:(HKQuantityType *)quantityType @@ -73,7 +74,7 @@ - (void)fetchSleepCategorySamplesForPredicate:(NSPredicate *)predicate - limit:(NSUInteger)lim + limit:(NSUInteger)lim completion:(void (^)(NSArray *, NSError *))completion; @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m index 1c2c758d..238c2976 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m @@ -20,34 +20,34 @@ - (void)fetchMostRecentQuantitySampleOfType:(HKQuantityType *)quantityType completion:(void (^)(HKQuantity *, NSDate *, NSDate *, NSError *))completion { NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] - initWithKey:HKSampleSortIdentifierEndDate - ascending:NO - ]; + initWithKey:HKSampleSortIdentifierEndDate + ascending:NO + ]; HKSampleQuery *query = [[HKSampleQuery alloc] - initWithSampleType:quantityType - predicate:predicate - limit:1 - sortDescriptors:@[timeSortDescriptor] - resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) { - - if (!results) { - if (completion) { - completion(nil, nil, nil, error); - } - return; - } - - if (completion) { - // If quantity isn't in the database, return nil in the completion block. - HKQuantitySample *quantitySample = results.firstObject; - HKQuantity *quantity = quantitySample.quantity; - NSDate *startDate = quantitySample.startDate; - NSDate *endDate = quantitySample.endDate; - completion(quantity, startDate, endDate, error); - } - } - ]; + initWithSampleType:quantityType + predicate:predicate + limit:1 + sortDescriptors:@[timeSortDescriptor] + resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) { + + if (!results) { + if (completion) { + completion(nil, nil, nil, error); + } + return; + } + + if (completion) { + // If quantity isn't in the database, return nil in the completion block. + HKQuantitySample *quantitySample = results.firstObject; + HKQuantity *quantity = quantitySample.quantity; + NSDate *startDate = quantitySample.startDate; + NSDate *endDate = quantitySample.endDate; + completion(quantity, startDate, endDate, error); + } + } + ]; [self.healthStore executeQuery:query]; } @@ -253,7 +253,7 @@ - (void)setObserverForType:(HKSampleType *)type } - (void)fetchSleepCategorySamplesForPredicate:(NSPredicate *)predicate - limit:(NSUInteger)lim + limit:(NSUInteger)lim completion:(void (^)(NSArray *, NSError *))completion { NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate @@ -290,22 +290,22 @@ - (void)fetchSleepCategorySamplesForPredicate:(NSPredicate *)predicate NSString *valueString; switch (val) { - case HKCategoryValueSleepAnalysisInBed: - valueString = @"INBED"; - break; - case HKCategoryValueSleepAnalysisAsleep: - valueString = @"ASLEEP"; - break; - default: - valueString = @"UNKNOWN"; - break; - } + case HKCategoryValueSleepAnalysisInBed: + valueString = @"INBED"; + break; + case HKCategoryValueSleepAnalysisAsleep: + valueString = @"ASLEEP"; + break; + default: + valueString = @"UNKNOWN"; + break; + } NSDictionary *elem = @{ - @"value" : valueString, - @"startDate" : startDateString, - @"endDate" : endDateString, - }; + @"value" : valueString, + @"startDate" : startDateString, + @"endDate" : endDateString, + }; [data addObject:elem]; } @@ -331,11 +331,11 @@ - (void)fetchSleepCategorySamplesForPredicate:(NSPredicate *)predicate // endDate:endDate]; - HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:categoryType - predicate:predicate - limit:lim - sortDescriptors:@[timeSortDescriptor] - resultsHandler:handlerBlock]; + HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:categoryType + predicate:predicate + limit:lim + sortDescriptors:@[timeSortDescriptor] + resultsHandler:handlerBlock]; [self.healthStore executeQuery:query]; @@ -384,10 +384,10 @@ - (void)fetchCorrelationSamplesOfType:(HKQuantityType *)quantityType NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate]; NSDictionary *elem = @{ - @"correlation" : sample, - @"startDate" : startDateString, - @"endDate" : endDateString, - }; + @"correlation" : sample, + @"startDate" : startDateString, + @"endDate" : endDateString, + }; [data addObject:elem]; } @@ -412,15 +412,15 @@ - (void)fetchSumOfSamplesTodayForType:(HKQuantityType *)quantityType 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); - } - }]; + 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]; } @@ -433,17 +433,17 @@ - (void)fetchSumOfSamplesOnDayForType:(HKQuantityType *)quantityType 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]; - NSDate *startDate = result.startDate; - NSDate *endDate = result.endDate; - if (completionHandler) { + quantitySamplePredicate:predicate + options:HKStatisticsOptionCumulativeSum + completionHandler:^(HKStatisticsQuery *query, HKStatistics *result, NSError *error) { + HKQuantity *sum = [result sumQuantity]; + NSDate *startDate = result.startDate; + NSDate *endDate = result.endDate; + if (completionHandler) { double value = [sum doubleValueForUnit:unit]; completionHandler(value,startDate, endDate, error); - } - }]; + } + }]; [self.healthStore executeQuery:query]; } @@ -509,13 +509,15 @@ - (void)fetchCumulativeSumStatisticsCollection:(HKQuantityType *)quantityType limit:(NSUInteger)lim intervalDay:(NSUInteger)intervalDay intervalHour:(NSUInteger)intervalHour + intervalMinute:(NSUInteger)intervalMinute completion:(void (^)(NSArray *, NSError *))completionHandler { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *interval = [[NSDateComponents alloc] init]; interval.day = intervalDay; interval.hour = intervalHour; - + interval.minute = intervalMinute; + NSDateComponents *anchorComponents = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]]; @@ -701,10 +703,10 @@ - (void)fetchCumulativeSumStatisticsCollection:(HKQuantityType *)quantityType NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:endDate]; NSDictionary *elem = @{ - @"value" : @(value), - @"startDate" : startDateString, - @"endDate" : endDateString, - }; + @"value" : @(value), + @"startDate" : startDateString, + @"endDate" : endDateString, + }; [data addObject:elem]; } }];