Slather logo

Coverage for "ActionBox.swift" : 100.00%

(31 of 31 relevant lines covered)

RouteComposer/Classes/Router/Type Erasure/Boxes/ActionBox.swift

1
//
2
// RouteComposer
3
// ActionBox.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
struct ActionBox<A: Action>: AnyAction, AnyActionBox, CustomStringConvertible, MainThreadChecking {
17
18
    let action: A
19
20
    init(_ action: A) {
294x
21
        self.action = action
294x
22
    }
294x
23
24
    func perform(with viewController: UIViewController,
25
                 on existingController: UIViewController,
26
                 with postponedIntegrationHandler: PostponedActionIntegrationHandler,
27
                 nextAction: AnyAction?,
28
                 animated: Bool,
29
                 completion: @escaping (RoutingResult) -> Void) {
71x
30
        guard let typedExistingViewController = existingController as? A.ViewController else {
71x
31
            completion(.failure(RoutingError.typeMismatch(type: type(of: existingController),
1x
32
                                                          expectedType: ActionType.ViewController.self,
1x
33
                                                          .init("Action \(action.self) cannot be performed on \(existingController)."))))
1x
34
            return
1x
35
        }
70x
36
        assertIfNotMainThread()
70x
37
        postponedIntegrationHandler.purge(animated: animated, completion: { result in
70x
38
            guard result.isSuccessful else {
70x
39
                completion(result)
1x
40
                return
1x
41
            }
69x
42
            self.action.perform(with: viewController, on: typedExistingViewController, animated: animated) { result in
69x
43
                self.assertIfNotMainThread()
69x
44
                completion(result)
69x
45
            }
69x
46
        })
69x
47
    }
70x
48
49
    func perform(embedding viewController: UIViewController, in childViewControllers: inout [UIViewController]) {
1x
50
        childViewControllers.append(viewController)
1x
51
    }
1x
52
53
    public var description: String {
68x
54
        String(describing: action)
68x
55
    }
68x
56
57
    func isEmbeddable(to container: ContainerViewController.Type) -> Bool {
17x
58
        false
17x
59
    }
17x
60
61
}