Slather logo

Coverage for "SearchOptions.swift" : 91.67%

(22 of 24 relevant lines covered)

RouteComposer/Classes/SearchOptions.swift

1
//
2
// RouteComposer
3
// SearchOptions.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
15
/// A set of options for the `findViewController` method
16
public struct SearchOptions: OptionSet, CaseIterable, CustomStringConvertible {
17
18
    public let rawValue: Int
19
20
    public init(rawValue: Int) {
23500x
21
        self.rawValue = rawValue
23500x
22
    }
23500x
23
24
    // MARK: Options
25
26
    /// Compare to a view controller provided
27
    public static let current = SearchOptions(rawValue: 1 << 0)
28
29
    /// If a view controller is a container, search in its visible view controllers
30
    public static let visible = SearchOptions(rawValue: 1 << 1)
31
32
    /// If a view controller is a container, search in all the view controllers it contains
33
    public static let contained = SearchOptions(rawValue: 1 << 2)
34
35
    /// Start search from the view controller provided and search in all view controllers it presented
36
    public static let presented = SearchOptions(rawValue: 1 << 3)
37
38
    /// Start search from the view controller provided and search in all view controllers that are presenting it
39
    public static let presenting = SearchOptions(rawValue: 1 << 4)
40
41
    /// Start search from the view controller provided and search in all its parent view controllers
42
    public static let parent = SearchOptions(rawValue: 1 << 5)
43
44
    // MARK: Combinations
45
46
    /// If a view controller is a container, search in all the view controllers it contains
47
    public static let currentAllStack: SearchOptions = [.current, .contained]
48
49
    /// If a view controller is a container, search in all visible view controllers it contains
50
    public static let currentVisibleOnly: SearchOptions = [.current, .visible]
51
52
    /// Iterate through the all visible view controllers in the stack.
53
    public static let allVisible: SearchOptions = [.currentVisibleOnly, .presented, .presenting]
54
55
    /// Iterate through the all view controllers in the stack.
56
    public static let fullStack: SearchOptions = [.current, .contained, .presented, .presenting]
57
58
    /// Iterate through the all view controllers on the current level and all the view controllers
59
    /// presented from the current level.
60
    public static let currentAndUp: SearchOptions = [.currentAllStack, .presented]
61
62
    /// Iterate through the all view controllers on the current level and all the view controllers
63
    /// that are presenting the current level.
64
    public static let currentAndDown: SearchOptions = [.currentAllStack, .presenting]
65
66
    // MARK: Methods
67
68
    public static var allCases: [SearchOptions] {
264x
69
        [.current, .visible, .contained, .presented, .presenting, .parent]
264x
70
    }
264x
71
72
    public var description: String {
264x
73
        SearchOptions.allCases.compactMap { option in
1580x
74
            guard self.contains(option) else {
1580x
75
                return nil
639x
76
            }
945x
77
            switch option {
945x
78
            case .current: return "current"
945x
79
            case .visible: return "visible"
945x
80
            case .contained: return "contained"
945x
81
            case .presented: return "presented"
945x
82
            case .presenting: return "presenting"
945x
83
            case .parent: return "parent"
945x
84
            default:
945x
85
                assertionFailure("Unknown SearchOptions")
!
86
                return nil
!
87
            }
945x
88
        }.joined(separator: ", ")
945x
89
    }
264x
90
91
}