From de2171aedb0f83bbc62cc0e939dd98ef4492e4b8 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Fri, 25 Aug 2023 14:35:20 -0500 Subject: [PATCH] converted to use real time rotor Signed-off-by: Matt Bruce --- VDSSample/Protocols/CustomRotorable.swift | 45 ++++++----------------- 1 file changed, 12 insertions(+), 33 deletions(-) diff --git a/VDSSample/Protocols/CustomRotorable.swift b/VDSSample/Protocols/CustomRotorable.swift index acecd19..66c64e8 100644 --- a/VDSSample/Protocols/CustomRotorable.swift +++ b/VDSSample/Protocols/CustomRotorable.swift @@ -23,41 +23,12 @@ extension CustomRotorable { accessibilityCustomRotors = (accessibilityCustomRotors ?? []).filter { $0.name != name } //create new rotor - 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 + let newRotor = UIAccessibilityCustomRotor(name: name) { [weak self] predicate in + guard let self else { return nil } - guard let self, let rootView = self.rootView else { return nil } + let views = self.view.accessibleElements(with: trait) - //create the view accessibleElements cache if it doesn't exist - if self.views == nil { - self.views = rootView.accessibleElements(with: trait) - } - - guard let views = self.views, !views.isEmpty else { return nil } + guard !views.isEmpty else { return nil } let currentIndex = views.firstIndex(where: { $0 === predicate.currentItem.targetElement }) let count = views.count @@ -88,6 +59,14 @@ private class AccessibilityCustomRotor: UIAccessibilityCustomRotor { return UIAccessibilityCustomRotorItemResult(targetElement: views[nextIndex], targetRange: nil) } + + //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) } } }