swift-libs/Sources/Coordination/Routers/BaseNavigationRouter.swift
Javier Cicchelli c4d09cd808 [Setup] Non Apple platforms (#13)
This PR contains the work done to address the issue #12, with regards to provide basic support of non Apple platforms.

To provide further details about the work done:
- [x] flattened the folder structure, especially now that the idea to filter folders based on platform is being discarded;
- [x] implemented precompiler processors to filter out platform-specific source code;
- [x] updated the `Package` file to provide basic support for non-Apple platforms;
- [x] added and also improved some targets to the `Makefile` file to smooth the current development workflows;
- [x] updated the `.gitignore` file with references to the `.vscode` folder and the `.env` file;
- [x] updated the Swift tools version to v5.7.

Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Reviewed-on: #13
2023-04-28 17:37:09 +00:00

75 lines
2.4 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftLibs open source project
//
// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors
// Licensed under the EUPL 1.2 or later.
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftLibs project authors
//
//===----------------------------------------------------------------------===//
#if os(iOS)
import UIKit
/// This is a base class for the `NavigationRouter` concrete router implementations.
public class BaseNavigationRouter: NSObject {
// MARK: Properties
/// A navigation controller to use within this concrete router.
var navigationController: UINavigationController
/// Dictionary that persist `onDismiss` closure for its respective view controllers until one of the later is dismissed.
var onDismissForViewController: [UIViewController: Router.OnDismissedClosure] = [:]
// MARK: Initialisers
/// Initialise this router.
/// - Parameter navigationController: A `UINavigationController` navigation controller instance to use in this router.
init(navigationController: UINavigationController) {
self.navigationController = navigationController
super.init()
self.navigationController.delegate = self
}
// MARK: Functions
/// Executes the `onDismiss` closure for a given view controller.
/// - Parameter viewController: A `UIViewController` view controller instance for which the on dismiss closure will be executed.
func performOnDismissed(for viewController: UIViewController) {
guard let onDismiss = onDismissForViewController[viewController] else {
return
}
onDismiss()
onDismissForViewController[viewController] = nil
}
}
// MARK: - UINavigationControllerDelegate
extension BaseNavigationRouter: UINavigationControllerDelegate {
// MARK: Functions
public func navigationController(
_ navigationController: UINavigationController,
didShow viewController: UIViewController,
animated: Bool
) {
guard let dismissedViewController = navigationController.transitionCoordinator?.viewController(forKey: .from) else {
return
}
performOnDismissed(for: dismissedViewController)
}
}
#endif