updated for maintaining a clickable count

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-03-30 08:43:55 -05:00
parent f164429ece
commit 248caeb480
6 changed files with 21 additions and 40 deletions

View File

@ -17,15 +17,12 @@ open class Control: UIControl, Handlerable, ViewProtocol, Resettable, UserInfoab
//-------------------------------------------------- //--------------------------------------------------
public var subject = PassthroughSubject<Void, Never>() public var subject = PassthroughSubject<Void, Never>()
public var subscribers = Set<AnyCancellable>() public var subscribers = Set<AnyCancellable>()
internal var onClickSubscriber: AnyCancellable? { public var onClickSubscriber: AnyCancellable? {
willSet { willSet {
if let onClickSubscriber { if let onClickSubscriber {
onClickSubscriber.cancel() 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() } } open override var isSelected: Bool { didSet { didChange() } }
internal var enabledHighlight: Bool = false public var touchUpInsideCount: Int = 0
var isHighlightAnimating = false var isHighlightAnimating = false
open override var isHighlighted: Bool { open override var isHighlighted: Bool {
didSet { didSet {
if isHighlightAnimating == false && enabledHighlight { if isHighlightAnimating == false && isClickable {
isHighlightAnimating = true isHighlightAnimating = true
UIView.animate(withDuration: 0.1, animations: { [weak self] in UIView.animate(withDuration: 0.1, animations: { [weak self] in
self?.updateView() self?.updateView()

View File

@ -19,6 +19,7 @@ public protocol Buttonable: UIControl, Surfaceable, Disabling {
@objc(VDSButtonBase) @objc(VDSButtonBase)
open class ButtonBase: UIButton, Buttonable, Handlerable, ViewProtocol, Resettable, UserInfoable, Clickable { open class ButtonBase: UIButton, Buttonable, Handlerable, ViewProtocol, Resettable, UserInfoable, Clickable {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Configuration Properties // MARK: - Configuration Properties
//-------------------------------------------------- //--------------------------------------------------
@ -29,15 +30,12 @@ open class ButtonBase: UIButton, Buttonable, Handlerable, ViewProtocol, Resettab
//-------------------------------------------------- //--------------------------------------------------
public var subject = PassthroughSubject<Void, Never>() public var subject = PassthroughSubject<Void, Never>()
public var subscribers = Set<AnyCancellable>() public var subscribers = Set<AnyCancellable>()
internal var onClickSubscriber: AnyCancellable? { public var onClickSubscriber: AnyCancellable? {
willSet { willSet {
if let onClickSubscriber { if let onClickSubscriber {
onClickSubscriber.cancel() onClickSubscriber.cancel()
} }
} }
didSet {
enabledHighlight = onClickSubscriber != nil
}
} }
//-------------------------------------------------- //--------------------------------------------------
@ -60,13 +58,13 @@ open class ButtonBase: UIButton, Buttonable, Handlerable, ViewProtocol, Resettab
open var userInfo = [String: Primitive]() open var userInfo = [String: Primitive]()
internal var enabledHighlight: Bool = false public var touchUpInsideCount: Int = 0
internal var isHighlightAnimating = false internal var isHighlightAnimating = false
open override var isHighlighted: Bool { open override var isHighlighted: Bool {
didSet { didSet {
if isHighlightAnimating == false && enabledHighlight { if isHighlightAnimating == false && isClickable {
isHighlightAnimating = true isHighlightAnimating = true
UIView.animate(withDuration: 0.1, animations: { [weak self] in UIView.animate(withDuration: 0.1, animations: { [weak self] in
self?.updateView() self?.updateView()

View File

@ -172,7 +172,6 @@ open class EntryField: Control {
open override func setup() { open override func setup() {
super.setup() super.setup()
enabledHighlight = false
isAccessibilityElement = true isAccessibilityElement = true
accessibilityTraits = .button accessibilityTraits = .button
addSubview(stackView) addSubview(stackView)

View File

@ -63,7 +63,7 @@ open class Tilelet: TileContainer {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Public Properties // MARK: - Public Properties
//-------------------------------------------------- //--------------------------------------------------
internal override var onClickSubscriber: AnyCancellable? { public override var onClickSubscriber: AnyCancellable? {
didSet { didSet {
isAccessibilityElement = onClickSubscriber != nil isAccessibilityElement = onClickSubscriber != nil
} }

View File

@ -7,8 +7,12 @@
import Foundation import Foundation
import UIKit 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 { extension Clickable where Self: Handlerable {
public func addEvent(event: UIControl.Event, block: @escaping (Self)->()) { public func addEvent(event: UIControl.Event, block: @escaping (Self)->()) {
@ -17,25 +21,11 @@ extension Clickable where Self: Handlerable {
block(c) block(c)
}).store(in: &subscribers) }).store(in: &subscribers)
} }
internal var isClickable: Bool { return touchUpInsideCount > 0 }
} }
extension Clickable where Self: ButtonBase { extension Clickable {
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 {
public var onClick: ((Self) -> ())? { public var onClick: ((Self) -> ())? {
get { return nil } get { return nil }
set { set {
@ -45,6 +35,7 @@ extension Clickable where Self: Control {
newValue(c) newValue(c)
} }
} else { } else {
onClickSubscriber?.cancel()
onClickSubscriber = nil onClickSubscriber = nil
} }
} }

View File

@ -21,10 +21,8 @@ public final class UIControlSubscription<SubscriberType: Subscriber, Control: UI
self.event = event self.event = event
//allow highlight for VDS.Controls on "onClick" events //allow highlight for VDS.Controls on "onClick" events
if let c = control as? VDS.Control, event == .touchUpInside { if let c = control as? Clickable, event == .touchUpInside {
c.enabledHighlight = true c.touchUpInsideCount += 1
} else if let c = control as? VDS.ButtonBase, event == .touchUpInside {
c.enabledHighlight = true
} }
control.addTarget(self, action: #selector(eventHandler), for: event) control.addTarget(self, action: #selector(eventHandler), for: event)
} }
@ -40,10 +38,8 @@ public final class UIControlSubscription<SubscriberType: Subscriber, Control: UI
deinit { deinit {
//remove highlight for VDS.Controls on "onClick" events //remove highlight for VDS.Controls on "onClick" events
if let c = control as? VDS.Control, event == .touchUpInside { if let c = control as? Clickable, event == .touchUpInside, c.touchUpInsideCount > 0 {
c.enabledHighlight = false c.touchUpInsideCount -= 1
} else if let c = control as? VDS.ButtonBase, event == .touchUpInside {
c.enabledHighlight = false
} }
} }