From 32197885691db515fbf38d5e878e048700e32a54 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Tue, 29 Aug 2023 11:04:38 -0500 Subject: [PATCH] refactored code Signed-off-by: Matt Bruce --- .../Selector/SelectorGroupBase.swift | 46 +++++++++++++------ VDS/Components/Checkbox/CheckboxGroup.swift | 11 ++--- VDS/Components/RadioBox/RadioBoxGroup.swift | 2 +- .../RadioButton/RadioButtonGroup.swift | 12 ++--- 4 files changed, 43 insertions(+), 28 deletions(-) diff --git a/VDS/BaseClasses/Selector/SelectorGroupBase.swift b/VDS/BaseClasses/Selector/SelectorGroupBase.swift index 36dd78a5..5c6cacef 100644 --- a/VDS/BaseClasses/Selector/SelectorGroupBase.swift +++ b/VDS/BaseClasses/Selector/SelectorGroupBase.swift @@ -9,8 +9,36 @@ import Foundation import UIKit import Combine +public protocol SelectorGroup { + associatedtype SelectorItemType: Control + var items: [SelectorItemType] { get set } + var hasSelectedItem: Bool { get } +} + +extension SelectorGroup { + public var hasSelectedItem: Bool { items.filter { $0.isSelected == true }.count > 0 } +} + +public protocol SelectorGroupMultiSelect: SelectorGroup {} +extension SelectorGroupMultiSelect { + /// Current Selected Control for this group. + public var selectedItems: [SelectorItemType]? { + let selected = items.filter{ $0.isSelected == true } + guard selected.count > 0 else { return nil } + return selected + } +} + +public protocol SelectorGroupSingleSelect: SelectorGroup {} +extension SelectorGroupSingleSelect { + /// Current Selected Control for this group. + public var selectedItem: SelectorItemType? { + return items.filter { $0.isSelected == true }.first + } +} + /// Base Class used for any Grouped Form Control of a Selector Type. -open class SelectorGroupBase: Control, Changeable { +open class SelectorGroupBase: Control, SelectorGroup, Changeable { //-------------------------------------------------- // MARK: - Private Properties @@ -45,18 +73,6 @@ open class SelectorGroupBase: Control, Changeable { } } } - - /// Current Selected Control for this group. - open var selectedItems: [SelectorItemType]? { - let selected = items.filter{ $0.isSelected == true } - guard selected.count > 0 else { return nil } - return selected - } - - /// Current Selected Control for this group. - open var selectedItem: SelectorItemType? { - return items.filter { $0.isSelected == true }.first - } open var onChangeSubscriber: AnyCancellable? { willSet { @@ -81,7 +97,7 @@ open class SelectorGroupBase: Control, Changeable { } } } - + //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- @@ -112,3 +128,5 @@ open class SelectorGroupBase: Control, Changeable { items.forEach{ $0.reset() } } } + + diff --git a/VDS/Components/Checkbox/CheckboxGroup.swift b/VDS/Components/Checkbox/CheckboxGroup.swift index e117a574..fd1808be 100644 --- a/VDS/Components/Checkbox/CheckboxGroup.swift +++ b/VDS/Components/Checkbox/CheckboxGroup.swift @@ -12,7 +12,7 @@ import UIKit /// asking a customer which attributes they would like to filter their search by. This uses ``CheckboxItem`` /// to allow user selection. @objc(VDSCheckboxGroup) -open class CheckboxGroup: SelectorGroupBase { +open class CheckboxGroup: SelectorGroupBase, SelectorGroupMultiSelect { //-------------------------------------------------- // MARK: - Initializers //-------------------------------------------------- @@ -64,17 +64,14 @@ open class CheckboxGroup: SelectorGroupBase { get { _showError } set { var newShowError = newValue - let anySelected = items.filter { $0.isSelected == true }.count > 0 - if anySelected && newShowError { + if hasSelectedItem && newShowError { newShowError = false } - items.forEach { handler in - handler.showError = newShowError - } + items.forEach { $0.showError = newShowError } _showError = newShowError } } - + //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- diff --git a/VDS/Components/RadioBox/RadioBoxGroup.swift b/VDS/Components/RadioBox/RadioBoxGroup.swift index f6fa3fa4..4a7c4196 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: SelectorGroupBase { +open class RadioBoxGroup: SelectorGroupBase, SelectorGroupSingleSelect { //-------------------------------------------------- // MARK: - Initializers diff --git a/VDS/Components/RadioButton/RadioButtonGroup.swift b/VDS/Components/RadioButton/RadioButtonGroup.swift index 7a2ee9af..7e013d19 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: SelectorGroupBase { +open class RadioButtonGroup: SelectorGroupBase, SelectorGroupSingleSelect { //-------------------------------------------------- // MARK: - Initializers @@ -54,18 +54,18 @@ open class RadioButtonGroup: SelectorGroupBase { setNeedsUpdate() } } - + private var _showError: Bool = false + + /// Whether not to show the error. open var showError: Bool { get { _showError } set { var newShowError = newValue - if selectedItem != nil, newShowError { + if hasSelectedItem && newShowError { newShowError = false } - items.forEach { handler in - handler.showError = newShowError - } + items.forEach { $0.showError = newShowError } _showError = newShowError } }