From 6c4d42898449e8f6cb848b5d39b1acb1cbb208b4 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Wed, 19 Jun 2024 17:17:46 -0500 Subject: [PATCH] override for accessibility new impl Signed-off-by: Matt Bruce --- VDS/BaseClasses/Selector/SelectorBase.swift | 11 ++--- .../Selector/SelectorGroupBase.swift | 7 +++ .../Selector/SelectorItemBase.swift | 22 +++++++++- .../Pagination/PaginationButton.swift | 5 --- VDS/Components/RadioBox/RadioBoxItem.swift | 43 +++++++++++++++---- 5 files changed, 68 insertions(+), 20 deletions(-) diff --git a/VDS/BaseClasses/Selector/SelectorBase.swift b/VDS/BaseClasses/Selector/SelectorBase.swift index 31d317cf..35999cf6 100644 --- a/VDS/BaseClasses/Selector/SelectorBase.swift +++ b/VDS/BaseClasses/Selector/SelectorBase.swift @@ -135,14 +135,15 @@ open class SelectorBase: Control, SelectorControlable { onChange = nil } - public var accessibilityDefaultAction: (() -> Void)? - - public override func accessibilityActivate() -> Bool { - if let accessibilityDefaultAction { - accessibilityDefaultAction() + open override func accessibilityActivate() -> Bool { + guard isEnabled, isUserInteractionEnabled else { return false } + + if let accessibilityAction { + accessibilityAction(self) } else { toggle() } + return true } } diff --git a/VDS/BaseClasses/Selector/SelectorGroupBase.swift b/VDS/BaseClasses/Selector/SelectorGroupBase.swift index 184f8e07..4df1ca5c 100644 --- a/VDS/BaseClasses/Selector/SelectorGroupBase.swift +++ b/VDS/BaseClasses/Selector/SelectorGroupBase.swift @@ -70,6 +70,13 @@ open class SelectorGroupBase: Control, SelectorGrou self?.didSelect(handler) self?.setNeedsUpdate() } + + selector.accessibilityAction = { [weak self] handler in + guard let handler = handler as? SelectorItemType else { return } + self?.didSelect(handler) + self?.setNeedsUpdate() + } + mainStackView.addArrangedSubview(selector) } } diff --git a/VDS/BaseClasses/Selector/SelectorItemBase.swift b/VDS/BaseClasses/Selector/SelectorItemBase.swift index 3a04b86d..2cd7eead 100644 --- a/VDS/BaseClasses/Selector/SelectorItemBase.swift +++ b/VDS/BaseClasses/Selector/SelectorItemBase.swift @@ -147,6 +147,15 @@ open class SelectorItemBase: Control, Errorable, Changea open var accessibilityValueText: String? + open override var accessibilityDefaultAction: ((Control) -> Void)? { + didSet { + selectorView.accessibilityAction = { [weak self] selectorItemBase in + guard let self else { return } + accessibilityAction?(self) + } + } + } + open var accessibilityLabelText: String { var accessibilityLabels = [String]() @@ -186,7 +195,7 @@ open class SelectorItemBase: Control, Errorable, Changea toggle() } - selectorView.accessibilityDefaultAction = { [weak self] in + selectorView.accessibilityAction = { [weak self] _ in guard let self, isEnabled else { return } toggle() } @@ -366,4 +375,15 @@ open class SelectorItemBase: Control, Errorable, Changea /// This will change to state of the Selector. open func toggle() {} + open override func accessibilityActivate() -> Bool { + guard isEnabled, isUserInteractionEnabled else { return false } + + if let accessibilityAction { + accessibilityAction(self) + } else { + toggle() + } + + return true + } } diff --git a/VDS/Components/Pagination/PaginationButton.swift b/VDS/Components/Pagination/PaginationButton.swift index 05ef64ee..d7aaf8e2 100644 --- a/VDS/Components/Pagination/PaginationButton.swift +++ b/VDS/Components/Pagination/PaginationButton.swift @@ -78,11 +78,6 @@ open class PaginationButton: ButtonBase { tintColor = color super.updateView() } - - open override func accessibilityActivate() -> Bool { - sendActions(for: .touchUpInside) - return true - } } extension PaginationButton { diff --git a/VDS/Components/RadioBox/RadioBoxItem.swift b/VDS/Components/RadioBox/RadioBoxItem.swift index 417df508..71b05494 100644 --- a/VDS/Components/RadioBox/RadioBoxItem.swift +++ b/VDS/Components/RadioBox/RadioBoxItem.swift @@ -74,9 +74,7 @@ open class RadioBoxItem: Control, Changeable, FormFieldable, Groupable { } /// Selector for this RadioBox. - open var selectorView = UIView().with { - $0.translatesAutoresizingMaskIntoConstraints = false - } + open var selectorView = View() /// If provided, the RadioBox text will be rendered. open var text: String? { didSet { setNeedsUpdate() } } @@ -133,20 +131,33 @@ open class RadioBoxItem: Control, Changeable, FormFieldable, Groupable { open var accessibilityValueText: String? + open override var accessibilityAction: ((Control) -> Void)? { + didSet { + selectorView.accessibilityAction = { [weak self] selectorItemBase in + guard let self else { return } + accessibilityAction?(self) + } + } + } + open var accessibilityLabelText: String { var accessibilityLabels = [String]() + if isSelected { + accessibilityLabels.append("selected") + } + accessibilityLabels.append("Radiobox") - if let text { + if let text, !text.isEmpty { accessibilityLabels.append(text) } - if let text = subText { + if let text = subText, !text.isEmpty { accessibilityLabels.append(text) } - if let text = subTextRight { + if let text = subTextRight, !text.isEmpty { accessibilityLabels.append(text) } @@ -189,6 +200,16 @@ open class RadioBoxItem: Control, Changeable, FormFieldable, Groupable { onClick = { control in control.toggle() } + + if #available(iOS 17.0, *) { + accessibilityHintBlock = { [weak self] in + + return "foo" + } + } else { + // Fallback on earlier versions + } + } /// Called once when a view is initialized and is used to Setup additional UI or other constants and configurations. @@ -199,7 +220,7 @@ open class RadioBoxItem: Control, Changeable, FormFieldable, Groupable { selectorView.isAccessibilityElement = true selectorView.accessibilityTraits = .button addSubview(selectorView) - selectorView.isUserInteractionEnabled = false + selectorView.isUserInteractionEnabled = true selectorView.addSubview(selectorStackView) @@ -281,7 +302,7 @@ open class RadioBoxItem: Control, Changeable, FormFieldable, Groupable { : accessibilityValueText } } - + open override var accessibilityElements: [Any]? { get { var items = [Any]() @@ -322,7 +343,11 @@ open class RadioBoxItem: Control, Changeable, FormFieldable, Groupable { return super.hitTest(point, with: event) } } - + + open func getSelectorView() -> UIView { + selectorView + } + //-------------------------------------------------- // MARK: - Private Methods //--------------------------------------------------