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

[Accessibility] Turn UIKit Tab into an accessibility container #982

Merged
Merged
Show file tree
Hide file tree
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
42 changes: 31 additions & 11 deletions core/Sources/Components/Tab/View/UIKit/TabItemUIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public final class TabItemUIView: UIControl {
return self.viewModel.isSelected
}
set {
if newValue {
self.accessibilityTraits.insert(.selected)
} else {
self.accessibilityTraits.remove(.selected)
}
self.viewModel.updateState(isSelected: newValue)
}
}
Expand Down Expand Up @@ -139,7 +144,7 @@ public final class TabItemUIView: UIControl {
newBadge.isHidden.toggle()
newBadge.isHidden.toggle()
}

self.invalidateIntrinsicContentSize()
}
}
Expand Down Expand Up @@ -236,15 +241,6 @@ public final class TabItemUIView: UIControl {
}
}

public override var isHighlighted: Bool {
get {
return self.viewModel.isPressed
}
set {
self.viewModel.updateState(isPressed: newValue)
}
}

/// A Boolean value indicating whether the control is in the enabled state.
///
/// Set the value of this property to true to enable the control or false to disable it. An enabled control is capable of responding to user interactions, whereas a disabled control ignores touch events and may draw itself differently.
Expand All @@ -254,6 +250,11 @@ public final class TabItemUIView: UIControl {
return self.viewModel.isEnabled
}
set {
if newValue {
self.accessibilityTraits.remove(.notEnabled)
} else {
self.accessibilityTraits.insert(.notEnabled)
}
self.viewModel.updateState(isEnabled: newValue)
}
}
Expand Down Expand Up @@ -338,6 +339,8 @@ public final class TabItemUIView: UIControl {
self.setupConstraints()
self.enableTouch()
self.setupSubscriptions()
self.isAccessibilityElement = true
self.accessibilityTraits.insert(.button)
}

required init?(coder: NSCoder) {
Expand All @@ -363,6 +366,22 @@ public final class TabItemUIView: UIControl {
self.setNeedsLayout()
}

// MARK: - Control functions
public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
self.viewModel.isPressed = true
}

public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
self.viewModel.isPressed = false
}

public override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesCancelled(touches, with: event)
self.viewModel.isPressed = false
}

// MARK: - Private functions
private func setupSubscriptions() {
self.viewModel.$tabStateAttributes.subscribe(in: &self.subscriptions) { [weak self] attributes in
Expand Down Expand Up @@ -391,7 +410,7 @@ public final class TabItemUIView: UIControl {
self.bringSubviewToFront(self.bottomLine)

self.setupColors(attributes: self.viewModel.tabStateAttributes)

self.addOrRemoveIcon(self.viewModel.content.icon)
self.addOrRemoveTitle(self.viewModel.content.title)
}
Expand Down Expand Up @@ -471,6 +490,7 @@ public final class TabItemUIView: UIControl {
self.label.textColor = self.viewModel.tabStateAttributes.colors.label.uiColor

self.label.text = text
self.accessibilityLabel = text
self.label.isHidden = text == nil

self.invalidateIntrinsicContentSize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ final class TabItemUIViewTests: TestCase {
}

// When
self.sut.isHighlighted = true
self.sut.touchesBegan(Set<UITouch>(), with: nil)

// Then
waitForExpectations(timeout: 1)
Expand Down Expand Up @@ -174,7 +174,7 @@ final class TabItemUIViewTests: TestCase {
}

// When
self.sut.isHighlighted = false
self.sut.touchesEnded(Set<UITouch>(), with: nil)

// Then
waitForExpectations(timeout: 1)
Expand Down
11 changes: 8 additions & 3 deletions core/Sources/Components/Tab/View/UIKit/TabUIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public final class TabUIView: UIControl {
self.setupConstraints()
self.enableTouch()
self.setupSubscriptions()
self.setupAccessibility()
}

required init?(coder: NSCoder) {
Expand Down Expand Up @@ -462,9 +463,6 @@ public final class TabUIView: UIControl {

// MARK: - Private Functions
private func setupViews(items: [TabUIItemContent]) {

self.accessibilityIdentifier = TabAccessibilityIdentifier.tab

let tabItemViews = items.map{ item in
return TabItemUIView(
theme: theme,
Expand Down Expand Up @@ -517,6 +515,12 @@ public final class TabUIView: UIControl {
stackView.distribution = self.apportionsSegmentWidthsByContent ? .fill : .fillEqually
}

private func setupAccessibility() {
self.accessibilityTraits.insert(.tabBar)
self.isAccessibilityElement = false
self.accessibilityContainerType = .semanticGroup
}

private func setTabItems(content: [TabUIItemContent]) {
self.viewModel.content = content

Expand All @@ -530,6 +534,7 @@ public final class TabUIView: UIControl {
}
self.stackView.removeArrangedSubviews()
self.stackView.addArrangedSubviews(items)
self.accessibilityElements?.append(contentsOf: items)

self.updateAccessibilityIdentifiers()
self.invalidateIntrinsicContentSize()
Expand Down
Loading