From b7413d536bda45ab3be72f65aec5b32ae13c68bb Mon Sep 17 00:00:00 2001 From: Kyle Matthew Hedden Date: Mon, 5 Dec 2022 19:58:38 -0500 Subject: [PATCH] shifted behavior calls to extension for better support. added molecular setup callback for navigation titleView. --- .../Protocols/PageBehaviorProtocol.swift | 4 +-- .../NavigationController.swift | 14 +------- .../UINavigationController+Extension.swift | 34 +++++++++++++++---- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/MVMCoreUI/Behaviors/Protocols/PageBehaviorProtocol.swift b/MVMCoreUI/Behaviors/Protocols/PageBehaviorProtocol.swift index 10dc3020..d5f65323 100644 --- a/MVMCoreUI/Behaviors/Protocols/PageBehaviorProtocol.swift +++ b/MVMCoreUI/Behaviors/Protocols/PageBehaviorProtocol.swift @@ -31,7 +31,7 @@ public protocol PageMoleculeTransformationBehavior: PageBehaviorProtocol { func willSetupMolecule(with model: MoleculeModelProtocol, updating view: MoleculeViewProtocol?) func didSetupMolecule(view: MoleculeViewProtocol, withModel: MoleculeModelProtocol) func willSetupNavigationBar(with model: NavigationItemModelProtocol, updating view: UINavigationBar) - func didSetupNavigationBar(view: UINavigationBar, withModel: NavigationItemModelProtocol) + func didSetupNavigationBar(view: UINavigationBar, with model: NavigationItemModelProtocol) } public extension PageMoleculeTransformationBehavior { @@ -40,7 +40,7 @@ public extension PageMoleculeTransformationBehavior { func willSetupMolecule(with model: MoleculeModelProtocol, updating view: MoleculeViewProtocol?) {} func didSetupMolecule(view: MoleculeViewProtocol, withModel: MoleculeModelProtocol) {} func willSetupNavigationBar(with model: NavigationItemModelProtocol, updating view: UINavigationBar) {} - func didSetupNavigationBar(view: UINavigationBar, withModel: NavigationItemModelProtocol) {} + func didSetupNavigationBar(view: UINavigationBar, with model: NavigationItemModelProtocol) {} } public protocol PageVisibilityBehavior: PageBehaviorProtocol { diff --git a/MVMCoreUI/Containers/NavigationController/NavigationController.swift b/MVMCoreUI/Containers/NavigationController/NavigationController.swift index 329ff423..eb43201a 100644 --- a/MVMCoreUI/Containers/NavigationController/NavigationController.swift +++ b/MVMCoreUI/Containers/NavigationController/NavigationController.swift @@ -87,23 +87,11 @@ extension NavigationController: MVMCoreViewManagerProtocol { private func updateNavigationView(with model: NavigationItemModelProtocol, for viewController: UIViewController) { guard let topViewController = topViewController else { return } - if let pageBehaviorController = viewController as? PageBehaviorHandlerProtocol { - pageBehaviorController.executeBehaviors { (behavior: PageMoleculeTransformationBehavior) in - behavior.willSetupNavigationBar(with: model, updating: navigationBar) - } - } - - setNavigationItem(with: model, for: topViewController) + setNavigationItem(with: model, for: topViewController, coordinatingWith: viewController as? PageBehaviorHandlerProtocol) setNavigationBarUI(with: model) navigationBar.setNeedsLayout() navigationBar.layoutIfNeeded() - - if let pageBehaviorController = viewController as? PageBehaviorHandlerProtocol { - pageBehaviorController.executeBehaviors { (behavior: PageMoleculeTransformationBehavior) in - behavior.didSetupNavigationBar(view: navigationBar, withModel: model) - } - } } public func displayedViewController(_ viewController: UIViewController) { diff --git a/MVMCoreUI/Containers/NavigationController/UINavigationController+Extension.swift b/MVMCoreUI/Containers/NavigationController/UINavigationController+Extension.swift index e63028d4..9a0665b9 100644 --- a/MVMCoreUI/Containers/NavigationController/UINavigationController+Extension.swift +++ b/MVMCoreUI/Containers/NavigationController/UINavigationController+Extension.swift @@ -11,13 +11,24 @@ import Foundation public extension UINavigationController { /// Convenience function for setting the navigation item. - func setNavigationItem(with model: NavigationItemModelProtocol, for viewController: UIViewController) { + func setNavigationItem(with model: NavigationItemModelProtocol, for viewController: UIViewController, coordinatingWith pageBehaviorController: PageBehaviorHandlerProtocol? = nil) { + + let behaviorHandler = pageBehaviorController ?? viewController as? PageBehaviorHandlerProtocol; + + behaviorHandler?.executeBehaviors { (behavior: PageMoleculeTransformationBehavior) in + behavior.willSetupNavigationBar(with: model, updating: navigationBar) + } + viewController.navigationItem.title = model.title viewController.navigationItem.accessibilityLabel = model.title viewController.navigationItem.hidesBackButton = model.hidesSystemBackButton viewController.navigationItem.leftItemsSupplementBackButton = !model.hidesSystemBackButton setNavigationButtons(with: model, for: viewController) - setNavigationTitleView(with: model, for: viewController) + setNavigationTitleView(with: model, for: viewController, coordinatingWith: pageBehaviorController) + + behaviorHandler?.executeBehaviors { (behavior: PageMoleculeTransformationBehavior) in + behavior.didSetupNavigationBar(view: navigationBar, with: model) + } } /// Convenience function for setting the navigation buttons. @@ -48,11 +59,22 @@ public extension UINavigationController { } /// Convenience function for setting the navigation titleView. - func setNavigationTitleView(with model: NavigationItemModelProtocol, for viewController: UIViewController) { + func setNavigationTitleView(with model: NavigationItemModelProtocol, for viewController: UIViewController, coordinatingWith pageBehaviorController: PageBehaviorHandlerProtocol? = nil) { + guard let titleViewModel = model.titleView else { return } + let delegate = (viewController as? MVMCoreViewControllerProtocol)?.delegateObject?() as? MVMCoreUIDelegateObject - if let titleViewModel = model.titleView, - let molecule = ModelRegistry.createMolecule(titleViewModel, delegateObject: delegate, additionalData: nil) { - viewController.navigationItem.titleView = molecule + + let behaviorHandler = pageBehaviorController ?? viewController as? PageBehaviorHandlerProtocol; + behaviorHandler?.executeBehaviors { (behavior: PageMoleculeTransformationBehavior) in + behavior.willSetupMolecule(with: titleViewModel, updating: nil) + } + + guard let molecule = ModelRegistry.createMolecule(titleViewModel, delegateObject: delegate, additionalData: nil) else { return } + + viewController.navigationItem.titleView = molecule + + behaviorHandler?.executeBehaviors { (behavior: PageMoleculeTransformationBehavior) in + behavior.didSetupMolecule(view: molecule, withModel: titleViewModel) } }