44 lines
1.0 KiB
Swift
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
|
|
}
|
|
}
|
|
}
|
|
}
|