forked from siteline/swiftui-introspect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlatformViewVersion.swift
125 lines (102 loc) · 5 KB
/
PlatformViewVersion.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#if !os(watchOS)
import SwiftUI
public struct PlatformViewVersionPredicate<SwiftUIViewType: IntrospectableViewType, PlatformSpecificEntity: PlatformEntity> {
let selector: IntrospectionSelector<PlatformSpecificEntity>?
private init<Version: PlatformVersion>(
_ versions: [PlatformViewVersion<Version, SwiftUIViewType, PlatformSpecificEntity>],
matches: (PlatformViewVersion<Version, SwiftUIViewType, PlatformSpecificEntity>) -> Bool
) {
if let matchingVersion = versions.first(where: matches) {
self.selector = matchingVersion.selector ?? .default
} else {
self.selector = nil
}
}
public static func iOS(_ versions: (iOSViewVersion<SwiftUIViewType, PlatformSpecificEntity>)...) -> Self {
Self(versions, matches: \.isCurrent)
}
@_spi(Advanced)
public static func iOS(_ versions: PartialRangeFrom<iOSViewVersion<SwiftUIViewType, PlatformSpecificEntity>>) -> Self {
Self([versions.lowerBound], matches: \.isCurrentOrPast)
}
public static func tvOS(_ versions: (tvOSViewVersion<SwiftUIViewType, PlatformSpecificEntity>)...) -> Self {
Self(versions, matches: \.isCurrent)
}
@_spi(Advanced)
public static func tvOS(_ versions: PartialRangeFrom<tvOSViewVersion<SwiftUIViewType, PlatformSpecificEntity>>) -> Self {
Self([versions.lowerBound], matches: \.isCurrentOrPast)
}
public static func macOS(_ versions: (macOSViewVersion<SwiftUIViewType, PlatformSpecificEntity>)...) -> Self {
Self(versions, matches: \.isCurrent)
}
@_spi(Advanced)
public static func macOS(_ versions: PartialRangeFrom<macOSViewVersion<SwiftUIViewType, PlatformSpecificEntity>>) -> Self {
Self([versions.lowerBound], matches: \.isCurrentOrPast)
}
public static func visionOS(_ versions: (visionOSViewVersion<SwiftUIViewType, PlatformSpecificEntity>)...) -> Self {
Self(versions, matches: \.isCurrent)
}
@_spi(Advanced)
public static func visionOS(_ versions: PartialRangeFrom<visionOSViewVersion<SwiftUIViewType, PlatformSpecificEntity>>) -> Self {
Self([versions.lowerBound], matches: \.isCurrentOrPast)
}
}
public typealias iOSViewVersion<SwiftUIViewType: IntrospectableViewType, PlatformSpecificEntity: PlatformEntity> =
PlatformViewVersion<iOSVersion, SwiftUIViewType, PlatformSpecificEntity>
public typealias tvOSViewVersion<SwiftUIViewType: IntrospectableViewType, PlatformSpecificEntity: PlatformEntity> =
PlatformViewVersion<tvOSVersion, SwiftUIViewType, PlatformSpecificEntity>
public typealias macOSViewVersion<SwiftUIViewType: IntrospectableViewType, PlatformSpecificEntity: PlatformEntity> =
PlatformViewVersion<macOSVersion, SwiftUIViewType, PlatformSpecificEntity>
public typealias visionOSViewVersion<SwiftUIViewType: IntrospectableViewType, PlatformSpecificEntity: PlatformEntity> =
PlatformViewVersion<visionOSVersion, SwiftUIViewType, PlatformSpecificEntity>
public enum PlatformViewVersion<Version: PlatformVersion, SwiftUIViewType: IntrospectableViewType, PlatformSpecificEntity: PlatformEntity>: Sendable {
@_spi(Internals) case available(Version, IntrospectionSelector<PlatformSpecificEntity>?)
@_spi(Internals) case unavailable
@_spi(Advanced) public init(for version: Version, selector: IntrospectionSelector<PlatformSpecificEntity>? = nil) {
self = .available(version, selector)
}
@_spi(Advanced) public static func unavailable(file: StaticString = #file, line: UInt = #line) -> Self {
let filePath = file.withUTF8Buffer { String(decoding: $0, as: UTF8.self) }
let fileName = URL(fileURLWithPath: filePath).lastPathComponent
print(
"""
If you're seeing this, someone forgot to mark \(fileName):\(line) as unavailable.
This won't have any effect, but it should be disallowed altogether.
Please report it upstream so we can properly fix it by using the following link:
https://github.com/siteline/swiftui-introspect/issues/new?title=`\(fileName):\(line)`+should+be+marked+unavailable
"""
)
return .unavailable
}
private var version: Version? {
if case .available(let version, _) = self {
return version
} else {
return nil
}
}
fileprivate var selector: IntrospectionSelector<PlatformSpecificEntity>? {
if case .available(_, let selector) = self {
return selector
} else {
return nil
}
}
fileprivate var isCurrent: Bool {
version?.isCurrent ?? false
}
fileprivate var isCurrentOrPast: Bool {
version?.isCurrentOrPast ?? false
}
}
// This conformance isn't meant to be used directly by the user,
// it's only to satisfy requirements for forming ranges (e.g. `.v15...`).
extension PlatformViewVersion: Comparable {
public static func == (lhs: Self, rhs: Self) -> Bool {
true
}
public static func < (lhs: Self, rhs: Self) -> Bool {
true
}
}
#endif