refactor for use in both UIControl and UIView
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
dba0879b99
commit
1e9aa876db
@ -9,12 +9,12 @@ import Foundation
|
|||||||
import UIKit
|
import UIKit
|
||||||
import Combine
|
import Combine
|
||||||
|
|
||||||
public protocol Clickable: ViewProtocol where Self: UIControl {
|
public protocol Clickable: ViewProtocol {
|
||||||
/// Sets the primary Subscriber used for the UIControl event .touchUpInside.
|
/// Sets the primary Subscriber used for the UIControl event .touchUpInside.
|
||||||
var onClickSubscriber: AnyCancellable? { get set }
|
var onClickSubscriber: AnyCancellable? { get set }
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Clickable {
|
extension Clickable where Self: UIControl {
|
||||||
/// Allows the setting of a completion block against the onClickSubscriber cancellable. This will
|
/// Allows the setting of a completion block against the onClickSubscriber cancellable. This will
|
||||||
/// completion block will get executed against the UIControl publisher for the 'touchUpInside' action.
|
/// completion block will get executed against the UIControl publisher for the 'touchUpInside' action.
|
||||||
public var onClick: ((Self) -> ())? {
|
public var onClick: ((Self) -> ())? {
|
||||||
@ -34,3 +34,24 @@ extension Clickable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension Clickable where Self: UIView {
|
||||||
|
/// Allows the setting of a completion block against the onClickSubscriber cancellable. This will
|
||||||
|
/// completion block will get executed against the UIControl publisher for the 'touchUpInside' action.
|
||||||
|
public var onClick: ((Self) -> ())? {
|
||||||
|
get { return nil }
|
||||||
|
set {
|
||||||
|
onClickSubscriber?.cancel()
|
||||||
|
if let newValue {
|
||||||
|
onClickSubscriber = publisher(for: UITapGestureRecognizer())
|
||||||
|
.sink { [weak self] _ in
|
||||||
|
guard let self, self.isEnabled else { return }
|
||||||
|
newValue(self)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
onClickSubscriber = nil
|
||||||
|
}
|
||||||
|
setNeedsUpdate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user