Skip to content

Commit 72be177

Browse files
authored
Adding Binding DynamicProperty implementation (#31)
* [Optimize] Update folder structure * Update Binding DynamicProperty implementation
1 parent 499da60 commit 72be177

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

Sources/OpenSwiftUI/DataAndStorage/ModelData/Binding/Binding.swift

+51-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
//
55
// Created by Kyle on 2023/11/5.
66
// Lastest Version: iOS 15.5
7-
// Status: Blocked by DynamicProperty
7+
// Status: Complete
8+
// ID: 5436F2B399369BE3B016147A5F8FE9F2
89

910
/// A property wrapper type that can read and write a value owned by a source of
1011
/// truth.
@@ -297,9 +298,55 @@ extension Binding {
297298
}
298299

299300
extension Binding: DynamicProperty {
300-
// TODO:
301-
// public static func _makeProperty<V>(in buffer: inout _DynamicPropertyBuffer, container: _GraphValue<V>, fieldOffset: Int, inputs: inout _GraphInputs) {
302-
// }
301+
private struct ScopedLocation: Location {
302+
var base: AnyLocation<Value>
303+
var wasRead: Bool
304+
305+
init(base: AnyLocation<Value>) {
306+
self.base = base
307+
self.wasRead = base.wasRead
308+
}
309+
310+
func get() -> Value {
311+
base.get()
312+
}
313+
314+
func set(_ value: Value, transaction: Transaction) {
315+
base.set(value, transaction: transaction)
316+
}
317+
318+
func update() -> (Value, Bool) {
319+
base.update()
320+
}
321+
}
322+
323+
private struct Box: DynamicPropertyBox {
324+
var location: LocationBox<ScopedLocation>?
325+
326+
typealias Property = Binding
327+
func destroy() {}
328+
func reset() {}
329+
mutating func update(property: inout Property, phase: _GraphInputs.Phase) -> Bool {
330+
if let location {
331+
if location.location.base !== property.location {
332+
self.location = LocationBox(location: ScopedLocation(base: property.location))
333+
if location.wasRead {
334+
self.location!.wasRead = true
335+
}
336+
}
337+
} else {
338+
location = LocationBox(location: ScopedLocation(base: property.location))
339+
}
340+
let (value, isUpdated) = location!.update()
341+
property.location = location!
342+
property._value = value
343+
return isUpdated ? location!.wasRead : false
344+
}
345+
}
346+
347+
public static func _makeProperty<V>(in buffer: inout _DynamicPropertyBuffer, container: _GraphValue<V>, fieldOffset: Int, inputs: inout _GraphInputs) {
348+
buffer.append(Box(), fieldOffset: fieldOffset)
349+
}
303350
}
304351

305352
// MARK: - Binding Internal API

Sources/OpenSwiftUI/DataAndStorage/ModelData/DynamicProperty/DynamicPropertyBox.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ protocol DynamicPropertyBox<Property>: DynamicProperty {
1010
associatedtype Property: DynamicProperty
1111
func destroy()
1212
func reset()
13-
func update(property: inout Property, phase: _GraphInputs.Phase) -> Bool
13+
mutating func update(property: inout Property, phase: _GraphInputs.Phase) -> Bool
1414
func getState<Value>(type: Value.Type) -> Binding<Value>?
1515
}
1616

0 commit comments

Comments
 (0)