// // TopNotificationHandler.swift // MVMCoreUI // // Created by Scott Pfeil on 4/11/23. // Copyright © 2023 Verizon Wireless. All rights reserved. // import MVMCore import Dispatch import Combine public protocol NotificationTransitionDelegateProtocol { @MainActor func show(notification: UIView) async @MainActor func hide(notification: UIView) async @MainActor func update(with model: TopNotificationModel) } public class NotificationOperation: MVMCoreOperation { public let notification: UIView public var notificationModel: TopNotificationModel /// The delegate that manages transitioning the notification. private let transitionDelegate: NotificationTransitionDelegateProtocol /// The notification animation transition operation (show or hide). private var transitionOperation: MVMCoreOperation? /// The stop timer for non-persistent notifications. private var timerSource: DispatchSourceTimer? /// Determines if the operation is ready. Certain notifications are only meant to be displayed on certain pages. public var isDisplayable: Bool { get { var isDisplayable: Bool = true displayableQueue.sync { isDisplayable = _isDisplayable } return isDisplayable } set { guard newValue != isDisplayable else { return } displayableQueue.async(flags: .barrier) { [weak self] in self?._isDisplayable = newValue } } } private var displayableQueue = DispatchQueue(label: "displayable", attributes: .concurrent) private var _isDisplayable: Bool = true { willSet { guard super.isReady else { return } willChangeValue(for: \.isReady) } didSet { guard super.isReady else { return } didChangeValue(for: \.isReady) } } /// This operation is ready only if this notification is allowed to show. public override var isReady: Bool { get { guard !isCancelled else { return super.isReady } return super.isReady && isDisplayable } } public actor Properties { private var isDisplayed: Bool = false private var isAnimating: Bool = false fileprivate func set(displayed: Bool) { isDisplayed = displayed } public func getIsDisplayed() -> Bool { return isDisplayed } fileprivate func set(animating: Bool) { isAnimating = animating } public func getIsAnimating() -> Bool { return isAnimating } } public var properties = Properties() // A flag for tracking if the operation needs to be re-added because it was cancelled for a higher priority notification. public var reAddAfterCancel = false public init(with notification: UIView, notificationModel: TopNotificationModel, transitionDelegate: NotificationTransitionDelegateProtocol) { self.notification = notification self.notificationModel = notificationModel self.transitionDelegate = transitionDelegate super.init() queuePriority = notificationModel.priority } public override func main() { guard !checkAndHandleForCancellation() else { return } add { [weak self] in guard let self = self else { return } await self.showNotification() guard !self.isCancelled else { // Cancelled, dismiss immediately. self.stop() return } self.updateStopTimer() } } public func stop() { if let timerSource = timerSource { timerSource.cancel() } Task { guard await properties.getIsDisplayed(), await !properties.getIsAnimating() else { return } add { [weak self] in guard let self = self else { return } await self.hideNotification() guard !self.isCancelled, !self.notificationModel.persistent else { return } self.markAsFinished() } } } /// Adds the transition of the notification to the queue. private func add(transition: @escaping () async -> Void) { Task { guard await properties.getIsDisplayed(), await !properties.getIsAnimating() else { return } transitionOperation = MVMCoreBlockOperation(block: { blockOperation in guard !blockOperation.checkAndHandleForCancellation() else { return } Task { await transition() blockOperation.markAsFinished() } }) transitionOperation?.completionBlock = { [weak self] in self?.transitionOperation = nil } // Add the animation to the navigation queue to avoid animation collisions. await MVMCoreNavigationHandler.shared()?.addNavigationOperation(transitionOperation!) } } private func updateStopTimer() { if let timerSource = timerSource { timerSource.cancel() } guard !notificationModel.persistent else { return } timerSource = DispatchSource.makeTimerSource() timerSource?.setEventHandler(handler: { [weak self] in print("SSSS TIMER EVENT FIRED FOR: \(String(describing: self?.notificationModel.type))") guard let self = self, !self.isFinished, !self.checkAndHandleForCancellation() else { return } /* // If accessible and focused, do not collapse until unfocused. if (!forceful && [MVMCoreUIUtility viewContainsAccessiblityFocus:self]) { self.hideCompletionHandler = completionHandler; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accessibilityFocusChanged:) name:UIAccessibilityElementFocusedNotification object:nil]; return; } */ self.stop() }) timerSource?.setCancelHandler(handler: { [weak self] in print("SSSS TIMER EVENT CANCELLED FOR: \(String(describing: self?.notificationModel.type))") }) timerSource?.schedule(deadline: .now() + .seconds(notificationModel.dismissTime)) } public override func cancel() { super.cancel() Task { if await !properties.getIsDisplayed() { // Cancel any pending show transitions. transitionOperation?.cancel() } // Do nothing if animating. guard await !properties.getIsAnimating() else { return } if await properties.getIsDisplayed() { stop() } else if isExecuting { markAsFinished() } } } @MainActor private func showNotification() async { await properties.set(animating: true) await transitionDelegate.show(notification: notification) await properties.set(displayed: true) await properties.set(animating: false) NotificationHandler.shared().onNotificationShown.send((notification, notificationModel)) } @MainActor private func hideNotification() async { await properties.set(animating: true) await transitionDelegate.hide(notification: notification) await properties.set(displayed: false) await properties.set(animating: false) NotificationHandler.shared().onNotificationDismissed.send((notification, notificationModel)) } /// Updates the notification with the new model. public func update(with model: TopNotificationModel) { self.notificationModel = model queuePriority = model.priority guard isExecuting, !isCancelled else { return } updateStopTimer() Task { @MainActor in transitionDelegate.update(with: notificationModel) } } func copy(with zone: NSZone? = nil) -> Any { let operation = NotificationOperation(with: notification, notificationModel: notificationModel, transitionDelegate: transitionDelegate) operation.reAddAfterCancel = reAddAfterCancel operation.isDisplayable = isDisplayable for dependency in dependencies { operation.addDependency(dependency) } operation.name = name operation.qualityOfService = qualityOfService return operation } } public class NotificationHandler { /// The operation queue of top notification operations. private var queue = OperationQueue() public var transitionDelegate: NotificationTransitionDelegateProtocol private var delegateObject: MVMCoreUIDelegateObject? /// Publishes when a notification is shown. public let onNotificationShown = PassthroughSubject<(UIView, TopNotificationModel), Never>() /// Publishes when a notification is dismissed. public let onNotificationDismissed = PassthroughSubject<(UIView, TopNotificationModel), Never>() /// Returns the handler stored in the CoreUIObject public static func shared() -> Self { return MVMCoreActionUtility.fatalClassCheck(object: CoreUIObject.sharedInstance()?.topNotificationHandler) } public init(with transitionDelegate: NotificationTransitionDelegateProtocol) { self.transitionDelegate = transitionDelegate registerWithNotificationCenter() registerForPageChanges() } // MARK: - JSON Handling /// Registers with the notification center to know when json is updated. private func registerWithNotificationCenter() { NotificationCenter.default.addObserver(self, selector: #selector(responseJSONUpdated(notification:)), name: NSNotification.Name(rawValue: NotificationResponseLoaded), object: nil) } /// Registers to know when pages change. private func registerForPageChanges() { MVMCoreNavigationHandler.shared()?.addDelegate(self) } /// Checks for new top alert json @objc private func responseJSONUpdated(notification: Notification) async { guard let loadObject = (notification.userInfo?[String(describing: MVMCoreLoadObject.self)] as? MVMCoreLoadObject) else { return } // Dismiss any top alerts that server wants us to dismiss/ if let disableType = loadObject.responseInfoMap?.optionalStringForKey("disableType") { NotificationHandler.shared().hideTopAlertView(of: disableType) } // Show any new top alert. guard let responseJSON = loadObject.responseJSON, let json = responseJSON.optionalDictionaryForKey(KeyTopAlert) else { return } Task { await showNotification(for: json, delegateObject: delegateObject) } } public func showNotification(for json: [AnyHashable: Any], delegateObject: MVMCoreUIDelegateObject?) async { do { let model = try TopNotificationModel.decode(json: json, delegateObject: delegateObject) try await showNotification(for: model, delegateObject: delegateObject) } catch { if let errorObject = MVMCoreErrorObject.createErrorObject(for: error, location: "\(self)") { MVMCoreUILoggingHandler.addError(toLog: errorObject) } } } // MARK: - Operation Handling /// Adds the operation to the queue. private func add(operation: NotificationOperation) { operation.completionBlock = { [weak self] in // If the alert was cancelled to show another with higher priority, re-add to the operation when cancelled to the queue. if operation.reAddAfterCancel { let newOperation: NotificationOperation = operation.copy() as! NotificationOperation newOperation.reAddAfterCancel = false self?.add(operation: newOperation) } operation.completionBlock = nil } let currentPageType = (MVMCoreUISplitViewController.main()?.getCurrentDetailViewController() as? MVMCoreViewControllerProtocol)?.pageType operation.updateDisplayable(by: currentPageType) queue.addOperation(operation) reevaluteQueue() } /// Checks for existing top alert object of same type and updates it. Only happens for molecular top alerts. Returns true if we updated. private func checkAndUpdateExisting(with model: TopNotificationModel) -> Bool { for case let operation as NotificationOperation in queue.operations { guard operation.notificationModel.type == model.type else { continue } operation.update(with: model) let pageType = (MVMCoreUISplitViewController.main()?.getCurrentDetailViewController() as? MVMCoreViewControllerProtocol)?.pageType operation.updateDisplayable(by: pageType) reevaluteQueue() return true } return false } /// Re-evaluates the queue operations private func reevaluteQueue() { var highestReadyOperation: NotificationOperation? var executingOperation: NotificationOperation? for case let operation as NotificationOperation in queue.operations { guard !operation.isCancelled, !operation.isFinished else { continue } if operation.isReady, highestReadyOperation == nil || operation.queuePriority.rawValue > highestReadyOperation!.queuePriority.rawValue { highestReadyOperation = operation } if operation.isExecuting { executingOperation = operation } } guard let currentOperation = executingOperation else { return } // Cancel the executing operation if it is no longer ready to run. Re-add for later if it is persistent. guard currentOperation.isReady else { currentOperation.reAddAfterCancel = currentOperation.notificationModel.persistent currentOperation.cancel() return } // If the highest priority operation is not executing, and the executing operation is persistent, cancel it. if let newOperation = highestReadyOperation, currentOperation != newOperation, currentOperation.notificationModel.persistent { currentOperation.reAddAfterCancel = true currentOperation.cancel() } } // MARK: - Show and hide public func isTopAlertShowing() -> Bool { return queue.operations.first(where: { operation in return operation.isExecuting }) != nil } public func hasPersistentTopAlert(of type: String) -> Bool { return queue.operations.first(where: { operation in guard operation.isExecuting, let operation = operation as? NotificationOperation else { return false } return operation.notificationModel.persistent && operation.notificationModel.type == type }) as? NotificationOperation != nil } /// Creates the view and queues up the notification. public func showNotification(for model: TopNotificationModel, delegateObject: MVMCoreUIDelegateObject? = nil) async throws { guard !checkAndUpdateExisting(with: model) else { return } let view = try await createMolecule(for: model, delegateObject: delegateObject) let operation = NotificationOperation(with: view, notificationModel: model, transitionDelegate: transitionDelegate) NotificationHandler.shared().add(operation: operation) } /// Cancel the current top alert view. public func hideTopAlertView() { guard let currentOperation = queue.operations.first(where: { operation in return operation.isExecuting }) as? NotificationOperation else { return } currentOperation.notificationModel.persistent = false currentOperation.reAddAfterCancel = false currentOperation.cancel() } /// Cancel all operations of this type. public func hideTopAlertView(of type: String) { for operation in queue.operations { guard let operation = operation as? NotificationOperation, operation.notificationModel.type == type else { continue } operation.reAddAfterCancel = false operation.cancel() } } /// Cancel all persistent operations of this type. public func hidePersistentTopAlertView(of type: String) { for operation in queue.operations { guard let operation = operation as? NotificationOperation, operation.notificationModel.persistent, operation.notificationModel.type == type else { continue } operation.reAddAfterCancel = false operation.cancel() } } /// Finds an cancels top alerts associated with the object. public func removeTopAlert(for object: TopNotificationModel) { for operation in queue.operations { guard let operation = operation as? NotificationOperation, operation.notificationModel.id == object.id else { return } operation.reAddAfterCancel = false operation.cancel() } } public func removeAllTopAlerts() { queue.cancelAllOperations() } public func getCurrentNotification() async -> (UIView, TopNotificationModel)? { for operation in queue.operations { guard operation.isExecuting, let operation = operation as? NotificationOperation, await operation.properties.getIsDisplayed() else { continue } return (operation.notification, operation.notificationModel) } return nil } /// Creates and returns the molecule view. @MainActor private func createMolecule(for model: TopNotificationModel, delegateObject: MVMCoreUIDelegateObject? = nil) throws -> UIView { do { guard let molecule = ModelRegistry.createMolecule(model.molecule, delegateObject: delegateObject, additionalData: nil) else { throw ModelRegistry.Error.decoderOther(message: "Molecule not mapped") } return molecule } } } extension NotificationHandler: MVMCorePresentationDelegateProtocol { // Update displayable for each top alert operation when page type changes, in top queue priority order. public func navigationController(_ navigationController: UINavigationController, displayedViewController viewController: UIViewController) { guard queue.operations.count > 0 else { return } let viewController = MVMCoreUIUtility.getViewControllerTraversingManagers(viewController) guard viewController == MVMCoreUISplitViewController.main()?.getCurrentViewController() else { return } let pageType = (viewController as? MVMCoreViewControllerProtocol)?.pageType queue.operations.compactMap { $0 as? NotificationOperation }.sorted { $0.queuePriority.rawValue > $1.queuePriority.rawValue }.forEach { $0.updateDisplayable(by: pageType) } reevaluteQueue() } } extension NotificationOperation { /// Updates if the operation is displayable based on the page type. func updateDisplayable(by pageType: String?) { guard let pages = notificationModel.pages else { isDisplayable = true return } guard let pageType = pageType else { isDisplayable = false return } isDisplayable = pages.contains(pageType) } }