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 = {
SleepAnalysis: "SleepAnalysis",
StepCount: "StepCount",
Steps: "Steps",
Weight: "Weight"
Weight: "Weight",
Workout: "Workout"
}
......@@ -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;
......
......@@ -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];
HKQuantityType *samplesType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:predicateType, predicateDate, nil]];
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";
......
......@@ -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,7 +131,28 @@
NSMutableArray *data = [NSMutableArray arrayWithCapacity:1];
dispatch_async(dispatch_get_main_queue(), ^{
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];
......@@ -148,13 +169,14 @@
[data addObject:elem];
}
}
completion(data, error);
});
}
};
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 {
......
......@@ -49,6 +49,8 @@
@"SleepAnalysis" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis],
// Mindfulness
@"MindfulSession" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierMindfulSession],
//workouts
@"Workout" : [HKObjectType workoutType],
};
return readPerms;
}
......
......@@ -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)
......
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