From 421ea789913e88f905ca41c25aa2ba408df7fedb Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Mon, 12 Sep 2022 15:51:20 -0500 Subject: [PATCH] refactored to use modelHandler instead direct model change Signed-off-by: Matt Bruce --- VDS/Components/Checkbox/CheckboxGroup.swift | 12 +++------ VDS/Components/RadioBox/RadioBoxGroup.swift | 21 ++++----------- VDS/Components/RadioButton/RadioButton.swift | 11 +++++++- .../RadioButton/RadioButtonGroup.swift | 27 ++++++------------- 4 files changed, 26 insertions(+), 45 deletions(-) diff --git a/VDS/Components/Checkbox/CheckboxGroup.swift b/VDS/Components/Checkbox/CheckboxGroup.swift index 28e01fe7..1702419d 100644 --- a/VDS/Components/Checkbox/CheckboxGroup.swift +++ b/VDS/Components/Checkbox/CheckboxGroup.swift @@ -10,16 +10,10 @@ import UIKit public class CheckboxGroup: CheckboxGroupBase { public override func didSelect(_ selectedControl: Checkbox) { - for (index, control) in selectorViews.enumerated() { - //only change the old and new - if control == selectedControl { - let updated = model.selectors[index].copyWith { - $0.selected.toggle() - } - model.selectors[index] = updated - } + selectedControl.toggle() + DispatchQueue.main.asyncAfter(deadline: .now() + Constants.ModelStateDebounce) { [weak self] in + self?.sendActions(for: .valueChanged) } - sendActions(for: .valueChanged) } } diff --git a/VDS/Components/RadioBox/RadioBoxGroup.swift b/VDS/Components/RadioBox/RadioBoxGroup.swift index 04fdfa13..d6eaf576 100644 --- a/VDS/Components/RadioBox/RadioBoxGroup.swift +++ b/VDS/Components/RadioBox/RadioBoxGroup.swift @@ -11,23 +11,12 @@ import UIKit public class RadioBoxGroup: RadioBoxGroupBase { public override func didSelect(_ selectedControl: RadioBox) { - for (index, control) in selectorViews.enumerated() { - //only change the old and new - if control == selectedControl { - let updated = model.selectors[index].copyWith { - $0.selected = true - } - model.selectors[index] = updated - - } else if control.isSelected { - let updated = model.selectors[index].copyWith { - $0.selected = false - } - model.selectors[index] = updated - - } + let oldSelectedControl = selectorViews.filter { $0.isSelected == true }.first + oldSelectedControl?.toggle() + selectedControl.toggle() + DispatchQueue.main.asyncAfter(deadline: .now() + Constants.ModelStateDebounce) { [weak self] in + self?.sendActions(for: .valueChanged) } - sendActions(for: .valueChanged) } } diff --git a/VDS/Components/RadioButton/RadioButton.swift b/VDS/Components/RadioButton/RadioButton.swift index 8dc7bbc8..6e53d334 100644 --- a/VDS/Components/RadioButton/RadioButton.swift +++ b/VDS/Components/RadioButton/RadioButton.swift @@ -10,7 +10,16 @@ import UIKit import VDSColorTokens import VDSFormControlsTokens -public class RadioButton: RadioButtonBase{ } +public class RadioButton: RadioButtonBase{ + //for groups allows "toggle" + open override func toggle() { + //removed error + if hasError && isSelected == false { + hasError.toggle() + } + isSelected.toggle() + } +} public class SoloRadioButton: RadioButtonBase{ public override func initialSetup() { diff --git a/VDS/Components/RadioButton/RadioButtonGroup.swift b/VDS/Components/RadioButton/RadioButtonGroup.swift index c2d42266..360f0f7f 100644 --- a/VDS/Components/RadioButton/RadioButtonGroup.swift +++ b/VDS/Components/RadioButton/RadioButtonGroup.swift @@ -11,26 +11,15 @@ import UIKit public class RadioButtonGroup: RadioButtonGroupBase { public override func didSelect(_ selectedControl: RadioButton) { - for (index, control) in selectorViews.enumerated() { - //only change the old and new - if control == selectedControl { - let updated = model.selectors[index].copyWith { - $0.selected = true - } - model.selectors[index] = updated - if hasError { - hasError = false - } - - } else if control.isSelected { - let updated = model.selectors[index].copyWith { - $0.selected = false - } - model.selectors[index] = updated - - } + let oldSelectedControl = selectorViews.filter { $0.isSelected == true }.first + oldSelectedControl?.toggle() + selectedControl.toggle() + if hasError { + hasError = false + } + DispatchQueue.main.asyncAfter(deadline: .now() + Constants.ModelStateDebounce) { [weak self] in + self?.sendActions(for: .valueChanged) } - sendActions(for: .valueChanged) } }