diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.h index f1477ff6..6d62c597 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.h @@ -12,5 +12,6 @@ - (void)saveFood:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)saveWater:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)getFoodSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.m index 490e2954..56d55203 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.m @@ -406,4 +406,33 @@ - (void)saveWater:(NSDictionary *)input callback:(RCTResponseSenderBlock)callbac }]; } +- (void)getFoodSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { + + HKCorrelationType *foodtype = [HKCorrelationType correlationTypeForIdentifier:HKCorrelationTypeIdentifierFood]; + + 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; + } + + NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionNone]; + NSPredicate *userEnterDataPredicate = [HKQuery predicateForObjectsWithMetadataKey:HKMetadataKeyWasUserEntered operatorType:NSEqualToPredicateOperatorType value: [NSNumber numberWithBool:true]]; + NSPredicate *allPredicates = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:predicate,userEnterDataPredicate, nil]]; + + [self fetchCorrelationFoodSamplesOfType: foodtype predicate:allPredicates ascending: ascending limit: limit completion:completion:^(NSArray *results, NSError *error) { + if(results){ + callback(@[[NSNull null], results]); + return; + } else { + NSLog(@"error getting food nutirions samples: %@", error); + callback(@[RCTMakeError(@"Error getting food nutirions samples", nil, nil)]); + return; + } + }]; +} + @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h index 357aa895..c63daaf1 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h @@ -65,4 +65,11 @@ limit:(NSUInteger)lim completion:(void (^)(NSArray *, NSError *))completion; + +- (void)fetchCorrelationFoodSamplesOfType:(HKQuantityType *)quantityType + predicate:(NSPredicate *)predicate + ascending:(BOOL)asc + limit:(NSUInteger)lim + completion:(void (^)(NSArray *, NSError *))completion; + @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m index 240e0d61..acba03ef 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m @@ -252,6 +252,54 @@ - (void)setObserverForType:(HKSampleType *)type }]; } +- (void)fetchCorrelationFoodSamplesOfType:(HKQuantityType *)quantityType + predicate:(NSPredicate *)predicate + ascending:(BOOL)asc + limit:(NSUInteger)lim + 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 (HKCorrelation *sample in results) { + NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate]; + NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate]; + + NSDictionary *elem = @{ + @"correlation" : sample, + @"startDate" : startDateString, + @"endDate" : endDateString, + }; + [data addObject:elem]; + } + completion(data, error); + }); + } + }; + + HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType + predicate:predicate + limit:lim + sortDescriptors:@[timeSortDescriptor] + resultsHandler:handlerBlock]; + + [self.healthStore executeQuery:query]; +} + - (void)fetchSleepCategorySamplesForPredicate:(NSPredicate *)predicate limit:(NSUInteger)lim completion:(void (^)(NSArray *, NSError *))completion { diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m index 5ff608c3..dbb80584 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m @@ -46,6 +46,8 @@ - (nullable HKObjectType *)getReadPermFromText:(nonnull NSString*)key { return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; }else if ([@"DistanceWalkingRunning" isEqualToString: key]) { return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning]; + }else if ([@"DistanceSwimming" isEqualToString: key]) { + return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceSwimming]; }else if ([@"DistanceCycling" isEqualToString: key]) { return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling]; }else if ([@"BasalEnergyBurned" isEqualToString: key]) { @@ -96,6 +98,11 @@ - (nullable HKObjectType *)getReadPermFromText:(nonnull NSString*)key { } else if ([@"MindfulSession" isEqualToString: key]){ return [HKObjectType workoutType]; } + + // workouts + if ([@"Workout" isEqualToString: key]){ + return [HKObjectType workoutType]; + } return nil; } diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index a7219a1f..56dff8a2 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -189,6 +189,10 @@ @implementation RCTAppleHealthKit { [self saveFood:input callback:callback]; } +RCT_EXPORT_METHOD(getFoodSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self getFoodSamples:input callback:callback]; +} RCT_EXPORT_METHOD(saveWater:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) {