diff --git a/CareKit Sample/CKCareKitManager+Sample.swift b/CareKit Sample/CKCareKitManager+Sample.swift index e79b45c..48bbc20 100644 --- a/CareKit Sample/CKCareKitManager+Sample.swift +++ b/CareKit Sample/CKCareKitManager+Sample.swift @@ -78,7 +78,23 @@ internal extension OCKStore { You can modify this text with any instructions of your choice! """ - addTasks([meds, rehab], callbackQueue: .main) { result in + /** + * This is not an intended feature of CareKit out of the box, but it lets us group medications. + * Please study this implementation thoroughly before using it out of the box. + */ + let groupedMedications = ["Acetaminophen", "Adderall", "Amitriptyline", "Amlodipine", "Amoxicillin"] + var scheduleItems = [OCKSchedule]() + for med in groupedMedications { + scheduleItems.append(OCKSchedule.dailyAtTime(hour: 8, minutes: 0, start: startOfWeek1, end: endOfWeek1, text: med)) + } + let groupedSchedule = OCKSchedule(composing: scheduleItems) + let groupedInstruction = "This task is for \(scheduleItems.count) medication(s)" + + var morningGroup = OCKTask(id: "morning-group", title: "Morning Pills", carePlanUUID: nil, schedule: groupedSchedule) + morningGroup.instructions = groupedInstruction + + + addTasks([meds, rehab, morningGroup], callbackQueue: .main) { result in switch result { case let .success(tasks): print("Added \(tasks.count) tasks") diff --git a/CareKit Sample/CKCareKitManager.swift b/CareKit Sample/CKCareKitManager.swift index bc6f8b4..1d8970f 100644 --- a/CareKit Sample/CKCareKitManager.swift +++ b/CareKit Sample/CKCareKitManager.swift @@ -25,7 +25,7 @@ class CKCareKitManager: NSObject { override init() { super.init() - coreDataStore = OCKStore(name: "CKSampleCareKitStore", securityApplicationGroupIdentifier: nil, type: coreDataStoreType, remote: CKCareKitRemoteSyncWithFirestore()) + coreDataStore = OCKStore(name: "CKSampleCareKitStore", securityApplicationGroupIdentifier: nil, type: coreDataStoreType/*, remote: CKCareKitRemoteSyncWithFirestore()*/) initStore(forceUpdate: coreDataStoreType == .inMemory) diff --git a/CareKit Sample/Tabs/Schedule/ScheduleViewController.swift b/CareKit Sample/Tabs/Schedule/ScheduleViewController.swift index 44b9ced..81123b4 100644 --- a/CareKit Sample/Tabs/Schedule/ScheduleViewController.swift +++ b/CareKit Sample/Tabs/Schedule/ScheduleViewController.swift @@ -16,6 +16,8 @@ class ScheduleViewController: OCKDailyPageViewController { override func viewDidLoad() { super.viewDidLoad() title = "Schedule" + + navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Print Outcomes", style: .plain, target: self, action: #selector(printOutcomes)) } override func dailyPageViewController(_ dailyPageViewController: OCKDailyPageViewController, prepare listViewController: OCKListViewController, for date: Date) { @@ -28,6 +30,10 @@ class ScheduleViewController: OCKDailyPageViewController { let rehabViewController = OCKGridTaskViewController(taskID: "rehab", eventQuery: OCKEventQuery(for: date), storeManager: storeManager) listViewController.appendViewController(rehabViewController, animated: true) + // Morning Group + let morningController = OCKChecklistTaskViewController(taskID: "morning-group", eventQuery: OCKEventQuery(for: date), storeManager: storeManager) + listViewController.appendViewController(morningController, animated: true) + // Charts let rehabConfig = OCKDataSeriesConfiguration(taskID: "rehab", legendTitle: "Rehab", gradientStartColor: .systemGray, gradientEndColor: .systemGray, markerSize: 6, eventAggregator: .countOutcomeValues) let rehabCharts = OCKCartesianChartViewController(plotType: .bar, selectedDate: date, configurations: [rehabConfig], storeManager: storeManager) @@ -46,6 +52,52 @@ class ScheduleViewController: OCKDailyPageViewController { } +extension ScheduleViewController { + + @objc fileprivate func printOutcomes() { + let query = OCKOutcomeQuery() + storeManager.store.fetchAnyOutcomes(query: query, callbackQueue: .main) { (result) in + switch result { + case .failure(let error): + print(error) + case .success(let outcomes): + for anyOutcome in outcomes { + let outcome = anyOutcome as! OCKOutcome + print("Outcome for task \(outcome.taskUUID) with index \(outcome.taskOccurrenceIndex) and value \(outcome.values)") + self.printTask(byId: outcome.taskUUID, withIndex: outcome.taskOccurrenceIndex) + } + } + } + + } + + fileprivate func printTask(byId id: UUID, withIndex index: Int) { + var query = OCKTaskQuery() + query.uuids = [id] + + storeManager.store.fetchAnyTasks(query: query, callbackQueue: .main) { (result) in + switch result { + case .failure(let error): + print(error) + case .success(let tasks): + for task in tasks { + print("------start task") + print(task.id) + print(task.title ?? "no title") + print(task.instructions ?? "no instructions") + print("scheduled:") + for (i, element) in task.schedule.elements.enumerated() { + print("\(index == i ? "[X]": "")" + "\t" + (element.text ?? "")) + } + print("------end task") + } + } + } + + } + +} + private extension View { func formattedHostingController() -> UIHostingController { let viewController = UIHostingController(rootView: self)