-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathCardPresentPaymentsModalViewController.swift
490 lines (398 loc) · 15 KB
/
CardPresentPaymentsModalViewController.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import UIKit
import SwiftUI
import SafariServices
/// UI containing modals presented in the Card Present Payments flows.
final class CardPresentPaymentsModalViewController: UIViewController, CardReaderModalFlowViewControllerProtocol {
/// The view model providing configuration for this view controller
/// and support for user actions
private var viewModel: CardPresentPaymentsModalViewModel
@IBOutlet weak var containerView: UIView!
@IBOutlet weak var mainStackView: UIStackView!
@IBOutlet weak var bottomPaddingRegular: NSLayoutConstraint!
@IBOutlet weak var primaryActionButtonsStackView: UIStackView!
@IBOutlet weak var buttonsSpacer: UIView!
@IBOutlet private weak var topTitleLabel: UILabel!
@IBOutlet private weak var topSubtitleLabel: UILabel!
@IBOutlet private weak var bottomTitleLabel: UILabel!
@IBOutlet private weak var bottomSubtitleLabel: UILabel!
@IBOutlet private weak var primaryButton: UIButton!
@IBOutlet private weak var secondaryButton: UIButton!
@IBOutlet weak var auxiliaryButton: UIButton!
@IBOutlet private weak var imageView: UIImageView!
@IBOutlet private weak var extraInfoButton: UIButton!
@IBOutlet private weak var actionButtonsView: UIView!
@IBOutlet private weak var bottomLabels: UIStackView!
@IBOutlet weak var heightConstraint: NSLayoutConstraint!
@IBOutlet weak var widthConstraint: NSLayoutConstraint!
private var loadingView: UIView?
init(viewModel: CardPresentPaymentsModalViewModel) {
self.viewModel = viewModel
super.init(nibName: Self.nibName, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
createViews()
initializeContent()
setBackgroundColor()
setButtonsActions()
styleContent()
populateContent()
}
func setViewModel(_ newViewModel: CardPresentPaymentsModalViewModel) {
self.viewModel = newViewModel
if isViewLoaded {
populateContent()
}
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
resetHeightAndWidth()
}
private func resetHeightAndWidth() {
if traitCollection.containsTraits(in: UITraitCollection(verticalSizeClass: .compact)) {
primaryActionButtonsStackView.axis = .horizontal
primaryActionButtonsStackView.distribution = .fillProportionally
mainStackView.distribution = .fillProportionally
heightConstraint.constant = Constants.modalWidth
widthConstraint.constant = Constants.modalHeight
} else {
primaryActionButtonsStackView.axis = .vertical
primaryActionButtonsStackView.distribution = .fill
mainStackView.distribution = .fill
heightConstraint.constant = Constants.modalHeight
widthConstraint.constant = Constants.modalWidth
}
updateImageAndLoadingVisibility()
heightConstraint.priority = .required
widthConstraint.priority = .required
configureSpacer()
}
private func updateImageAndLoadingVisibility() {
if traitCollection.containsTraits(in: UITraitCollection(verticalSizeClass: .compact)) {
imageView.isHidden = true
loadingView?.isHidden = true
} else {
if viewModel.showLoadingIndicator {
imageView.isHidden = true
loadingView?.isHidden = false
} else {
imageView.isHidden = false
loadingView?.isHidden = true
}
}
}
}
// MARK: - View configuration
private extension CardPresentPaymentsModalViewController {
func setBackgroundColor() {
containerView.backgroundColor = .tertiarySystemBackground
}
func createViews() {
createLoadingIndicator()
}
func createLoadingIndicator() {
let loadingIndicator = ProgressView()
.progressViewStyle(IndefiniteCircularProgressViewStyle(size: 96.0))
.padding()
let host = ConstraintsUpdatingHostingController(rootView: loadingIndicator)
host.view.backgroundColor = .tertiarySystemBackground
add(host)
guard let index = mainStackView.arrangedSubviews.firstIndex(of: imageView) else {
return
}
mainStackView.insertArrangedSubview(host.view, at: index)
loadingView = host.view
}
func styleContent() {
styleTopTitle()
if shouldShowTopSubtitle() {
styleTopSubtitle()
}
if shouldShowBottomLabels() {
styleBottomLabels()
}
if shouldShowActionButtons() {
styleActionButtons()
}
}
func styleTopTitle() {
topTitleLabel.applyBodyStyle()
topTitleLabel.numberOfLines = 0
}
func styleTopSubtitle() {
topSubtitleLabel.applyTitleStyle()
topSubtitleLabel.numberOfLines = 0
}
func styleBottomLabels() {
actionButtonsView.isHidden = true
bottomLabels.isHidden = false
configureSpacer()
styleBottomTitle()
styleBottomSubtitle()
}
func styleBottomTitle() {
bottomTitleLabel.applySubheadlineStyle()
bottomTitleLabel.numberOfLines = 0
}
func styleBottomSubtitle() {
bottomSubtitleLabel.applyFootnoteStyle()
bottomSubtitleLabel.numberOfLines = 0
}
func styleActionButtons() {
actionButtonsView.isHidden = false
bottomLabels.isHidden = true
configureSpacer()
stylePrimaryButton()
styleSecondaryButton()
styleAuxiliaryButton()
}
func stylePrimaryButton() {
primaryButton.applyPrimaryButtonStyle()
primaryButton.titleLabel?.adjustsFontSizeToFitWidth = true
primaryButton.titleLabel?.minimumScaleFactor = 0.3
primaryButton.titleLabel?.lineBreakMode = .byClipping
}
func styleSecondaryButton() {
secondaryButton.applyPaymentsModalCancelButtonStyle()
secondaryButton.titleLabel?.adjustsFontSizeToFitWidth = true
secondaryButton.titleLabel?.minimumScaleFactor = 0.3
secondaryButton.titleLabel?.lineBreakMode = .byClipping
}
func styleAuxiliaryButton() {
if viewModel.actionsMode != .secondaryActionAndAuxiliaryButton {
auxiliaryButton.applyLinkButtonStyle()
}
auxiliaryButton.titleLabel?.minimumScaleFactor = 0.3
auxiliaryButton.titleLabel?.adjustsFontSizeToFitWidth = true
auxiliaryButton.titleLabel?.lineBreakMode = .byClipping
}
func initializeContent() {
topTitleLabel.text = ""
topSubtitleLabel.text = ""
bottomTitleLabel.text = ""
bottomTitleLabel.attributedText = nil
bottomSubtitleLabel.text = ""
bottomSubtitleLabel.attributedText = nil
}
func populateContent() {
configureAccessibility()
configureTopTitle()
if shouldShowTopSubtitle() {
configureTopSubtitle()
}
configureImageView()
if shouldShowActionButtons() {
configureActionButtonsView()
styleActionButtons()
} else {
hideActionButtonsView()
}
if shouldShowBottomLabels() {
configureBottomLabels()
}
resetHeightAndWidth()
}
func configureTopTitle() {
topTitleLabel.text = viewModel.topTitle
topTitleLabel.accessibilityIdentifier = Accessibility.topTitleLabel
}
func configureTopSubtitle() {
topSubtitleLabel.text = viewModel.topSubtitle
}
func configureBottomLabels() {
bottomLabels.isHidden = false
configureBottomTitle()
configureBottomSubtitle()
}
func configureBottomTitle() {
if let bottomAttributedTitle = viewModel.bottomAttributedTitle {
bottomTitleLabel.attributedText = bottomAttributedTitle
} else {
bottomTitleLabel.text = viewModel.bottomTitle
}
}
func configureBottomSubtitle() {
guard shouldShowBottomSubtitle() else {
bottomSubtitleLabel.isHidden = true
return
}
bottomSubtitleLabel.isHidden = false
if let bottomAttributedSubtitle = viewModel.bottomAttributedSubtitle {
bottomSubtitleLabel.attributedText = bottomAttributedSubtitle
} else {
bottomSubtitleLabel.text = viewModel.bottomSubtitle
}
}
func configureImageView() {
imageView.image = viewModel.image
}
func setButtonsActions() {
primaryButton.on(.touchUpInside) { [weak self] _ in
self?.didTapPrimaryButton()
}
secondaryButton.on(.touchUpInside) { [weak self] _ in
self?.didTapSecondaryButton()
}
auxiliaryButton.on(.touchUpInside) { [weak self] _ in
self?.didTapAuxiliaryButton()
}
}
func configureActionButtonsView() {
actionButtonsView.isHidden = false
bottomLabels.isHidden = true
configurePrimaryButton()
configureSecondaryButton()
configureAuxiliaryButton()
}
func hideActionButtonsView() {
actionButtonsView.isHidden = true
configureSpacer()
}
func configurePrimaryButton() {
guard shouldShowPrimaryActionButton() else {
primaryButton.isHidden = true
return
}
primaryButton.isHidden = false
primaryButton.setTitle(viewModel.primaryButtonTitle, for: .normal)
primaryButton.accessibilityIdentifier = Accessibility.primaryButton
}
func configureSecondaryButton() {
guard shouldShowBottomActionButton() else {
secondaryButton.isHidden = true
return
}
secondaryButton.isHidden = false
secondaryButton.setTitle(viewModel.secondaryButtonTitle, for: .normal)
secondaryButton.accessibilityIdentifier = Accessibility.secondaryButton
}
func configureAuxiliaryButton() {
guard shouldShowAuxiliaryButton() else {
auxiliaryButton.isHidden = true
return
}
auxiliaryButton.isHidden = false
auxiliaryButton.accessibilityIdentifier = Accessibility.auxiliaryButton
// Prevents UI flicker when loading different content
UIView.performWithoutAnimation {
auxiliaryButton.setTitle(viewModel.auxiliaryButtonTitle, for: .normal)
auxiliaryButton.setAttributedTitle(viewModel.auxiliaryAttributedButtonTitle, for: .normal)
var config = UIButton.Configuration.plain()
config.contentInsets = Constants.auxiliaryButtonInsets
config.titleAlignment = .leading
auxiliaryButton.configuration = config
view.layoutIfNeeded()
}
}
func configureSpacer() {
let enabled = !shouldShowActionButtons()
if isRegularClassSize {
buttonsSpacer.isHidden = true
// For iPads, instead of a flexible spacer we expand the bottom margin with an extra 127px.
// This would be the space equivalents to the primary and secondary buttons being visible
// - 32px of spacing between buttons container and the rest of the content
// - 40px for the primary button
// - 15px of spacing between buttons
// - 40px for the secondary button
bottomPaddingRegular.constant = 42 + (enabled ? 127 : 0)
} else {
// For compact screens (iPhones, or iPad in split mode), we us a flexible spacer with a low
// content hugging priority to ensure it takes all the available space, leaving the rest of
// the visible items aligned to the top of the stack view
buttonsSpacer.isHidden = !enabled
}
}
var isRegularClassSize: Bool {
traitCollection.verticalSizeClass == .regular && traitCollection.horizontalSizeClass == .regular
}
}
// MARK: - View layout configuration
private extension CardPresentPaymentsModalViewController {
func shouldShowTopSubtitle() -> Bool {
viewModel.textMode != .reducedTopInfo
}
func shouldShowBottomLabels() -> Bool {
viewModel.textMode != .noBottomInfo
}
func shouldShowActionButtons() -> Bool {
viewModel.actionsMode != .none
}
func shouldShowBottomSubtitle() -> Bool {
let textMode = viewModel.textMode
return textMode == .fullInfo ||
textMode == .reducedTopInfo
}
func shouldShowPrimaryActionButton() -> Bool {
[.oneAction, .twoAction, .twoActionAndAuxiliary]
.contains(viewModel.actionsMode)
}
func shouldShowBottomActionButton() -> Bool {
[.secondaryOnlyAction, .twoAction, .twoActionAndAuxiliary, .secondaryActionAndAuxiliaryButton]
.contains(viewModel.actionsMode)
}
func shouldShowAuxiliaryButton() -> Bool {
[.twoActionAndAuxiliary, .secondaryActionAndAuxiliaryButton].contains(viewModel.actionsMode)
}
}
// MARK: - Accessibility
private extension CardPresentPaymentsModalViewController {
func configureAccessibility() {
accessibilityTraits = .staticText
UIAccessibility.post(notification: .announcement, argument: viewModel.accessibilityLabel)
}
}
// MARK: - Actions
private extension CardPresentPaymentsModalViewController {
@objc func didTapPrimaryButton() {
viewModel.didTapPrimaryButton(in: self)
}
@objc func didTapSecondaryButton() {
viewModel.didTapSecondaryButton(in: self)
}
@objc func didTapAuxiliaryButton() {
viewModel.didTapAuxiliaryButton(in: self)
}
}
// MARK: - Constants
private extension CardPresentPaymentsModalViewController {
enum Constants {
static let modalHeight: CGFloat = 382
static let modalWidth: CGFloat = 280
static let auxiliaryButtonInsets = NSDirectionalEdgeInsets(top: 8, leading: 8, bottom: 8, trailing: 8)
}
}
// MARK: - Tests
extension CardPresentPaymentsModalViewController {
func getTopTitleLabel() -> UILabel {
return topTitleLabel
}
func getTopSubtitleLabel() -> UILabel {
return topSubtitleLabel
}
func getImageView() -> UIImageView {
return imageView
}
func getBottomTitleLabel() -> UILabel {
return bottomTitleLabel
}
func getBottomSubtitleLabel() -> UILabel {
return bottomSubtitleLabel
}
func getPrimaryActionButton() -> UIButton {
return primaryButton
}
func getSecondaryActionButton() -> UIButton {
return secondaryButton
}
}
private extension CardPresentPaymentsModalViewController {
enum Accessibility {
static let topTitleLabel = "card-present-payments-modal-title-label"
static let primaryButton = "card-present-payments-modal-primary-button"
static let secondaryButton = "card-present-payments-modal-secondary-button"
static let auxiliaryButton = "card-present-payments-modal-auxiliary-button"
}
}