1 |
//
|
|
2 |
// RouteComposer
|
|
3 |
// BaseStep.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 |
protocol EntitiesProvider {
|
|
17 |
|
|
18 |
var finder: AnyFinder? { get }
|
|
19 |
|
|
20 |
var factory: AnyFactory? { get }
|
|
21 |
|
|
22 |
}
|
|
23 |
|
|
24 |
protocol TaskProvider {
|
|
25 |
|
|
26 |
var interceptor: AnyRoutingInterceptor? { get }
|
|
27 |
|
|
28 |
var contextTask: AnyContextTask? { get }
|
|
29 |
|
|
30 |
var postTask: AnyPostRoutingTask? { get }
|
|
31 |
}
|
|
32 |
|
|
33 |
struct BaseStep: RoutingStep,
|
|
34 |
ChainableStep,
|
|
35 |
InterceptableStep,
|
|
36 |
PerformableStep,
|
|
37 |
CustomStringConvertible {
|
|
38 |
|
|
39 |
private var previousStep: RoutingStep?
|
|
40 |
|
|
41 |
let factory: AnyFactory?
|
|
42 |
|
|
43 |
let finder: AnyFinder?
|
|
44 |
|
|
45 |
let interceptor: AnyRoutingInterceptor?
|
|
46 |
|
|
47 |
let postTask: AnyPostRoutingTask?
|
|
48 |
|
|
49 |
let contextTask: AnyContextTask?
|
|
50 |
|
|
51 |
init(entitiesProvider: EntitiesProvider,
|
|
52 |
taskProvider: TaskProvider) {
|
274x |
53 |
self.finder = entitiesProvider.finder
|
274x |
54 |
self.factory = entitiesProvider.factory
|
274x |
55 |
self.interceptor = taskProvider.interceptor
|
274x |
56 |
self.contextTask = taskProvider.contextTask
|
274x |
57 |
self.postTask = taskProvider.postTask
|
274x |
58 |
}
|
274x |
59 |
|
|
60 |
func getPreviousStep(with context: AnyContext) -> RoutingStep? {
|
258x |
61 |
previousStep
|
258x |
62 |
}
|
258x |
63 |
|
|
64 |
func perform(with context: AnyContext) throws -> PerformableStepResult {
|
220x |
65 |
guard let viewController = try finder?.findViewController(with: context) else {
|
220x |
66 |
if let factory {
|
163x |
67 |
return .build(factory)
|
155x |
68 |
} else {
|
155x |
69 |
return .none
|
8x |
70 |
}
|
8x |
71 |
}
|
57x |
72 |
return .success(viewController)
|
57x |
73 |
}
|
220x |
74 |
|
|
75 |
mutating func from(_ step: RoutingStep) {
|
248x |
76 |
previousStep = step
|
248x |
77 |
}
|
248x |
78 |
|
|
79 |
public var description: String {
|
208x |
80 |
var finderDescription = "None"
|
208x |
81 |
var factoryDescription = "None"
|
208x |
82 |
if let finder {
|
208x |
83 |
finderDescription = String(describing: finder)
|
144x |
84 |
}
|
208x |
85 |
if let factory {
|
208x |
86 |
factoryDescription = String(describing: factory)
|
187x |
87 |
}
|
208x |
88 |
return "BaseStep<\(finderDescription) : \(factoryDescription))>"
|
208x |
89 |
}
|
208x |
90 |
|
|
91 |
}
|
|