Apple Health Frameworks

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

Part 2: Dive Into More Details

08. Create a CheckIn 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: 07. Add a Vaccination Task Next episode: 09. Work With StoreManager

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.

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

Part 2, Episode 09, Create CheckIn task

In this episode, I want to show you how to make a daily Symptom & Measurement Tracker. You should be familiar with the flow by now. So let’s begin.

  static func makeCheckin() -> OCKTask {
    let schedule = OCKSchedule.dailyAtTime(
      hour: 8,
      minutes: 0,
      start: Date(),
      end: nil,
      text: nil
    )
    let task = OCKTask(
      id: TaskModel.checkIn.rawValue,
      title: "Check In",
      carePlanUUID: nil,
      schedule: schedule
    )
    return task
  }
    let taskList = [TaskManager.makeOnboarding(),
                    TaskManager.makeVaccinationCheck(),
                    TaskManager.makeCheckin()]
  private static var musclePainItem: ORKFormItem {
    let musclePainAnswerFormat = ORKAnswerFormat.scale(
      withMaximumValue: 10,
      minimumValue: 1,
      defaultValue: 5,
      step: 1,
      vertical: false,
      maximumValueDescription: "Very painful",
      minimumValueDescription: "No pain")
    let musclePainItem = ORKFormItem(
      identifier: IdentifierModel.checkinMuscle.rawValue,
      text: "How would you rate your muscle pain?",
      answerFormat: musclePainAnswerFormat)
    musclePainItem.isOptional = false
    return musclePainItem
  }

  private static var headacheItem: ORKFormItem {
    let headacheAnswerFormat = ORKAnswerFormat.scale(
      withMaximumValue: 10,
      minimumValue: 1,
      defaultValue: 5,
      step: 1,
      vertical: false,
      maximumValueDescription: "Very painful",
      minimumValueDescription: "No pain")
    let headacheItem = ORKFormItem(
      identifier: IdentifierModel.checkinHeadache.rawValue,
      text: "How would you rate your headache?",
      answerFormat: headacheAnswerFormat)
    headacheItem.isOptional = false
    return headacheItem
  }
  private static var tirednessItem: ORKFormItem {
    let tirednessAnswerFormat = ORKAnswerFormat.scale(
      withMaximumValue: 10,
      minimumValue: 0,
      defaultValue: 5,
      step: 1,
      vertical: false,
      maximumValueDescription: nil,
      minimumValueDescription: nil)
    let tirednessItem = ORKFormItem(
      identifier: IdentifierModel.checkinTiredness.rawValue,
      text: "How would you rate your tiredness?",
      answerFormat: tirednessAnswerFormat)
    tirednessItem.isOptional = false
    return tirednessItem
  }
  private static var feverItem: ORKFormItem {
    let feverAnswerFormat = ORKAnswerFormat.continuousScale(
      withMaximumValue: 42,
      minimumValue: 35,
      defaultValue: 37,
      maximumFractionDigits: 2,
      vertical: true,
      maximumValueDescription: "°C",
      minimumValueDescription: "°C")
    let feverItem = ORKFormItem(
      identifier: IdentifierModel.checkinFever.rawValue,
      text: "What is your body temprature?",
      answerFormat: feverAnswerFormat)
    feverItem.isOptional = false
    return feverItem
  }
  private static var nauseaItem: ORKFormItem {
    let nauseaAnswerFormat = ORKAnswerFormat.scale(
      withMaximumValue: 5,
      minimumValue: 0,
      defaultValue: 2,
      step: 1,
      vertical: false,
      maximumValueDescription: nil,
      minimumValueDescription: nil)
    let nauseaItem = ORKFormItem(
      identifier: IdentifierModel.checkinNausea.rawValue,
      text: "How would you rate your nausea?",
      answerFormat: nauseaAnswerFormat)
    nauseaItem.isOptional = true
    return nauseaItem
  }
  static func checkInSurvey() -> ORKTask {
    let formStep = ORKFormStep(
      identifier: IdentifierModel.checkinForm.rawValue,
      title: "Check In",
      text: "Please answer the following questions.")
    formStep.formItems = [tirednessItem, headacheItem, musclePainItem, feverItem, nauseaItem]
    formStep.isOptional = false
    let surveyTask = ORKOrderedTask(identifier: IdentifierModel.checkinStep.rawValue, steps: [formStep])
    return surveyTask
  }
  case checkIn
    case .checkIn:
      let viewController = OCKSurveyTaskViewController(
        taskID: TaskModel.checkIn.rawValue,
        eventQuery: OCKEventQuery(for: date),
        storeManager: storeManager,
        survey: SurveyManager.checkInSurvey(),
        extractOutcome: { _ in return [OCKOutcomeValue(Date())] })
      viewController.surveyDelegate = delegate

      listViewController.appendViewController(viewController, animated: false)
    }
    // Check the Date for future blocker
    let isFuture = Calendar.current.compare(date, to: Date(), toGranularity: .day) == .orderedDescending
          viewController.view.isUserInteractionEnabled = !isFuture
          viewController.view.alpha = isFuture ? 0.4 : 1.0
TaskViewModel.checkIfInputTaskIsComplete(
      input: .vaccinationCheck,
      storeManager: self.storeManager) { vaccinationIsComplete in
        if vaccinationIsComplete {
          TaskViewModel.makeTaskViewController(
            input: .checkIn,
            date: date,
            storeManager: self.storeManager,
            listViewController: listViewController,
            delegate: self)
        }
      }