From 1306c9e2a9a84cb2edcf2ec16626aec9bb4181bf Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Wed, 31 Aug 2022 13:12:40 -0500 Subject: [PATCH] updated for not caching model, just the inputId Signed-off-by: Matt Bruce --- VDS/Components/RadioBox/RadioBoxGroup.swift | 6 ++-- .../RadioBox/RadioBoxGroupModel.swift | 2 +- .../RadioButton/RadioButtonGroup.swift | 8 ++--- .../RadioButton/RadioButtonGroupModel.swift | 2 +- VDS/Components/RadioSwatch/RadioSwatch.swift | 2 +- .../RadioSwatch/RadioSwatchGroup.swift | 32 ++++++++++++++++--- .../RadioSwatch/RadioSwatchGroupModel.swift | 2 +- .../SelectorGroupModelHandlerable.swift | 26 +++++++++++---- VDS/Protocols/SelectorGroupModelable.swift | 16 +++++++++- 9 files changed, 73 insertions(+), 23 deletions(-) diff --git a/VDS/Components/RadioBox/RadioBoxGroup.swift b/VDS/Components/RadioBox/RadioBoxGroup.swift index 7295e220..e32c7ff0 100644 --- a/VDS/Components/RadioBox/RadioBoxGroup.swift +++ b/VDS/Components/RadioBox/RadioBoxGroup.swift @@ -16,10 +16,10 @@ public class RadioBoxGroup: Control, SelectorGroupSel //-------------------------------------------------- public var selectorViews: [ModelHandlerType] = [] - @Proxy(\.model.selectedModel) - public var selectedModel: ModelHandlerType.ModelType? { + @Proxy(\.model.selectedInputId) + public var selectedInputId: String? { didSet{ - if hasError, selectedModel != nil { + if hasError, selectedInputId != nil { hasError = false } sendActions(for: .valueChanged) diff --git a/VDS/Components/RadioBox/RadioBoxGroupModel.swift b/VDS/Components/RadioBox/RadioBoxGroupModel.swift index 8919944c..49d55b92 100644 --- a/VDS/Components/RadioBox/RadioBoxGroupModel.swift +++ b/VDS/Components/RadioBox/RadioBoxGroupModel.swift @@ -16,7 +16,7 @@ public struct DefaultRadioBoxGroupModel: RadioBoxGroupModel { public var surface: Surface = .light public var disabled: Bool = false public var selectors: [SelectorModelType] - public var selectedModel: DefaultRadioBoxModel? + public var selectedInputId: String? public var hasError: Bool = false public var errorText: String? public init() { selectors = [] } diff --git a/VDS/Components/RadioButton/RadioButtonGroup.swift b/VDS/Components/RadioButton/RadioButtonGroup.swift index e1349329..8299c88d 100644 --- a/VDS/Components/RadioButton/RadioButtonGroup.swift +++ b/VDS/Components/RadioButton/RadioButtonGroup.swift @@ -16,10 +16,10 @@ public class RadioButtonGroup: Control, SelectorGr //-------------------------------------------------- public var selectorViews: [ModelHandlerType] = [] - @Proxy(\.model.selectedModel) - public var selectedModel: ModelHandlerType.ModelType? { + @Proxy(\.model.selectedInputId) + public var selectedInputId: String? { didSet{ - if hasError, selectedModel != nil { + if hasError, selectedInputId != nil { hasError = false } sendActions(for: .valueChanged) @@ -30,7 +30,7 @@ public class RadioButtonGroup: Control, SelectorGr get { model.hasError } set { var newHasError = newValue - if selectedModel != nil, newHasError { + if selectedInputId != nil, newHasError { newHasError = false } let selectors = model.selectors.compactMap { existing in diff --git a/VDS/Components/RadioButton/RadioButtonGroupModel.swift b/VDS/Components/RadioButton/RadioButtonGroupModel.swift index 277b47c0..b449107f 100644 --- a/VDS/Components/RadioButton/RadioButtonGroupModel.swift +++ b/VDS/Components/RadioButton/RadioButtonGroupModel.swift @@ -14,12 +14,12 @@ extension RadioButtonGroupModel { } public struct DefaultRadioButtonGroupModel: RadioButtonGroupModel { + public var selectedInputId: String? public typealias SelectorModelType = DefaultRadioButtonModel public var inputId: String? public var value: AnyHashable? public var surface: Surface = .light public var disabled: Bool = false - public var selectedModel: DefaultRadioButtonModel? public var selectors: [SelectorModelType] public var hasError: Bool = false public var errorText: String? diff --git a/VDS/Components/RadioSwatch/RadioSwatch.swift b/VDS/Components/RadioSwatch/RadioSwatch.swift index 72bc6ace..c5091a92 100644 --- a/VDS/Components/RadioSwatch/RadioSwatch.swift +++ b/VDS/Components/RadioSwatch/RadioSwatch.swift @@ -207,8 +207,8 @@ open class RadioSwatchBase: Control, Cha let backgroundColor = radioSwatchBackgroundColorConfiguration.getColor(viewModel) let borderColor = viewModel.selected ? radioSwatchBorderColorConfiguration.getColor(viewModel) : .clear let fillBorderColor = radioSwatchFillBorderColorConfiguration.getColor(viewModel) - selectorView.backgroundColor = backgroundColor + print("input: \(viewModel.inputId) enabled: \(viewModel.disabled) surface: \(viewModel.surface) selected: \(viewModel.selected) backgroundColor: \(backgroundColor.hexString)") selectorView.layer.borderColor = borderColor.cgColor selectorView.layer.cornerRadius = selectorView.bounds.width * 0.5 selectorView.layer.borderWidth = viewModel.selected ? selectorBorderWidth : 0 diff --git a/VDS/Components/RadioSwatch/RadioSwatchGroup.swift b/VDS/Components/RadioSwatch/RadioSwatchGroup.swift index 85cc2035..c892dc8e 100644 --- a/VDS/Components/RadioSwatch/RadioSwatchGroup.swift +++ b/VDS/Components/RadioSwatch/RadioSwatchGroup.swift @@ -12,6 +12,7 @@ public class RadioSwatchGroup: RadioSwatchGroupBase: Control, Changable, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate where GroupModelType.SelectorModelType == ModelHandlerType.ModelType { +public class RadioSwatchGroupBase: Control, Changable, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate where GroupModelType.SelectorModelType == ModelHandlerType.ModelType { //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- - @Proxy(\.model.selectedModel) - public var selectedModel: ModelHandlerType.ModelType? { + @Proxy(\.model.selectedInputId) + public var selectedInputId: String? { didSet { sendActions(for: .valueChanged) } } + public var selectedModel: ModelHandlerType.ModelType? { + guard let selectedInputId else { return nil } + if let index = model.selectors.firstIndex(where: { element in + return element.inputId == selectedInputId + }) { + return model.selectors[index] + } else { + return nil + } + } + public var onChange: Blocks.ActionBlock? //-------------------------------------------------- @@ -128,7 +140,7 @@ public class RadioSwatchGroupBase ModelHandlerType.ModelType? { + if let index = model.selectors.firstIndex(where: { element in + return element.inputId == viewModel.inputId + }) { + return model.selectors[index] + } else { + return nil + } + } + //-------------------------------------------------- // MARK: - UICollectionViewDelegateFlowLayout //-------------------------------------------------- diff --git a/VDS/Components/RadioSwatch/RadioSwatchGroupModel.swift b/VDS/Components/RadioSwatch/RadioSwatchGroupModel.swift index 5f0fd4f7..c7aae431 100644 --- a/VDS/Components/RadioSwatch/RadioSwatchGroupModel.swift +++ b/VDS/Components/RadioSwatch/RadioSwatchGroupModel.swift @@ -28,7 +28,7 @@ public struct DefaultRadioSwatchGroupModel: RadioSwatchGroupModel { public var surface: Surface = .light public var disabled: Bool = false public var selectors: [SelectorModelType] - public var selectedModel: DefaultRadioSwatchModel? + public var selectedInputId: String? public init() { selectors = [] } public init(selectors: [SelectorModelType]){ self.selectors = selectors diff --git a/VDS/Protocols/SelectorGroupModelHandlerable.swift b/VDS/Protocols/SelectorGroupModelHandlerable.swift index 72307e3d..a11ae77c 100644 --- a/VDS/Protocols/SelectorGroupModelHandlerable.swift +++ b/VDS/Protocols/SelectorGroupModelHandlerable.swift @@ -70,12 +70,23 @@ extension SelectorGroupModelHandlerable { ///MARK: Groups that allow single selections public protocol SelectorGroupSelectedModelHandlerable: SelectorGroupModelHandlerable { - var selectedModel: ModelHandlerType.ModelType? { get set } + var selectedInputId: String? { get set } func didSelect(selector: ModelHandlerType.ModelType) } extension SelectorGroupSelectedModelHandlerable { + public var selectedModel: ModelHandlerType.ModelType? { + guard let selectedInputId else { return nil } + if let index = model.selectors.firstIndex(where: { element in + return element.inputId == selectedInputId + }) { + return model.selectors[index] + } else { + return nil + } + } + public func createModelHandler(selector: ModelHandlerType.ModelType) -> ModelHandlerType { //create view let newSelectorView = ModelHandlerType(with: selector) @@ -101,20 +112,23 @@ extension SelectorGroupSelectedModelHandlerable { } public func didSelect(selector: ModelHandlerType.ModelType) { - if var oldSelectedModel = selectedModel { - oldSelectedModel.selected = false + + if let selectedModel, let cached = getCachedSelector(viewModel: selectedModel) { + let oldSelectedModel = cached.copyWith { + $0.selected = false + } replace(viewModel: oldSelectedModel) } - + //only select is selected if selector.selected { var newSelectedModel = selector newSelectedModel.selected = true replace(viewModel: newSelectedModel) - selectedModel = newSelectedModel + selectedInputId = selector.inputId } else { //ensure current value is removed - selectedModel = nil + selectedInputId = nil } } } diff --git a/VDS/Protocols/SelectorGroupModelable.swift b/VDS/Protocols/SelectorGroupModelable.swift index 3ccca82d..48ff392d 100644 --- a/VDS/Protocols/SelectorGroupModelable.swift +++ b/VDS/Protocols/SelectorGroupModelable.swift @@ -15,5 +15,19 @@ public protocol SelectorGroupModelable: Modelable, FormFieldable { ///MARK: Groups that allow single selections public protocol SelectorGroupSelectedModelable: SelectorGroupModelable { - var selectedModel: SelectorModelType? { get set } + var selectedInputId: String? { get set } + var selectedModel: SelectorModelType? { get } +} + +extension SelectorGroupSelectedModelable { + public var selectedModel: SelectorModelType? { + guard let selectedInputId else { return nil } + if let index = selectors.firstIndex(where: { element in + return element.inputId == selectedInputId + }) { + return selectors[index] + } else { + return nil + } + } }