Commit 9372ca36 authored by Greg Wilson's avatar Greg Wilson

implemented new HK query and exported RCT function getDailyStepSamples

parent d37b7975
...@@ -15,4 +15,6 @@ ...@@ -15,4 +15,6 @@
- (void)fitness_getDailyStepCounts:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_getDailyStepCounts:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getDailyStepSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
@end @end
...@@ -78,4 +78,52 @@ ...@@ -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 @end
...@@ -29,4 +29,16 @@ ...@@ -29,4 +29,16 @@
limit:(NSUInteger)lim limit:(NSUInteger)lim
completion:(void (^)(NSArray *, NSError *))completion; 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 @end
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
limit:1 limit:1
sortDescriptors:@[timeSortDescriptor] sortDescriptors:@[timeSortDescriptor]
resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) { resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
if (!results) { if (!results) {
if (completion) { if (completion) {
completion(nil, nil, nil, error); completion(nil, nil, nil, error);
...@@ -37,8 +38,8 @@ ...@@ -37,8 +38,8 @@
NSDate *endDate = quantitySample.endDate; NSDate *endDate = quantitySample.endDate;
completion(quantity, startDate, endDate, error); completion(quantity, startDate, endDate, error);
} }
}];
}];
[self.healthStore executeQuery:query]; [self.healthStore executeQuery:query];
} }
...@@ -72,8 +73,6 @@ ...@@ -72,8 +73,6 @@
for (HKQuantitySample *sample in results) { for (HKQuantitySample *sample in results) {
HKQuantity *quantity = sample.quantity; HKQuantity *quantity = sample.quantity;
// NSDate *startDate = sample.startDate;
// NSDate *endDate = sample.endDate;
double value = [quantity doubleValueForUnit:unit]; double value = [quantity doubleValueForUnit:unit];
NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate]; NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate];
...@@ -104,38 +103,46 @@ ...@@ -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]; NSPredicate *predicate = [RCTAppleHealthKit predicateForSamplesToday];
HKStatisticsQuery *query = [[HKStatisticsQuery alloc] initWithQuantityType:quantityType HKStatisticsQuery *query = [[HKStatisticsQuery alloc] initWithQuantityType:quantityType
quantitySamplePredicate:predicate quantitySamplePredicate:predicate
options:HKStatisticsOptionCumulativeSum options:HKStatisticsOptionCumulativeSum
completionHandler:^(HKStatisticsQuery *query, HKStatistics *result, NSError *error) { completionHandler:^(HKStatisticsQuery *query, HKStatistics *result, NSError *error) {
HKQuantity *sum = [result sumQuantity];
HKQuantity *sum = [result sumQuantity];
if (completionHandler) { if (completionHandler) {
double value = [sum doubleValueForUnit:unit]; double value = [sum doubleValueForUnit:unit];
completionHandler(value, error); completionHandler(value, error);
} }
}];
}];
[self.healthStore executeQuery:query]; [self.healthStore executeQuery:query];
} }
- (void)fetchSumOfSamplesOnDayForType:(HKQuantityType *)quantityType unit:(HKUnit *)unit day:(NSDate *)day completion:(void (^)(double, NSError *))completionHandler { - (void)fetchSumOfSamplesOnDayForType:(HKQuantityType *)quantityType
NSPredicate *predicate = [RCTAppleHealthKit predicateForSamplesOnDay:day]; unit:(HKUnit *)unit
day:(NSDate *)day
completion:(void (^)(double, NSError *))completionHandler {
NSPredicate *predicate = [RCTAppleHealthKit predicateForSamplesOnDay:day];
HKStatisticsQuery *query = [[HKStatisticsQuery alloc] initWithQuantityType:quantityType HKStatisticsQuery *query = [[HKStatisticsQuery alloc] initWithQuantityType:quantityType
quantitySamplePredicate:predicate quantitySamplePredicate:predicate
options:HKStatisticsOptionCumulativeSum options:HKStatisticsOptionCumulativeSum
completionHandler:^(HKStatisticsQuery *query, HKStatistics *result, NSError *error) { completionHandler:^(HKStatisticsQuery *query, HKStatistics *result, NSError *error) {
HKQuantity *sum = [result sumQuantity]; HKQuantity *sum = [result sumQuantity];
if (completionHandler) { if (completionHandler) {
double value = [sum doubleValueForUnit:unit]; double value = [sum doubleValueForUnit:unit];
completionHandler(value, error); completionHandler(value, error);
} }
}]; }];
[self.healthStore executeQuery:query]; [self.healthStore executeQuery:query];
...@@ -147,8 +154,7 @@ ...@@ -147,8 +154,7 @@
unit:(HKUnit *)unit unit:(HKUnit *)unit
startDate:(NSDate *)startDate startDate:(NSDate *)startDate
endDate:(NSDate *)endDate endDate:(NSDate *)endDate
completion:(void (^)(NSArray *, NSError *))completionHandler completion:(void (^)(NSArray *, NSError *))completionHandler {
{
NSCalendar *calendar = [NSCalendar currentCalendar]; NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *interval = [[NSDateComponents alloc] init]; NSDateComponents *interval = [[NSDateComponents alloc] init];
...@@ -174,7 +180,6 @@ ...@@ -174,7 +180,6 @@
} }
NSMutableArray *data = [NSMutableArray arrayWithCapacity:1]; NSMutableArray *data = [NSMutableArray arrayWithCapacity:1];
[results enumerateStatisticsFromDate:startDate [results enumerateStatisticsFromDate:startDate
toDate:endDate toDate:endDate
withBlock:^(HKStatistics *result, BOOL *stop) { withBlock:^(HKStatistics *result, BOOL *stop) {
...@@ -207,4 +212,99 @@ ...@@ -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 @end
...@@ -31,4 +31,6 @@ ...@@ -31,4 +31,6 @@
+ (NSString *)stringFromOptions:(NSDictionary *)options key:(NSString *)key withDefault:(NSString *)defaultValue; + (NSString *)stringFromOptions:(NSDictionary *)options key:(NSString *)key withDefault:(NSString *)defaultValue;
+ (bool)boolFromOptions:(NSDictionary *)options key:(NSString *)key withDefault:(bool)defaultValue; + (bool)boolFromOptions:(NSDictionary *)options key:(NSString *)key withDefault:(bool)defaultValue;
- (NSMutableArray *)reverseNSMutableArray:(NSMutableArray *)array;
@end @end
...@@ -278,4 +278,23 @@ ...@@ -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 @end
...@@ -109,6 +109,11 @@ RCT_EXPORT_METHOD(getMultiDayStepCounts:(NSDictionary *)input callback:(RCTRespo ...@@ -109,6 +109,11 @@ RCT_EXPORT_METHOD(getMultiDayStepCounts:(NSDictionary *)input callback:(RCTRespo
[self fitness_getDailyStepCounts:input callback:callback]; [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) RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
......
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