Notes: 07. Add a Vaccination Task
Please check your local health authorities for guidance and find out when you are able to get vaccinated.
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.
So to add a vaccination task to the project, you need two pieces. One is OCKTask which you’ll add inside TaskManager; then, you’ll store it to the OCKStoreManager. Two is when the user wants to open that task and do the survey, which you add a survey by making an ORKTask inside of SurveyManager and then using it as an input of OCKSurveyTaskViewController and finally pass it to OCKDailyPageViewController.
Let’s jump to the code.
From the project navigator inside the ViewModels folder, open TaskManager, and after // Make Vaccination - CareKitTask make a Vaccination task this.
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
}
Here you just made a schedule for the Vaccination task to tell the OCKDailyPageViewController to show a task inside the calendar every day until the user gets the vaccination done.
Next, you have to add it to the OCKStoreManager, so open the SceneDelegate from the project navigator, and inside of the taskList array right after // Add Vaccination Task to the StoreManager comment, add the task that you just made:
let taskList = [TaskManager.makeOnboarding(),
TaskManager.makeVaccinationCheck()]
The next step is to make the survey, from project navigator inside the ViewModels folder, open SurveyManager, and at the bottom of the class right after // Prepare Vaccination Survey - ResearchTask comment, add a function:
static func vaccinationSurvey() -> ORKTask {
}
Inside the function add the first step of the survey:
// 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."
Here you just made an instruction step to welcome user to the survey, Then add the next step:
// 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
Here you add a QuestionStep to ask the user for the birthday. Next step is a vaccine type:
// 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
Here is the same as before, QuestionStep, but having a different type of answer, which is a single choice. Now it’s time to get the date and time of vaccination:
// 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
You add another QuestionStep to the survey. And last step is completionStep:
// 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 ])
You add a simple CompletionStep and afterward return an ORKOrderedTask with all the steps you made.
In the project navigator inside the Models folder, open TaskModels and add a new case to the TaskModel right after // Add Vaccination Task comment:
case vaccinationCheck
So when you add this case, Xcode start complaining about usage of this enum in the TaskViewModel class, so open it from the project navigator inside of ViewModels folder and fulfill the switch case by adding the .vaccinationCheck case right after // Make Vaccination Check ViewController
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)
}
}
Here you check the vaccination task state; if it’s not completed, you make a viewController as a type of OCKSurveyTaskViewController and put the vaccinationSurvey function from SurveyManager as one of the inputs.
Last step is to open TaskViewContoller and add this right after this // Ask ViewModel to Make VaccinationCheck ViewController comment:
TaskViewModel.checkIfInputTaskIsComplete(
input: .onboarding,
storeManager: storeManager) { onboardingIsComplete in
if onboardingIsComplete {
TaskViewModel.makeTaskViewController(
input: .vaccinationCheck,
date: date,
storeManager: self.storeManager,
listViewController: listViewController,
delegate: self)
}
}
You check if the onboarding task was completed then you add a vaccination check task to the list. Build and run the project to see how it looks, and just keep in mind, for now, you keep the storeManager on the memory, which means every time you run the app, the storeManager is empty, and you start fresh.