refactored to using UIControl publisher for .valuechanged

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-08-18 14:11:34 -05:00
parent 95540d44d5
commit 1b3bcfab25
3 changed files with 17 additions and 25 deletions

View File

@ -292,13 +292,5 @@ open class SelectorBase<ModelType: SelectorModel>: Control<ModelType>, Changable
setNeedsLayout() setNeedsLayout()
layoutIfNeeded() layoutIfNeeded()
} }
public func selectedPublisher() -> AnyPublisher<ModelType, Never> {
self.publisher(for: \.isSelected)
.map({ _ in
return self.model
})
.eraseToAnyPublisher()
}
} }

View File

@ -91,11 +91,11 @@ open class SelectorGroupBase<SelectorType, SelectorGroupType: SelectorGroupModel
let newSelectorView = SelectorHandlerType(with: selectorModel) let newSelectorView = SelectorHandlerType(with: selectorModel)
//add the selectedPublisher for the change //add the selectedPublisher for the change
newSelectorView.selectedPublisher() newSelectorView.publisher(for: .valueChanged)
.sink { [weak self] model in .sink(receiveValue: { [weak self] control in
guard self?.model.selectors.count ?? 0 > 0 else { return } guard self?.model.selectors.count ?? 0 > 0 else { return }
self?.didSelect(selector: model) self?.didSelect(selector: control.model)
} })
.store(in: &subscribers) .store(in: &subscribers)
//add model update to the subscribers //add model update to the subscribers

View File

@ -10,22 +10,22 @@ import UIKit
import Combine import Combine
/// A custom subscription to capture UIControl target events. /// A custom subscription to capture UIControl target events.
final class UIControlSubscription<SubscriberType: Subscriber, Control: UIControl>: Subscription where SubscriberType.Input == Control { public final class UIControlSubscription<SubscriberType: Subscriber, Control: UIControl>: Subscription where SubscriberType.Input == Control {
private var subscriber: SubscriberType? private var subscriber: SubscriberType?
private let control: Control private let control: Control
init(subscriber: SubscriberType, control: Control, event: UIControl.Event) { public init(subscriber: SubscriberType, control: Control, event: UIControl.Event) {
self.subscriber = subscriber self.subscriber = subscriber
self.control = control self.control = control
control.addTarget(self, action: #selector(eventHandler), for: event) control.addTarget(self, action: #selector(eventHandler), for: event)
} }
func request(_ demand: Subscribers.Demand) { public func request(_ demand: Subscribers.Demand) {
// We do nothing here as we only want to send events when they occur. // We do nothing here as we only want to send events when they occur.
// See, for more info: https://developer.apple.com/documentation/combine/subscribers/demand // See, for more info: https://developer.apple.com/documentation/combine/subscribers/demand
} }
func cancel() { public func cancel() {
subscriber = nil subscriber = nil
} }
@ -39,15 +39,15 @@ final class UIControlSubscription<SubscriberType: Subscriber, Control: UIControl
} }
/// A custom `Publisher` to work with our custom `UIControlSubscription`. /// A custom `Publisher` to work with our custom `UIControlSubscription`.
struct UIControlPublisher<Control: UIControl>: Publisher { public struct UIControlPublisher<Control: UIControl>: Publisher {
typealias Output = Control public typealias Output = Control
typealias Failure = Never public typealias Failure = Never
let control: Control public let control: Control
let controlEvents: UIControl.Event public let controlEvents: UIControl.Event
init(control: Control, events: UIControl.Event) { public init(control: Control, events: UIControl.Event) {
self.control = control self.control = control
self.controlEvents = events self.controlEvents = events
} }
@ -58,16 +58,16 @@ struct UIControlPublisher<Control: UIControl>: Publisher {
/// - Parameters: /// - Parameters:
/// - subscriber: The subscriber to attach to this `Publisher`. /// - subscriber: The subscriber to attach to this `Publisher`.
/// once attached it can begin to receive values. /// once attached it can begin to receive values.
func receive<S>(subscriber: S) where S : Subscriber, S.Failure == UIControlPublisher.Failure, S.Input == UIControlPublisher.Output { public func receive<S>(subscriber: S) where S : Subscriber, S.Failure == UIControlPublisher.Failure, S.Input == UIControlPublisher.Output {
subscriber.receive(subscription: UIControlSubscription(subscriber: subscriber, control: control, event: controlEvents)) subscriber.receive(subscription: UIControlSubscription(subscriber: subscriber, control: control, event: controlEvents))
} }
} }
/// Extending the `UIControl` types to be able to produce a `UIControl.Event` publisher. /// Extending the `UIControl` types to be able to produce a `UIControl.Event` publisher.
protocol CombineCompatible { } public protocol CombineCompatible { }
extension UIControl: CombineCompatible { } extension UIControl: CombineCompatible { }
extension CombineCompatible where Self: UIControl { extension CombineCompatible where Self: UIControl {
func publisher(for events: UIControl.Event) -> UIControlPublisher<Self> { public func publisher(for events: UIControl.Event) -> UIControlPublisher<Self> {
return UIControlPublisher(control: self, events: events) return UIControlPublisher(control: self, events: events)
} }
} }