From 681a51110db54cfb48dd7eb8413e310a31fc3c2d Mon Sep 17 00:00:00 2001 From: david-swift Date: Mon, 11 Nov 2024 12:59:18 +0100 Subject: [PATCH] Add support for non-updating state properties --- Sources/Model/Data Flow/State.swift | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/Sources/Model/Data Flow/State.swift b/Sources/Model/Data Flow/State.swift index 35ebf2d..30a536f 100644 --- a/Sources/Model/Data Flow/State.swift +++ b/Sources/Model/Data Flow/State.swift @@ -18,8 +18,10 @@ public struct State: StateProtocol { } nonmutating set { rawValue = newValue - content.update = true - StateManager.updateViews(force: forceUpdates) + if !blockUpdates { + content.update = true + StateManager.updateViews(force: forceUpdates) + } writeValue?(newValue) } } @@ -47,7 +49,10 @@ public struct State: StateProtocol { } /// Whether to force update the views when the value changes. - var forceUpdates: Bool + var forceUpdates = false + + /// Whether to block updates. + var blockUpdates = false /// The closure for initializing the state property's value. var getInitialValue: () -> Value @@ -67,21 +72,35 @@ public struct State: StateProtocol { self.forceUpdates = forceUpdates } + /// Initialize a property representing a state in the view with an autoclosure. + /// - Parameters: + /// - wrappedValue: The wrapped value. + /// - blockUpdates: Whether updates to this state value should not update the UI. + /// + /// This can be useful for storing data and reading this data on special occasions, e.g. on startup. + public init(wrappedValue: @autoclosure @escaping () -> Value, blockUpdates: Bool) { + getInitialValue = wrappedValue + self.blockUpdates = blockUpdates + } + /// Initialize a property representing a state in the view with an explicit closure. /// - Parameters: /// - wrappedValue: Get the wrapped value. /// - writeValue: Perform additional operations when the value changes. /// - forceUpdates: Whether to force update all available views when the property gets modified. + /// - blockUpdates: Whether updates to this state value should not update the UI. /// - /// This initializer can be used to get data from the disk. + /// This initializer can be used e.g. to get data from the disk. public init( wrappedValue: @escaping () -> Value, writeValue: ((Value) -> Void)? = nil, - forceUpdates: Bool = false + forceUpdates: Bool = false, + blockUpdates: Bool = false ) { getInitialValue = wrappedValue self.writeValue = writeValue self.forceUpdates = forceUpdates + self.blockUpdates = blockUpdates } /// Get the initial value.