Slather logo

Coverage for "LastStepInChainAssembly.swift" : 86.36%

(19 of 22 relevant lines covered)

RouteComposer/Classes/Assemblies/Helpers/LastStepInChainAssembly.swift

1
//
2
// RouteComposer
3
// LastStepInChainAssembly.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
/// Helper class to build a chain of steps. Can not be used directly.
17
public struct LastStepInChainAssembly<ViewController: UIViewController, Context> {
18
19
    // MARK: Properties
20
21
    let previousSteps: [RoutingStep]
22
23
    // MARK: Methods
24
25
    init(previousSteps: [RoutingStep]) {
202x
26
        self.previousSteps = previousSteps
202x
27
    }
202x
28
29
    /// Assembles all the provided settings.
30
    ///
31
    /// - Returns: The instance of `DestinationStep` with all the settings provided inside.
32
    public func assemble() -> DestinationStep<ViewController, Context> {
202x
33
        DestinationStep(chain(previousSteps))
202x
34
    }
202x
35
36
    private func chain(_ steps: [RoutingStep]) -> RoutingStep {
202x
37
        guard let lastStep = steps.last else {
202x
38
            preconditionFailure("No steps provided to chain.")
!
39
        }
202x
40
202x
41
        let firstStep = steps.dropLast().reversed().reduce(lastStep) { result, currentStep in
248x
42
            guard var step = currentStep as? BaseStep else {
248x
43
                assertionFailure("\(currentStep) can not be chained to non chainable step \(result)")
!
44
                return currentStep
!
45
            }
248x
46
            step.from(result)
248x
47
            return step
248x
48
        }
248x
49
202x
50
        return firstStep
202x
51
    }
202x
52
53
}