Skip to content

Instantly share code, notes, and snippets.

@kiarashvosough1999
Created June 30, 2022 20:40
Show Gist options
  • Save kiarashvosough1999/2c5936350743b7d097bea00bc4028414 to your computer and use it in GitHub Desktop.
Save kiarashvosough1999/2c5936350743b7d097bea00bc4028414 to your computer and use it in GitHub Desktop.
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