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 UIKit
|
||||||
import Combine
|
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.
|
/// 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
|
// 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? {
|
open var onChangeSubscriber: AnyCancellable? {
|
||||||
willSet {
|
willSet {
|
||||||
@ -81,7 +97,7 @@ open class SelectorGroupBase<SelectorItemType: Control>: Control, Changeable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Overrides
|
// MARK: - Overrides
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -112,3 +128,5 @@ open class SelectorGroupBase<SelectorItemType: Control>: Control, Changeable {
|
|||||||
items.forEach{ $0.reset() }
|
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``
|
/// asking a customer which attributes they would like to filter their search by. This uses ``CheckboxItem``
|
||||||
/// to allow user selection.
|
/// to allow user selection.
|
||||||
@objc(VDSCheckboxGroup)
|
@objc(VDSCheckboxGroup)
|
||||||
open class CheckboxGroup: SelectorGroupBase<CheckboxItem> {
|
open class CheckboxGroup: SelectorGroupBase<CheckboxItem>, SelectorGroupMultiSelect {
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Initializers
|
// MARK: - Initializers
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -64,17 +64,14 @@ open class CheckboxGroup: SelectorGroupBase<CheckboxItem> {
|
|||||||
get { _showError }
|
get { _showError }
|
||||||
set {
|
set {
|
||||||
var newShowError = newValue
|
var newShowError = newValue
|
||||||
let anySelected = items.filter { $0.isSelected == true }.count > 0
|
if hasSelectedItem && newShowError {
|
||||||
if anySelected && newShowError {
|
|
||||||
newShowError = false
|
newShowError = false
|
||||||
}
|
}
|
||||||
items.forEach { handler in
|
items.forEach { $0.showError = newShowError }
|
||||||
handler.showError = newShowError
|
|
||||||
}
|
|
||||||
_showError = newShowError
|
_showError = newShowError
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Overrides
|
// MARK: - Overrides
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import Foundation
|
|||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
@objc(VDSRadioBoxGroup)
|
@objc(VDSRadioBoxGroup)
|
||||||
open class RadioBoxGroup: SelectorGroupBase<RadioBoxItem> {
|
open class RadioBoxGroup: SelectorGroupBase<RadioBoxItem>, SelectorGroupSingleSelect {
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Initializers
|
// MARK: - Initializers
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import Foundation
|
|||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
@objc(VDSRadioButtonGroup)
|
@objc(VDSRadioButtonGroup)
|
||||||
open class RadioButtonGroup: SelectorGroupBase<RadioButtonItem> {
|
open class RadioButtonGroup: SelectorGroupBase<RadioButtonItem>, SelectorGroupSingleSelect {
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Initializers
|
// MARK: - Initializers
|
||||||
@ -54,18 +54,18 @@ open class RadioButtonGroup: SelectorGroupBase<RadioButtonItem> {
|
|||||||
setNeedsUpdate()
|
setNeedsUpdate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var _showError: Bool = false
|
private var _showError: Bool = false
|
||||||
|
|
||||||
|
/// Whether not to show the error.
|
||||||
open var showError: Bool {
|
open var showError: Bool {
|
||||||
get { _showError }
|
get { _showError }
|
||||||
set {
|
set {
|
||||||
var newShowError = newValue
|
var newShowError = newValue
|
||||||
if selectedItem != nil, newShowError {
|
if hasSelectedItem && newShowError {
|
||||||
newShowError = false
|
newShowError = false
|
||||||
}
|
}
|
||||||
items.forEach { handler in
|
items.forEach { $0.showError = newShowError }
|
||||||
handler.showError = newShowError
|
|
||||||
}
|
|
||||||
_showError = newShowError
|
_showError = newShowError
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user