Slather logo

Coverage for "TabBarControllerAdapter.swift" : 100.00%

(40 of 40 relevant lines covered)

RouteComposer/Classes/Adapters/TabBarControllerAdapter.swift

1
//
2
// RouteComposer
3
// TabBarControllerAdapter.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
/// Default `ContainerAdapter` for `UITabBarController`
17
public struct TabBarControllerAdapter<VC: UITabBarController>: ConcreteContainerAdapter {
18
19
    // MARK: Properties
20
21
    weak var tabBarController: VC?
22
23
    // MARK: Methods
24
25
    public init(with navigationController: VC) {
361x
26
        self.tabBarController = navigationController
361x
27
    }
361x
28
29
    public var containedViewControllers: [UIViewController] {
164x
30
        guard let viewControllers = tabBarController?.viewControllers else {
164x
31
            return []
1x
32
        }
163x
33
        return viewControllers
163x
34
    }
164x
35
36
    public var visibleViewControllers: [UIViewController] {
355x
37
        guard let selectedViewController = tabBarController?.selectedViewController else {
355x
38
            return []
1x
39
        }
354x
40
        return [selectedViewController]
354x
41
    }
355x
42
43
    public func makeVisible(_ viewController: UIViewController, animated: Bool, completion: @escaping (_: RoutingResult) -> Void) {
29x
44
        guard let tabBarController else {
29x
45
            completion(.failure(RoutingError.compositionFailed(.init("\(String(describing: VC.self)) has been deallocated"))))
1x
46
            return
1x
47
        }
28x
48
        guard tabBarController.selectedViewController != viewController else {
28x
49
            completion(.success)
1x
50
            return
1x
51
        }
27x
52
        guard contains(viewController) else {
27x
53
            completion(.failure(RoutingError.compositionFailed(.init("\(String(describing: tabBarController)) does not contain \(String(describing: viewController))"))))
2x
54
            return
2x
55
        }
25x
56
25x
57
        tabBarController.selectedViewController = viewController
25x
58
        completion(.success)
25x
59
    }
25x
60
61
    public func setContainedViewControllers(_ containedViewControllers: [UIViewController], animated: Bool, completion: @escaping (_: RoutingResult) -> Void) {
2x
62
        guard let tabBarController else {
2x
63
            completion(.failure(RoutingError.compositionFailed(.init("\(String(describing: VC.self)) has been deallocated"))))
1x
64
            return
1x
65
        }
1x
66
        tabBarController.setViewControllers(containedViewControllers, animated: animated)
1x
67
        completion(.success)
1x
68
    }
1x
69
70
}