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],
type: WidgetType.Type
) -> [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 {
return peer
}
if let array = modified as? Body {
return Wrapper { array }
}
return Wrapper { viewContent.map { $0.getModified(modifiers: modifiers) } }
}

View File

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

View File

@ -4,9 +4,13 @@ import SampleBackends
struct DemoView: SimpleView {
var view: Body {
Backend1.TestWidget1()
TestView()
testContent
Wrapper {
Backend1.TestWidget1()
Backend1.Button("") {
}
TestView()
testContent
}
}
@ViewBuilder
@ -20,7 +24,7 @@ struct DemoView: SimpleView {
struct TestView: SimpleView {
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 { }
}