From d159075f83d7016a6951f1449321e9c6526d7ac4 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Fri, 14 Jul 2023 11:53:57 -0500 Subject: [PATCH] added new updateStrategy Signed-off-by: Matt Bruce --- VDS/Classes/Control.swift | 9 +++++---- VDS/Classes/View.swift | 8 +++++--- VDS/Components/Buttons/Button/ButtonBase.swift | 8 +++++--- VDS/Components/Label/Label.swift | 8 +++++--- VDS/Protocols/Handlerable.swift | 9 ++++++++- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/VDS/Classes/Control.swift b/VDS/Classes/Control.swift index 5e4e41d6..dafce1f7 100644 --- a/VDS/Classes/Control.swift +++ b/VDS/Classes/Control.swift @@ -142,10 +142,11 @@ open class Control: UIControl, Handlerable, ViewProtocol, Resettable, UserInfoab //-------------------------------------------------- open override func didMoveToWindow() { super.didMoveToWindow() - if window != nil { - print("sss - \(Self.self) changed to delayed update strategy") - updateStrategy = .delayed - } + // Keep the strategy if it's 'alwaysImmediate' + guard updateStrategy != .alwaysImmediate else { return } + + // Update the strategy based on whether the view is in a window + updateStrategy = window == nil ? .delayed : .immediate } /// Update this view based off of property changes diff --git a/VDS/Classes/View.swift b/VDS/Classes/View.swift index f9b763fd..7ce4adcb 100644 --- a/VDS/Classes/View.swift +++ b/VDS/Classes/View.swift @@ -74,9 +74,11 @@ open class View: UIView, Handlerable, ViewProtocol, Resettable, UserInfoable { //-------------------------------------------------- open override func didMoveToWindow() { super.didMoveToWindow() - if window != nil { - updateStrategy = .delayed - } + // Keep the strategy if it's 'alwaysImmediate' + guard updateStrategy != .alwaysImmediate else { return } + + // Update the strategy based on whether the view is in a window + updateStrategy = window == nil ? .delayed : .immediate } /// Update this view based off of property changes diff --git a/VDS/Components/Buttons/Button/ButtonBase.swift b/VDS/Components/Buttons/Button/ButtonBase.swift index aebd0265..c6c78fa5 100644 --- a/VDS/Components/Buttons/Button/ButtonBase.swift +++ b/VDS/Components/Buttons/Button/ButtonBase.swift @@ -151,9 +151,11 @@ open class ButtonBase: UIButton, Buttonable, Handlerable, ViewProtocol, Resettab //-------------------------------------------------- open override func didMoveToWindow() { super.didMoveToWindow() - if window != nil { - updateStrategy = .delayed - } + // Keep the strategy if it's 'alwaysImmediate' + guard updateStrategy != .alwaysImmediate else { return } + + // Update the strategy based on whether the view is in a window + updateStrategy = window == nil ? .delayed : .immediate } override open var intrinsicContentSize: CGSize { diff --git a/VDS/Components/Label/Label.swift b/VDS/Components/Label/Label.swift index 82df109e..f4f76b6c 100644 --- a/VDS/Components/Label/Label.swift +++ b/VDS/Components/Label/Label.swift @@ -110,9 +110,11 @@ open class Label: UILabel, Handlerable, ViewProtocol, Resettable, UserInfoable { //-------------------------------------------------- open override func didMoveToWindow() { super.didMoveToWindow() - if window != nil { - updateStrategy = .delayed - } + // Keep the strategy if it's 'alwaysImmediate' + guard updateStrategy != .alwaysImmediate else { return } + + // Update the strategy based on whether the view is in a window + updateStrategy = window == nil ? .delayed : .immediate } open func updateView() { diff --git a/VDS/Protocols/Handlerable.swift b/VDS/Protocols/Handlerable.swift index cf3f841f..cacb904a 100644 --- a/VDS/Protocols/Handlerable.swift +++ b/VDS/Protocols/Handlerable.swift @@ -12,6 +12,7 @@ import UIKit public enum HandlerableUpdateStrategy { case immediate case delayed + case alwaysImmediate } public protocol Handlerable: AnyObject, Initable, Disabling, Surfaceable { @@ -40,10 +41,16 @@ extension Handlerable { switch updateStrategy { case .delayed: delayedSubject.send() - case .immediate: + case .immediate, .alwaysImmediate: immediateSubject.send() } } + + public func updateIfNeeded(_ oldValue: T, _ newValue: T) { + if oldValue != newValue { + setNeedsUpdate() + } + } } extension Handlerable where Self: UIControl {