Fix body's storage not initializing

This commit is contained in:
david-swift 2024-06-21 07:19:31 +02:00
parent e3c6946917
commit c7f37e0c1c
5 changed files with 46 additions and 16 deletions

View File

@ -76,7 +76,9 @@ extension Array: AnyView where Element == AnyView {
modifiers: [(AnyView) -> AnyView], modifiers: [(AnyView) -> AnyView],
type: WidgetType.Type type: WidgetType.Type
) -> [ViewStorage] { ) -> [ViewStorage] {
compactMap { $0.renderable(type: type, modifiers: modifiers) ? $0.storage(modifiers: [], type: type) : nil } compactMap { view in
view.renderable(type: type, modifiers: modifiers) ? view.storage(modifiers: modifiers, type: type) : nil
}
} }
} }

View File

@ -83,6 +83,9 @@ extension AnyView {
if let peer = modified as? Widget { if let peer = modified as? Widget {
return peer return peer
} }
if let array = modified as? Body {
return Wrapper { array }
}
return Wrapper { viewContent.map { $0.getModified(modifiers: modifiers) } } return Wrapper { viewContent.map { $0.getModified(modifiers: modifiers) } }
} }

View File

@ -11,7 +11,7 @@ import Observation
public struct StateWrapper: ConvenienceWidget { public struct StateWrapper: ConvenienceWidget {
/// The content. /// The content.
var content: () -> Body var content: Body
/// The state information (from properties with the `State` wrapper). /// The state information (from properties with the `State` wrapper).
var state: [String: StateProtocol] = [:] var state: [String: StateProtocol] = [:]
@ -24,7 +24,7 @@ public struct StateWrapper: ConvenienceWidget {
/// The debug tree's content. /// The debug tree's content.
public var debugTreeContent: [(String, body: Body)] { public var debugTreeContent: [(String, body: Body)] {
[("content", body: content())] [("content", body: content)]
} }
/// The identifier of the field storing whether to update the wrapper's content. /// The identifier of the field storing whether to update the wrapper's content.
@ -33,7 +33,7 @@ public struct StateWrapper: ConvenienceWidget {
/// Initialize a `StateWrapper`. /// Initialize a `StateWrapper`.
/// - Parameter content: The view content. /// - Parameter content: The view content.
public init(@ViewBuilder content: @escaping () -> Body) { public init(@ViewBuilder content: @escaping () -> Body) {
self.content = content self.content = content()
} }
/// Initialize a `StateWrapper`. /// Initialize a `StateWrapper`.
@ -41,7 +41,7 @@ public struct StateWrapper: ConvenienceWidget {
/// - content: The view content. /// - content: The view content.
/// - state: The state information. /// - state: The state information.
init(content: @escaping () -> Body, state: [String: StateProtocol]) { init(content: @escaping () -> Body, state: [String: StateProtocol]) {
self.content = content self.content = content()
self.state = state self.state = state
} }
@ -68,11 +68,10 @@ public struct StateWrapper: ConvenienceWidget {
property.value.content.storage.update = false property.value.content.storage.update = false
} }
} }
if let storage = storage.content[.mainContent]?.first { guard let storages = storage.content[.mainContent] else {
content() return
.widget(modifiers: modifiers)
.update(storage, modifiers: modifiers, updateProperties: updateProperties, type: type)
} }
content.update(storages, modifiers: modifiers, updateProperties: updateProperties, type: type)
} }
/// Get a view storage. /// Get a view storage.
@ -81,8 +80,8 @@ public struct StateWrapper: ConvenienceWidget {
/// - type: The type of the widgets. /// - type: The type of the widgets.
/// - Returns: The view storage. /// - Returns: The view storage.
public func container<WidgetType>(modifiers: [(AnyView) -> AnyView], type: WidgetType.Type) -> ViewStorage { public func container<WidgetType>(modifiers: [(AnyView) -> AnyView], type: WidgetType.Type) -> ViewStorage {
let content = content().storage(modifiers: modifiers, type: type) let content = content.storages(modifiers: modifiers, type: type)
let storage = ViewStorage(content.pointer, content: [.mainContent: [content]]) let storage = ViewStorage(nil, content: [.mainContent: content])
storage.state = state storage.state = state
observe(storage: storage) observe(storage: storage)
return storage return storage
@ -92,7 +91,7 @@ public struct StateWrapper: ConvenienceWidget {
/// - Parameter storage: The view storage /// - Parameter storage: The view storage
func observe(storage: ViewStorage) { func observe(storage: ViewStorage) {
withObservationTracking { withObservationTracking {
_ = content().getDebugTree(parameters: true, type: AnyView.self) _ = content.getDebugTree(parameters: true, type: AnyView.self)
} onChange: { } onChange: {
storage.fields[updateID] = true storage.fields[updateID] = true
UpdateManager.updateViews() UpdateManager.updateViews()

View File

@ -4,9 +4,13 @@ import SampleBackends
struct DemoView: SimpleView { struct DemoView: SimpleView {
var view: Body { var view: Body {
Backend1.TestWidget1() Wrapper {
TestView() Backend1.TestWidget1()
testContent Backend1.Button("") {
}
TestView()
testContent
}
} }
@ViewBuilder @ViewBuilder
@ -20,7 +24,7 @@ struct DemoView: SimpleView {
struct TestView: SimpleView { struct TestView: SimpleView {
var view: Body { var view: Body {
[] Backend2.TestWidget4()
} }
} }

View File

@ -54,6 +54,28 @@ public enum Backend1 {
} }
public struct Button: BackendWidget {
public init(_ label: String, action: @escaping () -> Void) {
}
public var debugTreeContent: [(String, body: Body)] {
[]
}
public var debugTreeParameters: [(String, value: any CustomStringConvertible)] {
[]
}
public func container<WidgetType>(modifiers: [(any AnyView) -> any AnyView], type: WidgetType.Type) -> ViewStorage {
.init(nil)
}
public func update<WidgetType>(_ storage: ViewStorage, modifiers: [(any AnyView) -> any AnyView], updateProperties: Bool, type: WidgetType.Type) {
}
}
public protocol BackendWidget: Widget { } public protocol BackendWidget: Widget { }
} }