-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSubmissionStepModifiers.swift
105 lines (78 loc) · 3.35 KB
/
SubmissionStepModifiers.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
//
// StepModifiers.swift
// SMARTMarkers
//
// Created by Raheel Sayeed on 2/25/21.
// Copyright © 2021 Boston Children's Hospital. All rights reserved.
//
import Foundation
import ResearchKit
class ReviewStepModifier: ORKStepModifier {
override func modifyStep(_ step: ORKStep, with taskResult: ORKTaskResult) {
let task = step.task as! SubmissionTask
(step as! SMSubmissionPermitStep).formItems = task.session.tasks.map ({ $0.sm_asFormItem() })
}
}
class ConsentStepModifier: ORKStepModifier {
override func modifyStep(_ step: ORKStep, with taskResult: ORKTaskResult) {
guard let selected = taskResult.stepResult(forStepIdentifier: kSM_Submission_Review) else {
return
}
let task = step.task as! SubmissionTask
if 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
}) {
let submissionNotice = "\(selectedTasksIdentifiers.count) Selected\n\nWill be submitted to: \(task.session.server!.name ?? "FHIR Server") at \(task.session.server!.baseURL.host ?? ""). Proceed?"
step.text = submissionNotice
}
}
}
open class SMSubmissionErrorNoticeModifier: ORKStepModifier {
open override func modifyStep(_ step: ORKStep, with taskResult: ORKTaskResult) {
let task = step.task as! SubmissionTask
if let result = taskResult.stepResult(forStepIdentifier: kSM_Submission_InProgress)?.results?.last as? ORKBooleanQuestionResult {
let skip = result.booleanAnswer == 1
if !skip {
let rule = ORKDirectStepNavigationRule(destinationStepIdentifier: kSM_Submission_Review)
task.setNavigationRule(rule, forTriggerStepIdentifier: step.identifier)
}
}
}
}
extension TaskController {
func sm_asFormItem() -> ORKFormItem {
let reportChoices = reports?.sm_asTextChoiceAnswerFormat()
let formItem = ORKFormItem(identifier: instrument?.sm_title ?? "#", text: instrument?.sm_title, answerFormat: reportChoices)
formItem.isOptional = true
return formItem
}
}
extension Reports {
func sm_asTextChoiceAnswerFormat() -> ORKTextChoiceAnswerFormat? {
guard hasReportsToSubmit else {
return nil
}
var choices = [ORKTextChoice]()
for sub in reportsToSubmit() ?? [] {
guard let bundle = sub.bundle else { continue }
let title = "#\(bundle.sm_resourceCount()) health data resources"
let content = """
\(bundle.sm_ContentSummary() ?? "")
Status: \(sub.status.rawValue)
Task Id: \(sub.taskId)
"""
let choice = ORKTextChoice(
text: title,
detailText: content,
value: sub.taskId as NSCoding & NSCopying & NSObjectProtocol,
exclusive: false
)
choices.append(choice)
}
return ORKTextChoiceAnswerFormat(style: .multipleChoice, textChoices: choices)
}
}