diff --git a/Constants/Permissions.js b/Constants/Permissions.js index 927fe4b33fd449c5a96b1580907d9db1232eea1b..9d44aec53c9d069fe984b0824e45cbf00c820a4f 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 5a38559ff92218dc50e2c2f1bdd9ebb24408ad16..9f201735aaf2b06f00e7614cc897590b09152653 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 09994bdaf7d06114be69c486a70e2dbff4013b79..23c8ea36ca5493b5e9c6991395efd632bc19468a 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 a0c04864db896452cc687e5ae5d72198f50e270e..d296a67b54efb0ddf48b13e13f8c50e344f5a6a7 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 b22e6f346770c9153c0223fb5006e186592aff6f..702208127a293c0c46264c85dafc582d7c0fb71b 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 35a2823dd8e5173b8182182ca4ba7cd007d9e8c8..04e3d575180d93a83a62a5fc11de34d00e8773d9 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)