refactored code
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
0657445fd9
commit
3219788569
@ -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<SelectorItemType: Control>: Control, Changeable {
|
||||
open class SelectorGroupBase<SelectorItemType: Control>: Control, SelectorGroup, Changeable {
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Private Properties
|
||||
@ -45,18 +73,6 @@ open class SelectorGroupBase<SelectorItemType: Control>: 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<SelectorItemType: Control>: Control, Changeable {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Overrides
|
||||
//--------------------------------------------------
|
||||
@ -112,3 +128,5 @@ open class SelectorGroupBase<SelectorItemType: Control>: Control, Changeable {
|
||||
items.forEach{ $0.reset() }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -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<CheckboxItem> {
|
||||
open class CheckboxGroup: SelectorGroupBase<CheckboxItem>, SelectorGroupMultiSelect {
|
||||
//--------------------------------------------------
|
||||
// MARK: - Initializers
|
||||
//--------------------------------------------------
|
||||
@ -64,17 +64,14 @@ open class CheckboxGroup: SelectorGroupBase<CheckboxItem> {
|
||||
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
|
||||
//--------------------------------------------------
|
||||
|
||||
@ -9,7 +9,7 @@ import Foundation
|
||||
import UIKit
|
||||
|
||||
@objc(VDSRadioBoxGroup)
|
||||
open class RadioBoxGroup: SelectorGroupBase<RadioBoxItem> {
|
||||
open class RadioBoxGroup: SelectorGroupBase<RadioBoxItem>, SelectorGroupSingleSelect {
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Initializers
|
||||
|
||||
@ -9,7 +9,7 @@ import Foundation
|
||||
import UIKit
|
||||
|
||||
@objc(VDSRadioButtonGroup)
|
||||
open class RadioButtonGroup: SelectorGroupBase<RadioButtonItem> {
|
||||
open class RadioButtonGroup: SelectorGroupBase<RadioButtonItem>, SelectorGroupSingleSelect {
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Initializers
|
||||
@ -54,18 +54,18 @@ open class RadioButtonGroup: SelectorGroupBase<RadioButtonItem> {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user