Skip to content

Commit 84a810e

Browse files
authored
Merge pull request #140 from cashapp/entin/invert-colors-view
Modernize InvertColorsViewController
2 parents 6204740 + b3e77c4 commit 84a810e

File tree

1 file changed

+65
-37
lines changed

1 file changed

+65
-37
lines changed

Example/AccessibilitySnapshot/InvertColorsViewController.swift

+65-37
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,16 @@ final class InvertColorsViewController: AccessibilityViewController {
2121

2222
// MARK: - Private Properties
2323

24-
private let nestedSubviews: [UIView] = (0..<5).map { _ in UIView() }
25-
26-
private let statusLabel: UILabel = .init()
27-
2824
private var notificationObserver: AnyObject?
2925

30-
// MARK: - UIViewController
31-
32-
override func viewDidLoad() {
33-
super.viewDidLoad()
34-
35-
let nestedViews: [UIView] = [view] + nestedSubviews
36-
for (view, subview) in zip(nestedViews.dropLast(), nestedViews.dropFirst()) {
37-
view.addSubview(subview)
38-
}
39-
40-
nestedSubviews[0].backgroundColor = .red
41-
42-
nestedSubviews[1].backgroundColor = .blue
43-
44-
nestedSubviews[2].backgroundColor = .green
45-
nestedSubviews[2].accessibilityIgnoresInvertColors = true
46-
47-
nestedSubviews[3].backgroundColor = .yellow
26+
private var rootView: View {
27+
return view as! View
28+
}
4829

49-
nestedSubviews[4].backgroundColor = .purple
50-
nestedSubviews[4].accessibilityIgnoresInvertColors = true
30+
// MARK: - UIViewController
5131

52-
statusLabel.textColor = .white
53-
nestedSubviews[2].addSubview(statusLabel)
32+
override func loadView() {
33+
view = View()
5434
}
5535

5636
override func viewWillAppear(_ animated: Bool) {
@@ -76,22 +56,70 @@ final class InvertColorsViewController: AccessibilityViewController {
7656
}
7757
}
7858

79-
override func viewDidLayoutSubviews() {
80-
super.viewDidLayoutSubviews()
59+
// MARK: - Private Methods
60+
61+
private func updateStatusLabel() {
62+
rootView.statusLabel.text = UIAccessibility.isInvertColorsEnabled ? "Enabled" : "Disabled"
63+
rootView.setNeedsLayout()
64+
}
65+
66+
}
67+
68+
// MARK: -
69+
70+
private extension InvertColorsViewController {
71+
72+
final class View: UIView {
8173

82-
for view in nestedSubviews {
83-
view.frame = view.superview!.bounds.inset(left: 30, top: 60, right: 30, bottom: 60)
74+
// MARK: - Life Cycle
75+
76+
override init(frame: CGRect) {
77+
super.init(frame: frame)
78+
79+
let nestedViews: [UIView] = [self] + nestedSubviews
80+
for (view, subview) in zip(nestedViews.dropLast(), nestedViews.dropFirst()) {
81+
view.addSubview(subview)
82+
}
83+
84+
nestedSubviews[0].backgroundColor = .red
85+
86+
nestedSubviews[1].backgroundColor = .blue
87+
88+
nestedSubviews[2].backgroundColor = .green
89+
nestedSubviews[2].accessibilityIgnoresInvertColors = true
90+
91+
nestedSubviews[3].backgroundColor = .yellow
92+
93+
nestedSubviews[4].backgroundColor = .purple
94+
nestedSubviews[4].accessibilityIgnoresInvertColors = true
95+
96+
statusLabel.textColor = .white
97+
nestedSubviews[2].addSubview(statusLabel)
8498
}
8599

86-
statusLabel.sizeToFit()
87-
statusLabel.alignToSuperview(.topCenter, inset: 20)
88-
}
100+
@available(*, unavailable)
101+
required init?(coder: NSCoder) {
102+
fatalError("init(coder:) has not been implemented")
103+
}
89104

90-
// MARK: - Private Methods
105+
// MARK: - Private Properties
106+
107+
private let nestedSubviews: [UIView] = (0..<5).map { _ in UIView() }
108+
109+
let statusLabel: UILabel = .init()
110+
111+
// MARK: - UIView
112+
113+
override func layoutSubviews() {
114+
let nestedViews: [UIView] = [self] + nestedSubviews
115+
for (containingView, subview) in zip(nestedViews.dropLast(), nestedViews.dropFirst()) {
116+
subview.frame = containingView.bounds.inset(left: 30, top: 60, right: 30, bottom: 60)
117+
}
118+
119+
statusLabel.sizeToFit()
120+
statusLabel.alignToSuperview(.topCenter, inset: 20)
121+
}
91122

92-
private func updateStatusLabel() {
93-
statusLabel.text = UIAccessibility.isInvertColorsEnabled ? "Enabled" : "Disabled"
94-
view.setNeedsLayout()
95123
}
96124

97125
}

0 commit comments

Comments
 (0)