-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathQuestionnaireController.swift
155 lines (127 loc) · 5.32 KB
/
QuestionnaireController.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
//
// QuestionnaireController.swift
// C3PRO
//
// Created by Pascal Pfiffner on 5/20/15.
// Copyright © 2015 Boston Children's Hospital. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import ResearchKit
import SMART
/**
Instances of this class can prepare questionnaires and get a callback when preparation has finished.
Use `prepareQuestionnaireViewController()`, which fulfills any questionnaire dependencies before calling the callback, in which you get a
handle to a `ORKTaskViewController` view controller that you can present on the UI.
The `whenCompleted` callback is called when the user completes the questionnaire without cancelling nor error and provides the responses in
a `QuestionnaireResponse` resource.
The `whenCancelledOrFailed` callback is called when the questionnaire is cancelled (error = nil) or finishes with an error.
See [Questionnaire/README.md](https://github.com/C3-PRO/c3-pro-ios-framework/tree/master/Sources/Questionnaire#questionnairecontroller) for detailed instructions.
*/
open class QuestionnaireController: NSObject, ORKTaskViewControllerDelegate {
/// The questionnaire the controller represents.
public final var questionnaire: Questionnaire?
/// Callback called when the user finishes the questionnaire without error.
public final var whenCompleted: ((_ viewController: ORKTaskViewController, _ answers: QuestionnaireResponse?) -> Void)?
/// Callback to be called when the questionnaire is cancelled (Error = nil) or finishes with an error.
public final var whenCancelledOrFailed: ((ORKTaskViewController, Error?) -> Void)?
/// The logger to use, if any.
open var logger: OAuth2Logger?
/**
Designated initializer.
- parameter questionnaire: The `Questionnaire` the receiver should handle
*/
public init(questionnaire: Questionnaire) {
self.questionnaire = questionnaire
}
// MARK: - Questionnaire
/**
Attempts to fulfill the promise, calling the callback when done, either with a task representing the questionnaire or an error.
- parameter callback: The callback once preparation has concluded, either with an ORKTask or an error. Called on the main queue.
*/
func prepareQuestionnaire(callback: @escaping ((ORKTask?, Error?) -> Void)) {
if let questionnaire = questionnaire {
logger?.trace("C3-PRO", msg: "Fulfilling promise for \(questionnaire)")
let promise = QuestionnairePromise(questionnaire: questionnaire)
promise.fulfill(requiring: nil) { errors in
DispatchQueue.main.async {
var multiErrors: Error?
if let errs = errors {
multiErrors = C3Error.multipleErrors(errs)
}
if let tsk = promise.task {
if let errors = multiErrors {
self.logger?.debug("C3-PRO", msg: "Successfully prepared questionnaire but encountered errors:\n\(errors)")
}
self.logger?.trace("C3-PRO", msg: "Promise for \(questionnaire) fulfilled")
callback(tsk, multiErrors)
}
else {
let err = multiErrors ?? C3Error.questionnaireUnknownError
self.logger?.trace("C3-PRO", msg: "Promise for \(questionnaire) fulfilled with error \(err)")
callback(nil, err)
}
}
}
}
else {
callOnMainThread {
callback(nil, C3Error.questionnaireNotPresent)
}
}
}
/**
Attempts to fulfill the promise, calling the callback when done.
- parameter callback: Callback to be called on the main queue, either with a task view controller prepared for the questionnaire task or an
error
*/
open func prepareQuestionnaireViewController(callback: @escaping ((ORKTaskViewController?, Error?) -> Void)) {
prepareQuestionnaire() { task, error in
if let task = task {
let viewController = ORKTaskViewController(task: task, taskRun: nil)
viewController.delegate = self
callback(viewController, error)
}
else {
callback(nil, error)
}
}
}
// MARK: - Task View Controller Delegate
open func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith reason: ORKTaskViewControllerFinishReason, error: Error?) {
if let error = error {
didFailWithError(taskViewController, error: error)
}
else {
didFinish(taskViewController, reason: reason)
}
}
// MARK: - Questionnaire Answers
func didFinish(_ viewController: ORKTaskViewController, reason: ORKTaskViewControllerFinishReason) {
switch reason {
case .failed:
didFailWithError(viewController, error: C3Error.questionnaireFinishedWithError)
case .completed:
whenCompleted?(viewController, viewController.result.c3_asQuestionnaireResponse(for: viewController.task))
case .discarded:
didFailWithError(viewController, error: nil)
case .saved:
// TODO: support saving tasks
didFailWithError(viewController, error: nil)
}
}
func didFailWithError(_ viewController: ORKTaskViewController, error: Error?) {
whenCancelledOrFailed?(viewController, error)
}
}