Skip to content

Commit 4e6b6a8

Browse files
authored
Undeprecate preview traits? (#353)
* Undeprecate preview traits * wip * docs
1 parent 83beaba commit 4e6b6a8

File tree

9 files changed

+49
-103
lines changed

9 files changed

+49
-103
lines changed

README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,11 @@ and `UUID()`, and we'd have to wait for real world time to pass, making the test
164164
But, controllable dependencies aren't only useful for tests. They can also be used in Xcode
165165
previews. Suppose the feature above makes use of a clock to sleep for an amount of time before
166166
something happens in the view. If you don't want to literally wait for time to pass in order to see
167-
how the view changes, you can override the clock dependency to be an "immediate" clock using
168-
`prepareDependencies`:
167+
how the view changes, you can override the clock dependency to be an "immediate" clock using the
168+
`.dependencies` preview trait:
169169

170170
```swift
171-
#Preview {
172-
let _ = prepareDependencies {
173-
$0.continuousClock = .immediate
174-
}
175-
171+
#Preview(trait: .dependencies { $0.continuousClock = ImmediateClock() }) {
176172
// All access of '@Dependency(\.continuousClock)' in this preview will
177173
// use an immediate clock.
178174
FeatureView(model: FeatureModel())

Sources/Dependencies/DependencyValues.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -579,11 +579,7 @@ public final class CachedValues: @unchecked Sendable {
579579
}
580580
if !CachedValues.isAccessingCachedDependencies {
581581
value = CachedValues.$isAccessingCachedDependencies.withValue(true) {
582-
#if canImport(SwiftUI) && compiler(>=6)
583-
return previewValues[key]
584-
#else
585-
return Key.previewValue
586-
#endif
582+
return Key.previewValue
587583
}
588584
} else {
589585
value = Key.previewValue

Sources/Dependencies/Documentation.docc/Articles/Lifetimes.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,9 @@ feature behaves in very specific states. For example, if you wanted to see how y
218218
when the `fetchUser` endpoint throws an error, you can update the preview like so:
219219

220220
```swift
221-
#Preview {
222-
let _ = prepareDependencies {
223-
$0.apiClient.fetchUser = { _ in throw SomeError() }
224-
}
221+
#Preview(traits: .dependencies {
222+
$0.apiClient.fetchUser = { _ in throw SomeError() }
223+
}) {
225224
FeatureView(model: FeatureModel())
226225
}
227226
```

Sources/Dependencies/Documentation.docc/Articles/LivePreviewTest.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,8 @@ func testFeature() {
306306
[unimplemented-docs]: https://pointfreeco.github.io/xctest-dynamic-overlay/main/documentation/xctestdynamicoverlay/unimplemented(_:fileid:line:)-5098a
307307
[issue-reporting-gh]: http://github.com/pointfreeco/xctest-dynamic-overlay
308308

309+
## Topics
310+
311+
### Previews
312+
313+
- ``DeveloperToolsSupport/PreviewTrait``

Sources/Dependencies/Documentation.docc/Articles/QuickStart.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,11 @@ and `UUID()`, and we'd have to wait for real world time to pass, making the test
118118
But, controllable dependencies aren't only useful for tests. They can also be used in Xcode
119119
previews. Suppose the feature above makes use of a clock to sleep for an amount of time before
120120
something happens in the view. If you don't want to literally wait for time to pass in order to see
121-
how the view changes, you can override the clock dependency to be an "immediate" clock using
122-
``prepareDependencies(_:)``:
121+
how the view changes, you can override the clock dependency to be an "immediate" clock using the
122+
``DeveloperToolsSupport/PreviewTrait/dependencies(_:)`` preview trait:
123123

124124
```swift
125-
#Preview {
126-
let _ = prepareDependencies {
127-
$0.continuousClock = .immediate
128-
}
129-
125+
#Preview(trait: .dependencies { $0.continuousClock = ImmediateClock() }) {
130126
// All access of '@Dependency(\.continuousClock)' in this preview will
131127
// use an immediate clock.
132128
FeatureView(model: FeatureModel())

