Skip to content

Commit

Permalink
Merge pull request #773 from adevinta/enhancement/textlink
Browse files Browse the repository at this point in the history
[Textlink#772] Manage accessibility
  • Loading branch information
robergro authored Feb 19, 2024
2 parents 4b4bcf4 + 156f252 commit 0ea0a21
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public struct TextLinkView: View {
}
.buttonStyle(PressedButtonStyle(isPressed: self.$viewModel.isHighlighted))
.accessibilityIdentifier(TextLinkAccessibilityIdentifier.view)
.accessibilityAddTraits(.isButton)
}

// MARK: - View Builder
Expand Down
16 changes: 15 additions & 1 deletion core/Sources/Components/TextLink/View/UIKit/TextLinkUIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public final class TextLinkUIView: UIControl {
}
set {
self.viewModel.text = newValue
self.accessibilityLabelManager.internalValue = newValue
}
}

Expand Down Expand Up @@ -182,6 +183,15 @@ public final class TextLinkUIView: UIControl {
}
}

public override var accessibilityLabel: String? {
get {
return self.accessibilityLabelManager.value
}
set {
self.accessibilityLabelManager.value = newValue
}
}

// MARK: - Private Properties

private let viewModel: TextLinkViewModel
Expand All @@ -191,6 +201,8 @@ public final class TextLinkUIView: UIControl {

@ScaledUIMetric private var contentStackViewSpacing: CGFloat = 0

private var accessibilityLabelManager = AccessibilityLabelManager()

private var subscriptions = Set<AnyCancellable>()

// MARK: - Initialization
Expand Down Expand Up @@ -242,8 +254,10 @@ public final class TextLinkUIView: UIControl {
// Add subviews
self.addSubview(self.contentStackView)

// Identifiers
// Accessibility
self.accessibilityTraits = [.button]
self.accessibilityIdentifier = TextLinkAccessibilityIdentifier.view
self.isAccessibilityElement = true

// View properties
self.backgroundColor = .clear
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ final class TextLinkComponentUIView: ComponentUIView {
self.viewModel.controlTypeConfigurationItemViewModel.buttonTitle = controlType.name
self.setControl(from: controlType)
}

self.viewModel.$isCustomAccessibilityLabel.subscribe(in: &self.subcriptions) { [weak self] isCustomAccessibilityLabel in
guard let self = self else { return }
self.componentView.accessibilityLabel = isCustomAccessibilityLabel ? "My Textlink Label" : nil
}
}

private func setControl(from controlType: TextLinkControlType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ final class TextLinkComponentUIViewModel: ComponentUIViewModel {
)
}()

lazy var isCustomAccessibilityLabelConfigurationItemViewModel: ComponentsConfigurationItemUIViewModel = {
return .init(
name: "is Custom AccessibilityLabel",
type: .toggle(isOn: self.isCustomAccessibilityLabel),
target: (source: self, action: #selector(self.isCustomAccessibilityLabelChanged))
)
}()

// MARK: - Methods

override func configurationItemsViewModel() -> [ComponentsConfigurationItemUIViewModel] {
Expand All @@ -171,7 +179,8 @@ final class TextLinkComponentUIViewModel: ComponentUIViewModel {
self.textAlignmentConfigurationItemViewModel,
self.lineBreakModeConfigurationItemViewModel,
self.isLineLimitConfigurationItemViewModel,
self.controlTypeConfigurationItemViewModel
self.controlTypeConfigurationItemViewModel,
self.isCustomAccessibilityLabelConfigurationItemViewModel
]
}

Expand All @@ -189,6 +198,7 @@ final class TextLinkComponentUIViewModel: ComponentUIViewModel {
@Published var lineBreakMode: NSLineBreakMode
@Published var isLineLimit: Bool
@Published var controlType: TextLinkControlType
@Published var isCustomAccessibilityLabel: Bool

// MARK: - Initialization

Expand All @@ -202,7 +212,8 @@ final class TextLinkComponentUIViewModel: ComponentUIViewModel {
textAlignment: NSTextAlignment = .natural,
lineBreakMode: NSLineBreakMode = .byWordWrapping,
isLineLimit: Bool = false,
controlType: TextLinkControlType = .action
controlType: TextLinkControlType = .action,
isCustomAccessibilityLabel: Bool = false
) {
self.theme = theme
self.intent = intent
Expand All @@ -214,6 +225,7 @@ final class TextLinkComponentUIViewModel: ComponentUIViewModel {
self.lineBreakMode = lineBreakMode
self.isLineLimit = isLineLimit
self.controlType = controlType
self.isCustomAccessibilityLabel = isCustomAccessibilityLabel

super.init(identifier: "TextLink")
}
Expand Down Expand Up @@ -262,6 +274,10 @@ extension TextLinkComponentUIViewModel {
@objc func presentControlTypeSheet() {
self.showControlTypeSheetSubject.send(TextLinkControlType.allCases)
}

@objc func isCustomAccessibilityLabelChanged() {
self.isCustomAccessibilityLabel.toggle()
}
}

// MARK: - NSLineBreakMode
Expand Down

0 comments on commit 0ea0a21

Please sign in to comment.