From 248caeb480b044fd5bd3407e7a738914157cc633 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Thu, 30 Mar 2023 08:43:55 -0500 Subject: [PATCH] updated for maintaining a clickable count Signed-off-by: Matt Bruce --- VDS/Classes/Control.swift | 9 +++---- .../Buttons/Button/ButtonBase.swift | 10 +++---- .../TextFields/EntryField/EntryField.swift | 1 - VDS/Components/Tilelet/Tilelet.swift | 2 +- VDS/Protocols/Clickable.swift | 27 +++++++------------ VDS/Publishers/UIControlPublisher.swift | 12 +++------ 6 files changed, 21 insertions(+), 40 deletions(-) diff --git a/VDS/Classes/Control.swift b/VDS/Classes/Control.swift index 49ab79b4..2131df8c 100644 --- a/VDS/Classes/Control.swift +++ b/VDS/Classes/Control.swift @@ -17,15 +17,12 @@ open class Control: UIControl, Handlerable, ViewProtocol, Resettable, UserInfoab //-------------------------------------------------- public var subject = PassthroughSubject() public var subscribers = Set() - internal var onClickSubscriber: AnyCancellable? { + public var onClickSubscriber: AnyCancellable? { willSet { if let onClickSubscriber { onClickSubscriber.cancel() } } - didSet { - enabledHighlight = onClickSubscriber != nil - } } //-------------------------------------------------- @@ -41,12 +38,12 @@ open class Control: UIControl, Handlerable, ViewProtocol, Resettable, UserInfoab open override var isSelected: Bool { didSet { didChange() } } - internal var enabledHighlight: Bool = false + public var touchUpInsideCount: Int = 0 var isHighlightAnimating = false open override var isHighlighted: Bool { didSet { - if isHighlightAnimating == false && enabledHighlight { + if isHighlightAnimating == false && isClickable { isHighlightAnimating = true UIView.animate(withDuration: 0.1, animations: { [weak self] in self?.updateView() diff --git a/VDS/Components/Buttons/Button/ButtonBase.swift b/VDS/Components/Buttons/Button/ButtonBase.swift index 335722b2..322f1250 100644 --- a/VDS/Components/Buttons/Button/ButtonBase.swift +++ b/VDS/Components/Buttons/Button/ButtonBase.swift @@ -19,6 +19,7 @@ public protocol Buttonable: UIControl, Surfaceable, Disabling { @objc(VDSButtonBase) open class ButtonBase: UIButton, Buttonable, Handlerable, ViewProtocol, Resettable, UserInfoable, Clickable { + //-------------------------------------------------- // MARK: - Configuration Properties //-------------------------------------------------- @@ -29,15 +30,12 @@ open class ButtonBase: UIButton, Buttonable, Handlerable, ViewProtocol, Resettab //-------------------------------------------------- public var subject = PassthroughSubject() public var subscribers = Set() - internal var onClickSubscriber: AnyCancellable? { + public var onClickSubscriber: AnyCancellable? { willSet { if let onClickSubscriber { onClickSubscriber.cancel() } } - didSet { - enabledHighlight = onClickSubscriber != nil - } } //-------------------------------------------------- @@ -60,13 +58,13 @@ open class ButtonBase: UIButton, Buttonable, Handlerable, ViewProtocol, Resettab open var userInfo = [String: Primitive]() - internal var enabledHighlight: Bool = false + public var touchUpInsideCount: Int = 0 internal var isHighlightAnimating = false open override var isHighlighted: Bool { didSet { - if isHighlightAnimating == false && enabledHighlight { + if isHighlightAnimating == false && isClickable { isHighlightAnimating = true UIView.animate(withDuration: 0.1, animations: { [weak self] in self?.updateView() diff --git a/VDS/Components/TextFields/EntryField/EntryField.swift b/VDS/Components/TextFields/EntryField/EntryField.swift index 321aa074..a773ac43 100644 --- a/VDS/Components/TextFields/EntryField/EntryField.swift +++ b/VDS/Components/TextFields/EntryField/EntryField.swift @@ -172,7 +172,6 @@ open class EntryField: Control { open override func setup() { super.setup() - enabledHighlight = false isAccessibilityElement = true accessibilityTraits = .button addSubview(stackView) diff --git a/VDS/Components/Tilelet/Tilelet.swift b/VDS/Components/Tilelet/Tilelet.swift index 1a956680..c6d36858 100644 --- a/VDS/Components/Tilelet/Tilelet.swift +++ b/VDS/Components/Tilelet/Tilelet.swift @@ -63,7 +63,7 @@ open class Tilelet: TileContainer { //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- - internal override var onClickSubscriber: AnyCancellable? { + public override var onClickSubscriber: AnyCancellable? { didSet { isAccessibilityElement = onClickSubscriber != nil } diff --git a/VDS/Protocols/Clickable.swift b/VDS/Protocols/Clickable.swift index 62c771ce..1bf454f6 100644 --- a/VDS/Protocols/Clickable.swift +++ b/VDS/Protocols/Clickable.swift @@ -7,8 +7,12 @@ import Foundation import UIKit +import Combine -public protocol Clickable where Self: UIControl {} +public protocol Clickable where Self: UIControl { + var touchUpInsideCount: Int { get set } + var onClickSubscriber: AnyCancellable? { get set } +} extension Clickable where Self: Handlerable { public func addEvent(event: UIControl.Event, block: @escaping (Self)->()) { @@ -17,25 +21,11 @@ extension Clickable where Self: Handlerable { block(c) }).store(in: &subscribers) } + + internal var isClickable: Bool { return touchUpInsideCount > 0 } } -extension Clickable where Self: ButtonBase { - public var onClick: ((Self) -> ())? { - get { return nil } - set { - if let newValue { - onClickSubscriber = publisher(for: .touchUpInside) - .sink { c in - newValue(c) - } - } else { - onClickSubscriber = nil - } - } - } -} - -extension Clickable where Self: Control { +extension Clickable { public var onClick: ((Self) -> ())? { get { return nil } set { @@ -45,6 +35,7 @@ extension Clickable where Self: Control { newValue(c) } } else { + onClickSubscriber?.cancel() onClickSubscriber = nil } } diff --git a/VDS/Publishers/UIControlPublisher.swift b/VDS/Publishers/UIControlPublisher.swift index c7ccfa0c..6b8fa45d 100644 --- a/VDS/Publishers/UIControlPublisher.swift +++ b/VDS/Publishers/UIControlPublisher.swift @@ -21,10 +21,8 @@ public final class UIControlSubscription 0 { + c.touchUpInsideCount -= 1 } }