-
Notifications
You must be signed in to change notification settings - Fork 3
/
SubmissionStep.swift
148 lines (110 loc) · 4.5 KB
/
SubmissionStep.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
//
// SubmissionStep.swift
// SMARTMarkers
//
// Created by Raheel Sayeed on 9/18/19.
// Copyright © 2019 Boston Children's Hospital. All rights reserved.
//
import Foundation
import ResearchKit
import SMART
open class SMSubmissionPermitStep: ORKFormStep {
public override init(identifier: String) {
super.init(identifier: identifier)
self.title = "Report Submission"
self.text = "New reports have been generated. Please select the ones to be generated.\nCaution: Unselected reports will be discarded."
self.isOptional = false
}
required public init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
open class SMSubmissionServerNotice: ORKQuestionStep {
public override init(identifier: String) {
super.init(identifier: identifier)
self.title = "Report Submission"
self.question = "Proceed?"
self.answerFormat = ORKBooleanAnswerFormat()
self.isOptional = false
}
required public init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class SMSubmissionInProgressStep: ORKWaitStep {
override init(identifier: String) {
super.init(identifier: identifier)
self.title = "Submitting"
self.text = "Please wait\nData is being submitted..."
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
open override func stepViewControllerClass() -> AnyClass {
return SMSubmissionInProgressStepVeiwController.self
}
}
class SMSubmissionInProgressStepVeiwController: ORKWaitStepViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
updateText("Please wait\nData is being submitted...")
let submissionTask = taskViewController!.task as! SubmissionTask
let session = submissionTask.session
// Get the selected reports to submit from the review-step
if let selected = taskViewController?.result.stepResult(forStepIdentifier: kSM_Submission_Review) {
// Get selected tasks
let selectedTasksIdentifiers = selected.results?.compactMap({ (result) -> String? in
let result = result as! ORKChoiceQuestionResult
if let taskIds = result.answer as? [String] {
return taskIds.first
}
return nil
})
//
_ = session.tasks.compactMap { (taskController) -> [InstrumentResult]? in
var toSubmit = [InstrumentResult]()
selectedTasksIdentifiers?.forEach({ (taskId) in
if let sb = taskController.reports?.instrumentResult(for: taskId) {
sb.canSubmit = true
toSubmit.append(sb)
}
})
return toSubmit.isEmpty ? nil : toSubmit
}
let group = DispatchGroup()
var submissionErrors = [Error]()
for taskController in session.tasks {
group.enter()
taskController.reports!.submit(to: session.server!, patient: session.patient!, request: taskController.request) { (success, errors) in
if let errors = errors {
submissionErrors.append(contentsOf: errors)
}
group.leave()
}
}
group.notify(queue: .main) {
let success = submissionErrors.isEmpty
let submissionResult = ORKBooleanQuestionResult(identifier: kSM_Submission_Result)
submissionResult.booleanAnswer = success ? 1 : 0
self.addResult(submissionResult)
self.goForward()
}
}
}
}
open class SMSubmissionConclusionStep: ORKCompletionStep {
public override var allowsBackNavigation: Bool {
return false
}
}
open class SMSubmissionErrorNotice: ORKInstructionStep {
public override init(identifier: String) {
super.init(identifier: identifier)
self.title = "Submission Issue"
self.text = "Some errors were encountered while attempting to submit"
self.detailText = "Please try again."
}
required public init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}