diff --git a/VDS/Classes/SelectorGroupHandlerBase.swift b/VDS/Classes/SelectorGroupHandlerBase.swift index 3b67019d..5fee8d6c 100644 --- a/VDS/Classes/SelectorGroupHandlerBase.swift +++ b/VDS/Classes/SelectorGroupHandlerBase.swift @@ -7,14 +7,23 @@ import Foundation import UIKit +import Combine -public class SelectorGroupHandlerBase: Control { +public class SelectorGroupHandlerBase: Control, Changeable { //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- public var selectorViews: [HandlerType] = [] + public var onChangeSubscriber: AnyCancellable? { + willSet { + if let onChangeSubscriber { + onChangeSubscriber.cancel() + } + } + } + //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- diff --git a/VDS/Components/Checkbox/Checkbox.swift b/VDS/Components/Checkbox/Checkbox.swift index cfff406e..f0d690cd 100644 --- a/VDS/Components/Checkbox/Checkbox.swift +++ b/VDS/Components/Checkbox/Checkbox.swift @@ -7,13 +7,13 @@ import Foundation import UIKit +import Combine import VDSColorTokens import VDSFormControlsTokens -import Combine /// Checkboxes are a multi-select component through which a customer indicates a choice. If a binary choice, the component is a checkbox. If the choice has multiple options, the component is a ``CheckboxGroup``. @objc(VDSCheckboxBase) -open class Checkbox: Control, Errorable { +open class Checkbox: Control, Errorable, Changeable { //-------------------------------------------------- // MARK: - Initializers @@ -63,6 +63,14 @@ open class Checkbox: Control, Errorable { //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- + public var onChangeSubscriber: AnyCancellable? { + willSet { + if let onChangeSubscriber { + onChangeSubscriber.cancel() + } + } + } + open var label = Label().with { $0.setContentCompressionResistancePriority(.required, for: .vertical) $0.textPosition = .left diff --git a/VDS/Components/RadioBox/RadioBox.swift b/VDS/Components/RadioBox/RadioBox.swift index b71c0342..f5f8d9b9 100644 --- a/VDS/Components/RadioBox/RadioBox.swift +++ b/VDS/Components/RadioBox/RadioBox.swift @@ -7,12 +7,12 @@ import Foundation import UIKit +import Combine import VDSColorTokens import VDSFormControlsTokens -import Combine @objc(VDSRadioBox) -open class RadioBox: Control { +open class RadioBox: Control, Changeable { //-------------------------------------------------- // MARK: - Initializers @@ -56,6 +56,14 @@ open class RadioBox: Control { //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- + public var onChangeSubscriber: AnyCancellable? { + willSet { + if let onChangeSubscriber { + onChangeSubscriber.cancel() + } + } + } + open var textLabel = Label().with { $0.setContentCompressionResistancePriority(.required, for: .vertical) $0.textPosition = .left diff --git a/VDS/Components/RadioButton/RadioButton.swift b/VDS/Components/RadioButton/RadioButton.swift index 76d29b46..b3b35f3e 100644 --- a/VDS/Components/RadioButton/RadioButton.swift +++ b/VDS/Components/RadioButton/RadioButton.swift @@ -7,11 +7,12 @@ import Foundation import UIKit +import Combine import VDSColorTokens import VDSFormControlsTokens @objc(VDSRadioButton) -open class RadioButton: Control, Errorable { +open class RadioButton: Control, Errorable, Changeable { //-------------------------------------------------- // MARK: - Initializers @@ -61,6 +62,14 @@ open class RadioButton: Control, Errorable { //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- + public var onChangeSubscriber: AnyCancellable? { + willSet { + if let onChangeSubscriber { + onChangeSubscriber.cancel() + } + } + } + open var label = Label().with { $0.setContentCompressionResistancePriority(.required, for: .vertical) $0.textPosition = .left diff --git a/VDS/Components/TextFields/EntryField/EntryField.swift b/VDS/Components/TextFields/EntryField/EntryField.swift index a773ac43..20f221cb 100644 --- a/VDS/Components/TextFields/EntryField/EntryField.swift +++ b/VDS/Components/TextFields/EntryField/EntryField.swift @@ -12,7 +12,7 @@ import VDSFormControlsTokens import Combine @objc(VDSEntryField) -open class EntryField: Control { +open class EntryField: Control, Changeable { //-------------------------------------------------- // MARK: - Enums //-------------------------------------------------- @@ -98,6 +98,14 @@ open class EntryField: Control { //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- + public var onChangeSubscriber: AnyCancellable? { + willSet { + if let onChangeSubscriber { + onChangeSubscriber.cancel() + } + } + } + open var titleLabel = Label().with { $0.setContentCompressionResistancePriority(.required, for: .vertical) $0.attributes = [] diff --git a/VDS/Components/Toggle/Toggle.swift b/VDS/Components/Toggle/Toggle.swift index 05349779..0f6bc797 100644 --- a/VDS/Components/Toggle/Toggle.swift +++ b/VDS/Components/Toggle/Toggle.swift @@ -18,7 +18,7 @@ import Combine Knob: The circular indicator that slides on the container. */ @objc(VDSToggle) -open class Toggle: Control { +open class Toggle: Control, Changeable { //-------------------------------------------------- // MARK: - Enums //-------------------------------------------------- @@ -107,6 +107,14 @@ open class Toggle: Control { //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- + public var onChangeSubscriber: AnyCancellable? { + willSet { + if let onChangeSubscriber { + onChangeSubscriber.cancel() + } + } + } + open var label = Label().with { $0.setContentCompressionResistancePriority(.required, for: .vertical) } diff --git a/VDS/Protocols/Clickable.swift b/VDS/Protocols/Clickable.swift index 4ffecf2b..a202850a 100644 --- a/VDS/Protocols/Clickable.swift +++ b/VDS/Protocols/Clickable.swift @@ -15,13 +15,6 @@ public protocol Clickable: Handlerable where Self: UIControl { } extension Clickable { - public func addEvent(event: UIControl.Event, block: @escaping (Self)->()) { - publisher(for: event) - .sink(receiveValue: { c in - block(c) - }).store(in: &subscribers) - } - public var onClick: ((Self) -> ())? { get { return nil } set { diff --git a/VDS/Protocols/Handlerable.swift b/VDS/Protocols/Handlerable.swift index f9ad8770..41ef01b5 100644 --- a/VDS/Protocols/Handlerable.swift +++ b/VDS/Protocols/Handlerable.swift @@ -34,3 +34,12 @@ extension Handlerable where Self: UIView { subject.send() } } + +extension Handlerable where Self: UIControl { + public func addEvent(event: UIControl.Event, block: @escaping (Self)->()) { + publisher(for: event) + .sink(receiveValue: { c in + block(c) + }).store(in: &subscribers) + } +}