Defined the Coordinator and the Router public protocols.

This commit is contained in:
Javier Cicchelli 2023-04-16 14:39:41 +02:00
parent 77a2472dbf
commit 3ed652061b
4 changed files with 380 additions and 0 deletions

View File

@ -0,0 +1,76 @@
//
// Coordinator.swift
// Coordinator
//
// Created by Javier Cicchelli on 11/04/2023.
// Copyright © 2023 Röck+Cöde. All rights reserved.
//
/// This protocol organize the flow logic between view controllers in the app.
public protocol Coordinator: AnyObject {
// MARK: Properties
/// The child coordinators that are being currently presented.
var children: [Coordinator] { get set }
/// The router that handles how the view controllers in the coordinators will be shown or dismissed.
var router: Router { get }
// MARK: Functions
/// Present the coordinator animatedly or not, depending on the given `animated` parameter, and also pass a closure that should be called on dismissal.
/// - Parameters:
/// - animated: A boolean that represents whether the coordinator should be dismissed animatedly or not.
/// - onDismiss: A closure to be called or executed when the presented coordinator is dismissed.
func present(animated: Bool, onDismiss: Router.OnDismissedClosure?)
}
// MARK: - Coordinator+Implementations
public extension Coordinator {
/// Present a child coordinator animatedly or not, depending on the given `animated` parameter, and also pass a closure that should be called on dismissal.
/// - Parameters:
/// - child: A child coordinator to be presented.
/// - animated: A boolean that represents whether the coordinator should be dismissed animatedly or not.
/// - onDismiss: A closure to be called or executed when the presented coordinator is dismissed.
func present(
child: Coordinator,
animated: Bool,
onDismiss: Router.OnDismissedClosure? = nil
) {
store(child)
child.present(animated: animated) { [weak self, weak child] in
guard let self, let child else {
return
}
self.free(child)
onDismiss?()
}
}
/// Dismiss the coordinator animatedly or not, depending on the given `animated` parameter.
/// - Parameter animated: A boolean that represents whether the coordinator should be dismissed animatedly or not.
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 }
}
}

View File

@ -0,0 +1,61 @@
//
// Router.swift
// Coordinator
//
// Created by Javier Cicchelli on 11/04/2023.
// Copyright © 2023 Röck+Cöde. All rights reserved.
//
#if canImport(UIKit)
import UIKit
#endif
/// This protocol defines how view controllers will be shown and dismissed.
public protocol Router: AnyObject {
// MARK: Typealiases
typealias OnDismissedClosure = () -> Void
// MARK: Functions
#if canImport(UIKit)
/// Present a view controller animatedly or not, depending on the given `animated` parameter, and also pass a closure that should be called on dismissal.
/// - Parameters:
/// - viewController: A `UIViewController` view controller instance to present.
/// - animated: A boolean that represents whether the view controller should be dismissed animatedly or not.
/// - onDismiss: A closure to be called or executed when the presented view controller is dismissed.
func present(
_ viewController: UIViewController,
animated: Bool,
onDismiss: OnDismissedClosure?
)
#endif
/// Dismiss a view controller animatedly or not, depending on the given `animated` parameter.
/// - Parameter animated: A boolean that represents whether the view controller should be dismissed animatedly or not.
func dismiss(animated: Bool)
}
#if canImport(UIKit)
// MARK: - Router+Implementations
public extension Router {
// MARK: Functions
/// Present a view controller animatedly or not, depending on the given `animated` parameter.
/// - Parameters:
/// - viewController: A `UIViewController` view controller instance to present.
/// - animated: A boolean that represents whether the view controller should be dismissed animatedly or not.
func present(_ viewController: UIViewController, animated: Bool) {
present(
viewController,
animated: animated,
onDismiss: nil
)
}
}
#endif

View File

@ -0,0 +1,106 @@
//
// TestCoordinators.swift
// CoordinatorTests
//
// Created by Javier Cicchelli on 11/04/2023.
// Copyright © 2023 Röck+Cöde. All rights reserved.
//
import Coordinator
import UIKit
// MARK: - Test coordinators
class SomeCoordinator: Coordinator {
// MARK: Properties
var children: [Coordinator] = []
var router: Router
// MARK: Initialisers
init(router: Router) {
self.router = router
}
// MARK: Functions
func present(animated: Bool, onDismiss: (() -> Void)?) {
router.present(
SomeViewController(),
animated: animated,
onDismiss: onDismiss
)
}
}
class SomeOtherCoordinator: Coordinator {
// MARK: Properties
var children: [Coordinator] = []
var router: Router
// MARK: Initialisers
init(router: Router) {
self.router = router
}
// MARK: Functions
func present(animated: Bool, onDismiss: (() -> Void)?) {
router.present(
SomeOtherViewController(),
animated: animated,
onDismiss: onDismiss
)
}
}
// MARK: - SpyRouter
class SpyRouter: Router {
// MARK: Enumerations
enum State {
case initialised
case presented
case dismissed
}
// MARK: Properties
var state: State = .initialised
var viewController: UIViewController?
var animated: Bool?
var onDismiss: OnDismissedClosure?
// MARK: Functions
func present(
_ viewController: UIViewController,
animated: Bool,
onDismiss: OnDismissedClosure?
) {
self.viewController = viewController
self.animated = animated
self.onDismiss = onDismiss
self.state = .presented
}
func dismiss(animated: Bool) {
self.animated = animated
self.state = .dismissed
}
}
// MARK: - Test view controllers
class SomeViewController: UIViewController {}
class SomeOtherViewController: UIViewController {}

View File

@ -0,0 +1,137 @@
//
// CoordinatorTests.swift
// CoordinatorTests
//
// Created by Javier Cicchelli on 11/04/2023.
// Copyright © 2023 Röck+Cöde. All rights reserved.
//
import Coordinator
import UIKit
import XCTest
final class CoordinatorTests: XCTestCase {
// MARK: Properties
private var router: SpyRouter!
private var coordinator: Coordinator!
// MARK: Tests
func test_present_withoutOnDismissClosure() {
// Executing this test on the main thread is required as a `UIViewController` instance in being initialised here.
DispatchQueue.main.async {
// GIVEN
self.router = SpyRouter()
self.coordinator = SomeCoordinator(router: self.router)
// WHEN
self.coordinator.present(animated: false, onDismiss: nil)
// THEN
XCTAssertTrue(self.coordinator.children.isEmpty)
XCTAssertEqual(self.router.state, .presented)
XCTAssertTrue(self.router.viewController is SomeViewController)
XCTAssertFalse(self.router.animated ?? true)
XCTAssertNil(self.router.onDismiss)
}
}
func test_present_withOnDismissClosure() {
// Executing this test on the main thread is required as a `UIViewController` instance in being initialised here.
DispatchQueue.main.async {
// GIVEN
self.router = SpyRouter()
self.coordinator = SomeOtherCoordinator(router: self.router)
// WHEN
self.coordinator.present(animated: true) {}
// THEN
XCTAssertTrue(self.coordinator.children.isEmpty)
XCTAssertEqual(self.router.state, .presented)
XCTAssertTrue(self.router.viewController is SomeOtherViewController)
XCTAssertTrue(self.router.animated ?? false)
XCTAssertNotNil(self.router.onDismiss)
}
}
func test_presentChild_withoutOnDismissClosure() {
// Executing this test on the main thread is required as a `UIViewController` instance in being initialised here.
DispatchQueue.main.async {
// GIVEN
self.router = SpyRouter()
self.coordinator = SomeCoordinator(router: self.router)
let child = SomeOtherCoordinator(router: self.router)
// WHEN
self.coordinator.present(child: child, animated: false)
// THEN
XCTAssertFalse(self.coordinator.children.isEmpty)
XCTAssertEqual(self.coordinator.children.count, 1)
XCTAssertEqual(self.router.state, .presented)
XCTAssertTrue(self.router.viewController is SomeOtherViewController)
XCTAssertFalse(self.router.animated ?? true)
XCTAssertNil(self.router.onDismiss)
}
}
func test_presentChild_withOnDismissClosure() {
// Executing this test on the main thread is required as a `UIViewController` instance in being initialised here.
DispatchQueue.main.async {
// GIVEN
self.router = SpyRouter()
self.coordinator = SomeOtherCoordinator(router: self.router)
let child = SomeCoordinator(router: self.router)
// WHEN
self.coordinator.present(child: child, animated: true) {}
// THEN
XCTAssertFalse(self.coordinator.children.isEmpty)
XCTAssertEqual(self.coordinator.children.count, 1)
XCTAssertEqual(self.router.state, .presented)
XCTAssertTrue(self.router.viewController is SomeViewController)
XCTAssertTrue(self.router.animated ?? false)
XCTAssertNotNil(self.router.onDismiss)
}
}
func test_dismiss_notAnimated() {
// GIVEN
router = SpyRouter()
coordinator = SomeOtherCoordinator(router: router)
// WHEN
coordinator.dismiss(animated: false)
// THEN
XCTAssertTrue(coordinator.children.isEmpty)
XCTAssertEqual(router.state, .dismissed)
XCTAssertNil(router.viewController)
XCTAssertFalse(router.animated ?? true)
XCTAssertNil(router.onDismiss)
}
func test_dismiss_animated() {
// GIVEN
router = SpyRouter()
coordinator = SomeOtherCoordinator(router: router)
// WHEN
coordinator.dismiss(animated: true)
// THEN
XCTAssertTrue(coordinator.children.isEmpty)
XCTAssertEqual(router.state, .dismissed)
XCTAssertNil(router.viewController)
XCTAssertTrue(router.animated ?? false)
XCTAssertNil(router.onDismiss)
}
}