From 94d44c5d5476ec5c85ff978aaf091c27d75cdb68 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Wed, 29 Mar 2023 15:37:32 -0500 Subject: [PATCH] refactored clickable removed old clickable code Signed-off-by: Matt Bruce --- VDS/Classes/Control.swift | 2 +- VDS/Components/Buttons/Button/Button.swift | 4 +- .../Buttons/Button/ButtonBase.swift | 2 +- .../Buttons/TextLink/TextLink.swift | 4 +- .../Buttons/TextLinkCaret/TextLinkCaret.swift | 4 +- VDS/Components/Checkbox/Checkbox.swift | 4 +- VDS/Components/RadioBox/RadioBox.swift | 4 +- VDS/Components/RadioButton/RadioButton.swift | 4 +- VDS/Components/RadioSwatch/RadioSwatch.swift | 6 +-- .../TileContainer/TileContainer.swift | 13 ++--- VDS/Components/Tilelet/Tilelet.swift | 6 +-- VDS/Components/Toggle/Toggle.swift | 6 +-- VDS/Protocols/Clickable.swift | 47 ++++++++++--------- 13 files changed, 43 insertions(+), 63 deletions(-) diff --git a/VDS/Classes/Control.swift b/VDS/Classes/Control.swift index 0efbec97..49ab79b4 100644 --- a/VDS/Classes/Control.swift +++ b/VDS/Classes/Control.swift @@ -10,7 +10,7 @@ import UIKit import Combine @objc(VDSControl) -open class Control: UIControl, Handlerable, ViewProtocol, Resettable, UserInfoable { +open class Control: UIControl, Handlerable, ViewProtocol, Resettable, UserInfoable, Clickable { //-------------------------------------------------- // MARK: - Combine Properties diff --git a/VDS/Components/Buttons/Button/Button.swift b/VDS/Components/Buttons/Button/Button.swift index 13823178..0b52e0fe 100644 --- a/VDS/Components/Buttons/Button/Button.swift +++ b/VDS/Components/Buttons/Button/Button.swift @@ -17,7 +17,7 @@ public enum ButtonSize: String, CaseIterable { } @objc(VDSButton) -open class Button: ButtonBase, Useable, Clickable { +open class Button: ButtonBase, Useable { //-------------------------------------------------- // MARK: - Private Properties @@ -30,8 +30,6 @@ open class Button: ButtonBase, Useable, Clickable { //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- - open var onClick: ((Button) -> ())? { didSet { setupOnClick() } } - open override var availableSizes: [ButtonSize] { [.large, .small] } open var use: Use = .primary { didSet { didChange() }} diff --git a/VDS/Components/Buttons/Button/ButtonBase.swift b/VDS/Components/Buttons/Button/ButtonBase.swift index b8f2d098..335722b2 100644 --- a/VDS/Components/Buttons/Button/ButtonBase.swift +++ b/VDS/Components/Buttons/Button/ButtonBase.swift @@ -18,7 +18,7 @@ public protocol Buttonable: UIControl, Surfaceable, Disabling { } @objc(VDSButtonBase) -open class ButtonBase: UIButton, Buttonable, Handlerable, ViewProtocol, Resettable, UserInfoable { +open class ButtonBase: UIButton, Buttonable, Handlerable, ViewProtocol, Resettable, UserInfoable, Clickable { //-------------------------------------------------- // MARK: - Configuration Properties //-------------------------------------------------- diff --git a/VDS/Components/Buttons/TextLink/TextLink.swift b/VDS/Components/Buttons/TextLink/TextLink.swift index 3d0ec706..a253a406 100644 --- a/VDS/Components/Buttons/TextLink/TextLink.swift +++ b/VDS/Components/Buttons/TextLink/TextLink.swift @@ -12,7 +12,7 @@ import VDSFormControlsTokens import Combine @objc(VDSTextLink) -open class TextLink: ButtonBase, Clickable { +open class TextLink: ButtonBase { //-------------------------------------------------- // MARK: - Private Properties @@ -22,8 +22,6 @@ open class TextLink: ButtonBase, Clickable { //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- - open var onClick: ((TextLink) -> ())? { didSet { setupOnClick() } } - open var size: ButtonSize = .large { didSet { didChange() }} open override var availableSizes: [ButtonSize] { [.large, .small] } diff --git a/VDS/Components/Buttons/TextLinkCaret/TextLinkCaret.swift b/VDS/Components/Buttons/TextLinkCaret/TextLinkCaret.swift index a5521a4e..f6ba09a0 100644 --- a/VDS/Components/Buttons/TextLinkCaret/TextLinkCaret.swift +++ b/VDS/Components/Buttons/TextLinkCaret/TextLinkCaret.swift @@ -12,7 +12,7 @@ import VDSFormControlsTokens import Combine @objc(VDSTextLinkCaret) -open class TextLinkCaret: ButtonBase, Clickable { +open class TextLinkCaret: ButtonBase { //-------------------------------------------------- // MARK: - Enums @@ -24,8 +24,6 @@ open class TextLinkCaret: ButtonBase, Clickable { //-------------------------------------------------- // MARK: - Private Properties //-------------------------------------------------- - open var onClick: ((TextLinkCaret) -> ())? { didSet { setupOnClick() } } - open override var textStyle: TextStyle { TextStyle.boldBodyLarge } diff --git a/VDS/Components/Checkbox/Checkbox.swift b/VDS/Components/Checkbox/Checkbox.swift index 5bc4047b..cfff406e 100644 --- a/VDS/Components/Checkbox/Checkbox.swift +++ b/VDS/Components/Checkbox/Checkbox.swift @@ -13,7 +13,7 @@ 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, Clickable { +open class Checkbox: Control, Errorable { //-------------------------------------------------- // MARK: - Initializers @@ -63,8 +63,6 @@ open class Checkbox: Control, Errorable, Clickable { //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- - open var onClick: ((Checkbox) -> ())? { didSet { setupOnClick() }} - 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 d9b93b07..b71c0342 100644 --- a/VDS/Components/RadioBox/RadioBox.swift +++ b/VDS/Components/RadioBox/RadioBox.swift @@ -12,7 +12,7 @@ import VDSFormControlsTokens import Combine @objc(VDSRadioBox) -open class RadioBox: Control, Clickable { +open class RadioBox: Control { //-------------------------------------------------- // MARK: - Initializers @@ -56,8 +56,6 @@ open class RadioBox: Control, Clickable { //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- - open var onClick: ((RadioBox) -> ())? { didSet { setupOnClick() }} - 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 83606b05..76d29b46 100644 --- a/VDS/Components/RadioButton/RadioButton.swift +++ b/VDS/Components/RadioButton/RadioButton.swift @@ -11,7 +11,7 @@ import VDSColorTokens import VDSFormControlsTokens @objc(VDSRadioButton) -open class RadioButton: Control, Errorable, Clickable { +open class RadioButton: Control, Errorable { //-------------------------------------------------- // MARK: - Initializers @@ -61,8 +61,6 @@ open class RadioButton: Control, Errorable, Clickable { //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- - open var onClick: ((RadioButton) -> ())? { didSet { setupOnClick() } } - open var label = Label().with { $0.setContentCompressionResistancePriority(.required, for: .vertical) $0.textPosition = .left diff --git a/VDS/Components/RadioSwatch/RadioSwatch.swift b/VDS/Components/RadioSwatch/RadioSwatch.swift index 967a824c..211a3ff5 100644 --- a/VDS/Components/RadioSwatch/RadioSwatch.swift +++ b/VDS/Components/RadioSwatch/RadioSwatch.swift @@ -12,7 +12,7 @@ import VDSFormControlsTokens import Combine @objc(VDSRadioSwatch) -open class RadioSwatch: Control, Clickable { +open class RadioSwatch: Control { //-------------------------------------------------- // MARK: - Initializers @@ -31,9 +31,7 @@ open class RadioSwatch: Control, Clickable { //-------------------------------------------------- // MARK: - Public Properties - //-------------------------------------------------- - open var onClick: ((RadioSwatch) -> ())? { didSet { setupOnClick() } } - + //-------------------------------------------------- public var selectorView = UIView().with { $0.translatesAutoresizingMaskIntoConstraints = false } diff --git a/VDS/Components/TileContainer/TileContainer.swift b/VDS/Components/TileContainer/TileContainer.swift index 371e5bc3..bf0d24a9 100644 --- a/VDS/Components/TileContainer/TileContainer.swift +++ b/VDS/Components/TileContainer/TileContainer.swift @@ -11,12 +11,7 @@ import VDSFormControlsTokens import UIKit @objc(VDSTileContainer) -open class TileContainer: TileContainerBase, Clickable { - public var onClick: ((TileContainer) -> ())? { didSet { setupOnClick() } } -} - -@objc(VDSTileContainerBase) -open class TileContainerBase: Control { +open class TileContainer: Control { //-------------------------------------------------- // MARK: - Initializers @@ -315,13 +310,13 @@ open class TileContainerBase: Control { } } -extension TileContainerBase { +extension TileContainer { class BackgroundColorConfiguration: ObjectColorable { - typealias ObjectType = TileContainerBase + typealias ObjectType = TileContainer required init() { } - func getColor(_ object: TileContainerBase) -> UIColor { + func getColor(_ object: TileContainer) -> UIColor { switch object.color { case .white: diff --git a/VDS/Components/Tilelet/Tilelet.swift b/VDS/Components/Tilelet/Tilelet.swift index 09a59499..1a956680 100644 --- a/VDS/Components/Tilelet/Tilelet.swift +++ b/VDS/Components/Tilelet/Tilelet.swift @@ -12,7 +12,7 @@ import UIKit import Combine @objc(VDSTilelet) -open class Tilelet: TileContainerBase, Clickable { +open class Tilelet: TileContainer { //-------------------------------------------------- // MARK: - Enums @@ -62,9 +62,7 @@ open class Tilelet: TileContainerBase, Clickable { //-------------------------------------------------- // MARK: - Public Properties - //-------------------------------------------------- - open var onClick: ((Tilelet) -> ())? { didSet { setupOnClick() } } - + //-------------------------------------------------- internal override var onClickSubscriber: AnyCancellable? { didSet { isAccessibilityElement = onClickSubscriber != nil diff --git a/VDS/Components/Toggle/Toggle.swift b/VDS/Components/Toggle/Toggle.swift index b9e13b6b..05349779 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, Clickable { +open class Toggle: Control { //-------------------------------------------------- // MARK: - Enums //-------------------------------------------------- @@ -70,9 +70,7 @@ open class Toggle: Control, Clickable { //-------------------------------------------------- // MARK: - Configuration Properties - //-------------------------------------------------- - open var onClick: ((Toggle) -> ())? { didSet { setupOnClick() } } - + //-------------------------------------------------- // Sizes are from InVision design specs. public let toggleSize = CGSize(width: 52, height: 24) public let toggleContainerSize = CGSize(width: 52, height: 44) diff --git a/VDS/Protocols/Clickable.swift b/VDS/Protocols/Clickable.swift index 3ce3eed6..62c771ce 100644 --- a/VDS/Protocols/Clickable.swift +++ b/VDS/Protocols/Clickable.swift @@ -8,10 +8,7 @@ import Foundation import UIKit -public protocol Clickable where Self: UIControl { - var onClick: ((Self) -> ())? { get set } - func setupOnClick() -} +public protocol Clickable where Self: UIControl {} extension Clickable where Self: Handlerable { public func addEvent(event: UIControl.Event, block: @escaping (Self)->()) { @@ -22,28 +19,34 @@ extension Clickable where Self: Handlerable { } } -extension Clickable where Self: Control { - public func setupOnClick() { - if let onClick { - onClickSubscriber = publisher(for: .touchUpInside) - .sink(receiveValue: { c in - onClick(c) - }) - } else { - onClickSubscriber = nil +extension Clickable where Self: ButtonBase { + public var onClick: ((Self) -> ())? { + get { return nil } + set { + if let newValue { + onClickSubscriber = publisher(for: .touchUpInside) + .sink { c in + newValue(c) + } + } else { + onClickSubscriber = nil + } } } } -extension Clickable where Self: ButtonBase { - public func setupOnClick() { - if let onClick { - onClickSubscriber = publisher(for: .touchUpInside) - .sink(receiveValue: { c in - onClick(c) - }) - } else { - onClickSubscriber = nil +extension Clickable where Self: Control { + public var onClick: ((Self) -> ())? { + get { return nil } + set { + if let newValue { + onClickSubscriber = publisher(for: .touchUpInside) + .sink { c in + newValue(c) + } + } else { + onClickSubscriber = nil + } } } }