Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,16 @@ private struct ChildEnvironment<Value>: StatefulRule, AsyncAttribute, CustomStri
}
}
let valueChanged = if !environmentChanged {
// FIXME: The map logic is buggy
oldValue.map{ compareValues($0, newValue, mode: .equatableUnlessPOD) } ?? true
// SwiftUI implementation:
// oldValue.map { compareValues($0, newValue, mode: .equatableUnlessPOD) } ?? true
oldValue.map{ !compareValues($0, newValue, mode: .equatableUnlessPOD) } ?? true
} else {
true
}
let keyPathChanged = if !valueChanged {
// FIXME: The map logic is buggy
oldKeyPath.map({ $0 == newKeyPath }) ?? true
// SwiftUI implementation:
// oldKeyPath.map { $0 === newKeyPath } ?? true
oldKeyPath.map{ $0 !== newKeyPath } ?? true
} else {
true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import Testing

extension EnvironmentValues {
@Entry fileprivate var customValue = 0
@Entry fileprivate var customValue2 = 0
}

@Observable
private final class Model {
var value = 0
}

@MainActor
Expand Down Expand Up @@ -36,4 +42,67 @@ struct EnvironmentKeyTransformModifierCompatibilityTests {
)
}
}

@Test
func environmentKeyTransformDifferentKeyPath() async throws {
@MainActor
enum Count {
static var contentBody: Int = 0
static var subviewBody: Int = 0
}

struct ContentView: View {
@State private var model = Model()

var body: some View {
let _ = {
Count.contentBody += 1
}()
Subview()
.transformEnvironment(model.value == 0 ? \.customValue : \.customValue2) {
$0 = model.value
}
.onAppear {
model.value = 2
}
}
}

struct Subview: View {
@Environment(\.customValue) private var value
@Environment(\.customValue2) private var value2

var body: some View {
let _ = {
Count.subviewBody += 1
switch Count.subviewBody {
case 1:
#expect(value == 0)
#expect(value2 == 0)
case 2:
#expect(value == 0)
#if OPENSWIFTUI
#expect(value2 == 2)
#else
// SwiftUI known implementation issue
withKnownIssue {
#expect(value2 == 2)
}
#expect(value2 == 0)
#endif
default:
Issue.record()
}
}()
Color.red
}
}
try await triggerLayoutWithWindow(expectedCount: 0) { _ in
PlatformHostingController(
rootView: ContentView()
)
}
#expect(Count.contentBody == 2)
#expect(Count.subviewBody == 2)
}
}