Skip to content

Commit 9bb2696

Browse files
committed
Removed public EnvironmentKey apis except self[]
1 parent 141d630 commit 9bb2696

File tree

4 files changed

+13
-47
lines changed

4 files changed

+13
-47
lines changed

Examples/Sources/GreetingGeneratorExample/GreetingGeneratorApp.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct GreetingGeneratorApp: App {
3030
Toggle("Selectable Greeting", active: $isGreetingSelectable)
3131
if let latest = greetings.last {
3232
LatestGreetingDisplay()
33-
.environment(LatestGreetingKey.self, latest)
33+
.environment(\.latestGreeting, latest)
3434
.padding(.top, 5)
3535
.textSelectionEnabled(isGreetingSelectable)
3636

@@ -55,7 +55,7 @@ struct GreetingGeneratorApp: App {
5555

5656
/// This intermediate view exists to show the usage of custom environment keys. In reality it is not necessary.
5757
struct LatestGreetingDisplay: View {
58-
@Environment(LatestGreetingKey.self) var value: String?
58+
@Environment(\.latestGreeting) var value: String?
5959

6060
var body: some View {
6161
Text(value ?? "nil")
@@ -66,3 +66,10 @@ struct LatestGreetingKey: EnvironmentKey {
6666
typealias Value = String?
6767
static let defaultValue: Value = nil
6868
}
69+
70+
extension EnvironmentValues {
71+
var latestGreeting: String? {
72+
get { self[LatestGreetingKey.self] }
73+
set { self[LatestGreetingKey.self] = newValue }
74+
}
75+
}

Sources/SwiftCrossUI/Environment/Environment.swift

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,21 @@
3636
/// ```
3737
@propertyWrapper
3838
public struct Environment<Value>: DynamicProperty {
39-
var location: EnvironmentLocation<Value>
39+
var keyPath: KeyPath<EnvironmentValues, Value>
4040
var value: Box<Value?>
4141

4242
public func update(
4343
with environment: EnvironmentValues,
4444
previousValue: Self?
4545
) {
46-
switch location {
47-
case .keyPath(let keyPath):
48-
value.value = environment[keyPath: keyPath]
49-
case .environmentKey(let environmentKey):
50-
value.value = (environment[environmentKey])
51-
}
46+
value.value = environment[keyPath: keyPath]
5247
}
5348

5449
public var wrappedValue: Value {
5550
guard let value = value.value else {
5651
fatalError(
5752
"""
58-
Environment value at \(location.debugDescription) used before initialization. Don't \
53+
Environment value at \(keyPath) used before initialization. Don't \
5954
use @Environment properties before SwiftCrossUI requests the \
6055
view's body.
6156
"""
@@ -65,28 +60,7 @@ public struct Environment<Value>: DynamicProperty {
6560
}
6661

6762
public init(_ keyPath: KeyPath<EnvironmentValues, Value>) {
68-
self.location = .keyPath(keyPath)
63+
self.keyPath = keyPath
6964
value = Box(value: nil)
7065
}
71-
72-
public init<Key: EnvironmentKey>(_ type: Key.Type) where Value == Key.Value {
73-
self.location = .environmentKey(type)
74-
self.value = Box(value: nil)
75-
}
76-
}
77-
78-
enum EnvironmentLocation<Value> {
79-
case keyPath(KeyPath<EnvironmentValues, Value>)
80-
case environmentKey(any EnvironmentKey<Value>.Type)
81-
}
82-
83-
extension EnvironmentLocation: CustomDebugStringConvertible {
84-
var debugDescription: String {
85-
switch self {
86-
case .keyPath(let keyPath):
87-
"EnvironmentLocation.keyPath(\(keyPath))"
88-
case .environmentKey(let environmentKey):
89-
"EnvironmentLocation.environmentKey(\(environmentKey))"
90-
}
91-
}
9266
}

Sources/SwiftCrossUI/Environment/EnvironmentValues.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,6 @@ public struct EnvironmentValues {
222222
environment[keyPath: keyPath] = newValue
223223
return environment
224224
}
225-
226-
/// Returns a copy of the environment with the specified key set to the
227-
/// provided new value.
228-
public func with<T: EnvironmentKey>(_ key: T.Type, _ newValue: T.Value) -> Self {
229-
var environment = self
230-
environment[key] = newValue
231-
return environment
232-
}
233225
}
234226

235227
/// A key that can be used to extend the environment with new properties.

Sources/SwiftCrossUI/Views/Modifiers/EnvironmentModifier.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@ package struct EnvironmentModifier<Child: View>: View {
3939
}
4040

4141
extension View {
42-
/// Modifies the environment of the View its applied to.
43-
public func environment<T: EnvironmentKey>(_ key: T.Type, _ newValue: T.Value) -> some View {
44-
EnvironmentModifier(self) { environment in
45-
environment.with(key, newValue)
46-
}
47-
}
48-
4942
/// Modifies the environment of the View its applied to
5043
public func environment<T>(_ keyPath: WritableKeyPath<EnvironmentValues, T>, _ newValue: T)
5144
-> some View

0 commit comments

Comments
 (0)