From 61c5d5adf80bd4c6cf8970b840dd78135ee693cf Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Wed, 31 Aug 2022 16:12:08 -0500 Subject: [PATCH] convert to kyle code for default handlers Signed-off-by: Matt Bruce --- VDS/Classes/Control.swift | 14 +---- VDS/Components/Checkbox/Checkbox.swift | 12 +++- VDS/Components/RadioBox/RadioBox.swift | 12 +++- VDS/Components/RadioButton/RadioButton.swift | 14 ++++- VDS/Components/RadioSwatch/RadioSwatch.swift | 13 ++++- VDS/Components/Toggle/Toggle.swift | 12 +++- .../SelectorGroupModelHandlerable.swift | 58 ++++++++----------- 7 files changed, 77 insertions(+), 58 deletions(-) diff --git a/VDS/Classes/Control.swift b/VDS/Classes/Control.swift index 8ae59708..457ceaa7 100644 --- a/VDS/Classes/Control.swift +++ b/VDS/Classes/Control.swift @@ -92,19 +92,7 @@ open class Control: UIControl, ModelHandlerable, ViewProto sendActions(for: .touchUpInside) return true } - - //-------------------------------------------------- - // MARK: - Actions - //-------------------------------------------------- - open override func sendActions(for controlEvents: UIControl.Event) { - super.sendActions(for: controlEvents) - if controlEvents.contains(.touchUpInside) && executeDefaultAction{ - defaultAction() - } - } - - open func defaultAction() { } - + //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- diff --git a/VDS/Components/Checkbox/Checkbox.swift b/VDS/Components/Checkbox/Checkbox.swift index 5179c98c..e92f6894 100644 --- a/VDS/Components/Checkbox/Checkbox.swift +++ b/VDS/Components/Checkbox/Checkbox.swift @@ -11,7 +11,15 @@ import VDSColorTokens import VDSFormControlsTokens import Combine -public class Checkbox: CheckboxBase{} +public class Checkbox: CheckboxBase{ + public override func initialSetup() { + super.initialSetup() + publisher(for: .touchUpInside) + .sink { control in + control.toggle() + }.store(in: &subscribers) + } +} open class CheckboxBase: Control, Changable { @@ -210,7 +218,7 @@ open class CheckboxBase: Control, Changable } /// This will checkbox the state of the Selector and execute the actionBlock if provided. - open override func defaultAction() { + open func toggle() { //removed error if hasError && isSelected == false { hasError.toggle() diff --git a/VDS/Components/RadioBox/RadioBox.swift b/VDS/Components/RadioBox/RadioBox.swift index 7354f295..74beb155 100644 --- a/VDS/Components/RadioBox/RadioBox.swift +++ b/VDS/Components/RadioBox/RadioBox.swift @@ -11,7 +11,15 @@ import VDSColorTokens import VDSFormControlsTokens import Combine -public class RadioBox: RadioBoxBase{} +public class RadioBox: RadioBoxBase{ + public override func initialSetup() { + super.initialSetup() + publisher(for: .touchUpInside) + .sink { control in + control.toggle() + }.store(in: &subscribers) + } +} open class RadioBoxBase: Control, Changable { @@ -209,7 +217,7 @@ open class RadioBoxBase: Control, Changable } /// This will radioBox the state of the Selector and execute the actionBlock if provided. - open override func defaultAction() { + open func toggle() { //removed error if hasError && isSelected == false { hasError.toggle() diff --git a/VDS/Components/RadioButton/RadioButton.swift b/VDS/Components/RadioButton/RadioButton.swift index 0b9de50b..8dc7bbc8 100644 --- a/VDS/Components/RadioButton/RadioButton.swift +++ b/VDS/Components/RadioButton/RadioButton.swift @@ -10,7 +10,17 @@ import UIKit import VDSColorTokens import VDSFormControlsTokens -public class RadioButton: RadioButtonBase{} +public class RadioButton: RadioButtonBase{ } + +public class SoloRadioButton: RadioButtonBase{ + public override func initialSetup() { + super.initialSetup() + publisher(for: .touchUpInside) + .sink { control in + control.toggle() + }.store(in: &subscribers) + } +} open class RadioButtonBase: Control, Changable { @@ -209,7 +219,7 @@ open class RadioButtonBase: Control, Cha } /// This will checkbox the state of the Selector and execute the actionBlock if provided. - open override func defaultAction() { + open func toggle() { guard !isSelected else { return } //removed error diff --git a/VDS/Components/RadioSwatch/RadioSwatch.swift b/VDS/Components/RadioSwatch/RadioSwatch.swift index 34f65eee..33f385db 100644 --- a/VDS/Components/RadioSwatch/RadioSwatch.swift +++ b/VDS/Components/RadioSwatch/RadioSwatch.swift @@ -11,7 +11,15 @@ import VDSColorTokens import VDSFormControlsTokens import Combine -public class RadioSwatch: RadioSwatchBase{} +public class RadioSwatch: RadioSwatchBase{ + public override func initialSetup() { + super.initialSetup() + publisher(for: .touchUpInside) + .sink { control in + control.toggle() + }.store(in: &subscribers) + } +} open class RadioSwatchBase: Control, Changable { @@ -126,7 +134,7 @@ open class RadioSwatchBase: Control, Cha onChange = nil } - open override func defaultAction() { + open func toggle() { isSelected.toggle() sendActions(for: .valueChanged) onChange?() @@ -213,7 +221,6 @@ open class RadioSwatchBase: Control, Cha 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/Toggle/Toggle.swift b/VDS/Components/Toggle/Toggle.swift index 62e01de6..e28bd505 100644 --- a/VDS/Components/Toggle/Toggle.swift +++ b/VDS/Components/Toggle/Toggle.swift @@ -18,7 +18,15 @@ import Combine Knob: The circular indicator that slides on the container. */ -public class Toggle: ToggleBase{} +public class Toggle: ToggleBase{ + public override func initialSetup() { + super.initialSetup() + publisher(for: .touchUpInside) + .sink { control in + control.toggle() + }.store(in: &subscribers) + } +} open class ToggleBase: Control, Changable { @@ -316,7 +324,7 @@ open class ToggleBase: Control, Changable { } /// This will toggle the state of the Toggle and execute the actionBlock if provided. - open override func defaultAction() { + open func toggle() { isOn.toggle() sendActions(for: .valueChanged) onChange?() diff --git a/VDS/Protocols/SelectorGroupModelHandlerable.swift b/VDS/Protocols/SelectorGroupModelHandlerable.swift index a11ae77c..a67b94cd 100644 --- a/VDS/Protocols/SelectorGroupModelHandlerable.swift +++ b/VDS/Protocols/SelectorGroupModelHandlerable.swift @@ -70,16 +70,14 @@ extension SelectorGroupModelHandlerable { ///MARK: Groups that allow single selections public protocol SelectorGroupSelectedModelHandlerable: SelectorGroupModelHandlerable { - var selectedInputId: String? { get set } - func didSelect(selector: ModelHandlerType.ModelType) + func didSelect(_ selectedControl: ModelHandlerType) } 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 element.selected == true }) { return model.selectors[index] } else { @@ -92,43 +90,35 @@ extension SelectorGroupSelectedModelHandlerable { let newSelectorView = ModelHandlerType(with: selector) //add the selectedPublisher for the change - newSelectorView.publisher(for: .valueChanged) - .sink(receiveValue: { [weak self] control in - guard self?.model.selectors.count ?? 0 > 0 else { return } - self?.didSelect(selector: control.model) - }) - .store(in: &subscribers) - - //add model update to the subscribers - newSelectorView.handlerPublisher() - .sink { [weak self] model in - if let cached = self?.getCachedSelector(viewModel: model), newSelectorView.shouldUpdateView(viewModel: cached) { - self?.replace(viewModel: model) - } + newSelectorView + .publisher(for: .touchUpInside) + .sink { [weak self] control in + self?.didSelect(control) } .store(in: &subscribers) + return newSelectorView + } - public func didSelect(selector: ModelHandlerType.ModelType) { - - if let selectedModel, let cached = getCachedSelector(viewModel: selectedModel) { - let oldSelectedModel = cached.copyWith { - $0.selected = false + public func didSelect(_ selectedControl: ModelHandlerType) { + //only changes local model in control, + //this is now disconnected from the parent model + selectorViews.forEach { control in + //only change the old and new + if control == selectedControl { + if var cached = getCachedSelector(viewModel: control.model) { + cached.selected = true + replace(viewModel: cached) + } + } else if control.isSelected { + if var cached = getCachedSelector(viewModel: control.model) { + cached.selected = false + replace(viewModel: cached) + } } - replace(viewModel: oldSelectedModel) - } - - //only select is selected - if selector.selected { - var newSelectedModel = selector - newSelectedModel.selected = true - replace(viewModel: newSelectedModel) - selectedInputId = selector.inputId - } else { - //ensure current value is removed - selectedInputId = nil } + } }