Skip to content

Commit a5bff1d

Browse files
committed
Fix index issue in DynamicContainer
1 parent c5070c9 commit a5bff1d

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

Example/OpenSwiftUIUITests/View/DynamicViewContent/ForEachUITests.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,52 @@ struct ForEachUITests {
6565
// openSwiftUIAssertAnimationSnapshot(of: ContentView())
6666
}
6767
}
68+
69+
@Test(
70+
.bug(
71+
"https://github.com/OpenSwiftUIProject/OpenSwiftUI/issues/669",
72+
id: 669
73+
)
74+
)
75+
func dynamicContainerIndex() {
76+
struct ContentView1: View {
77+
var body: some View {
78+
VStack {
79+
Color.red
80+
Color.green
81+
ForEach(0 ..< 1) { index in
82+
Color.red.frame(width: 20, height: 20)
83+
Spacer()
84+
Color.blue.frame(width: 20, height: 20)
85+
}
86+
}
87+
}
88+
}
89+
struct ContentView2: View {
90+
var body: some View {
91+
VStack {
92+
Color.red
93+
Color.green
94+
ForEach(0 ..< 1) { index in
95+
Spacer()
96+
Color.blue
97+
}
98+
}
99+
}
100+
}
101+
struct ContentView3: View {
102+
var body: some View {
103+
VStack {
104+
Color.red.frame(width: 10, height: 10)
105+
Color.green.frame(width: 20, height: 20)
106+
ForEach(0 ..< 1) { index in
107+
Color.blue.frame(width: 40, height: 40)
108+
}
109+
}
110+
}
111+
}
112+
openSwiftUIAssertSnapshot(of: ContentView1(), named: "1")
113+
openSwiftUIAssertSnapshot(of: ContentView2(), named: "2")
114+
openSwiftUIAssertSnapshot(of: ContentView3(), named: "3")
115+
}
68116
}

Sources/OpenSwiftUICore/Layout/Dynamic/DynamicContainer.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ package struct DynamicContainer {
4646
fileprivate(set) var seed: UInt32 = .zero
4747

4848
func viewIndex(id: ID) -> Int? {
49-
guard let value = indexMap[id.uniqueId] else {
49+
guard let index = indexMap[id.uniqueId] else {
5050
return nil
5151
}
52-
return value + Int(id.viewIndex)
52+
let item = items[index]
53+
return Int(item.precedingViewCount + id.viewIndex)
5354
}
5455

5556
func item(for subgraph: Subgraph) -> ItemInfo? {

0 commit comments

Comments
 (0)