refactored code

This commit is contained in:
Krishna Kishore Bandaru 2023-09-27 12:18:58 +05:30
parent 00427f9e77
commit 6b2e29ae62

View File

@ -179,11 +179,12 @@ open class AccessibilityHandler {
stop = true stop = true
} }
} }
return (delegate as? UIViewController)?.view?.getMoleculeViews { (subView: MoleculeViewProtocol) in return (delegate as? UIViewController)?.view?.getMoleculeViews { (subView: MoleculeViewProtocol, stop: inout Bool) in
guard let modelElement, let moleculeModel = (subView as? MoleculeViewModelProtocol)?.moleculeModel, guard let modelElement, let moleculeModel = (subView as? MoleculeViewModelProtocol)?.moleculeModel,
moleculeModel.id == modelElement.id else { moleculeModel.id == modelElement.id else {
return false return false
} }
stop = true
return true return true
}.first }.first
} }
@ -242,9 +243,9 @@ open class AccessibilityHandlerBehavior: PageVisibilityBehavior {
private func identifyAndPrepareRotors() { private func identifyAndPrepareRotors() {
var rotorElements: [UIAccessibilityCustomRotor] = [] var rotorElements: [UIAccessibilityCustomRotor] = []
let currentViewController = ((delegateObj?.moleculeDelegate as? MVMCoreViewManagerViewControllerProtocol)?.manager as? SubNavManagerController) ?? (delegateObj?.moleculeDelegate as? ViewController) let currentViewController = ((delegateObj?.moleculeDelegate as? MVMCoreViewManagerViewControllerProtocol)?.manager as? SubNavManagerController) ?? (delegateObj?.moleculeDelegate as? ViewController)
for element in RotorType.allCases { for type in RotorType.allCases {
if let elements = getTraitMappedElements(template: currentViewController, type: element), if let elements = getTraitMappedElements(template: currentViewController, type: type),
let rotor = createRotor(elements, for: element) { let rotor = createRotor(elements, for: type) {
rotorElements.append(rotor) rotorElements.append(rotor)
} }
} }
@ -289,23 +290,23 @@ open class AccessibilityHandlerBehavior: PageVisibilityBehavior {
return result return result
}) })
let headerViewElements = currentViewController.tableView.tableHeaderView?.getMoleculeViews { let headerViewElements = currentViewController.tableView.tableHeaderView?.getMoleculeViews { (subView: MoleculeViewProtocol, _) in
(subView: MoleculeViewProtocol) in subView.accessibilityTraits.contains(type.trait) subView.accessibilityTraits.contains(type.trait)
} as? [Any] ?? [] } as? [Any] ?? []
let footerViewElements = currentViewController.tableView.tableFooterView?.getMoleculeViews { let footerViewElements = currentViewController.tableView.tableFooterView?.getMoleculeViews { (subView: MoleculeViewProtocol, _) in
(subView: MoleculeViewProtocol) in subView.accessibilityTraits.contains(type.trait) subView.accessibilityTraits.contains(type.trait)
} as? [Any] ?? [] } as? [Any] ?? []
let otherInteractiveElements = currentViewController.view?.getMoleculeViews(excludedViews: [tableView, tableView.tableFooterView, tableView.tableHeaderView] let otherInteractiveElements = currentViewController.view?.getMoleculeViews(excludedViews: [tableView, tableView.tableFooterView, tableView.tableHeaderView]
.compactMap { $0 }) { (subView: MoleculeViewProtocol) in .compactMap { $0 }) { (subView: MoleculeViewProtocol, _) in
subView.accessibilityTraits.contains(type.trait) subView.accessibilityTraits.contains(type.trait)
} as? [Any] ?? [] } as? [Any] ?? []
return headerViewElements + otherInteractiveElements + (rotorElements as [Any]) + footerViewElements return headerViewElements + otherInteractiveElements + (rotorElements as [Any]) + footerViewElements
} else if let currentViewController = template as? MoleculeStackTemplate { //Stack templates } else if let currentViewController = template as? MoleculeStackTemplate { //Stack templates
return currentViewController.view?.getMoleculeViews { return currentViewController.view?.getMoleculeViews { (subView: MoleculeViewProtocol, _) in
(subView: MoleculeViewProtocol) in subView.accessibilityTraits.contains(type.trait) subView.accessibilityTraits.contains(type.trait)
} }
} }
return nil return nil
@ -329,10 +330,10 @@ open class AccessibilityHandlerBehavior: PageVisibilityBehavior {
var rotorElement = elements[self.currentRotorIndex - 1] var rotorElement = elements[self.currentRotorIndex - 1]
if let element = rotorElement as? (model: MoleculeModelProtocol, indexPath: IndexPath) { if let element = rotorElement as? (model: MoleculeModelProtocol, indexPath: IndexPath) {
tableView.scrollToRow(at: element.indexPath, at: .middle, animated: false) tableView.scrollToRow(at: element.indexPath, at: .middle, animated: false)
rotorElement = tableView.cellForRow(at: element.indexPath)?.getMoleculeViews { rotorElement = tableView.cellForRow(at: element.indexPath)?.getMoleculeViews { (subView: MoleculeViewProtocol, stop: inout Bool) in
(subView: MoleculeViewProtocol) in subView.accessibilityTraits.contains(type.trait) guard subView.accessibilityTraits.contains(type.trait), (subView as? MoleculeViewModelProtocol)?.moleculeModel?.id == element.model.id else { return false }
}.filter { stop = true
($0 as? MoleculeViewModelProtocol)?.moleculeModel?.id == element.model.id return true
}.first as Any }.first as Any
} }
UIAccessibility.post(notification: .layoutChanged, argument: rotorElement) UIAccessibility.post(notification: .layoutChanged, argument: rotorElement)
@ -369,9 +370,17 @@ extension UIView {
} }
} }
func getMoleculeViews<T>(excludedViews: [UIView]? = nil, filter: ((T) -> Bool)) -> [T] { func getMoleculeViews<T>(excludedViews: [UIView]? = nil, filter: ((T, inout Bool) -> Bool)) -> [T] {
return getNestedSubviews(excludedViews: excludedViews).compactMap { var stop = false
filter($0) ? $0 : nil var results: [T] = []
for element: T in getNestedSubviews(excludedViews: excludedViews) {
if filter(element, &stop) {
results.append(element)
}
if stop {
break
}
} }
return results
} }
} }