Skip to content
This repository was archived by the owner on Dec 22, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion CareKit Sample/CKCareKitManager+Sample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion CareKit Sample/CKCareKitManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
52 changes: 52 additions & 0 deletions CareKit Sample/Tabs/Schedule/ScheduleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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<Self> {
let viewController = UIHostingController(rootView: self)
Expand Down