updated selector groups

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-08-10 10:53:23 -05:00
parent a55bea38de
commit 103cec3db2
2 changed files with 36 additions and 18 deletions

View File

@ -21,8 +21,32 @@ public class RadioButton: RadioButtonBase<DefaultRadioButtonModel>{
}
}
public class RadioButtonGroupModel: SelectorGroupModelBase<DefaultRadioButtonModel> {}
public class RadioButtonGroup: SelectorGroupBase<DefaultRadioButtonModel, RadioButton> {}
public struct RadioButtonGroupModel: SelectorGroupModel{
public typealias SelectorType = DefaultRadioButtonModel
public var inputId: String?
public var value: AnyHashable?
public var surface: Surface = .light
public var disabled: Bool = false
public var selectors: [SelectorType]
public init() { selectors = [] }
public init(selectors: [SelectorType]){
self.selectors = selectors
}
}
public class RadioButtonGroup: SelectorGroup<DefaultRadioButtonModel, RadioButtonGroupModel, RadioButton> {
public override func didSelect(selector: RadioButton) {
//deselect current selector
self.selectedView?.isSelected = false
// print("Selected Selector: \(inputId): \(selector.model.selected)")
selector.isSelected = true
self.selectedView = selector
// print("Selected Selector: \(inputId): \(selector.model.selected)")
// for selectorModel in viewModel.selectors {
// print("Cached Selector: \(selectorModel.inputId): \(selectorModel.selected)")
// }
}
}
open class RadioButtonBase<ModelType: RadioButtonModel>: SelectorBase<ModelType> {

View File

@ -9,24 +9,25 @@ import Foundation
import UIKit
public protocol SelectorGroupModel: Modelable, FormFieldable {
public protocol SelectorGroupModel<SelectorType>: Modelable, FormFieldable {
associatedtype SelectorType: SelectorModel
var selectors: [SelectorType] { get set }
}
open class SelectorGroupModelBase<SelectorType: SelectorModel>: SelectorGroupModel{
public struct DefaultSelectorGroupModel<SelectorType: SelectorModel>: SelectorGroupModel{
public var inputId: String?
public var value: AnyHashable?
public var surface: Surface = .light
public var disabled: Bool = false
public var selectors: [SelectorType]
required public init() { selectors = [] }
public init() { selectors = [] }
public init(selectors: [SelectorType]){
self.selectors = selectors
}
}
open class SelectorGroupBase<SelectorType: SelectorModel, SelectorHandlerType: SelectorBase<SelectorType>>: View<SelectorGroupModelBase<SelectorType>>, Changable {
open class SelectorGroup<SelectorType, SelectorGroupType: SelectorGroupModel<SelectorType>, SelectorHandlerType: SelectorBase<SelectorType>>: View<SelectorGroupType>, Changable {
public var selectorViews: [SelectorHandlerType] = []
public var selectedView: SelectorHandlerType?
public var onChange: Blocks.ActionBlock?
@ -86,6 +87,7 @@ open class SelectorGroupBase<SelectorType: SelectorModel, SelectorHandlerType: S
}
open override func updateView(viewModel: ModelType) {
//print("Selector Group update:")
func findSelectorView(inputId: String) -> SelectorHandlerType? {
return selectorViews.first(where: { existingSelectorView in
return existingSelectorView.model.inputId == inputId
@ -101,17 +103,18 @@ open class SelectorGroupBase<SelectorType: SelectorModel, SelectorHandlerType: S
let newSelectorView = SelectorHandlerType(with: selectorModel)
newSelectorView.didSelect = {[weak self] (inputId) in
guard let self else { return }
self.selectedView?.isSelected = false
if let inputId = selectorModel.inputId, let selector = findSelectorView(inputId: inputId) {
selector.isSelected = true
self.selectedView = selector
self.didSelect(selector: selector)
}
}
self.selectorViews.append(newSelectorView)
mainStackView.addArrangedSubview(newSelectorView)
}
}
}
open func didSelect(selector: SelectorHandlerType) { }
//--------------------------------------------------
// MARK: - Initializers
@ -127,13 +130,4 @@ open class SelectorGroupBase<SelectorType: SelectorModel, SelectorHandlerType: S
required public init?(coder: NSCoder) {
super.init(with: ModelType())
}
//--------------------------------------------------
// MARK: - Delegate
//--------------------------------------------------
public func didSelect(selector: SelectorHandlerType) {
selectedView?.isSelected = false
selector.isSelected = true
selectedView = selector
}
}