-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSessionController.swift
319 lines (239 loc) · 10 KB
/
SessionController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//
// SessionController.swift
// PPMG
//
// Created by raheel on 4/4/22.
//
import Foundation
import ResearchKit
import UIKit
import SMART
/**
`SessionController` for Multiple Tasks
SessionController is to create a single unified user session to generate data from multiple tasks. Can also submit generated FHIR Bundles to the `FHIR Server` for a given `Patient`
*/
open class SessionController {
/// Session identifier; a UUID string
public let identifier: String
/// Task controllers
public internal(set) var tasks: [TaskController]
/// `Patient` for which a session is created; optional
public internal(set) var patient: Patient?
/// `FHIR Server` for submission of the results
public internal(set) var server: Server?
/// Callback to call if a task has been cancelled
public var onCancellation: ((_ task: TaskController) -> Void)?
/// Callback to call when all tasks have concluded
public var onConclusion: ((_ sessionResult: StudyTaskResult) -> Void)?
/// LearnMore steps color
public var learnMoreStepColor: UIColor?
/**
Designated Initializer
- parameter tasks: Array of `TaskControllers` that need to be administered
- parameter patient: optional `Patient`; needed for submission to `FHIR Server`
- parameter server: optional `Server`; needed for submission to `FHIR Server`
*/
public init(_ tasks: [TaskController], patient: Patient?, server: Server?) {
identifier = UUID().uuidString
self.tasks = tasks
self.patient = patient
self.server = server
}
/**
Prepares a `SessionViewController with all child `ORKTaskViewControllers`
*/
open func prepareController(callback: @escaping ((_ controller: SessionViewController?, _ error: Error?) -> Void)) {
var taskViews = [ORKTaskViewController]()
var errors = [Error]()
let sem = DispatchSemaphore(value: 0)
for (i, task) in self.tasks.enumerated() {
smLog("[Session]\(i)---\(task.instrument?.sm_title ?? "")")
task.prepareSession { (taskViewController, error) in
smLog("[Session]------\(taskViewController?.task?.description ?? "")")
if let tvc = taskViewController {
taskViews.append(tvc)
}
if let error = error {
errors.append(error)
}
sem.signal()
}
sem.wait()
}
if taskViews.count > 0 {
var i = 0
taskViews.forEach({ $0.view.tag = i; i += 1; })
if server != nil, patient != nil {
// TODO: Add a Submission Task
}
let sessionView = SessionViewController(viewControllers: taskViews, for: self)
smLog(errors)
callback(sessionView, errors.isEmpty ? nil : errors.first)
}
else {
callback(nil, errors.first)
}
}
}
open class SessionViewController: UIViewController {
private var pages: UIPageViewController!
private var pageIndices: [Int]
private var taskResults: [Int: ORKTaskResult]
private var currentPageIndex: Int
public internal(set) var taskViewControllers: [ORKTaskViewController]
unowned var session: SessionController!
init(viewControllers: [ORKTaskViewController], for session: SessionController) {
self.taskViewControllers = viewControllers
pageIndices = [Int]()
currentPageIndex = NSNotFound
taskResults = [Int: ORKTaskResult]()
self.session = session
super.init(nibName: nil, bundle: nil)
taskViewControllers.forEach{ $0.delegate = self }
}
func taskDidChange() {
if !isViewLoaded { return }
currentPageIndex = NSNotFound
self.goto(taskIndex: 0, animated: false)
}
func goto(taskIndex: Int, animated: Bool) {
guard let taskController = taskViewController(index: taskIndex) else {
smLog("No view Controller")
return
}
var animated = animated
let currentTask = currentPageIndex
if currentTask == NSNotFound {
animated = false
}
let direction: UIPageViewController.NavigationDirection = (!animated || taskIndex > currentTask) ? .forward : .reverse
currentPageIndex = taskIndex
pages.setViewControllers([taskController], direction: direction, animated: animated) { finished in
if finished {
UIAccessibility.post(notification: .screenChanged, argument: nil)
}
}
}
func updateBackButton() {
smLog("update back button due \(currentPageIndex)")
let task = pages.viewControllers!.first as! ORKTaskViewController
task.currentStepViewController?.backButtonItem = self.backButtonItem()
}
func taskViewController(index: Int) -> UIViewController? {
if index >= taskViewControllers.count { return nil }
return taskViewControllers[index]
}
func navigate(delta: Int) -> Bool {
let pageCount = taskViewControllers.count
if (currentPageIndex == 0 && delta < 0) {
return false
}
else if (currentPageIndex >= (pageCount - 1) && delta > 0) {
return false
}
else {
self.goto(taskIndex: (currentPageIndex + delta), animated: true)
return true
}
}
@discardableResult
func goForward() -> Bool {
navigate(delta: 1)
}
@discardableResult
func goBackword() -> Bool {
navigate(delta: -1)
}
open override func viewDidLoad() {
super.viewDidLoad()
pages = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
pages.view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
pages.view.frame = self.view.bounds
self.view.addSubview(pages.view)
self.addChild(pages)
pages.didMove(toParent: self)
taskDidChange()
}
open override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
required public init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func backButtonItem() -> UIBarButtonItem {
let img = UIImage(named: "arrowLeft")
let back = UIBarButtonItem(image: img, style: .plain, target: self, action: #selector(backButtonTapped(_:)))
return back
}
@objc
func backButtonTapped(_ sender: Any?) {
smLog("back item pressed")
goBackword()
}
@objc
func cancelButtonTapped(_ sender: Any?) {
dismiss(animated: true, completion: nil)
}
}
extension SessionViewController: ORKTaskViewControllerDelegate {
func findTask(with taskView: ORKTaskViewController) -> TaskController {
let tag = taskView.view.tag
let task = session.tasks[tag]
return task
}
public func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith reason: ORKTaskViewControllerFinishReason, error: Error?) {
let sessionId = taskViewController.taskRunUUID.uuidString
var instrumentResults = [InstrumentResult]()
weak var ss = session
if reason == .discarded {
let index = taskViewController.view.tag
taskViewControllers[0...index].forEach { taskView in
let report = findTask(with: taskView)
.generateReports(
from: taskView,
result: taskView.result,
didFinishWith: .discarded,
error: error
)
instrumentResults.append(report)
}
dismiss(animated: true) {
ss?.onConclusion?(StudyTaskResult(sessionId: sessionId, result: instrumentResults))
}
}
else {
if goForward() == false {
// Session Ended with success, dismiss and grab all data
taskViewControllers.forEach { taskView in
let report = findTask(with: taskView)
.generateReports(
from: taskView,
result: taskView.result,
didFinishWith: .completed,
error: error
)
instrumentResults.append(report)
}
dismiss(animated: true) {
ss?.onConclusion?(StudyTaskResult(sessionId: sessionId, result: instrumentResults))
}
}
}
}
public func taskViewController(_ taskViewController: ORKTaskViewController, stepViewControllerWillAppear stepViewController: ORKStepViewController) {
let previous = taskViewController.stepViewControllerHasPreviousStep(stepViewController)
if false == previous && currentPageIndex > 0 {
stepViewController.backButtonItem = self.backButtonItem()
}
let hasNextStep = taskViewController.stepViewControllerHasNextStep(stepViewController)
if true == hasNextStep || currentPageIndex < (taskViewControllers.count - 1) {
stepViewController.continueButtonTitle = "Next"
}
}
public func taskViewController(_ taskViewController: ORKTaskViewController, learnMoreButtonPressedWith learnMoreStep: ORKLearnMoreInstructionStep, for stepViewController: ORKStepViewController) {
let clss = learnMoreStep.instantiateStepViewController(with: ORKResult())
let nav = UINavigationController(rootViewController: clss)
nav.view.tintColor = session.learnMoreStepColor
stepViewController.present(nav, animated: true, completion: nil)
}
}