Add support for accessing the widget data when inspecting
All checks were successful
Deploy Docs / publish (push) Successful in 55s
SwiftLint / SwiftLint (push) Successful in 3s

This commit is contained in:
david-swift 2025-02-10 17:54:22 +01:00
parent 681a51110d
commit 9f50e272f3
2 changed files with 21 additions and 7 deletions

View File

@ -9,7 +9,7 @@
struct AppearObserver: ConvenienceWidget {
/// The custom code to edit the widget.
var modify: (ViewStorage) -> Void
var modify: (ViewStorage, WidgetData) -> Void
/// The wrapped view.
var content: AnyView
@ -23,7 +23,7 @@ struct AppearObserver: ConvenienceWidget {
type: Data.Type
) -> ViewStorage where Data: ViewRenderData {
let storage = content.storage(data: data, type: type)
modify(storage)
modify(storage, data)
return storage
}
@ -50,10 +50,17 @@ extension AnyView {
/// Run a function on the widget when it appears for the first time.
/// - Parameter closure: The function.
/// - Returns: A view.
public func inspectOnAppear(_ closure: @escaping (ViewStorage) -> Void) -> AnyView {
public func inspectOnAppear(_ closure: @escaping (ViewStorage, WidgetData) -> Void) -> AnyView {
AppearObserver(modify: closure, content: self)
}
/// Run a function on the widget when it appears for the first time.
/// - Parameter closure: The function.
/// - Returns: A view.
public func inspectOnAppear(_ closure: @escaping (ViewStorage) -> Void) -> AnyView {
inspectOnAppear { storage, _ in closure(storage) }
}
/// Run a function when the view appears for the first time.
/// - Parameter closure: The function.
/// - Returns: A view.

View File

@ -9,7 +9,7 @@
struct InspectorWrapper: ConvenienceWidget {
/// The custom code to edit the widget.
var modify: (ViewStorage, Bool) -> Void
var modify: (ViewStorage, WidgetData, Bool) -> Void
/// The wrapped view.
var content: AnyView
@ -23,7 +23,7 @@ struct InspectorWrapper: ConvenienceWidget {
type: Data.Type
) -> ViewStorage where Data: ViewRenderData {
let storage = content.storage(data: data, type: type)
modify(storage, true)
modify(storage, data, true)
return storage
}
@ -40,7 +40,7 @@ struct InspectorWrapper: ConvenienceWidget {
type: Data.Type
) where Data: ViewRenderData {
content.updateStorage(storage, data: data, updateProperties: updateProperties, type: type)
modify(storage, updateProperties)
modify(storage, data, updateProperties)
}
}
@ -51,10 +51,17 @@ extension AnyView {
/// Run a custom code accessing the view's storage when initializing and updating the view.
/// - Parameter modify: Modify the storage. The boolean indicates whether state in parent views changed.
/// - Returns: A view.
public func inspect(_ modify: @escaping (ViewStorage, Bool) -> Void) -> AnyView {
public func inspect(_ modify: @escaping (ViewStorage, WidgetData, Bool) -> Void) -> AnyView {
InspectorWrapper(modify: modify, content: self)
}
/// Run a custom code accessing the view's storage when initializing and updating the view.
/// - Parameter modify: Modify the storage. The boolean indicates whether state in parent views changed.
/// - Returns: A view.
public func inspect(_ modify: @escaping (ViewStorage, Bool) -> Void) -> AnyView {
inspect { storage, _, updateProperties in modify(storage, updateProperties) }
}
/// Run a function when the view gets updated.
/// - Parameter onUpdate: The function.
/// - Returns: A view.