diff --git a/VDS/BaseClasses/Selector/SelectorGroupHandlerBase.swift b/VDS/BaseClasses/Selector/SelectorGroupHandlerBase.swift index e1a1ad38..048a8737 100644 --- a/VDS/BaseClasses/Selector/SelectorGroupHandlerBase.swift +++ b/VDS/BaseClasses/Selector/SelectorGroupHandlerBase.swift @@ -11,13 +11,53 @@ import Combine /// Base Class used for any Grouped Form Control of a Selector Type. open class SelectorGroupHandlerBase: Control, Changeable { + + //-------------------------------------------------- + // MARK: - Private Properties + //-------------------------------------------------- + open var mainStackView: UIStackView = { + return UIStackView().with { + $0.translatesAutoresizingMaskIntoConstraints = false + $0.alignment = .fill + $0.distribution = .fill + $0.axis = .vertical + $0.spacing = VDSLayout.Spacing.space3X.value + } + }() //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- /// Array of the HandlerType registered. - open var selectorViews: [HandlerType] = [] - + /// Array of HandlerType that the user will have the ability to select from. + open var selectorViews: [HandlerType] = [] { + willSet { + mainStackView.arrangedSubviews.forEach { $0.removeFromSuperview() } + } + + didSet { + for selector in selectorViews { + selector.onClick = { [weak self] handler in + self?.didSelect(handler) + self?.setNeedsUpdate() + } + mainStackView.addArrangedSubview(selector) + } + } + } + + /// Current Selected Control for this group. + open var selectedHandlers: [HandlerType]? { + let selected = selectorViews.filter{ $0.isSelected == true } + guard selected.count > 0 else { return nil } + return selected + } + + /// Current Selected Control for this group. + open var selectedHandler: HandlerType? { + return selectorViews.filter { $0.isSelected == true }.first + } + open var onChangeSubscriber: AnyCancellable? { willSet { if let onChangeSubscriber { @@ -45,6 +85,14 @@ open class SelectorGroupHandlerBase: Control, Changeable { //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- + /// Called once when a view is initialized and is used to Setup additional UI or other constants and configurations. + open override func setup() { + super.setup() + + addSubview(mainStackView) + mainStackView.pinToSuperView() + } + /// Handler for the Group to override on a select event. /// - Parameter selectedControl: Selected Control the user interacted. open func didSelect(_ selectedControl: HandlerType) { @@ -63,27 +111,4 @@ open class SelectorGroupHandlerBase: Control, Changeable { super.reset() selectorViews.forEach{ $0.reset() } } - - /// Used to update any Accessibility properties. - open override func updateAccessibility() { - super.updateAccessibility() - setAccessibilityLabel(for: selectorViews) - } -} - -open class SelectorGroupSelectedHandlerBase: SelectorGroupHandlerBase{ - - /// Current Selected Control for this group. - public var selectedHandler: HandlerType? { - return selectorViews.filter { $0.isSelected == true }.first - } - - open override func updateAccessibility() { - super.updateAccessibility() - if let selectedHandler, let value = selectedHandler.accessibilityValue, let label = selectedHandler.accessibilityLabel { - accessibilityValue = "\(label) \(value)" - } else { - accessibilityValue = nil - } - } } diff --git a/VDS/Components/Checkbox/CheckboxGroup.swift b/VDS/Components/Checkbox/CheckboxGroup.swift index 2a4bd73e..3895e523 100644 --- a/VDS/Components/Checkbox/CheckboxGroup.swift +++ b/VDS/Components/Checkbox/CheckboxGroup.swift @@ -31,29 +31,6 @@ open class CheckboxGroup: SelectorGroupHandlerBase { //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- - /// Arrary of ``CheckboxItem`` within this group that are selected. - open var selectedHandlers: [CheckboxItem]? { - let selected = selectorViews.filter{ $0.isSelected == true } - guard selected.count > 0 else { return nil } - return selected - } - - /// Array of ``CheckboxItem`` that the user will have the ability to select from. - open override var selectorViews: [CheckboxItem] { - willSet { - mainStackView.arrangedSubviews.forEach { $0.removeFromSuperview() } - } - - didSet { - for selector in selectorViews { - selector.onClick = { [weak self] handler in - self?.didSelect(handler) - } - mainStackView.addArrangedSubview(selector) - } - } - } - /// Array of ``CheckboxItemModel`` that will be used to build the selectorViews of type ``CheckboxItem``. open var selectorModels: [CheckboxItemModel]? { didSet { @@ -97,30 +74,14 @@ open class CheckboxGroup: SelectorGroupHandlerBase { _showError = newShowError } } - - //-------------------------------------------------- - // MARK: - Private Properties - //-------------------------------------------------- - private var mainStackView: UIStackView = { - return UIStackView().with { - $0.translatesAutoresizingMaskIntoConstraints = false - $0.alignment = .fill - $0.distribution = .fill - $0.axis = .vertical - $0.spacing = VDSLayout.Spacing.space6X.value - } - }() - + //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- /// Called once when a view is initialized and is used to Setup additional UI or other constants and configurations. open override func setup() { super.setup() - - addSubview(mainStackView) - - mainStackView.pinToSuperView() + mainStackView.spacing = VDSLayout.Spacing.space6X.value } public override func didSelect(_ selectedControl: CheckboxItem) { diff --git a/VDS/Components/RadioBox/RadioBoxGroup.swift b/VDS/Components/RadioBox/RadioBoxGroup.swift index 51fbb992..21d1cf68 100644 --- a/VDS/Components/RadioBox/RadioBoxGroup.swift +++ b/VDS/Components/RadioBox/RadioBoxGroup.swift @@ -9,7 +9,7 @@ import Foundation import UIKit @objc(VDSRadioBoxGroup) -open class RadioBoxGroup: SelectorGroupSelectedHandlerBase { +open class RadioBoxGroup: SelectorGroupHandlerBase { //-------------------------------------------------- // MARK: - Initializers @@ -25,37 +25,10 @@ open class RadioBoxGroup: SelectorGroupSelectedHandlerBase { public required init?(coder: NSCoder) { super.init(coder: coder) } - - //-------------------------------------------------- - // MARK: - Private Properties - //-------------------------------------------------- - private var mainStackView: UIStackView = { - return UIStackView().with { - $0.translatesAutoresizingMaskIntoConstraints = false - $0.spacing = VDSLayout.Spacing.space3X.value - } - }() - + //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- - /// Array of ``RadioBoxItem`` that the user will have the ability to select from. - public override var selectorViews: [RadioBoxItem] { - willSet { - mainStackView.arrangedSubviews.forEach { $0.removeFromSuperview() } - } - - didSet { - for selector in selectorViews { - selector.onClick = { [weak self] handler in - self?.didSelect(handler) - self?.setNeedsUpdate() - } - mainStackView.addArrangedSubview(selector) - } - } - } - /// Array of ``RadioBoxItemModel`` that will be used to build the selectorViews of type ``RadioBoxItem``. open var selectorModels: [RadioBoxItemModel]? { didSet { @@ -102,10 +75,7 @@ open class RadioBoxGroup: SelectorGroupSelectedHandlerBase { /// Called once when a view is initialized and is used to Setup additional UI or other constants and configurations. open override func setup() { super.setup() - addSubview(mainStackView) ensureDevice() - mainStackView.pinToSuperView() - NotificationCenter.default .publisher(for: UIDevice.orientationDidChangeNotification) .sink() { [weak self] _ in diff --git a/VDS/Components/RadioButton/RadioButtonGroup.swift b/VDS/Components/RadioButton/RadioButtonGroup.swift index 82c8ef66..c088c4a9 100644 --- a/VDS/Components/RadioButton/RadioButtonGroup.swift +++ b/VDS/Components/RadioButton/RadioButtonGroup.swift @@ -9,7 +9,7 @@ import Foundation import UIKit @objc(VDSRadioButtonGroup) -open class RadioButtonGroup: SelectorGroupSelectedHandlerBase { +open class RadioButtonGroup: SelectorGroupHandlerBase { //-------------------------------------------------- // MARK: - Initializers @@ -26,39 +26,9 @@ open class RadioButtonGroup: SelectorGroupSelectedHandlerBase { super.init(coder: coder) } - //-------------------------------------------------- - // MARK: - Private Properties - //-------------------------------------------------- - private var mainStackView: UIStackView = { - return UIStackView().with { - $0.translatesAutoresizingMaskIntoConstraints = false - $0.alignment = .fill - $0.distribution = .fill - $0.axis = .vertical - $0.spacing = VDSLayout.Spacing.space6X.value - } - }() - //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- - /// Array of ``RadioButtonItem`` that the user will have the ability to select from. - public override var selectorViews: [RadioButtonItem] { - willSet { - mainStackView.arrangedSubviews.forEach { $0.removeFromSuperview() } - } - - didSet { - for selector in selectorViews { - selector.onClick = { [weak self] handler in - self?.didSelect(handler) - self?.setNeedsUpdate() - } - mainStackView.addArrangedSubview(selector) - } - } - } - /// Array of ``RadioButtonItemModel`` that will be used to build the selectorViews of type ``RadioButtonItem``. open var selectorModels: [RadioButtonItemModel]? { didSet {