removed accessibility notification for webpages.

This commit is contained in:
Krishna Kishore Bandaru 2023-07-26 20:13:08 +05:30
parent 646210f1a3
commit 9bbcd6a8ea

View File

@ -13,7 +13,7 @@ import WebKit
public enum AccessibilityNotificationType: String, Codable { public enum AccessibilityNotificationType: String, Codable {
case controllerChanged, layoutChanged, screenChanged, announcement, webPageChanged, webPageLoaded case controllerChanged, layoutChanged, screenChanged, announcement
//TODO: - Foucs is shifting to respective element only if we add delay only on new viewcontroller appear. Need to investigate futher. //TODO: - Foucs is shifting to respective element only if we add delay only on new viewcontroller appear. Need to investigate futher.
//https://developer.apple.com/forums/thread/132699, //https://developer.apple.com/forums/thread/132699,
@ -23,10 +23,6 @@ public enum AccessibilityNotificationType: String, Codable {
switch self { switch self {
case .controllerChanged: case .controllerChanged:
return 1.5 return 1.5
case .webPageLoaded:
return 2.0
case .screenChanged, .layoutChanged:
return 0.0
default: default:
return 0.0 return 0.0
} }
@ -40,8 +36,6 @@ public enum AccessibilityNotificationType: String, Codable {
return .screenChanged return .screenChanged
case .layoutChanged, .controllerChanged: case .layoutChanged, .controllerChanged:
return .layoutChanged return .layoutChanged
case .webPageChanged, .webPageLoaded:
return .layoutChanged
} }
} }
} }
@ -50,15 +44,13 @@ public typealias ArgumentHandler = ((NavigationOperationType?) -> Any?)
public class AccessbilityOperation: MVMCoreOperation { public class AccessbilityOperation: MVMCoreOperation {
private let operationType: NavigationOperationType private let argument: Any?
private let argumentHandler: ArgumentHandler?
private let notificationType: AccessibilityNotificationType private let notificationType: AccessibilityNotificationType
private var timerSource: DispatchSourceTimer? private var timerSource: DispatchSourceTimer?
public init(notificationType: AccessibilityNotificationType, operationType: NavigationOperationType = .default, argumentHandler: ArgumentHandler?) { public init(notificationType: AccessibilityNotificationType, argument: Any?) {
self.notificationType = notificationType self.notificationType = notificationType
self.argumentHandler = argumentHandler self.argument = argument
self.operationType = operationType
} }
public override func main() { public override func main() {
@ -70,8 +62,7 @@ public class AccessbilityOperation: MVMCoreOperation {
timerSource?.setEventHandler { timerSource?.setEventHandler {
Task { @MainActor [weak self] in Task { @MainActor [weak self] in
if !(self?.isCancelled ?? false), let notification = self?.notificationType.accessibilityNotification { if !(self?.isCancelled ?? false), let notification = self?.notificationType.accessibilityNotification {
print("argumentHandler \(self?.argumentHandler?(self?.operationType))") UIAccessibility.post(notification: notification, argument: self?.argument)
UIAccessibility.post(notification: notification, argument: self?.argumentHandler?(self?.operationType))
self?.markAsFinished() self?.markAsFinished()
} else { } else {
self?.stop() self?.stop()
@ -99,13 +90,13 @@ open class AccessibilityHandler {
} }
public weak var delegate: MVMCoreViewControllerProtocol? public weak var delegate: MVMCoreViewControllerProtocol?
public var previousAccessiblityElement: Any? public var previousAccessiblityElement: Any?
public var anyCancellable: Set<AnyCancellable> = []
private var accessibilityOperationQueue: OperationQueue = { private var accessibilityOperationQueue: OperationQueue = {
let queue = OperationQueue() let queue = OperationQueue()
queue.maxConcurrentOperationCount = 1 queue.maxConcurrentOperationCount = 1
return queue return queue
}() }()
private var anyCancellable: Set<AnyCancellable> = []
private var accessibilityId: String? private var accessibilityId: String?
private var announcementText: String? private var announcementText: String?
private var hasTopNotitificationInPage: Bool = false private var hasTopNotitificationInPage: Bool = false
@ -127,7 +118,7 @@ open class AccessibilityHandler {
} }
/// Registers to know when pages change. /// Registers to know when pages change.
private func registerForPageChanges() { open func registerForPageChanges() {
MVMCoreNavigationHandler.shared()?.addDelegate(self) MVMCoreNavigationHandler.shared()?.addDelegate(self)
} }
@ -135,8 +126,6 @@ open class AccessibilityHandler {
//Since foucs shifted to other elements cancelling existing focus shift notifications if any //Since foucs shifted to other elements cancelling existing focus shift notifications if any
NotificationCenter.default.publisher(for: UIAccessibility.elementFocusedNotification) NotificationCenter.default.publisher(for: UIAccessibility.elementFocusedNotification)
.sink { [weak self] notification in .sink { [weak self] notification in
print("testing \(UIAccessibility.focusedElement(using: .notificationVoiceOver))")
print("testing \(notification.userInfo)")
self?.cancelAllOperations() self?.cancelAllOperations()
}.store(in: &anyCancellable) }.store(in: &anyCancellable)
} }
@ -145,7 +134,7 @@ open class AccessibilityHandler {
NotificationHandler.shared()?.onNotificationWillShow NotificationHandler.shared()?.onNotificationWillShow
.sink { [weak self] (_, model) in .sink { [weak self] (_, model) in
self?.hasTopNotitificationInPage = true self?.hasTopNotitificationInPage = true
self?.capturePreviousFocusElement(for: model.molecule) self?.capturePreviousFocusElement()
}.store(in: &anyCancellable) }.store(in: &anyCancellable)
NotificationHandler.shared()?.onNotificationShown NotificationHandler.shared()?.onNotificationShown
.sink { [weak self] (view, model) in .sink { [weak self] (view, model) in
@ -157,15 +146,15 @@ open class AccessibilityHandler {
}.store(in: &anyCancellable) }.store(in: &anyCancellable)
NotificationHandler.shared()?.onNotificationDismissed NotificationHandler.shared()?.onNotificationDismissed
.sink { [weak self] (view, model) in .sink { [weak self] (view, model) in
self?.postAccessbilityToPrevElement(for: model.molecule) self?.postAccessbilityToPrevElement()
}.store(in: &anyCancellable) }.store(in: &anyCancellable)
} }
open func capturePreviousFocusElement(for model: MoleculeModelProtocol) { open func capturePreviousFocusElement() {
previousAccessiblityElement = UIAccessibility.focusedElement(using: .notificationVoiceOver) previousAccessiblityElement = UIAccessibility.focusedElement(using: .notificationVoiceOver)
} }
open func postAccessbilityToPrevElement(for model: MoleculeModelProtocol) { open func postAccessbilityToPrevElement() {
post(notification: .layoutChanged, argument: previousAccessiblityElement) post(notification: .layoutChanged, argument: previousAccessiblityElement)
} }
@ -177,15 +166,9 @@ open class AccessibilityHandler {
accessibilityOperationQueue.cancelAllOperations() accessibilityOperationQueue.cancelAllOperations()
} }
open func post(webpageChanged type: AccessibilityNotificationType) { public func post(notification type: AccessibilityNotificationType, argument: Any? = nil) {
post(notification: type)
}
public func post(notification type: AccessibilityNotificationType, operationType: NavigationOperationType = .default, argument: Any? = nil) {
guard UIAccessibility.isVoiceOverRunning else { return } guard UIAccessibility.isVoiceOverRunning else { return }
let accessbilityOperation = AccessbilityOperation(notificationType: type, operationType: operationType) { [weak self] in let accessbilityOperation = AccessbilityOperation(notificationType: type, argument: argument)
($0 == .tab) ? self?.getFirstFocusedElementOnScreen() : argument
}
add(operation: accessbilityOperation) add(operation: accessbilityOperation)
} }
@ -204,8 +187,7 @@ extension AccessibilityHandler: MVMCorePresentationDelegateProtocol {
previousAccessiblityElement = nil previousAccessiblityElement = nil
delegate = viewController as? MVMCoreViewControllerProtocol delegate = viewController as? MVMCoreViewControllerProtocol
if let announcementText { if let announcementText {
let accessbilityOperation = AccessbilityOperation(notificationType: .announcement) { _ in announcementText } post(notification: .announcement, argument: announcementText)
add(operation: accessbilityOperation)
} }
} }
@ -213,17 +195,13 @@ extension AccessibilityHandler: MVMCorePresentationDelegateProtocol {
open func navigationController(_ navigationController: UINavigationController, displayedViewController viewController: UIViewController) { open func navigationController(_ navigationController: UINavigationController, displayedViewController viewController: UIViewController) {
guard UIAccessibility.isVoiceOverRunning, guard UIAccessibility.isVoiceOverRunning,
canPostAccessbilityNotification(for: viewController) else { return } canPostAccessbilityNotification(for: viewController) else { return }
//TODO: - For Tabbar change: adding 1.5 sec delay to shift focus to the top. for Temp fix added to check on childern count //TODO: - For Tabbar change: adding 1.5 sec delay to shift focus to the top. for Temp fix added to check on childern count. If we have top notification in page on pageLoad, we have postnotification for shifting the focus so in this case we are not posting accessiblity notifcation
/*var navigationOperationType: NavigationType = .push
if let presentationStyle = delegate?.loadObject??.pageJSON?.optionalStringForKey(KeyPresentationStyle) ?? delegate?.loadObject??.requestParameters?.actionMap?.optionalStringForKey(KeyPresentationStyle), presentationStyle == "root" {
navigationOperationType = .set
}*/
if hasTopNotitificationInPage { if hasTopNotitificationInPage {
previousAccessiblityElement = getFirstFocusedElementOnScreen() previousAccessiblityElement = getFirstFocusedElementOnScreen()
} else { } else {
let accessbilityElement = getAccessbilityFocusedElement() let accessbilityElement = getAccessbilityFocusedElement()
let operationType: NavigationOperationType = navigationController.children.count == 1 ? .tab : .default //TODO: - need to identify the operationType let operationType: NavigationOperationType = navigationController.children.count == 1 ? .tab : .default //TODO: - need to identify the operationType
post(notification: operationType == .tab ? .controllerChanged : .layoutChanged, operationType: operationType, argument: accessbilityElement ?? getFirstFocusedElementOnScreen()) post(notification: operationType == .tab ? .controllerChanged : .layoutChanged, argument: accessbilityElement ?? getFirstFocusedElementOnScreen())
accessibilityId = nil accessibilityId = nil
} }
} }