This PR contains the work done to implement the necessary protocols and router to implement the Coordinators pattern in the app. Reviewed-on: #15 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
56 lines
1.4 KiB
Swift
56 lines
1.4 KiB
Swift
//
|
|
// 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)
|
|
}
|
|
|
|
}
|