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,8 +16,13 @@ ...@@ -16,8 +16,13 @@
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
ascending:NO
];
HKSampleQuery *query = [[HKSampleQuery alloc]
initWithSampleType:quantityType
predicate:predicate predicate:predicate
limit:1 limit:1
sortDescriptors:@[timeSortDescriptor] sortDescriptors:@[timeSortDescriptor]
...@@ -38,8 +43,8 @@ ...@@ -38,8 +43,8 @@
NSDate *endDate = quantitySample.endDate; NSDate *endDate = quantitySample.endDate;
completion(quantity, startDate, endDate, error); completion(quantity, startDate, endDate, error);
} }
}
}]; ];
[self.healthStore executeQuery:query]; [self.healthStore executeQuery:query];
} }
...@@ -52,13 +57,13 @@ ...@@ -52,13 +57,13 @@
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
sortDescriptors:@[timeSortDescriptor]
resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
// declare the block
void (^handlerBlock)(HKSampleQuery *query, NSArray *results, NSError *error);
// create and assign the block
handlerBlock = ^(HKSampleQuery *query, NSArray *results, NSError *error) {
if (!results) { if (!results) {
if (completion) { if (completion) {
completion(nil, error); completion(nil, error);
...@@ -90,8 +95,15 @@ ...@@ -90,8 +95,15 @@
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