refactored radio button group more

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-08-11 12:15:53 -05:00
parent 8e4d4d6ded
commit 8824ea95bc
4 changed files with 12 additions and 5 deletions

View File

@ -11,9 +11,9 @@ public class RadioButtonGroup: RadioButtonGroupBase<DefaultRadioButtonGroupModel
public class RadioButtonGroupBase<RadioButtonGroupModelType: SelectorGroupModel, RadioButtonType: RadioButtonBase<RadioButtonGroupModelType.SelectorType>>: SelectorGroupBase<RadioButtonGroupModelType.SelectorType, RadioButtonGroupModelType, RadioButtonType> {
public override func didSelect(selected: RadioButtonGroupModelType.SelectorType) {
public override func didSelect(selected: RadioButtonGroupModelType.SelectorType) {
for selectorModel in model.selectors {
print("Pre Cached Selector: \(selectorModel.inputId): \(selectorModel.selected)")
print("Pre Cached Selector: \(selectorModel.id): \(selectorModel.selected)")
}
if var oldSelectedModel = selectedModel {
@ -26,9 +26,9 @@ public class RadioButtonGroupBase<RadioButtonGroupModelType: SelectorGroupModel,
replace(viewModel: newSelectedModel)
selectedModel = newSelectedModel
for selectorModel in model.selectors {
print("Post Cached Selector: \(selectorModel.inputId): \(selectorModel.selected)")
print("Post Cached Selector: \(selectorModel.id): \(selectorModel.selected)")
}
print("\n")

View File

@ -14,6 +14,7 @@ public struct DefaultRadioButtonGroupModel: SelectorGroupModel{
public var value: AnyHashable?
public var surface: Surface = .light
public var disabled: Bool = false
public var selectedModel: DefaultRadioButtonModel?
public var selectors: [SelectorType]
public init() { selectors = [] }
public init(selectors: [SelectorType]){

View File

@ -12,7 +12,10 @@ import Combine
open class SelectorGroupBase<SelectorType, SelectorGroupType: SelectorGroupModel, SelectorHandlerType: SelectorBase<SelectorType>>: View<SelectorGroupType>, Changable where SelectorType == SelectorGroupType.SelectorType {
public var selectorViews: [SelectorHandlerType] = []
@Proxy(\.model.selectedModel)
public var selectedModel: SelectorType?
public var onChange: Blocks.ActionBlock?
//--------------------------------------------------
@ -88,11 +91,12 @@ open class SelectorGroupBase<SelectorType, SelectorGroupType: SelectorGroupModel
//add the selectedPublisher for the change
newSelectorView.selectedPublisher().sink { [weak self] model in
guard self?.model.selectors.count ?? 0 > 0 else { return }
self?.didSelect(selected: model)
}.store(in: &cancellables)
//add model update to the subscribers
newSelectorView.$model.sink { [weak self] model in
newSelectorView.$model.debounce(for: .seconds(Constants.ModelStateDebounce), scheduler: RunLoop.main).sink { [weak self] model in
if let cached = self?.getCachedSelector(viewModel: model), newSelectorView.shouldUpdateView(viewModel: cached) {
self?.replace(viewModel: model)
}

View File

@ -9,6 +9,7 @@ import Foundation
public protocol SelectorGroupModel<SelectorType>: Modelable, FormFieldable {
associatedtype SelectorType: SelectorModel
var selectedModel: SelectorType? { get set }
var selectors: [SelectorType] { get set }
}
@ -18,6 +19,7 @@ public struct DefaultSelectorGroupModel<SelectorType: SelectorModel>: SelectorGr
public var value: AnyHashable?
public var surface: Surface = .light
public var disabled: Bool = false
public var selectedModel: SelectorType?
public var selectors: [SelectorType]
public init() { selectors = [] }
public init(selectors: [SelectorType]){