Sources/Dependencies/Documentation.docc/Articles/WhatAreDependencies.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,7 @@ For previews, you can use the `.dependencies` preview trait to override the
104104
does not actually sleep for any amount of time:
105105

106106
```swift
107-
#Preview {
108-
let _ = prepareDependencies {
109-
$0.continuousClock = .immediate
110-
}
107+
#Preview(traits: .dependencies { $0.continuousClock = ImmediateClock() }) {
111108
FeatureView(model: FeatureModel())
112109
}
113110
```

Sources/Dependencies/Internal/Deprecations.swift

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,3 @@
1-
// MARK: - Deprecated after 1.6.3
2-
3-
#if canImport(SwiftUI) && compiler(>=6)
4-
import SwiftUI
5-
6-
@available(iOS 18, macOS 15, tvOS 18, watchOS 11, visionOS 2, *)
7-
extension PreviewTrait where T == Preview.ViewTraits {
8-
@available(
9-
*, deprecated,
10-
message: """
11-
Use 'withDependencies' or 'prepareDependencies' from the body of the preview, instead.
12-
"""
13-
)
14-
@_documentation(visibility: private)
15-
public static func dependency<Value>(
16-
_ keyPath: WritableKeyPath<DependencyValues, Value> & Sendable,
17-
_ value: Value
18-
) -> PreviewTrait {
19-
.dependencies { $0[keyPath: keyPath] = value }
20-
}
21-
22-
@available(
23-
*, deprecated,
24-
message: """
25-
Use 'withDependencies' or 'prepareDependencies' from the body of the preview, instead.
26-
"""
27-
)
28-
@_documentation(visibility: private)
29-
public static func dependency<Value: TestDependencyKey>(
30-
_ value: Value
31-
) -> PreviewTrait where Value == Value.Value {
32-
.dependencies { $0[Value.self] = value }
33-
}
34-
35-
@available(
36-
*, deprecated,
37-
message: """
38-
Use 'withDependencies' or 'prepareDependencies' from the body of the preview, instead.
39-
"""
40-
)
41-
@_documentation(visibility: private)
42-
public static func dependencies(
43-
_ updateValuesForPreview: (inout DependencyValues) -> Void
44-
) -> PreviewTrait {
45-
var copy = previewValues
46-
defer { previewValues = copy }
47-
updateValuesForPreview(&copy)
48-
return PreviewTrait()
49-
}
50-
}
51-
52-
nonisolated(unsafe) var previewValues = DependencyValues(context: .preview)
53-
#endif
54-
551
// MARK: - Deprecated after 0.4.2
562

573
extension AsyncStream {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,34 @@
1+
#if canImport(SwiftUI) && compiler(>=6)
2+
import SwiftUI
13

4+
@available(iOS 18, macOS 15, tvOS 18, watchOS 11, visionOS 2, *)
5+
extension PreviewTrait where T == Preview.ViewTraits {
6+
public static func dependency<Value>(
7+
_ keyPath: WritableKeyPath<DependencyValues, Value> & Sendable,
8+
_ value: @autoclosure @escaping @Sendable () -> Value
9+
) -> PreviewTrait {
10+
.dependencies { $0[keyPath: keyPath] = value() }
11+
}
12+
13+
public static func dependency<Value: TestDependencyKey>(
14+
_ value: Value
15+
) -> PreviewTrait where Value == Value.Value {
16+
.dependencies { $0[Value.self] = value }
17+
}
18+
19+
public static func dependencies(
20+
_ updateValuesForPreview: @escaping @Sendable (inout DependencyValues) -> Void
21+
) -> PreviewTrait {
22+
return .modifier(DependenciesPreviewModifier(updateValuesForPreview: updateValuesForPreview))
23+
}
24+
}
25+
26+
private struct DependenciesPreviewModifier: PreviewModifier {
27+
let updateValuesForPreview: @Sendable (inout DependencyValues) -> Void
28+
29+
func body(content: Content, context: ()) -> some View {
30+
prepareDependencies(updateValuesForPreview)
31+
return content
32+
}
33+
}
34+
#endif

Tests/DependenciesTests/PreviewTraitsTests.swift

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)