[Library] Coordination library (#15)

This PR contains the work done to implement the necessary protocols and router to implement the Coordinators pattern in the app.

Reviewed-on: rock-n-code/app-reviews#15
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit was merged in pull request #15.
This commit is contained in:
2024-03-21 16:40:30 +00:00
committed by javier
parent 394245dd29
commit 60cab50c1e
11 changed files with 534 additions and 0 deletions
@@ -0,0 +1,57 @@
//
// Coordinator.swift
// ReviewsCoordinationKit
//
// Created by Javier Cicchelli on 21/03/2024.
// Copyright © 2024 Röck+Cöde. All rights reserved.
//
public protocol Coordinator: AnyObject {
// MARK: Properties
var children: [Coordinator] { get set }
var router: Router { get }
// MARK: Functions
func present(animated: Bool, onDismiss: Router.OnDismissClosure?)
}
// MARK: - Implementations
public extension Coordinator {
// MARK: Functions
func present(
child: Coordinator,
animated: Bool,
onDismiss: Router.OnDismissClosure? = nil
) {
store(child)
child.present(animated: animated) { [weak self, weak child] in
guard let self, let child else { return }
self.free(child)
onDismiss?()
}
}
func dismiss(animated: Bool) {
router.dismiss(animated: animated)
}
}
// MARK: - Helpers
private extension Coordinator {
// MARK: Functions
func store(_ coordinator: Coordinator) {
children.append(coordinator)
}
func free(_ coordinator: Coordinator) {
children = children.filter { $0 !== coordinator }
}
}
@@ -0,0 +1,42 @@
//
// Router.swift
// ReviewsCoordinationKet
//
// Created by Javier Cicchelli on 21/03/2024.
// Copyright © 2024 Röck+Cöde. All rights reserved.
//
import UIKit
public protocol Router: AnyObject {
// MARK: Type aliases
typealias OnDismissClosure = () -> Void
// MARK: Functions
func present(
_ viewController: UIViewController,
animated: Bool,
onDismiss: OnDismissClosure?
)
func dismiss(animated: Bool)
}
// MARK: - Implementations
public extension Router {
// MARK: Functions
func present(
_ viewController: UIViewController,
animated: Bool
) {
present(
viewController,
animated: animated,
onDismiss: nil
)
}
}
@@ -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)
}
}
@@ -0,0 +1,56 @@
//
// File.swift
// ReviewsCoordinationKit
//
// Created by Javier Cicchelli on 21/03/2024.
// Copyright © 2024 Röck+Cöde. All rights reserved.
//
import UIKit
public class PushRouter: NavigationRouter {
// MARK: Constants
private let rootViewController: UIViewController?
// MARK: Initialisers
public init(
navigationController: UINavigationController,
rootViewController: UIViewController? = nil
) {
self.rootViewController = navigationController.viewControllers.first ?? rootViewController
super.init(navigationController: navigationController)
}
}
// MARK: - Router
extension PushRouter: Router {
// MARK: Functions
public func present(
_ viewController: UIViewController,
animated: Bool,
onDismiss: OnDismissClosure?
) {
onDismissForViewController[viewController] = onDismiss
navigationController.pushViewController(viewController, animated: animated)
}
public func dismiss(animated: Bool) {
guard let rootViewController else {
navigationController.popViewController(animated: animated)
return
}
performOnDismiss(for: rootViewController)
navigationController.popToViewController(
rootViewController,
animated: animated
)
}
}
@@ -0,0 +1,35 @@
//
// WindowRouter.swift
// ReviewsCoordinationKit
//
// Created by Javier Cicchelli on 21/03/2024.
// Copyright © 2024 Röck+Cöde. All rights reserved.
//
import UIKit
public class WindowRouter: Router {
// MARK: Constants
private let window: UIWindow?
// MARK: Initialisers
public init(window: UIWindow?) {
self.window = window
}
// MARK: Functions
public func present(
_ viewController: UIViewController,
animated: Bool,
onDismiss: OnDismissClosure?
) {
window?.rootViewController = viewController
window?.makeKeyAndVisible()
}
public func dismiss(animated: Bool) {
// Nothing to do here...
}
}