// // Copyright (c) 2023 Touch Instinct // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the Software), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // import Foundation import TISwiftUtils import UIKit open class BaseNavigationStackDeeplinkHandler: DeeplinkHandler { public var rootViewController: UIViewController public var asAnyHandlerClosure: Closure?> public init(rootViewController: UIViewController, asAnyHandlerClosure: @escaping Closure?>) { self.rootViewController = rootViewController self.asAnyHandlerClosure = asAnyHandlerClosure } // MARK: - DeeplinkHandler open func handle(deeplink: Deeplink) -> Operation? { if let handler = asAnyHandlerClosure(rootViewController), let operation = handler.handle(deeplink: deeplink) { return operation } let (_, operation) = findHandler(for: deeplink, in: rootViewController) return operation } // MARK: - Open methods open func findHandler(for deeplink: Deeplink, in viewController: UIViewController) -> (AnyDeeplinkHandler?, Operation?) { switch viewController { case let navController as UINavigationController: for controllerInNavigationStack in navController.viewControllers.reversed() { if let handler = asAnyHandlerClosure(controllerInNavigationStack), let operation = handler.handle(deeplink: deeplink) { return (handler, operation) } } case let tabController as UITabBarController: if let selectedController = tabController.selectedViewController, let handler = asAnyHandlerClosure(selectedController), let operation = handler.handle(deeplink: deeplink) { return (handler, operation) } else if var tabControllers = tabController.viewControllers { if tabController.selectedIndex != NSNotFound { tabControllers.remove(at: tabController.selectedIndex) } for tabController in tabControllers { if let handler = asAnyHandlerClosure(tabController), let operation = handler.handle(deeplink: deeplink) { return (handler, operation) } } } default: let handler = asAnyHandlerClosure(viewController) let operation = handler?.handle(deeplink: deeplink) return (handler, operation) } return (nil, nil) } }