From 9f50e272f3f875e8fd71b9a7de1c12d4a8d9b3e0 Mon Sep 17 00:00:00 2001 From: david-swift Date: Mon, 10 Feb 2025 17:54:22 +0100 Subject: [PATCH] Add support for accessing the widget data when inspecting --- Sources/View/AppearObserver.swift | 13 ++++++++++--- Sources/View/InspectorWrapper.swift | 15 +++++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Sources/View/AppearObserver.swift b/Sources/View/AppearObserver.swift index 035021a..773df74 100644 --- a/Sources/View/AppearObserver.swift +++ b/Sources/View/AppearObserver.swift @@ -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. diff --git a/Sources/View/InspectorWrapper.swift b/Sources/View/InspectorWrapper.swift index 1cd3bfb..f198dab 100644 --- a/Sources/View/InspectorWrapper.swift +++ b/Sources/View/InspectorWrapper.swift @@ -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.