Apple Health Frameworks

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

Part 3: Take Advantage of CareKit & ResearchKit

10. Make a Follow-Up 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: 09. Work With StoreManager Next episode: 11. Make an ActiveTask Using ResearchKit.

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: 10. Make a Follow-Up Vaccination Task

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

Transcript: 10. Make a Follow-Up Vaccination Task

Part 3, Episode 11, Make a follow-up Vaccination Task

In this episode, I want to show you how to add a new task by using an output of other tasks, for example, extracting the date of the first dose of vaccine and adding the follow-up reminder to inject the second dose after a month.

First thing is to open TaskModel from the project navigator and add a new case right after // Add secondVaccinationCheck Task comment.

  case secondVaccinationCheck

Next is to make a schedule for it by creating an OCKTask, Open TaskManager, and add this function right after // Make Next Vaccination - CareKitTask comment.

  static func makeNextVaccination(date: Date) -> OCKTask {
    let thisMorning = Calendar.current.startOfDay(for: date)
    let nextMonth = Calendar.current.date(
      byAdding: .month,
      value: 1,
      to: thisMorning)
    let schedule = OCKSchedule.dailyAtTime(
      hour: 0,
      minutes: 0,
      start: nextMonth ?? Date(),
      end: nil,
      text: nil,
      duration: .allDay)
    var task = OCKTask(
      id: TaskModel.secondVaccinationCheck.rawValue,
      title: "Vaccination Task",
      carePlanUUID: nil,
      schedule: schedule)
    task.instructions =
      "Please check your local health authorities for guidance and find out when you are able to get a vaccination."
    task.impactsAdherence = false
    return task
  }

Here you create a new OCKTask by making a schedule by adding one month to the input date.

Next is to open TaskViewModel and add a new case to do the same thing as the first VaccinationCheck does right after the // Add SecondVaccinationCheck Case comment.

    case .vaccinationCheck, .secondVaccinationCheck:

So when you initiate an OCKSurveyTaskViewController, there are different inputs available that you should fill up; one of them is extractOutcome; You have access to the survey result by this completion handler.

Let’s use the date of the Vaccination to be able to plan for the next vaccination, in the TaskViewModel in the VaccinationCheck Case, add these codes inside of the extractOutcome:

{ result in
              if let survey = result.results?.first(where: { $0.identifier == "vaccination.date" }) as? ORKStepResult,
              let dateResult = survey.results?.first as? ORKDateQuestionResult,
              let answer = dateResult.dateAnswer {
                TaskViewModel.prepareTasks(
                  storeManager: storeManager,
                  tasksList: [
                    TaskManager.makeNextVaccination(date: answer)
                  ])
              }
              return [OCKOutcomeValue(Date())] }

Here you get the survey and find the date out of the result, and at the end, you add a new task to the StoreManager to make the next vaccination by using the date that you just found.

Last step is to open TaskViewController and add the SecondVaccinationCheck to a valid id to be shown after the first vaccination was done.

Modify the condition right after // Add SecondVaccination id comment like this:

if id == TaskModel.checkIn || id == TaskModel.secondVaccinationCheck

Here you add the secondVaccinationCheck to be shown if it’s valid for a specific date. Make sure that before you build and run the app, you delete it because there was no scheduling for the second vaccination til now.

Build and run and go through all the steps and after vaccination, navigate to next month to see there are two tasks available for that point, CheckIn and Second Vaccination.