Commit 62fbdfdf authored by Greg Wilson's avatar Greg Wilson

standardized method naming in Body methods, standardized callback object and error messages

parent 85998baa
...@@ -10,19 +10,19 @@ ...@@ -10,19 +10,19 @@
@interface RCTAppleHealthKit (Methods_Body) @interface RCTAppleHealthKit (Methods_Body)
- (void)body_getCurrentWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_getLatestWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_getWeightSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_getWeightSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_getLatestBodyMassIndex:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_getLatestBodyMassIndex:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_saveBodyMassIndex:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_saveBodyMassIndex:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_getMostRecentHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_getLatestHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_getHeightSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_getHeightSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_saveHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_saveHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_getMostRecentBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_getLatestBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_getMostRecentLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
@end @end
...@@ -17,7 +17,9 @@ ...@@ -17,7 +17,9 @@
HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
HKUnit *stepsUnit = [HKUnit countUnit]; HKUnit *stepsUnit = [HKUnit countUnit];
[self fetchSumOfSamplesTodayForType:stepCountType unit:stepsUnit completion:^(double totalSteps, NSError *error) { [self fetchSumOfSamplesTodayForType:stepCountType
unit:stepsUnit
completion:^(double totalSteps, NSError *error) {
if (!totalSteps) { if (!totalSteps) {
NSLog(@"Either an error occured fetching the user's step count information or none has been stored yet. In your app, try to handle this gracefully."); NSLog(@"Either an error occured fetching the user's step count information or none has been stored yet. In your app, try to handle this gracefully.");
callback(@[RCTMakeError(@"Either an error occured fetching the user's step count information or none has been stored yet. In your app, try to handle this gracefully.", nil, nil)]); callback(@[RCTMakeError(@"Either an error occured fetching the user's step count information or none has been stored yet. In your app, try to handle this gracefully.", nil, nil)]);
...@@ -31,7 +33,8 @@ ...@@ -31,7 +33,8 @@
- (void)fitness_getStepCountForDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback - (void)fitness_getStepCountForDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{ {
NSDate *date = [RCTAppleHealthKit dateFromOptions:input]; NSDate *date = [RCTAppleHealthKit dateFromOptions:input key:@"date" withDefault:[NSDate date]];
if(date == nil) { if(date == nil) {
callback(@[RCTMakeError(@"could not parse date from options.date", nil, nil)]); callback(@[RCTMakeError(@"could not parse date from options.date", nil, nil)]);
return; return;
...@@ -40,7 +43,10 @@ ...@@ -40,7 +43,10 @@
HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
HKUnit *stepsUnit = [HKUnit countUnit]; HKUnit *stepsUnit = [HKUnit countUnit];
[self fetchSumOfSamplesOnDayForType:stepCountType unit:stepsUnit day:date completion:^(double totalSteps, NSError *error) { [self fetchSumOfSamplesOnDayForType:stepCountType
unit:stepsUnit
day:date
completion:^(double totalSteps, NSError *error) {
if (!totalSteps) { if (!totalSteps) {
NSLog(@"could not fetch step count for day: %@", error); NSLog(@"could not fetch step count for day: %@", error);
callback(@[RCTMakeError(@"could not fetch step count for day", error, nil)]); callback(@[RCTMakeError(@"could not fetch step count for day", error, nil)]);
...@@ -66,7 +72,11 @@ ...@@ -66,7 +72,11 @@
HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
HKUnit *stepsUnit = [HKUnit countUnit]; HKUnit *stepsUnit = [HKUnit countUnit];
[self fetchCumulativeSumStatisticsCollection:stepCountType unit:stepsUnit startDate:startDate endDate:endDate completion:^(NSArray *arr, NSError *err){ [self fetchCumulativeSumStatisticsCollection:stepCountType
unit:stepsUnit
startDate:startDate
endDate:endDate
completion:^(NSArray *arr, NSError *err){
if (err != nil) { if (err != nil) {
NSLog(@"error with fetchCumulativeSumStatisticsCollection: %@", err); NSLog(@"error with fetchCumulativeSumStatisticsCollection: %@", err);
callback(@[RCTMakeError(@"error with fetchCumulativeSumStatisticsCollection", err, nil)]); callback(@[RCTMakeError(@"error with fetchCumulativeSumStatisticsCollection", err, nil)]);
...@@ -93,7 +103,13 @@ ...@@ -93,7 +103,13 @@
HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
[self fetchCumulativeSumStatisticsCollection:stepCountType unit:unit startDate:startDate endDate:endDate ascending:ascending limit:limit completion:^(NSArray *arr, NSError *err){ [self fetchCumulativeSumStatisticsCollection:stepCountType
unit:unit
startDate:startDate
endDate:endDate
ascending:ascending
limit:limit
completion:^(NSArray *arr, NSError *err){
if (err != nil) { if (err != nil) {
NSLog(@"error with fetchCumulativeSumStatisticsCollection: %@", err); NSLog(@"error with fetchCumulativeSumStatisticsCollection: %@", err);
callback(@[RCTMakeError(@"error with fetchCumulativeSumStatisticsCollection", err, nil)]); callback(@[RCTMakeError(@"error with fetchCumulativeSumStatisticsCollection", err, nil)]);
...@@ -107,27 +123,16 @@ ...@@ -107,27 +123,16 @@
- (void)fitness_saveSteps:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback - (void)fitness_saveSteps:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{ {
// double height = [RCTAppleHealthKit doubleValueFromOptions:input];
double value = [RCTAppleHealthKit doubleFromOptions:input key:@"value" withDefault:(double)0]; double value = [RCTAppleHealthKit doubleFromOptions:input key:@"value" withDefault:(double)0];
NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil]; NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil];
NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]]; NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]];
HKUnit *unit = [HKUnit countUnit];
// NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input];
// HKUnit *heightUnit = [RCTAppleHealthKit hkUnitFromOptions:input];
// if(heightUnit == nil){
// heightUnit = [HKUnit inchUnit];
// }
if(startDate == nil || endDate == nil){ if(startDate == nil || endDate == nil){
callback(@[RCTMakeError(@"startDate and endDate are required in options", nil, nil)]); callback(@[RCTMakeError(@"startDate and endDate are required in options", nil, nil)]);
return; return;
} }
HKUnit *unit = [HKUnit countUnit];
HKQuantity *quantity = [HKQuantity quantityWithUnit:unit doubleValue:value]; HKQuantity *quantity = [HKQuantity quantityWithUnit:unit doubleValue:value];
HKQuantityType *type = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; HKQuantityType *type = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
HKQuantitySample *sample = [HKQuantitySample quantitySampleWithType:type quantity:quantity startDate:startDate endDate:endDate]; HKQuantitySample *sample = [HKQuantitySample quantitySampleWithType:type quantity:quantity startDate:startDate endDate:endDate];
...@@ -145,7 +150,6 @@ ...@@ -145,7 +150,6 @@
- (void)fitness_getDistanceWalkingRunningOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback - (void)fitness_getDistanceWalkingRunningOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{ {
HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit meterUnit]]; HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit meterUnit]];
...@@ -181,6 +185,7 @@ ...@@ -181,6 +185,7 @@
}]; }];
} }
- (void)fitness_getFlightsClimbedOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback - (void)fitness_getFlightsClimbedOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{ {
HKUnit *unit = [HKUnit countUnit]; HKUnit *unit = [HKUnit countUnit];
......
...@@ -16,30 +16,35 @@ ...@@ -16,30 +16,35 @@
predicate:(NSPredicate *)predicate predicate:(NSPredicate *)predicate
completion:(void (^)(HKQuantity *, NSDate *, NSDate *, NSError *))completion { completion:(void (^)(HKQuantity *, NSDate *, NSDate *, NSError *))completion {
NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate ascending:NO]; NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc]
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType initWithKey:HKSampleSortIdentifierEndDate
predicate:predicate ascending:NO
limit:1 ];
sortDescriptors:@[timeSortDescriptor]
resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) { HKSampleQuery *query = [[HKSampleQuery alloc]
initWithSampleType:quantityType
if (!results) { predicate:predicate
if (completion) { limit:1
completion(nil, nil, nil, error); sortDescriptors:@[timeSortDescriptor]
} resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
return;
} if (!results) {
if (completion) {
if (completion) { completion(nil, nil, nil, error);
// If quantity isn't in the database, return nil in the completion block. }
HKQuantitySample *quantitySample = results.firstObject; return;
HKQuantity *quantity = quantitySample.quantity; }
NSDate *startDate = quantitySample.startDate;
NSDate *endDate = quantitySample.endDate; if (completion) {
completion(quantity, startDate, endDate, error); // If quantity isn't in the database, return nil in the completion block.
} HKQuantitySample *quantitySample = results.firstObject;
HKQuantity *quantity = quantitySample.quantity;
}]; NSDate *startDate = quantitySample.startDate;
NSDate *endDate = quantitySample.endDate;
completion(quantity, startDate, endDate, error);
}
}
];
[self.healthStore executeQuery:query]; [self.healthStore executeQuery:query];
} }
...@@ -52,46 +57,53 @@ ...@@ -52,46 +57,53 @@
limit:(NSUInteger)lim limit:(NSUInteger)lim
completion:(void (^)(NSArray *, NSError *))completion { completion:(void (^)(NSArray *, NSError *))completion {
NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate ascending:asc]; NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType ascending:asc];
predicate:predicate
limit:lim // declare the block
sortDescriptors:@[timeSortDescriptor] void (^handlerBlock)(HKSampleQuery *query, NSArray *results, NSError *error);
resultsHandler:^(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 (!results) { if (completion) {
if (completion) { NSMutableArray *data = [NSMutableArray arrayWithCapacity:1];
completion(nil, error);
}
return;
}
if (completion) { dispatch_async(dispatch_get_main_queue(), ^{
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];
for (HKQuantitySample *sample in results) { NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate];
HKQuantity *quantity = sample.quantity; NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate];
double value = [quantity doubleValueForUnit:unit];
NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate]; NSDictionary *elem = @{
NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate]; @"value" : @(value),
@"startDate" : startDateString,
@"endDate" : endDateString,
};
NSDictionary *elem = @{ [data addObject:elem];
@"value" : @(value), }
@"startDate" : startDateString,
@"endDate" : endDateString,
};
[data addObject:elem]; completion(data, error);
} });
}
};
completion(data, error);
});
}
}]; HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType
predicate:predicate
limit:lim
sortDescriptors:@[timeSortDescriptor]
resultsHandler:handlerBlock];
[self.healthStore executeQuery:query]; [self.healthStore executeQuery:query];
} }
......
...@@ -42,7 +42,7 @@ RCT_EXPORT_METHOD(getDateOfBirth:(NSDictionary *)input callback:(RCTResponseSend ...@@ -42,7 +42,7 @@ RCT_EXPORT_METHOD(getDateOfBirth:(NSDictionary *)input callback:(RCTResponseSend
RCT_EXPORT_METHOD(getLatestWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) RCT_EXPORT_METHOD(getLatestWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{ {
[self body_getCurrentWeight:input callback:callback]; [self body_getLatestWeight:input callback:callback];
} }
RCT_EXPORT_METHOD(getWeightSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) RCT_EXPORT_METHOD(getWeightSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
...@@ -58,7 +58,7 @@ RCT_EXPORT_METHOD(saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBl ...@@ -58,7 +58,7 @@ RCT_EXPORT_METHOD(saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBl
RCT_EXPORT_METHOD(getLatestHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) RCT_EXPORT_METHOD(getLatestHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{ {
[self body_getMostRecentHeight:input callback:callback]; [self body_getLatestHeight:input callback:callback];
} }
RCT_EXPORT_METHOD(getHeightSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) RCT_EXPORT_METHOD(getHeightSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
...@@ -85,12 +85,12 @@ RCT_EXPORT_METHOD(saveBmi:(NSDictionary *)input callback:(RCTResponseSenderBlock ...@@ -85,12 +85,12 @@ RCT_EXPORT_METHOD(saveBmi:(NSDictionary *)input callback:(RCTResponseSenderBlock
RCT_EXPORT_METHOD(getLatestBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) RCT_EXPORT_METHOD(getLatestBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{ {
[self body_getMostRecentBodyFatPercentage:input callback:callback]; [self body_getLatestBodyFatPercentage:input callback:callback];
} }
RCT_EXPORT_METHOD(getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) RCT_EXPORT_METHOD(getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{ {
[self body_getMostRecentLeanBodyMass:input callback:callback]; [self body_getLatestLeanBodyMass:input callback: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