Skip to content

Undeprecate preview traits? #353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions Sources/Dependencies/DependencyValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -579,11 +579,7 @@ public final class CachedValues: @unchecked Sendable {
}
if !CachedValues.isAccessingCachedDependencies {
value = CachedValues.$isAccessingCachedDependencies.withValue(true) {
#if canImport(SwiftUI) && compiler(>=6)
return previewValues[key]
#else
return Key.previewValue
#endif
return Key.previewValue
}
} else {
value = Key.previewValue
Expand Down
54 changes: 0 additions & 54 deletions Sources/Dependencies/Internal/Deprecations.swift
Original file line number Diff line number Diff line change
@@ -1,57 +1,3 @@
// MARK: - Deprecated after 1.6.3

#if canImport(SwiftUI) && compiler(>=6)
import SwiftUI

@available(iOS 18, macOS 15, tvOS 18, watchOS 11, visionOS 2, *)
extension PreviewTrait where T == Preview.ViewTraits {
@available(
*, deprecated,
message: """
Use 'withDependencies' or 'prepareDependencies' from the body of the preview, instead.
"""
)
@_documentation(visibility: private)
public static func dependency<Value>(
_ keyPath: WritableKeyPath<DependencyValues, Value> & Sendable,
_ value: Value
) -> PreviewTrait {
.dependencies { $0[keyPath: keyPath] = value }
}

@available(
*, deprecated,
message: """
Use 'withDependencies' or 'prepareDependencies' from the body of the preview, instead.
"""
)
@_documentation(visibility: private)
public static func dependency<Value: TestDependencyKey>(
_ value: Value
) -> PreviewTrait where Value == Value.Value {
.dependencies { $0[Value.self] = value }
}

@available(
*, deprecated,
message: """
Use 'withDependencies' or 'prepareDependencies' from the body of the preview, instead.
"""
)
@_documentation(visibility: private)
public static func dependencies(
_ updateValuesForPreview: (inout DependencyValues) -> Void
) -> PreviewTrait {
var copy = previewValues
defer { previewValues = copy }
updateValuesForPreview(&copy)
return PreviewTrait()
}
}

nonisolated(unsafe) var previewValues = DependencyValues(context: .preview)
#endif

// MARK: - Deprecated after 0.4.2

extension AsyncStream {
Expand Down
33 changes: 33 additions & 0 deletions Sources/Dependencies/Traits/PreviewTrait.swift
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
#if canImport(SwiftUI) && compiler(>=6)
import SwiftUI

@available(iOS 18, macOS 15, tvOS 18, watchOS 11, visionOS 2, *)
extension PreviewTrait where T == Preview.ViewTraits {
public static func dependency<Value>(
_ keyPath: WritableKeyPath<DependencyValues, Value> & Sendable,
_ value: @escaping @Sendable () -> Value
) -> PreviewTrait {
.dependencies { $0[keyPath: keyPath] = value() }
}

public static func dependency<Value: TestDependencyKey>(
_ value: Value
) -> PreviewTrait where Value == Value.Value {
.dependencies { $0[Value.self] = value }
}

public static func dependencies(
_ updateValuesForPreview: @escaping @Sendable (inout DependencyValues) -> Void
) -> PreviewTrait {
return .modifier(DependenciesPreviewModifier(updateValuesForPreview: updateValuesForPreview))
}
}

private struct DependenciesPreviewModifier: PreviewModifier {
let updateValuesForPreview: @Sendable (inout DependencyValues) -> Void

func body(content: Content, context: ()) -> some View {
prepareDependencies(updateValuesForPreview)
return content
}
}
Comment on lines +26 to +33
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introducing a preview modifier allows us to delay when dependencies are prepared to the computation of the preview's body.

#endif
Loading