refactored to one selectorgrouphandlerbase

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-08-29 09:16:50 -05:00
parent bb158f506f
commit caf5415853
4 changed files with 55 additions and 129 deletions

View File

@ -12,11 +12,51 @@ import Combine
/// Base Class used for any Grouped Form Control of a Selector Type. /// Base Class used for any Grouped Form Control of a Selector Type.
open class SelectorGroupHandlerBase<HandlerType: Control>: Control, Changeable { open class SelectorGroupHandlerBase<HandlerType: Control>: 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 // MARK: - Public Properties
//-------------------------------------------------- //--------------------------------------------------
/// Array of the HandlerType registered. /// 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? { open var onChangeSubscriber: AnyCancellable? {
willSet { willSet {
@ -45,6 +85,14 @@ open class SelectorGroupHandlerBase<HandlerType: Control>: Control, Changeable {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Overrides // 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. /// Handler for the Group to override on a select event.
/// - Parameter selectedControl: Selected Control the user interacted. /// - Parameter selectedControl: Selected Control the user interacted.
open func didSelect(_ selectedControl: HandlerType) { open func didSelect(_ selectedControl: HandlerType) {
@ -63,27 +111,4 @@ open class SelectorGroupHandlerBase<HandlerType: Control>: Control, Changeable {
super.reset() super.reset()
selectorViews.forEach{ $0.reset() } selectorViews.forEach{ $0.reset() }
} }
/// Used to update any Accessibility properties.
open override func updateAccessibility() {
super.updateAccessibility()
setAccessibilityLabel(for: selectorViews)
}
}
open class SelectorGroupSelectedHandlerBase<HandlerType: Control>: SelectorGroupHandlerBase<HandlerType>{
/// 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
}
}
} }

View File

@ -31,29 +31,6 @@ open class CheckboxGroup: SelectorGroupHandlerBase<CheckboxItem> {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Public Properties // 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``. /// Array of ``CheckboxItemModel`` that will be used to build the selectorViews of type ``CheckboxItem``.
open var selectorModels: [CheckboxItemModel]? { open var selectorModels: [CheckboxItemModel]? {
didSet { didSet {
@ -98,29 +75,13 @@ open class CheckboxGroup: SelectorGroupHandlerBase<CheckboxItem> {
} }
} }
//--------------------------------------------------
// 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 // MARK: - Overrides
//-------------------------------------------------- //--------------------------------------------------
/// Called once when a view is initialized and is used to Setup additional UI or other constants and configurations. /// Called once when a view is initialized and is used to Setup additional UI or other constants and configurations.
open override func setup() { open override func setup() {
super.setup() super.setup()
mainStackView.spacing = VDSLayout.Spacing.space6X.value
addSubview(mainStackView)
mainStackView.pinToSuperView()
} }
public override func didSelect(_ selectedControl: CheckboxItem) { public override func didSelect(_ selectedControl: CheckboxItem) {

View File

@ -9,7 +9,7 @@ import Foundation
import UIKit import UIKit
@objc(VDSRadioBoxGroup) @objc(VDSRadioBoxGroup)
open class RadioBoxGroup: SelectorGroupSelectedHandlerBase<RadioBoxItem> { open class RadioBoxGroup: SelectorGroupHandlerBase<RadioBoxItem> {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Initializers // MARK: - Initializers
@ -26,36 +26,9 @@ open class RadioBoxGroup: SelectorGroupSelectedHandlerBase<RadioBoxItem> {
super.init(coder: coder) 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 // 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``. /// Array of ``RadioBoxItemModel`` that will be used to build the selectorViews of type ``RadioBoxItem``.
open var selectorModels: [RadioBoxItemModel]? { open var selectorModels: [RadioBoxItemModel]? {
didSet { didSet {
@ -102,10 +75,7 @@ open class RadioBoxGroup: SelectorGroupSelectedHandlerBase<RadioBoxItem> {
/// Called once when a view is initialized and is used to Setup additional UI or other constants and configurations. /// Called once when a view is initialized and is used to Setup additional UI or other constants and configurations.
open override func setup() { open override func setup() {
super.setup() super.setup()
addSubview(mainStackView)
ensureDevice() ensureDevice()
mainStackView.pinToSuperView()
NotificationCenter.default NotificationCenter.default
.publisher(for: UIDevice.orientationDidChangeNotification) .publisher(for: UIDevice.orientationDidChangeNotification)
.sink() { [weak self] _ in .sink() { [weak self] _ in

View File

@ -9,7 +9,7 @@ import Foundation
import UIKit import UIKit
@objc(VDSRadioButtonGroup) @objc(VDSRadioButtonGroup)
open class RadioButtonGroup: SelectorGroupSelectedHandlerBase<RadioButtonItem> { open class RadioButtonGroup: SelectorGroupHandlerBase<RadioButtonItem> {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Initializers // MARK: - Initializers
@ -26,39 +26,9 @@ open class RadioButtonGroup: SelectorGroupSelectedHandlerBase<RadioButtonItem> {
super.init(coder: coder) 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 // 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``. /// Array of ``RadioButtonItemModel`` that will be used to build the selectorViews of type ``RadioButtonItem``.
open var selectorModels: [RadioButtonItemModel]? { open var selectorModels: [RadioButtonItemModel]? {
didSet { didSet {