Commit 22894714 authored by Evgenii Evstropov's avatar Evgenii Evstropov

add simple (only to console output) support for Workouts retrieving

parent fd433d53
...@@ -29,5 +29,6 @@ export const Permissions = { ...@@ -29,5 +29,6 @@ export const Permissions = {
SleepAnalysis: "SleepAnalysis", SleepAnalysis: "SleepAnalysis",
StepCount: "StepCount", StepCount: "StepCount",
Steps: "Steps", Steps: "Steps",
Weight: "Weight" Weight: "Weight",
Workout: "Workout"
} }
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
@interface RCTAppleHealthKit (Methods_Fitness) @interface RCTAppleHealthKit (Methods_Fitness)
- (void)fitness_getStepCountOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (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_getDailyStepSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_saveSteps:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_saveSteps:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_initializeStepEventObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_initializeStepEventObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
......
...@@ -48,7 +48,7 @@ ...@@ -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]]; HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit countUnit]];
NSUInteger limit = [RCTAppleHealthKit uintFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit]; NSUInteger limit = [RCTAppleHealthKit uintFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit];
...@@ -72,9 +72,10 @@ ...@@ -72,9 +72,10 @@
[subPredicatesAux addObject:predicateDate]; [subPredicatesAux addObject:predicateDate];
[subPredicatesAux addObject:predicateType]; [subPredicatesAux addObject:predicateType];
subPredicates = [subPredicatesAux copy]; 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"]) { if ([type isEqual:@"Walking"]) {
samplesType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; samplesType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
} else if ([type isEqual:@"StairClimbing"]) { } else if ([type isEqual:@"StairClimbing"]) {
...@@ -85,6 +86,8 @@ ...@@ -85,6 +86,8 @@
} else if ([type isEqual:@"Cycling"]){ } else if ([type isEqual:@"Cycling"]){
samplesType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling]; samplesType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling];
unit = [HKUnit mileUnit]; unit = [HKUnit mileUnit];
} else if ([type isEqual:@"Workout"]){
samplesType = [HKObjectType workoutType];
} }
NSString * paramName = @"isTracked"; NSString * paramName = @"isTracked";
......
...@@ -105,7 +105,7 @@ ...@@ -105,7 +105,7 @@
[self.healthStore executeQuery:query]; [self.healthStore executeQuery:query];
} }
- (void)fetchQuantitySamplesOfType:(HKQuantityType *)quantityType - (void)fetchQuantitySamplesOfType:(HKSampleType *)type
unit:(HKUnit *)unit unit:(HKUnit *)unit
predicate:(NSPredicate *)predicate predicate:(NSPredicate *)predicate
ascending:(BOOL)asc ascending:(BOOL)asc
...@@ -131,22 +131,44 @@ ...@@ -131,22 +131,44 @@
NSMutableArray *data = [NSMutableArray arrayWithCapacity:1]; NSMutableArray *data = [NSMutableArray arrayWithCapacity:1];
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
if (type == [HKObjectType workoutType]) {
for (HKQuantitySample *sample in results) { for (HKWorkout *sample in results) {
HKQuantity *quantity = sample.quantity; HKQuantity *quantity = sample.quantity;
double value = [quantity doubleValueForUnit:unit]; double value = [quantity doubleValueForUnit:unit];
NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate]; NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate];
NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate]; NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate];
NSDictionary *elem = @{ NSDictionary *elem = @{
@"value" : @(value), @"value" : @(value),
@"startDate" : startDateString, @"startDate" : startDateString,
@"endDate" : endDateString, @"endDate" : endDateString,
paramName : @(param), paramName : @(param),
}; };
[data addObject:elem]; [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); completion(data, error);
...@@ -154,7 +176,7 @@ ...@@ -154,7 +176,7 @@
} }
}; };
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:type
predicate:predicate predicate:predicate
limit:lim limit:lim
sortDescriptors:@[timeSortDescriptor] sortDescriptors:@[timeSortDescriptor]
...@@ -168,7 +190,6 @@ ...@@ -168,7 +190,6 @@
- (void)fetchSleepCategorySamplesForPredicate:(NSPredicate *)predicate - (void)fetchSleepCategorySamplesForPredicate:(NSPredicate *)predicate
limit:(NSUInteger)lim limit:(NSUInteger)lim
completion:(void (^)(NSArray *, NSError *))completion { completion:(void (^)(NSArray *, NSError *))completion {
......
...@@ -49,6 +49,8 @@ ...@@ -49,6 +49,8 @@
@"SleepAnalysis" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis], @"SleepAnalysis" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis],
// Mindfulness // Mindfulness
@"MindfulSession" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierMindfulSession], @"MindfulSession" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierMindfulSession],
//workouts
@"Workout" : [HKObjectType workoutType],
}; };
return readPerms; return readPerms;
} }
......
...@@ -106,9 +106,9 @@ RCT_EXPORT_METHOD(getStepCount:(NSDictionary *)input callback:(RCTResponseSender ...@@ -106,9 +106,9 @@ RCT_EXPORT_METHOD(getStepCount:(NSDictionary *)input callback:(RCTResponseSender
[self fitness_getStepCountOnDay:input callback:callback]; [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) RCT_EXPORT_METHOD(getDailyStepCountSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment