Skip to content

Commit

Permalink
Added minutes as an interval value for getting the fitness data
Browse files Browse the repository at this point in the history
  • Loading branch information
flexy1994 committed Jan 16, 2020
1 parent cec5adb commit 833c4ca
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 88 deletions.
30 changes: 16 additions & 14 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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]);
Expand Down Expand Up @@ -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]);
}];
Expand Down Expand Up @@ -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]);
}];
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion RCTAppleHealthKit/RCTAppleHealthKit+Queries.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -73,7 +74,7 @@


- (void)fetchSleepCategorySamplesForPredicate:(NSPredicate *)predicate
limit:(NSUInteger)lim
limit:(NSUInteger)lim
completion:(void (^)(NSArray *, NSError *))completion;

@end
148 changes: 75 additions & 73 deletions RCTAppleHealthKit/RCTAppleHealthKit+Queries.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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];
}
Expand All @@ -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];
Expand Down Expand Up @@ -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];
}

Expand All @@ -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];
}
Expand All @@ -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];
}
Expand Down Expand Up @@ -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]];

Expand Down Expand Up @@ -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];
}
}];
Expand Down

0 comments on commit 833c4ca

Please sign in to comment.