From 1934da254318606fca7a52b317bbb54c1b9349ac Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Fri, 14 Jul 2023 08:17:38 -0500 Subject: [PATCH] added back subject into the flow Signed-off-by: Matt Bruce --- VDS/Classes/Control.swift | 28 ++++++++++--------- VDS/Classes/View.swift | 4 ++- .../Buttons/Button/ButtonBase.swift | 4 ++- VDS/Components/Label/Label.swift | 4 ++- VDS/Protocols/Handlerable.swift | 14 ++++++++++ 5 files changed, 38 insertions(+), 16 deletions(-) diff --git a/VDS/Classes/Control.swift b/VDS/Classes/Control.swift index 2823bb80..d8a6a6bb 100644 --- a/VDS/Classes/Control.swift +++ b/VDS/Classes/Control.swift @@ -16,6 +16,7 @@ open class Control: UIControl, Handlerable, ViewProtocol, Resettable, UserInfoab //-------------------------------------------------- // MARK: - Combine Properties //-------------------------------------------------- + public var subject = PassthroughSubject() /// Set of Subscribers for any Publishers for this Control public var subscribers = Set() @@ -84,18 +85,18 @@ open class Control: UIControl, Handlerable, ViewProtocol, Resettable, UserInfoab } } -// /// Override to deal with setNeedsUpdate() -// open override var isEnabled: Bool { -// get { !disabled } -// set { -// if disabled != !newValue { -// disabled = !newValue -// } -// isUserInteractionEnabled = isEnabled -// setNeedsUpdate() -// } -// } -// + /// Override to deal with setNeedsUpdate() + open override var isEnabled: Bool { + get { !disabled } + set { + if disabled != !newValue { + disabled = !newValue + } + isUserInteractionEnabled = isEnabled + setNeedsUpdate() + } + } + //-------------------------------------------------- // MARK: - Initializers //-------------------------------------------------- @@ -123,7 +124,8 @@ open class Control: UIControl, Handlerable, ViewProtocol, Resettable, UserInfoab if !initialSetupPerformed { initialSetupPerformed = true setup() - setNeedsUpdate() + setupNeedsUpdateEvent() + updateView() } } diff --git a/VDS/Classes/View.swift b/VDS/Classes/View.swift index 08bb2b34..e6e37aad 100644 --- a/VDS/Classes/View.swift +++ b/VDS/Classes/View.swift @@ -17,6 +17,7 @@ open class View: UIView, Handlerable, ViewProtocol, Resettable, UserInfoable { //-------------------------------------------------- // MARK: - Combine Properties //-------------------------------------------------- + public var subject = PassthroughSubject() public var subscribers = Set() //-------------------------------------------------- @@ -63,7 +64,8 @@ open class View: UIView, Handlerable, ViewProtocol, Resettable, UserInfoable { if !initialSetupPerformed { initialSetupPerformed = true setup() - setNeedsUpdate() + setupNeedsUpdateEvent() + updateView() } } diff --git a/VDS/Components/Buttons/Button/ButtonBase.swift b/VDS/Components/Buttons/Button/ButtonBase.swift index 00eed2db..b1eba0dd 100644 --- a/VDS/Components/Buttons/Button/ButtonBase.swift +++ b/VDS/Components/Buttons/Button/ButtonBase.swift @@ -28,6 +28,7 @@ open class ButtonBase: UIButton, Buttonable, Handlerable, ViewProtocol, Resettab //-------------------------------------------------- // MARK: - Combine Properties //-------------------------------------------------- + public var subject = PassthroughSubject() public var subscribers = Set() public var onClickSubscriber: AnyCancellable? { willSet { @@ -123,7 +124,8 @@ open class ButtonBase: UIButton, Buttonable, Handlerable, ViewProtocol, Resettab translatesAutoresizingMaskIntoConstraints = false accessibilityCustomActions = [] setup() - setNeedsUpdate() + setupNeedsUpdateEvent() + updateView() } } diff --git a/VDS/Components/Label/Label.swift b/VDS/Components/Label/Label.swift index 0ca91cd0..cd333fdc 100644 --- a/VDS/Components/Label/Label.swift +++ b/VDS/Components/Label/Label.swift @@ -16,6 +16,7 @@ open class Label: UILabel, Handlerable, ViewProtocol, Resettable, UserInfoable { //-------------------------------------------------- // MARK: - Combine Properties //-------------------------------------------------- + public var subject = PassthroughSubject() public var subscribers = Set() //-------------------------------------------------- @@ -84,7 +85,8 @@ open class Label: UILabel, Handlerable, ViewProtocol, Resettable, UserInfoable { accessibilityCustomActions = [] accessibilityTraits = .staticText setup() - setNeedsUpdate() + setupNeedsUpdateEvent() + updateView() } } diff --git a/VDS/Protocols/Handlerable.swift b/VDS/Protocols/Handlerable.swift index 14246735..856542b1 100644 --- a/VDS/Protocols/Handlerable.swift +++ b/VDS/Protocols/Handlerable.swift @@ -10,12 +10,26 @@ import Combine import UIKit public protocol Handlerable: AnyObject, Initable, Disabling, Surfaceable { + var subject: PassthroughSubject { get set } var subscribers: Set { get set } var shouldUpdateView: Bool { get set } func updateView() } extension Handlerable { + public func setupNeedsUpdateEvent() { + handlerPublisher().sink { [weak self] _ in + self?.updateView() + }.store(in: &subscribers) + } + + public func handlerPublisher() -> AnyPublisher { + subject + .eraseToAnyPublisher() + .debounce(for: .milliseconds(50), scheduler: RunLoop.main) + .eraseToAnyPublisher() + } + public func setNeedsUpdate() { if shouldUpdateView { shouldUpdateView = false