Slather logo

Coverage for "ContainerAdapter.swift" : 100.00%

(6 of 6 relevant lines covered)

RouteComposer/Classes/ContainerAdapter.swift

1
//
2
// RouteComposer
3
// ContainerAdapter.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
/// Provides universal properties and methods of the `ContainerViewController` instance.
17
///
18
/// `ContainerViewController`s are different from the simple ones in that they can contain child view controllers which
19
/// are also containers or simple ones. These view controllers are available out of the box: `UINavigationController`,
20
/// `UITabBarController` and so on, but there can be custom ones created as well.
21
///
22
/// All the container view controller have the following properties:
23
///  1. The list of all the view controllers that they contain.
24
///  2. One or more view controllers are currently visible.
25
///  3. They can make one of these view controllers visible.
26
///  4. They can replace all of their contained view controllers.
27
public protocol ContainerAdapter {
28
29
    // MARK: Properties to implement
30
31
    /// All `UIViewController` instances that adapting `ContainerViewController` currently has in the stack
32
    var containedViewControllers: [UIViewController] { get }
33
34
    /// The `UIViewController` instances out of the `containedViewControllers` that are currently visible on the screen
35
    /// The `visibleViewControllers` are the subset of the `containedViewControllers`.
36
    var visibleViewControllers: [UIViewController] { get }
37
38
    // MARK: Methods to implement
39
40
    /// Each container view controller adapter should implement this method for the `Router` to know how to make
41
    /// its particular child view controller visible.
42
    ///
43
    /// NB: `completion` block must be called.
44
    ///
45
    /// - Parameters:
46
    ///   - viewController: The `UIViewController` to make active (visible).
47
    ///   - animated: If `ContainerViewController` is able to do so - make container active animated or not.
48
    func makeVisible(_ viewController: UIViewController, animated: Bool, completion: @escaping (_: RoutingResult) -> Void)
49
50
    /// Each container view controller adapter should implement this method for the `Router` to know how to replace all the
51
    /// view controllers in this particular container view controller.
52
    ///
53
    /// NB: `completion` block must be called.
54
    ///
55
    /// - Parameters:
56
    ///   - containedViewControllers: A `UIViewController` instances to replace.
57
    ///   - animated: If `ContainerViewController` is able to do so - replace contained view controllers animated or not.
58
    func setContainedViewControllers(_ containedViewControllers: [UIViewController], animated: Bool, completion: @escaping (_: RoutingResult) -> Void)
59
60
}
61
62
// MARK: Helper methods
63
64
public extension ContainerAdapter {
65
66
    /// Checks if the provided view controller is present amongst the contained view controllers.
67
    ///
68
    /// - Parameter viewController: `UIViewController` instance
69
    /// - Returns: `true` if present, `false` otherwise.
70
    func contains(_ viewController: UIViewController) -> Bool {
56x
71
        containedViewControllers.contains(viewController)
56x
72
    }
56x
73
74
    /// Checks if the provided view controller is present amongst the visible view controllers.
75
    ///
76
    /// - Parameter viewController: `UIViewController` instance
77
    /// - Returns: `true` if present, `false` otherwise.
78
    func isVisible(_ viewController: UIViewController) -> Bool {
160x
79
        visibleViewControllers.contains(viewController)
160x
80
    }
160x
81
82
}