diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h index 086797426969dc6bd3c069fdbb3cccfe2eefffe8..472829cae1d16bb51f837b305f5a36514a4109c2 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h @@ -3,8 +3,7 @@ // RCTAppleHealthKit // // Created by Greg Wilson on 2016-06-26. -// This source code is licensed under the MIT-style license found in the -// LICENSE file in the root directory of this source tree. +// Copyright © 2016 Greg Wilson. All rights reserved. // #import "RCTAppleHealthKit.h" diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m index 8fec38d4bae8f3091f392da38d6f73340aeea24e..f29f7f84a9b4d8a48645fa85d93958c74f4bf1f8 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m @@ -3,8 +3,7 @@ // RCTAppleHealthKit // // Created by Greg Wilson on 2016-06-26. -// This source code is licensed under the MIT-style license found in the -// LICENSE file in the root directory of this source tree. +// Copyright © 2016 Greg Wilson. All rights reserved. // #import "RCTAppleHealthKit+Methods_Body.h" diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Characteristic.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Characteristic.h index 0909ebef17f19105c9d6af5ef535f8bc757f6993..6c0adcceee71627374b47e04caa5ed5ec94abf0b 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Characteristic.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Characteristic.h @@ -3,8 +3,7 @@ // RCTAppleHealthKit // // Created by Greg Wilson on 2016-06-29. -// This source code is licensed under the MIT-style license found in the -// LICENSE file in the root directory of this source tree. +// Copyright © 2016 Greg Wilson. All rights reserved. // #import "RCTAppleHealthKit.h" diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Characteristic.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Characteristic.m index e030b856567d5cb131769f438c87492062382d07..b7fb4f4226358f084889880784e6ff58a576e3c6 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Characteristic.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Characteristic.m @@ -3,8 +3,7 @@ // RCTAppleHealthKit // // Created by Greg Wilson on 2016-06-29. -// This source code is licensed under the MIT-style license found in the -// LICENSE file in the root directory of this source tree. +// Copyright © 2016 Greg Wilson. All rights reserved. // #import "RCTAppleHealthKit+Methods_Characteristic.h" diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h index 4172e3e8cd825a7dc62f2a967b6caed90fce7cf8..5a38559ff92218dc50e2c2f1bdd9ebb24408ad16 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h @@ -3,8 +3,7 @@ // RCTAppleHealthKit // // Created by Greg Wilson on 2016-06-26. -// This source code is licensed under the MIT-style license found in the -// LICENSE file in the root directory of this source tree. +// Copyright © 2016 Greg Wilson. All rights reserved. // #import "RCTAppleHealthKit.h" @@ -12,6 +11,7 @@ @interface RCTAppleHealthKit (Methods_Fitness) - (void)fitness_getStepCountOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)fitness_getStepCountSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_getDailyStepSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_saveSteps:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_initializeStepEventObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m index 10ef07b2ca624e371401eddcf0226c22820e558c..9decba15130346ab2fe2ed360445f96060560dc8 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m @@ -3,8 +3,7 @@ // RCTAppleHealthKit // // Created by Greg Wilson on 2016-06-26. -// This source code is licensed under the MIT-style license found in the -// LICENSE file in the root directory of this source tree. +// Copyright © 2016 Greg Wilson. All rights reserved. // #import "RCTAppleHealthKit+Methods_Fitness.h" @@ -49,6 +48,55 @@ }]; } +- (void)fitness_getStepCountSamples:(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]; + BOOL isTracked = [RCTAppleHealthKit boolFromOptions:input key:@"isTracked" withDefault:true]; + 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; + } + NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; + [formatter setDateFormat:@"YYYY-MM-dd-hh-mm-ss"]; + NSLog(@"samples interval: %@ %@",[formatter stringFromDate:startDate], [formatter stringFromDate:endDate]); + + // no isTracked + NSArray *subPredicates = [[NSArray alloc] init]; + NSMutableArray *subPredicatesAux = [[NSMutableArray alloc] init]; + NSPredicate *predicateDate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate]; + NSPredicate *predicateType = isTracked ? [NSPredicate predicateWithFormat:@"metadata.%K != YES", HKMetadataKeyWasUserEntered] : [NSPredicate predicateWithFormat:@"metadata.%K == YES", HKMetadataKeyWasUserEntered]; + [subPredicatesAux addObject:predicateDate]; + [subPredicatesAux addObject:predicateType]; + subPredicates = [subPredicatesAux copy]; + NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates]; + + HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; + + NSString * paramName = @"isTracked"; + + [self fetchQuantitySamplesOfType:stepCountType + unit:unit + predicate:predicate + ascending:ascending + limit:limit + additionalParamName:paramName + additionalParam:isTracked + completion:^(NSArray *results, NSError *error) { + if(results){ + callback(@[[NSNull null], results]); + return; + } else { + NSLog(@"error getting active energy burned samples: %@", error); + callback(@[RCTMakeError(@"error getting active energy burned samples", nil, nil)]); + return; + } + }]; +} + - (void)fitness_getDailyStepSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.h index cb71012dc54df8f5b8ebcf56bd704e48a41d820d..3afd6d9f1a6c37a71b252dead5d0473782ba1a04 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.h @@ -3,8 +3,7 @@ // RCTAppleHealthKit // // Created by Greg Wilson on 2016-11-06. -// This source code is licensed under the MIT-style license found in the -// LICENSE file in the root directory of this source tree. +// Copyright © 2016 Greg Wilson. All rights reserved. // #import "RCTAppleHealthKit.h" diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.m index faba4ed1739c4367fddc4158937ac85ad7a2c1ff..4b1898f87aaa69f835290bcd9131ef9f1b3baf68 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.m @@ -3,8 +3,7 @@ // RCTAppleHealthKit // // Created by Greg Wilson on 2016-11-06. -// This source code is licensed under the MIT-style license found in the -// LICENSE file in the root directory of this source tree. +// Copyright © 2016 Greg Wilson. All rights reserved. // diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h index 8ca43a773e8c85231626397ba2be8332ea0c68f8..d04e94363a6bfe34fb13d504bfac2b5720545ae9 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h @@ -3,8 +3,7 @@ // RCTAppleHealthKit // // Created by Greg Wilson on 2016-06-26. -// This source code is licensed under the MIT-style license found in the -// LICENSE file in the root directory of this source tree. +// Copyright © 2016 Greg Wilson. All rights reserved. // #import "RCTAppleHealthKit.h" @@ -25,6 +24,14 @@ ascending:(BOOL)asc limit:(NSUInteger)lim completion:(void (^)(NSArray *, NSError *))completion; +- (void)fetchQuantitySamplesOfType:(HKQuantityType *)quantityType + unit:(HKUnit *)unit + predicate:(NSPredicate *)predicate + ascending:(BOOL)asc + limit:(NSUInteger)lim + additionalParamName:(NSString *)paramName + additionalParam:(BOOL)param + completion:(void (^)(NSArray *, NSError *))completion; - (void)fetchCorrelationSamplesOfType:(HKQuantityType *)quantityType unit:(HKUnit *)unit predicate:(NSPredicate *)predicate diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m index 6ca460cd34f6922ddc63583a05599612534c9d1e..fab08e2fc8d8b64c7bd899298892342afb48f51c 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m @@ -3,8 +3,7 @@ // RCTAppleHealthKit // // Created by Greg Wilson on 2016-06-26. -// This source code is licensed under the MIT-style license found in the -// LICENSE file in the root directory of this source tree. +// Copyright © 2016 Greg Wilson. All rights reserved. // #import "RCTAppleHealthKit+Queries.h" @@ -106,7 +105,64 @@ [self.healthStore executeQuery:query]; } - +- (void)fetchQuantitySamplesOfType:(HKQuantityType *)quantityType + unit:(HKUnit *)unit + predicate:(NSPredicate *)predicate + ascending:(BOOL)asc + limit:(NSUInteger)lim + additionalParamName:(NSString *)paramName + additionalParam:(BOOL)param + completion:(void (^)(NSArray *, NSError *))completion { + + NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate + ascending:asc]; + + // declare the block + void (^handlerBlock)(HKSampleQuery *query, NSArray *results, NSError *error); + // create and assign the block + handlerBlock = ^(HKSampleQuery *query, NSArray *results, NSError *error) { + if (!results) { + if (completion) { + completion(nil, error); + } + return; + } + + if (completion) { + NSMutableArray *data = [NSMutableArray arrayWithCapacity:1]; + + dispatch_async(dispatch_get_main_queue(), ^{ + + for (HKQuantitySample *sample in results) { + HKQuantity *quantity = sample.quantity; + double value = [quantity doubleValueForUnit:unit]; + + NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate]; + NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate]; + + NSDictionary *elem = @{ + @"value" : @(value), + @"startDate" : startDateString, + @"endDate" : endDateString, + paramName : @(param), + }; + + [data addObject:elem]; + } + + completion(data, error); + }); + } + }; + + HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType + predicate:predicate + limit:lim + sortDescriptors:@[timeSortDescriptor] + resultsHandler:handlerBlock]; + + [self.healthStore executeQuery:query]; +} diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.h b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.h index 5cb0891bcfec2e07d51735cc97d69ded5ce8011c..73438fe45b41c11bc4fdd3e28277b9f7d3b0d96c 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.h @@ -3,8 +3,7 @@ // RCTAppleHealthKit // // Created by Greg Wilson on 2016-06-26. -// This source code is licensed under the MIT-style license found in the -// LICENSE file in the root directory of this source tree. +// Copyright © 2016 Greg Wilson. All rights reserved. // #import "RCTAppleHealthKit.h" diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m index 356accb0dc1a5f51e9dee64429c803dab32c9786..b22e6f346770c9153c0223fb5006e186592aff6f 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m @@ -3,8 +3,7 @@ // RCTAppleHealthKit // // Created by Greg Wilson on 2016-06-26. -// This source code is licensed under the MIT-style license found in the -// LICENSE file in the root directory of this source tree. +// Copyright © 2016 Greg Wilson. All rights reserved. // #import "RCTAppleHealthKit+TypesAndPermissions.h" diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h index 4501675e0f9f69232031ac6b10bdc0eded74637b..f5d33f380636b123fd370874e61fcbebb104c9e6 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h @@ -3,8 +3,7 @@ // RCTAppleHealthKit // // Created by Greg Wilson on 2016-06-26. -// This source code is licensed under the MIT-style license found in the -// LICENSE file in the root directory of this source tree. +// Copyright © 2016 Greg Wilson. All rights reserved. // #import "RCTAppleHealthKit.h" diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m index 7e0a834c646248c4ab37fa2aec85abc32838e325..9ffb0967d3a067a481f2f6bd2611caea4948991e 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m @@ -3,8 +3,7 @@ // RCTAppleHealthKit // // Created by Greg Wilson on 2016-06-26. -// This source code is licensed under the MIT-style license found in the -// LICENSE file in the root directory of this source tree. +// Copyright © 2016 Greg Wilson. All rights reserved. // #import "RCTAppleHealthKit+Utils.h" diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.h b/RCTAppleHealthKit/RCTAppleHealthKit.h index 27b8c0369040b37434aa8ce0d4928e5246de5951..39b603415c06570ff35b15f7a7c63b7e44b37606 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit.h @@ -3,8 +3,7 @@ // RCTAppleHealthKit // // Created by Greg Wilson on 2016-06-26. -// This source code is licensed under the MIT-style license found in the -// LICENSE file in the root directory of this source tree. +// Copyright © 2016 Greg Wilson. All rights reserved. // #import diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index bb8ac70343e91373c11abce2d649c38ddbb58b1e..35a2823dd8e5173b8182182ca4ba7cd007d9e8c8 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -3,8 +3,7 @@ // RCTAppleHealthKit // // Created by Greg Wilson on 2016-06-26. -// This source code is licensed under the MIT-style license found in the -// LICENSE file in the root directory of this source tree. +// Copyright © 2016 Greg Wilson. All rights reserved. // #import "RCTAppleHealthKit.h" @@ -107,6 +106,11 @@ RCT_EXPORT_METHOD(getStepCount:(NSDictionary *)input callback:(RCTResponseSender [self fitness_getStepCountOnDay:input callback:callback]; } +RCT_EXPORT_METHOD(getStepCountSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self fitness_getStepCountSamples:input callback:callback]; +} + RCT_EXPORT_METHOD(getDailyStepCountSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) { [self fitness_getDailyStepSamples:input callback:callback];