Slather logo

Coverage for "SplitControllerFactory.swift" : 100.00%

(27 of 27 relevant lines covered)

RouteComposer/Classes/Factories/SplitControllerFactory.swift

1
//
2
// RouteComposer
3
// SplitControllerFactory.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
///  The `ContainerFactory` that creates a `UISplitController` instance.
17
public struct SplitControllerFactory<VC: UISplitViewController, C>: ContainerFactory {
18
19
    // MARK: Associated types
20
21
    public typealias ViewController = VC
22
23
    public typealias Context = C
24
25
    // MARK: Properties
26
27
    /// A Xib file name
28
    public let nibName: String?
29
30
    /// A `Bundle` instance
31
    public let bundle: Bundle?
32
33
    /// `UISplitViewControllerDelegate` reference
34
    public private(set) weak var delegate: UISplitViewControllerDelegate?
35
36
    /// If 'true', hidden view can be presented and dismissed via a swipe gesture. Defaults to 'true'.
37
    public let presentsWithGesture: Bool?
38
39
    /// A property that controls how the primary view controller is hidden and displayed.
40
    /// A value of `.automatic` specifies the default behavior split view controller, which on an iPad,
41
    /// corresponds to an overlay mode in portrait and a side-by-side mode in landscape.
42
    public let preferredDisplayMode: UISplitViewController.DisplayMode?
43
44
    /// The additional configuration block
45
    public let configuration: ((_: VC) -> Void)?
46
47
    // MARK: Methods
48
49
    /// Constructor
50
    public init(nibName nibNameOrNil: String? = nil,
51
                bundle nibBundleOrNil: Bundle? = nil,
52
                delegate: UISplitViewControllerDelegate? = nil,
53
                presentsWithGesture: Bool? = nil,
54
                preferredDisplayMode: UISplitViewController.DisplayMode? = nil,
55
                configuration: ((_: VC) -> Void)? = nil) {
2x
56
        self.nibName = nibNameOrNil
2x
57
        self.bundle = nibBundleOrNil
2x
58
        self.delegate = delegate
2x
59
        self.preferredDisplayMode = preferredDisplayMode
2x
60
        self.presentsWithGesture = presentsWithGesture
2x
61
        self.configuration = configuration
2x
62
    }
2x
63
64
    public func build(with context: C, integrating coordinator: ChildCoordinator) throws -> VC {
2x
65
        let splitViewController = VC(nibName: nibName, bundle: bundle)
2x
66
        if let presentsWithGesture {
2x
67
            splitViewController.presentsWithGesture = presentsWithGesture
1x
68
        }
2x
69
        if let delegate {
2x
70
            splitViewController.delegate = delegate
1x
71
        }
2x
72
        if !coordinator.isEmpty {
2x
73
            splitViewController.viewControllers = try coordinator.build(integrating: splitViewController.viewControllers)
1x
74
        }
2x
75
        if let preferredDisplayMode {
2x
76
            splitViewController.preferredDisplayMode = preferredDisplayMode
1x
77
        }
2x
78
        if let configuration {
2x
79
            configuration(splitViewController)
1x
80
        }
2x
81
        return splitViewController
2x
82
    }
2x
83
84
}