vds_ios/VDS/Protocols/Clickable.swift
Matt Bruce 334f9b798c refactored more on clickable
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-03-30 09:06:24 -05:00

40 lines
938 B
Swift

//
// Clickable.swift
// VDS
//
// Created by Matt Bruce on 3/29/23.
//
import Foundation
import UIKit
import Combine
public protocol Clickable: Handlerable where Self: UIControl {
var touchUpInsideCount: Int { get set }
var onClickSubscriber: AnyCancellable? { get set }
}
extension Clickable {
public func addEvent(event: UIControl.Event, block: @escaping (Self)->()) {
publisher(for: event)
.sink(receiveValue: { c in
block(c)
}).store(in: &subscribers)
}
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
}
}
}
}