Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize InvertColorsViewController #140

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 65 additions & 37 deletions Example/AccessibilitySnapshot/InvertColorsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,16 @@ final class InvertColorsViewController: AccessibilityViewController {

// MARK: - Private Properties

private let nestedSubviews: [UIView] = (0..<5).map { _ in UIView() }

private let statusLabel: UILabel = .init()

private var notificationObserver: AnyObject?

// MARK: - UIViewController

override func viewDidLoad() {
super.viewDidLoad()

let nestedViews: [UIView] = [view] + nestedSubviews
for (view, subview) in zip(nestedViews.dropLast(), nestedViews.dropFirst()) {
view.addSubview(subview)
}

nestedSubviews[0].backgroundColor = .red

nestedSubviews[1].backgroundColor = .blue

nestedSubviews[2].backgroundColor = .green
nestedSubviews[2].accessibilityIgnoresInvertColors = true

nestedSubviews[3].backgroundColor = .yellow
private var rootView: View {
return view as! View
}

nestedSubviews[4].backgroundColor = .purple
nestedSubviews[4].accessibilityIgnoresInvertColors = true
// MARK: - UIViewController

statusLabel.textColor = .white
nestedSubviews[2].addSubview(statusLabel)
override func loadView() {
view = View()
}

override func viewWillAppear(_ animated: Bool) {
Expand All @@ -76,22 +56,70 @@ final class InvertColorsViewController: AccessibilityViewController {
}
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// MARK: - Private Methods

private func updateStatusLabel() {
rootView.statusLabel.text = UIAccessibility.isInvertColorsEnabled ? "Enabled" : "Disabled"
rootView.setNeedsLayout()
}

}

// MARK: -

private extension InvertColorsViewController {

final class View: UIView {

for view in nestedSubviews {
view.frame = view.superview!.bounds.inset(left: 30, top: 60, right: 30, bottom: 60)
// MARK: - Life Cycle

override init(frame: CGRect) {
super.init(frame: frame)

let nestedViews: [UIView] = [self] + nestedSubviews
for (view, subview) in zip(nestedViews.dropLast(), nestedViews.dropFirst()) {
view.addSubview(subview)
}

nestedSubviews[0].backgroundColor = .red

nestedSubviews[1].backgroundColor = .blue

nestedSubviews[2].backgroundColor = .green
nestedSubviews[2].accessibilityIgnoresInvertColors = true

nestedSubviews[3].backgroundColor = .yellow

nestedSubviews[4].backgroundColor = .purple
nestedSubviews[4].accessibilityIgnoresInvertColors = true

statusLabel.textColor = .white
nestedSubviews[2].addSubview(statusLabel)
}

statusLabel.sizeToFit()
statusLabel.alignToSuperview(.topCenter, inset: 20)
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

// MARK: - Private Methods
// MARK: - Private Properties

private let nestedSubviews: [UIView] = (0..<5).map { _ in UIView() }

let statusLabel: UILabel = .init()

// MARK: - UIView

override func layoutSubviews() {
let nestedViews: [UIView] = [self] + nestedSubviews
for (containingView, subview) in zip(nestedViews.dropLast(), nestedViews.dropFirst()) {
subview.frame = containingView.bounds.inset(left: 30, top: 60, right: 30, bottom: 60)
}

statusLabel.sizeToFit()
statusLabel.alignToSuperview(.topCenter, inset: 20)
}

private func updateStatusLabel() {
statusLabel.text = UIAccessibility.isInvertColorsEnabled ? "Enabled" : "Disabled"
view.setNeedsLayout()
}

}