Apple Health Frameworks

Nov 29 2022 · Swift 5, iOS 15, Xcode 13

Part 2: Dive Into More Details

07. Add a Vaccination Task

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 06. Learn More About ResearchKit Next episode: 08. Create a CheckIn Task

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 07. Add a Vaccination Task

Please check your local health authorities for guidance and find out when you are able to get vaccinated.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Part 2, Episode 08, Add a vaccination task

In this episode, I want to show you how to add a new task to the OCKDailyPageViewController. You should be familiar with this architecture by now but let’s take a deep look into it and use all of the knowledge that you got by now.

  static func makeVaccinationCheck() -> OCKTask {
    let schedule = OCKSchedule.dailyAtTime(
      hour: 0,
      minutes: 0,
      start: Date(),
      end: nil,
      text: nil,
      duration: .allDay)
    var task = OCKTask(
      id: TaskModel.vaccinationCheck.rawValue,
      title: "Vaccination Task",
      carePlanUUID: nil,
      schedule: schedule)
    task.instructions =
      "Please check your local health authorities for guidance and find out when you can get the vaccination."
    task.impactsAdherence = false
    return task
  }
    let taskList = [TaskManager.makeOnboarding(),
                    TaskManager.makeVaccinationCheck()]
static func vaccinationSurvey() -> ORKTask { 

}

    // The Welcome Instruction step.
    let welcomeInstructionStep = ORKInstructionStep(identifier: IdentifierModel.vaccinationWelcome.rawValue)
    welcomeInstructionStep.image = UIImage(named: "welcome-image")
    welcomeInstructionStep.imageContentMode = .scaleAspectFill
    welcomeInstructionStep.title = "Vaccination Survey!"
    welcomeInstructionStep.detailText = "Thank you for taking the Vaccination."

    // Birthday step
    let birthdayAnswerFormat = ORKAnswerFormat.dateAnswerFormat(
      withDefaultDate: nil,
      minimumDate: nil,
      maximumDate: Date(),
      calendar: nil)
    let birthdayStep = ORKQuestionStep(
      identifier: IdentifierModel.vaccinationBirthday.rawValue,
      title: "Step 1",
      question: "When is your birthday?",
      answer: birthdayAnswerFormat)
    birthdayStep.text = "This will help us to determin better overview based on your age."
    birthdayStep.isOptional = false
    // Vaccine Type step.
    let vaccineType = [
      ORKTextChoice(
        text: "Oxford–AstraZeneca",
        value: "Oxford–AstraZeneca" as NSCoding & NSCopying & NSObjectProtocol),
      ORKTextChoice(
        text: "Moderna",
        value: "Moderna" as NSCoding & NSCopying & NSObjectProtocol),
      ORKTextChoice(
        text: "Pfizer–BioNTech",
        value: "Pfizer–BioNTech" as NSCoding & NSCopying & NSObjectProtocol),
      ORKTextChoice(
        text: "Janssen",
        value: "Janssen" as NSCoding & NSCopying & NSObjectProtocol),
      ORKTextChoiceOther.choice(
        withText: "Other",
        detailText: nil,
        value: "Other" as NSCoding & NSCopying & NSObjectProtocol,
        exclusive: true,
        textViewPlaceholderText: "enter additional information")
    ]

    let vaccineTypeAnswerFormat = ORKAnswerFormat.choiceAnswerFormat(with: .singleChoice, textChoices: vaccineType)
    let vaccineTypeStep = ORKQuestionStep(
      identifier: IdentifierModel.vaccinationType.rawValue,
      title: "Step 2",
      question: "Which Vaccince did you take?",
      answer: vaccineTypeAnswerFormat,
      learnMoreItem: nil)
    vaccineTypeStep.text = "Please choose which Vaccine did you take this time?"
    vaccineTypeStep.isOptional = false
    //    Date and Time of vaccination
    let dateAnswerFormat = ORKAnswerFormat.dateTime()
    let dateStep = ORKQuestionStep(
      identifier: IdentifierModel.vaccinationDate.rawValue,
      title: "Step 3",
      question: "When did you get the vaccine?",
      answer: dateAnswerFormat)
    dateStep.text = "Date and Time of Vaccination"
    dateStep.isOptional = false
    // Completion Step
    let completionStep = ORKCompletionStep(identifier: IdentifierModel.vaccinationCompletion.rawValue)
    completionStep.title = "Task Complete"
    completionStep.text =
      "Thank you for taking the Vaccince. Now you can see more details in the app as well as followup tasks."
    return ORKOrderedTask(
      identifier: IdentifierModel.vaccinationStep.rawValue,
      steps: [ welcomeInstructionStep, birthdayStep, vaccineTypeStep, dateStep, completionStep ])
  case vaccinationCheck
    case .vaccinationCheck:
      TaskViewModel.checkIfInputTaskIsComplete(input: input, storeManager: storeManager) { isComplete in
        if !isComplete {
          let viewController = OCKSurveyTaskViewController(
            taskID: input.rawValue,
            eventQuery: OCKEventQuery(for: date),
            storeManager: storeManager,
            survey: SurveyManager.vaccinationSurvey(),
            extractOutcome: { _ in return [OCKOutcomeValue(Date())] })
          viewController.surveyDelegate = delegate
          listViewController.appendViewController(viewController, animated: false)
        }
      }
TaskViewModel.checkIfInputTaskIsComplete(
      input: .onboarding,
      storeManager: storeManager) { onboardingIsComplete in
      if onboardingIsComplete {
        TaskViewModel.makeTaskViewController(
          input: .vaccinationCheck,
          date: date,
          storeManager: self.storeManager,
          listViewController: listViewController,
          delegate: self)
      }
    }