Skip to content

Instantly share code, notes, and snippets.

@bishalg
Created May 12, 2016 12:40
Show Gist options
  • Save bishalg/665c820b6f8953d8b606acf9c999dafc to your computer and use it in GitHub Desktop.
Save bishalg/665c820b6f8953d8b606acf9c999dafc to your computer and use it in GitHub Desktop.
Navigable Ordered Task in swift example of ORKNavigableOrderedTask ResearchKit
// Created by Bishal Ghimire on 5/6/16.
// Copyright © 2016 Bishal Ghimire. All rights reserved.
import Foundation
import ResearchKit
class SurveyQuestion {
var task: ORKNavigableOrderedTask?
let choiceGood = ORKTextChoice(text: "Good", value: 1)
let choiceFair = ORKTextChoice(text: "Fair", value: 2)
let choicePoor = ORKTextChoice(text: "Poor", value: 3)
func answerGoodFairPoor() -> [ORKTextChoice] {
return [choiceGood, choiceFair, choicePoor]
}
func makeNavigableLoopTask() {
var steps = [ORKStep]()
var answerFormat: ORKAnswerFormat
var step: ORKStep
var textChoices: [ORKTextChoice]
var questionStep: ORKQuestionStep
// Intro Step
step = ORKInstructionStep(identifier: "introStep")
step.title = "This is demo of loop navigalbe ordered task"
steps.append(step)
// Loop target step
step = ORKInstructionStep(identifier: "loopAStep")
step.title = "You will return to this step"
steps.append(step)
// Branching paths
textChoices = [
ORKTextChoice(text: "Scale", value: "scale"),
ORKTextChoice(text: "Text Choice", value: "textchoice")
]
answerFormat = ORKTextChoiceAnswerFormat(style: .SingleChoice, textChoices: textChoices)
questionStep = ORKQuestionStep(identifier: "branchingStpe", title: "Which question do you prefer ?", answer: answerFormat)
questionStep.optional = false
steps.append(questionStep)
// Scale Question Step
let scaleAnserFormat = ORKAnswerFormat.continuousScaleAnswerFormatWithMaximumValue(10, minimumValue: 1, defaultValue: 8, maximumFractionDigits: 3, vertical: true, maximumValueDescription: "This is MAX", minimumValueDescription: "This is MIN")
step = ORKQuestionStep.init(identifier: "scleStep", title: "On a scale of 1 to 10, what is your mood?", answer: scaleAnserFormat)
steps.append(step)
// Text Choice question Step
textChoices = [
ORKTextChoice(text: "Good", value: "good"),
ORKTextChoice(text: "Bad", value: "bad")
]
answerFormat = ORKAnswerFormat.choiceAnswerFormatWithStyle(.SingleChoice, textChoices: textChoices)
questionStep = ORKQuestionStep(identifier: "textChoiceStep", title: "How is your mood?", answer: answerFormat)
questionStep.optional = false
steps.append(questionStep)
// Loop conditional step
answerFormat = ORKAnswerFormat.booleanAnswerFormat()
step = ORKQuestionStep(identifier: "loopBStep", title: "Do you want to repeat the survey ?", answer: answerFormat)
step.optional = false
steps.append(step)
step = ORKInstructionStep(identifier: "endStep")
step.title = "You have finished the task"
steps.append(step)
// Build Navigation Rules
let resultSelector: ORKResultSelector
let predicateRule: ORKPredicateStepNavigationRule
let directRule: ORKDirectStepNavigationRule
// From the branching step, go to either sclaeStep or textChoiceStep
resultSelector = ORKResultSelector(resultIdentifier: "branchingStep")
let predicateAnswerType = ORKResultPredicate.predicateForChoiceQuestionResultWithResultSelector(resultSelector, expectedAnswerValue: "scale")
predicateRule = ORKPredicateStepNavigationRule(resultPredicates: [predicateAnswerType],
destinationStepIdentifiers: ["scaleStep"],
defaultStepIdentifier: "textChoiceStep",
validateArrays: true)
task?.setNavigationRule(predicateRule, forTriggerStepIdentifier: "branchingStep")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment