Slather logo

Coverage for "AbstractFactory.swift" : 100.00%

(6 of 6 relevant lines covered)

RouteComposer/Classes/AbstractFactory.swift

1
//
2
// RouteComposer
3
// AbstractFactory.swift
4
// https://github.com/ekazaev/route-composer
5
//
6
// Created by Eugene Kazaev in 2018-2022.
7
// Distributed under the MIT license.
8
//
9
// Become a sponsor:
10
// https://github.com/sponsors/ekazaev
11
//
12
13
import Foundation
14
import UIKit
15
16
/// Base protocol for all types of factories.
17
/// An instance that extends `AbstractFactory` builds a `UIViewController` that will later be
18
/// integrated into the stack by the `Router`
19
public protocol AbstractFactory {
20
21
    // MARK: Associated types
22
23
    /// Type of `UIViewController` that `AbstractFactory` can build
24
    associatedtype ViewController: UIViewController
25
26
    /// `Context` to be passed into `UIViewController`
27
    associatedtype Context
28
29
    // MARK: Methods to implement
30
31
    /// The `Router` will call it before the navigation process and if the `AbstractFactory` is not able to
32
    /// build a view controller it should throw an exception. (example: it has to build a product view
33
    //  controller but there is no product code in context)
34
    ///
35
    /// - Parameter context: A `Context` instance that is provided to the `Router`.
36
    /// - Throws: The `RoutingError` if the `Factory` cannot prepare to build a `UIViewController` instance
37
    ///   with the `Context` instance provided.
38
    mutating func prepare(with context: Context) throws
39
40
}
41
42
// MARK: Helper methods where the Context is Any?
43
44
public extension AbstractFactory where Context == Any? {
45
46
    /// Prepares the `AbstractFactory`
47
    mutating func prepare() throws {
6x
48
        try prepare(with: nil)
6x
49
    }
6x
50
51
}
52
53
// MARK: Helper methods where the Context is Void
54
55
public extension AbstractFactory where Context == Void {
56
57
    /// Prepares the `AbstractFactory`
58
    mutating func prepare() throws {
2x
59
        try prepare(with: ())
2x
60
    }
2x
61
62
}