Implemented the NavigationRouter router in the Coordination library.

This commit is contained in:
Javier Cicchelli 2024-03-21 17:38:01 +01:00
parent 16de0feec6
commit 09cd434237

View File

@ -0,0 +1,55 @@
//
// NavigationRouter.swift
// ReviewsCoordinationKit
//
// Created by Javier Cicchelli on 21/03/2024.
// Copyright © 2024 Röck+Cöde. All rights reserved.
//
import UIKit
public class NavigationRouter: NSObject {
// MARK: Properties
var navigationController: UINavigationController
var onDismissForViewController: [UIViewController: Router.OnDismissClosure] = [:]
// MARK: Initialisers
init(navigationController: UINavigationController) {
self.navigationController = navigationController
super.init()
self.navigationController.delegate = self
}
// MARK: Functions
func performOnDismiss(for viewController: UIViewController) {
guard let onDismiss = onDismissForViewController[viewController] else {
return
}
onDismiss()
onDismissForViewController[viewController] = nil
}
}
// MARK: - UINavigationControllerDelegate
extension NavigationRouter: UINavigationControllerDelegate {
// MARK: Functions
public func navigationController(
_ navigationController: UINavigationController,
didShow viewController: UIViewController,
animated: Bool
) {
guard let viewControllerToDismiss = navigationController.transitionCoordinator?.viewController(forKey: .from) else {
return
}
performOnDismiss(for: viewControllerToDismiss)
}
}