vds_ios/VDS/Protocols/Clickable.swift
Matt Bruce 248caeb480 updated for maintaining a clickable count
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-03-30 08:43:55 -05:00

44 lines
1.0 KiB
Swift

//
// Clickable.swift
// VDS
//
// Created by Matt Bruce on 3/29/23.
//
import Foundation
import UIKit
import Combine
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)->()) {
publisher(for: event)
.sink(receiveValue: { c in
block(c)
}).store(in: &subscribers)
}
internal var isClickable: Bool { return touchUpInsideCount > 0 }
}
extension Clickable {
public var onClick: ((Self) -> ())? {
get { return nil }
set {
if let newValue {
onClickSubscriber = publisher(for: .touchUpInside)
.sink { c in
newValue(c)
}
} else {
onClickSubscriber?.cancel()
onClickSubscriber = nil
}
}
}
}