Apple CareKit symptom code in swift doesn't save data to CareStore

167 Views Asked by At

The following code was working partially. It display the question step and form. User was able to enter data, but when user click done nothing get save to care store and the display is not updated. Any idea?

class SymptomsVC1: UIViewController{


fileprivate let carePlanStoreManager = CarePlanStoreManager1.sharedCarePlanStoreManager
fileprivate let carePlanData: CarePlanData
fileprivate var symptomTrackerViewController: OCKSymptomTrackerViewController? = nil



required init?(coder aDecoder: NSCoder) {
    carePlanData = CarePlanData(carePlanStore: carePlanStoreManager.store)

    super.init(coder: aDecoder)

}



override func viewDidLoad() {
    super.viewDidLoad()
    setViewControllerTitle(self, title: "Symptoms Card")
    //creatMenuObject(self)


    let symptomTracker = OCKSymptomTrackerViewController.init(carePlanStore: carePlanStoreManager.store)
    symptomTracker.progressRingTintColor = UIColor.magenta
    symptomTracker.delegate = self
    symptomTracker.showEdgeIndicators = true
    // Setup the controller's title
    symptomTracker.title = NSLocalizedString("Symptoms Card", comment: "")

    //change left navigation "Back" button to menu button
    var backImage:UIImage = UIImage(named: "menu")!
    backImage = backImage.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
    let fakeBackButton = UIBarButtonItem(image: backImage, style: UIBarButtonItemStyle.bordered, target: symptomTracker.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)))
    symptomTracker.navigationItem.leftBarButtonItem = fakeBackButton;

    self.navigationController?.pushViewController(symptomTracker, animated: true)


}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


extension SymptomsVC1: OCKSymptomTrackerViewControllerDelegate {

  func symptomTrackerViewController(_ viewController:    OCKSymptomTrackerViewController, didSelectRowWithAssessmentEvent assessmentEvent: OCKCarePlanEvent) {

if viewController.progressRingTintColor == UIColor.magenta {

    guard let userInfo = assessmentEvent.activity.userInfo,
        let task: ORKTask = userInfo["ORKTask"] as? ORKTask else { return }

    let taskViewController = ORKTaskViewController(task: task, taskRun: nil)
    taskViewController.delegate = self

    present(taskViewController, animated: true, completion: nil)


     }
  }
}

extension SymptomsVC1: ORKTaskViewControllerDelegate {

func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith
reason: ORKTaskViewControllerFinishReason, error: Error?) {

defer {
    dismiss(animated: true, completion: nil)
}
print("task view controller clicked")
guard reason == .completed else { return }
guard let symptomTrackerViewController = symptomTrackerViewController,
      let event = symptomTrackerViewController.lastSelectedAssessmentEvent else { return }

let carePlanResult = carePlanStoreManager.buildCarePlanResultFrom(taskResult: taskViewController.result)
print("care plan result")
print(carePlanResult)
carePlanStoreManager.store.update(event, with: carePlanResult, state: .completed) {
    success, _, error in
    if !success {
        print(error?.localizedDescription)
    }
  }
 }
}
1

There are 1 best solutions below

0
On

I found the fix by adding one line of code in the following function :

override func viewDidLoad() {
super.viewDidLoad()
setViewControllerTitle(self, title: "Symptoms Card")
//creatMenuObject(self)


let symptomTracker = OCKSymptomTrackerViewController.init(carePlanStore: carePlanStoreManager.store)
symptomTracker.progressRingTintColor = UIColor.magenta
symptomTracker.delegate = self
symptomTracker.showEdgeIndicators = true
// Setup the controller's title
symptomTracker.title = NSLocalizedString("Symptoms Card", comment: "")

//*** add the following line, now the result show up in carestore ***
symptomTrackerViewController = symptomTracker 


//change left navigation "Back" button to menu button
var backImage:UIImage = UIImage(named: "menu")!
backImage = backImage.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
let fakeBackButton = UIBarButtonItem(image: backImage, style: UIBarButtonItemStyle.bordered, target: symptomTracker.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)))
symptomTracker.navigationItem.leftBarButtonItem = fakeBackButton;

self.navigationController?.pushViewController(symptomTracker, animated: true)

}