Created
June 30, 2022 20:40
-
-
Save kiarashvosough1999/2c5936350743b7d097bea00bc4028414 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import Combine | |
protocol UIControlPublishable: UIControl {} | |
extension UIControlPublishable { | |
func publisher(for event: UIControl.Event) -> UIControl.InteractionPublisher<Self> { | |
return InteractionPublisher(control: self, event: event) | |
} | |
} | |
extension UIControl: UIControlPublishable { | |
class InteractionSubscription<S: Subscriber, C: UIControl>: Subscription where S.Input == C { | |
private let subscriber: S? | |
private weak var control: C? | |
private let event: UIControl.Event | |
init(subscriber: S, | |
control: C?, | |
event: UIControl.Event) { | |
self.subscriber = subscriber | |
self.control = control | |
self.event = event | |
self.control?.addTarget(self, action: #selector(handleEvent), for: event) | |
} | |
@objc func handleEvent(_ sender: UIControl) { | |
guard let control = self.control else { | |
return | |
} | |
_ = self.subscriber?.receive(control) | |
} | |
func request(_ demand: Subscribers.Demand) {} | |
func cancel() { | |
self.control?.removeTarget(self, action: #selector(handleEvent), for: self.event) | |
self.control = nil | |
} | |
} | |
enum InteractionPublisherError: Error { | |
case objectFoundNil | |
} | |
struct InteractionPublisher<C: UIControl>: Publisher { | |
typealias Output = C | |
typealias Failure = InteractionPublisherError | |
private weak var control: C? | |
private let event: UIControl.Event | |
init(control: C, event: UIControl.Event) { | |
self.control = control | |
self.event = event | |
} | |
func receive<S>(subscriber: S) where S : Subscriber, InteractionPublisherError == S.Failure, C == S.Input { | |
guard let control = control else { | |
subscriber.receive(completion: .failure(.objectFoundNil)) | |
return | |
} | |
let subscription = InteractionSubscription( | |
subscriber: subscriber, | |
control: control, | |
event: event | |
) | |
subscriber.receive(subscription: subscription) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment