1 |
//
|
|
2 |
// RouteComposer
|
|
3 |
// FinderFactory.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 |
/// The `StepAssembly` transforms a `Finder` result as a `Factory` result. It is useful
|
|
17 |
/// when a `UIViewController` instance was built inside of the parent `ContainerFactory`.
|
|
18 |
public struct FinderFactory<F: Finder>: Factory {
|
|
19 |
|
|
20 |
// MARK: Associated types
|
|
21 |
|
|
22 |
public typealias ViewController = F.ViewController
|
|
23 |
|
|
24 |
public typealias Context = F.Context
|
|
25 |
|
|
26 |
// MARK: Properties
|
|
27 |
|
|
28 |
/// The additional configuration block
|
|
29 |
public let configuration: ((_: F.ViewController) -> Void)?
|
|
30 |
|
|
31 |
private let finder: F
|
|
32 |
|
|
33 |
// MARK: Methods
|
|
34 |
|
|
35 |
/// Constructor
|
|
36 |
///
|
|
37 |
/// - Parameters:
|
|
38 |
/// - finder: The `Finder` instance to be used by the `Factory`
|
|
39 |
public init?(finder: F, configuration: ((_: F.ViewController) -> Void)? = nil) {
|
100x |
40 |
guard !(finder is NilEntity) else {
|
100x |
41 |
return nil
|
20x |
42 |
}
|
80x |
43 |
self.finder = finder
|
80x |
44 |
self.configuration = configuration
|
80x |
45 |
}
|
80x |
46 |
|
|
47 |
public func build(with context: F.Context) throws -> F.ViewController {
|
22x |
48 |
guard let viewController = try finder.findViewController(with: context) else {
|
22x |
49 |
throw RoutingError.compositionFailed(.init("\(String(describing: finder)) hasn't found its view controller in the stack."))
|
1x |
50 |
}
|
21x |
51 |
if let configuration {
|
21x |
52 |
configuration(viewController)
|
1x |
53 |
}
|
21x |
54 |
return viewController
|
21x |
55 |
}
|
21x |
56 |
|
|
57 |
}
|
|