Skip to content

Commit 4e2a39c

Browse files
authored
Add notify API and add test case for ViewGraphGeometryObservers (#699)
1 parent c7cd9cf commit 4e2a39c

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

Sources/OpenSwiftUICore/View/Graph/ViewGraphGeometryObservers.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ package struct ViewGraphGeometryObservers<Measurer> where Measurer: ViewGraphGeo
7777
return result
7878
}
7979

80+
// [?]
81+
mutating func notify() {
82+
let keys = store.keys
83+
for proposal in keys {
84+
guard var observer = store[proposal] else { continue }
85+
guard case let .pending(oldSize, pending: newSize) = observer.storage else {
86+
continue
87+
}
88+
if oldSize != newSize {
89+
observer.callback(oldSize, newSize)
90+
}
91+
observer.storage = .value(newSize)
92+
store[proposal] = observer
93+
}
94+
}
95+
8096
/// Adds an observer for a specific layout proposal.
8197
///
8298
/// - Parameters:
@@ -202,7 +218,7 @@ package struct ViewGraphGeometryObservers<Measurer> where Measurer: ViewGraphGeo
202218
return true
203219
case let .pending(value, _):
204220
guard size != value else {
205-
self = .value(size)
221+
self = .value(size) // ?
206222
return false
207223
}
208224
self = .pending(value, pending: size)

Tests/OpenSwiftUICoreTests/View/Graph/ViewGraphGeometryObserversTests.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ private struct TestMeasurer: ViewGraphGeometryMeasurer {
1010
typealias Proposal = CGSize
1111
typealias Size = CGFloat
1212

13+
static var mockValue: CGFloat?
14+
1315
static func measure(given proposal: CGSize, in graph: ViewGraph) -> CGFloat {
14-
max(proposal.width, proposal.height)
16+
mockValue ?? max(proposal.width, proposal.height)
1517
}
1618

1719
static func measure(proposal: CGSize, layoutComputer: LayoutComputer, insets: EdgeInsets) -> CGFloat {
18-
max(proposal.width, proposal.height)
20+
mockValue ?? max(proposal.width, proposal.height)
1921
}
2022

2123
static var invalidValue: CGFloat = .nan
@@ -28,14 +30,19 @@ struct ViewGraphGeometryObserversTests {
2830
@MainActor
2931
@Test
3032
func observeCallback() async throws {
31-
// TODO: when the callback got called.
32-
await confirmation(expectedCount: 0) { confirm in
33+
await confirmation(expectedCount: 1) { confirm in
3334
var observers = Observers()
34-
observers.addObserver(for: CGSize(width: 10, height: 20)) { _, _ in
35+
observers.addObserver(for: CGSize(width: 10, height: 20)) { oldSize, newSize in
3536
confirm()
37+
#expect(oldSize.isApproximatelyEqual(to: 20.0))
38+
#expect(newSize.isApproximatelyEqual(to: 30.0))
3639
}
3740
let emptyViewGraph = ViewGraph(rootViewType: EmptyView.self)
3841
_ = observers.needsUpdate(graph: emptyViewGraph)
42+
TestMeasurer.mockValue = 30.0
43+
defer { TestMeasurer.mockValue = nil }
44+
_ = observers.needsUpdate(graph: emptyViewGraph)
45+
observers.notify()
3946
}
4047
}
4148
#endif

0 commit comments

Comments
 (0)