From 22894714311c81b05a0dce4a408ab239562c017b Mon Sep 17 00:00:00 2001 From: Evgenii Evstropov Date: Sun, 4 Feb 2018 18:59:13 +0100 Subject: [PATCH] add simple (only to console output) support for Workouts retrieving --- Constants/Permissions.js | 3 +- .../RCTAppleHealthKit+Methods_Fitness.h | 2 +- .../RCTAppleHealthKit+Methods_Fitness.m | 9 ++- RCTAppleHealthKit/RCTAppleHealthKit+Queries.m | 59 +++++++++++++------ .../RCTAppleHealthKit+TypesAndPermissions.m | 2 + RCTAppleHealthKit/RCTAppleHealthKit.m | 4 +- 6 files changed, 53 insertions(+), 26 deletions(-) diff --git a/Constants/Permissions.js b/Constants/Permissions.js index 927fe4b..9d44aec 100644 --- a/Constants/Permissions.js +++ b/Constants/Permissions.js @@ -29,5 +29,6 @@ export const Permissions = { SleepAnalysis: "SleepAnalysis", StepCount: "StepCount", Steps: "Steps", - Weight: "Weight" + Weight: "Weight", + Workout: "Workout" } diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h index 5a38559..9f20173 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h @@ -11,7 +11,7 @@ @interface RCTAppleHealthKit (Methods_Fitness) - (void)fitness_getStepCountOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; -- (void)fitness_getStepCountSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)fitness_getSamples:(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 09994bd..23c8ea3 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m @@ -48,7 +48,7 @@ }]; } -- (void)fitness_getStepCountSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +- (void)fitness_getSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit countUnit]]; NSUInteger limit = [RCTAppleHealthKit uintFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit]; @@ -72,9 +72,10 @@ [subPredicatesAux addObject:predicateDate]; [subPredicatesAux addObject:predicateType]; subPredicates = [subPredicatesAux copy]; - NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates]; + + NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:predicateType, predicateDate, nil]]; - HKQuantityType *samplesType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; + HKSampleType *samplesType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; if ([type isEqual:@"Walking"]) { samplesType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; } else if ([type isEqual:@"StairClimbing"]) { @@ -85,6 +86,8 @@ } else if ([type isEqual:@"Cycling"]){ samplesType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling]; unit = [HKUnit mileUnit]; + } else if ([type isEqual:@"Workout"]){ + samplesType = [HKObjectType workoutType]; } NSString * paramName = @"isTracked"; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m index a0c0486..d296a67 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m @@ -105,7 +105,7 @@ [self.healthStore executeQuery:query]; } -- (void)fetchQuantitySamplesOfType:(HKQuantityType *)quantityType +- (void)fetchQuantitySamplesOfType:(HKSampleType *)type unit:(HKUnit *)unit predicate:(NSPredicate *)predicate ascending:(BOOL)asc @@ -131,22 +131,44 @@ 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]; + if (type == [HKObjectType workoutType]) { + for (HKWorkout *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]; + + NSLog(@"%lu", (unsigned long)[sample workoutActivityType]); + NSLog(@"energy burned %f", [[sample totalEnergyBurned] doubleValueForUnit:[HKUnit kilocalorieUnit]]); + NSLog(@"total distance %f", [[sample totalDistance] doubleValueForUnit:[HKUnit mileUnit]]); + } + } else { + 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); @@ -154,7 +176,7 @@ } }; - HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType + HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:type predicate:predicate limit:lim sortDescriptors:@[timeSortDescriptor] @@ -168,7 +190,6 @@ - - (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 b22e6f3..7022081 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m @@ -49,6 +49,8 @@ @"SleepAnalysis" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis], // Mindfulness @"MindfulSession" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierMindfulSession], + //workouts + @"Workout" : [HKObjectType workoutType], }; return readPerms; } diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index 35a2823..04e3d57 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -106,9 +106,9 @@ RCT_EXPORT_METHOD(getStepCount:(NSDictionary *)input callback:(RCTResponseSender [self fitness_getStepCountOnDay:input callback:callback]; } -RCT_EXPORT_METHOD(getStepCountSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +RCT_EXPORT_METHOD(getSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) { - [self fitness_getStepCountSamples:input callback:callback]; + [self fitness_getSamples:input callback:callback]; } RCT_EXPORT_METHOD(getDailyStepCountSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) -- 2.26.2