Created
May 29, 2015 19:24
-
-
Save mattio/9126ecc4f08b9f0497d9 to your computer and use it in GitHub Desktop.
Conditional ORKOrderedTask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <ResearchKit/ResearchKit.h> | |
@interface MyConditionalTask : ORKOrderedTask <ORKTask> | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "MyConditionalTask.h" | |
@implementation MyConditionalTask | |
- (instancetype)init | |
{ | |
// I would not use hard coded strings for a production app, of course. | |
ORKInstructionStep *introductoryStep = [[ORKInstructionStep alloc] initWithIdentifier:@"kIntroStep"]; | |
introductoryStep.title = @"A Survey with a Conditional Question"; | |
ORKStep *feverStep = [ORKQuestionStep questionStepWithIdentifier:@"kFeverIdentifier" title:@"Do you have a fever now?" answer:[ORKBooleanAnswerFormat booleanAnswerFormat]]; | |
// Was testing this out to see what would happen. | |
ORKAnswerFormat *tempFormat = [ORKHealthKitQuantityTypeAnswerFormat answerFormatWithQuantityType:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyTemperature] unit:[HKUnit degreeFahrenheitUnit] style:ORKNumericAnswerStyleDecimal]; | |
ORKQuestionStep *currentTempStep = [ORKQuestionStep questionStepWithIdentifier:@"kTemperature" title:@"What is your current body temperature?" answer:tempFormat]; | |
currentTempStep.placeholder = @"Fahrenheit"; | |
ORKStep *lastFeverDateStep = [ORKQuestionStep questionStepWithIdentifier:@"kLastFever" title:@"What was the date of your last fever?" answer:[ORKAnswerFormat dateAnswerFormat]]; | |
self = [super initWithIdentifier:@"kConditionalTask" steps:@[introductoryStep, feverStep, currentTempStep, lastFeverDateStep]]; | |
return self; | |
} | |
#pragma mark - ORKTask | |
- (nullable ORKStep *)stepAfterStep:(nullable ORKStep *)step withResult:(ORKTaskResult * __nonnull)result | |
{ | |
NSString *currentStepIdentifier = step.identifier; | |
// I would not use hard coded strings for a production app, of course. | |
if ([currentStepIdentifier isEqualToString:@"kFeverIdentifier"]) | |
{ | |
ORKStepResult *currentStepResult = [result stepResultForStepIdentifier:currentStepIdentifier]; | |
ORKQuestionResult *currentQuestionResult = (ORKQuestionResult *)currentStepResult.firstResult; | |
if ([currentQuestionResult isKindOfClass:[ORKBooleanQuestionResult class]]) | |
{ | |
ORKBooleanQuestionResult *booleanResult = (ORKBooleanQuestionResult *)currentQuestionResult; | |
NSNumber *booleanAnswer = booleanResult.booleanAnswer; | |
if (booleanAnswer) | |
{ | |
// Too simplistic for production, but good enough for this early testing. | |
return booleanAnswer.boolValue ? self.steps[2] : self.steps[3]; | |
} | |
} | |
} | |
// Fall-through | |
return [super stepAfterStep:step withResult:result]; | |
} | |
- (nullable ORKStep *)stepBeforeStep:(nullable ORKStep *)step withResult:(ORKTaskResult * __nonnull)result | |
{ | |
NSString *currentStepIdentifier = step.identifier; | |
// I would not use hard coded strings for a production app, of course. | |
if ([currentStepIdentifier isEqualToString:@"kLastFever"]) | |
{ | |
ORKStepResult *feverStepResult = [result stepResultForStepIdentifier:@"kFeverIdentifier"]; | |
ORKQuestionResult *feverQuestionResult = (ORKQuestionResult *)feverStepResult.firstResult; | |
if ([feverQuestionResult isKindOfClass:[ORKBooleanQuestionResult class]]) | |
{ | |
ORKBooleanQuestionResult *booleanResult = (ORKBooleanQuestionResult *)feverQuestionResult; | |
NSNumber *booleanAnswer = booleanResult.booleanAnswer; | |
if (booleanAnswer) | |
{ | |
// Too simplistic for production, but good enough for this early testing. | |
return booleanAnswer.boolValue ? self.steps[2] : self.steps[1]; | |
} | |
} | |
} | |
// Fall-through | |
return [super stepBeforeStep:step withResult:result]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment