updated rotor logic

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-08-24 16:02:20 -05:00
parent a528dc291b
commit 883f9fffb4
2 changed files with 31 additions and 17 deletions

View File

@ -9,7 +9,6 @@ import Foundation
import UIKit
public protocol CustomRotorable: UIViewController {
var customRotorCache: [String: [UIView]] { get set }
var customRotors: [CustomRotorType] { get set }
}
@ -24,15 +23,41 @@ extension CustomRotorable {
accessibilityCustomRotors = (accessibilityCustomRotors ?? []).filter { $0.name != name }
//create new rotor
let newRotor = UIAccessibilityCustomRotor(name: name) { [weak self] predicate in
guard let self else { return nil }
let newRotor = AccessibilityCustomRotor(with: name, for: trait, rootView: self.view)
//append rotor
accessibilityCustomRotors?.append(newRotor)
}
/// Loads all of the custom rotors for the screen.
public func loadCustomRotors() {
customRotors.forEach { addCustomRotor(with: $0.name, for: $0.trait) }
}
}
private class AccessibilityCustomRotor: UIAccessibilityCustomRotor {
var views: [UIView]?
var trait: UIAccessibilityTraits
weak var rootView: UIView?
init (with name: String, for trait: UIAccessibilityTraits, rootView: UIView){
self.rootView = rootView
self.trait = trait
super.init(name: name, itemSearch: { _ in return nil })
self.updateSearch()
}
func updateSearch() {
itemSearchBlock = { [weak self] predicate in
guard let self, let rootView = self.rootView else { return nil }
//create the view accessibleElements cache if it doesn't exist
if self.customRotorCache[name] == nil {
self.customRotorCache[name] = self.view.accessibleElements(with: trait)
if self.views == nil {
self.views = rootView.accessibleElements(with: trait)
}
guard let views = self.customRotorCache[name], !views.isEmpty else { return nil }
guard let views = self.views, !views.isEmpty else { return nil }
let currentIndex = views.firstIndex(where: { $0 === predicate.currentItem.targetElement })
let count = views.count
@ -63,16 +88,6 @@ extension CustomRotorable {
return UIAccessibilityCustomRotorItemResult(targetElement: views[nextIndex], targetRange: nil)
}
//append rotor
accessibilityCustomRotors?.append(newRotor)
}
/// Loads all of the custom rotors for the screen.
public func loadCustomRotors() {
customRotorCache.removeAll()
UIAccessibility.post(notification: .screenChanged, argument: nil)
customRotors.forEach { addCustomRotor(with: $0.name, for: $0.trait) }
}
}

View File

@ -82,7 +82,6 @@ public class BaseViewController<Component: UIView>: UIViewController, Initable ,
//--------------------------------------------------
public var subscribers = Set<AnyCancellable>()
public var customRotorCache: [String: [UIView]] = [:]
public var customRotors: [CustomRotorType] = [
CustomRotorType(name: "Links", trait: .link),
CustomRotorType(name: "Buttons", trait: .button)