-
Notifications
You must be signed in to change notification settings - Fork 6
/
EligibilityCheckViewController.swift
300 lines (246 loc) · 9.73 KB
/
EligibilityCheckViewController.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
//
// EligibilityCheckViewController.swift
// C3PRO
//
// Created by Pascal Pfiffner on 22/10/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 UIKit
/**
View controller presenting all eligibility criteria provided in the receiver`s `requirements` property, allowing the user to proceed to a
summary page that informs of eligibility and allows to proceed to to consenting or not.
*/
open class EligibilityCheckViewController: UITableViewController {
var nextButton: UIBarButtonItem?
/// The eligibility criteria.
open var requirements: [EligibilityRequirement]?
/// Set this string to override the title message. Defaults to "You are eligible to join the study".
open var eligibleTitle: String?
/// Override point for the default eligible message "Tap the button below to begin the consent process".
open var eligibleMessage: String?
/// Override point for the default ineligible message "Thank you for your interest!\nUnfortunately, you are not eligible to join [...]".
open var ineligibleMessage: String?
/// Block executed if all eligibility requirements are met and the user taps the "Start Consent" button.
open var onStartConsent: ((_ viewController: EligibilityCheckViewController) -> Void)?
// MARK: - View Tasks
override open func viewDidLoad() {
super.viewDidLoad()
tableView.register(EligibilityCell.self, forCellReuseIdentifier: "EligibilityCell")
tableView.estimatedRowHeight = 120.0
tableView.rowHeight = UITableViewAutomaticDimension
let next = UIBarButtonItem(title: "Next".c3_localized("Next step"), style: .plain, target: self, action: #selector(EligibilityCheckViewController.verifyEligibility))
next.isEnabled = false
navigationItem.rightBarButtonItem = next
nextButton = next
enableDisableNext()
}
override open func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if isMovingToParentViewController, isEligible() ?? false {
showEligible(animated: true)
}
}
// MARK: - Actions
func enableDisableNext() {
if let reqs = requirements {
for req in reqs {
if nil == req.met {
nextButton?.isEnabled = false
return
}
}
}
nextButton?.isEnabled = true
}
func verifyEligibility() {
if let eligible = isEligible() {
if eligible {
showEligible(animated: true)
}
else {
showIneligible(animated: true)
}
}
else {
nextButton?.isEnabled = false
}
}
/** Determine current eligibility status.
- returns: True if all requirements are met, false if at least one is not met, nil if not all have been answered yet
*/
func isEligible() -> Bool? {
if let reqs = requirements {
for requirement in reqs {
if let met = requirement.met {
if met != requirement.mustBeMet {
return false
}
}
else {
return nil
}
}
}
// all requirements answered and met (or none to meet)
return true
}
/**
Eligible.
Push `EligibilityStatusViewController` informing about eligibility and presenting the “Start Consent” button that will execute the
`onStartConsent` block.
- parameter animated: Whether to animate the push
*/
open func showEligible(animated: Bool) {
let vc = EligibilityStatusViewController()
vc.titleText = eligibleTitle ?? "You are eligible to join the study".c3_localized
vc.subText = eligibleMessage ?? "Tap the button below to begin the consent process".c3_localized
vc.onActionButtonTap = { controller in
if let exec = self.onStartConsent {
exec(self)
}
else {
c3_warn("Tapped “Start Consent” but `onStartConsent` is not defined")
}
}
navigationController?.pushViewController(vc, animated: true)
}
/**
Ineligible.
Push EligibilityStatusViewController informing about non-eligibility, removing the other status and check view controllers from the
stack so that if pressing "< Back", the user lands back at where eligibility checking started.
- parameter animated: Whether to animate the push
*/
open func showIneligible(animated: Bool) {
let vc = EligibilityStatusViewController()
vc.subText = ineligibleMessage ?? "Thank you for your interest!\nUnfortunately, you are not eligible to join this study at this time.".c3_localized
if let navi = navigationController {
var vcs = navi.viewControllers.filter() { !($0 is EligibilityStatusViewController || $0 is EligibilityCheckViewController) }
vcs.append(vc)
navi.setViewControllers(vcs, animated: true)
}
else {
c3_warn("Incredible error, my navigation controller disappeared! I'm \(self)")
}
}
// MARK: - Table View Data Source
override open func numberOfSections(in tableView: UITableView) -> Int {
return requirements?.count ?? 0
}
override open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "EligibilityCell", for: indexPath) as! EligibilityCell
cell.item = requirements![indexPath.section]
cell.onButtonPress = { button in
self.enableDisableNext()
}
return cell
}
}
/**
Table view cell displaying an eligibility requirement.
*/
class EligibilityCell: UITableViewCell {
var titleLabel: UILabel?
var yesButton: UIButton?
var noButton: UIButton?
/// The requirement to show.
var item: EligibilityRequirement? {
didSet {
if let item = item {
titleLabel?.text = item.title
}
}
}
var onButtonPress: ((UIButton) -> Void)?
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
selectionStyle = .none
// title label
let title = UILabel()
title.translatesAutoresizingMaskIntoConstraints = false
title.font = UIFont.preferredFont(forTextStyle: .body)
title.numberOfLines = 0
title.textAlignment = .center
title.textColor = UIColor.black
titleLabel = title
// choice view
let choice = UIView()
choice.translatesAutoresizingMaskIntoConstraints = false
// yes and no buttons
let yes = UIButton(type: .custom)
let no = UIButton(type: .custom)
yes.translatesAutoresizingMaskIntoConstraints = false
no.translatesAutoresizingMaskIntoConstraints = false
yes.isSelected = false
no.isSelected = false
yes.setTitleColor(UIColor.lightGray, for: UIControlState())
no.setTitleColor(UIColor.lightGray, for: UIControlState())
yes.setTitleColor(self.tintColor, for: .selected)
no.setTitleColor(self.tintColor, for: .selected)
yes.addTarget(self, action: #selector(EligibilityCell.buttonDidPress(_:)), for: .touchUpInside)
no.addTarget(self, action: #selector(EligibilityCell.buttonDidPress(_:)), for: .touchUpInside)
let desc = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .body)
yes.titleLabel?.font = UIFont(descriptor: desc, size: desc.pointSize * 2)
no.titleLabel?.font = UIFont(descriptor: desc, size: desc.pointSize * 2)
yes.setTitle("Yes".c3_localized("Yes as in yes-i-meet-this-requirement"), for: UIControlState())
no.setTitle("No".c3_localized("No as in no-i-dont-meet-this-requirement"), for: UIControlState())
let sep = UIView()
sep.translatesAutoresizingMaskIntoConstraints = false
sep.backgroundColor = UIColor(white: 0.8, alpha: 1.0)
yesButton = yes
noButton = no
// layout
let buttons = ["yes": yes, "sep": sep, "no": no]
choice.addSubview(yes)
choice.addSubview(sep)
choice.addSubview(no)
choice.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[yes(==no)][sep(==0.5)][no]|", options: [], metrics: nil, views: buttons))
choice.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[yes]|", options: [], metrics: nil, views: buttons))
choice.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[sep]|", options: [], metrics: nil, views: buttons))
choice.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[no]|", options: [], metrics: nil, views: buttons))
let views = ["title": title, "choice": choice]
contentView.addSubview(title)
contentView.addSubview(choice)
contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-[title]-|", options: [], metrics: nil, views: views))
contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-[choice]-|", options: [], metrics: nil, views: views))
contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-[title]-[choice]-|", options: [], metrics: nil, views: views))
choice.addConstraint(NSLayoutConstraint(item: choice, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0.0, constant: 80.0))
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
// MARK: - Button Action
func buttonDidPress(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
if yesButton == sender {
item?.met = true
noButton?.isSelected = false
}
else {
item?.met = false
yesButton?.isSelected = false
}
if let exec = onButtonPress {
exec(sender)
}
}
override func tintColorDidChange() {
yesButton?.setTitleColor(self.tintColor, for: .selected)
noButton?.setTitleColor(self.tintColor, for: .selected)
super.tintColorDidChange()
}
}