Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added workout and swimming type support in read permissions and foodSamples function to fetch added foods #156

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 29 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Queries.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
48 changes: 48 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Queries.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down