From 37372c5487bf0d35784431b1ddfbe37b2e6281e0 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Wed, 24 Aug 2022 08:20:49 -0500 Subject: [PATCH] refactored to use Protocol extension methods Signed-off-by: Matt Bruce --- VDS/Components/Checkbox/CheckboxGroup.swift | 17 +------ VDS/Components/RadioBox/RadioBox.swift | 25 ++++------- VDS/Components/RadioBox/RadioBoxGroup.swift | 32 +++++++------ .../RadioBox/RadioBoxGroupModel.swift | 5 +-- .../RadioButton/RadioButtonGroup.swift | 45 +------------------ .../RadioButton/RadioButtonGroupModel.swift | 4 +- 6 files changed, 29 insertions(+), 99 deletions(-) diff --git a/VDS/Components/Checkbox/CheckboxGroup.swift b/VDS/Components/Checkbox/CheckboxGroup.swift index c7c303f4..f89637f3 100644 --- a/VDS/Components/Checkbox/CheckboxGroup.swift +++ b/VDS/Components/Checkbox/CheckboxGroup.swift @@ -80,12 +80,6 @@ public class CheckboxGroup: Control, SelectorGroupHan } open override func updateView(viewModel: ModelType) { - func findSelectorView(id: UUID) -> ModelHandlerType? { - return selectorViews.first(where: { existingSelectorView in - return existingSelectorView.model.id == id - }) - } - for selectorModel in viewModel.selectors { //see if view is there for the model if let foundSelectorView = findSelectorView(id: selectorModel.id) { @@ -93,16 +87,7 @@ public class CheckboxGroup: Control, SelectorGroupHan } else { //create view - let newSelectorView = ModelHandlerType(with: selectorModel) - - //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) - } - } - .store(in: &subscribers) + let newSelectorView = createModelHandler(selector: selectorModel) self.selectorViews.append(newSelectorView) mainStackView.addArrangedSubview(newSelectorView) diff --git a/VDS/Components/RadioBox/RadioBox.swift b/VDS/Components/RadioBox/RadioBox.swift index 733cded4..9fceef8e 100644 --- a/VDS/Components/RadioBox/RadioBox.swift +++ b/VDS/Components/RadioBox/RadioBox.swift @@ -285,10 +285,11 @@ open class RadioBoxBase: Control, Changable $0.forFalse.disabled.lightColor = VDSFormControlsColor.backgroundOnlight $0.forFalse.disabled.darkColor = VDSFormControlsColor.backgroundOndark - $0.forTrue.enabled.lightColor = VDSColor.elementsPrimaryOnlight - $0.forTrue.enabled.darkColor = VDSColor.elementsPrimaryOndark - $0.forTrue.disabled.lightColor = VDSColor.interactiveDisabledOnlight - $0.forTrue.disabled.darkColor = VDSColor.interactiveDisabledOndark + $0.forTrue.enabled.lightColor = VDSFormControlsColor.backgroundOnlight + $0.forTrue.enabled.darkColor = VDSFormControlsColor.backgroundOndark + $0.forTrue.disabled.lightColor = VDSFormControlsColor.backgroundOnlight + $0.forTrue.disabled.darkColor = VDSFormControlsColor.backgroundOndark + //error doesn't care enabled/disable $0.error.forTrue.lightColor = VDSColor.elementsPrimaryOnlight $0.error.forTrue.darkColor = VDSColor.elementsPrimaryOndark @@ -314,16 +315,7 @@ open class RadioBoxBase: Control, Changable $0.error.forFalse.darkColor = VDSColor.feedbackErrorOndark } }() - - private var radioBoxStrikethroughColorConfiguration: BinarySurfaceColorConfiguration = { - return BinarySurfaceColorConfiguration().with { - $0.forTrue.lightColor = VDSColor.elementsPrimaryOnlight - $0.forTrue.darkColor = VDSColor.elementsPrimaryOndark - $0.forFalse.lightColor = VDSColor.elementsSecondaryOnlight - $0.forFalse.darkColor = VDSColor.elementsSecondaryOndark - } - }() - + //-------------------------------------------------- // MARK: - RadioBox View Updates //-------------------------------------------------- @@ -335,7 +327,6 @@ open class RadioBoxBase: Control, Changable //get the colors let backgroundColor = radioBoxBackgroundColorConfiguration.getColor(viewModel) let borderColor = radioBoxBorderColorConfiguration.getColor(viewModel) - let strikethroughColor = radioBoxStrikethroughColorConfiguration.getColor(viewModel) let borderWidth = viewModel.selected ? selectorBorderWidthSelected : selectorBorderWidth if let shapeLayer = shapeLayer, let sublayers = layer.sublayers, sublayers.contains(shapeLayer) { @@ -358,11 +349,11 @@ open class RadioBoxBase: Control, Changable border.fillColor = nil border.opacity = 1.0 border.lineWidth = strikeThroughLineThickness - border.strokeColor = strikethroughColor.cgColor + border.strokeColor = borderColor.cgColor let linePath = UIBezierPath() - let offsetPercent: CGFloat = 0.0 + let offsetPercent: CGFloat = 0.005 linePath.move(to: CGPoint(x: selectorCornerRadius, y: bounds.height * (1 - offsetPercent))) linePath.addLine(to: CGPoint(x: bounds.width - selectorCornerRadius, y: bounds.height * offsetPercent)) border.path = linePath.cgPath diff --git a/VDS/Components/RadioBox/RadioBoxGroup.swift b/VDS/Components/RadioBox/RadioBoxGroup.swift index 494a8366..6d650aba 100644 --- a/VDS/Components/RadioBox/RadioBoxGroup.swift +++ b/VDS/Components/RadioBox/RadioBoxGroup.swift @@ -8,7 +8,7 @@ import Foundation import UIKit -public class RadioBoxGroup: Control, SelectorGroupHandlerable, Changable { +public class RadioBoxGroup: Control, SelectorGroupSelectedHandlerable, Changable { public typealias ModelHandlerType = RadioBox //-------------------------------------------------- @@ -16,9 +16,22 @@ public class RadioBoxGroup: Control, SelectorGroupHan //-------------------------------------------------- public var selectorViews: [ModelHandlerType] = [] + @Proxy(\.model.selectedModel) + public var selectedModel: ModelHandlerType.ModelType? { + didSet{ + if hasError, selectedModel != nil { + hasError = false + } + } + } + public var hasError: Bool { get { model.hasError } set { + var newHasError = newValue + if selectedModel != nil, newHasError { + newHasError = false + } let selectors = model.selectors.compactMap { existing in return existing.copyWith { $0.hasError = newValue @@ -81,12 +94,6 @@ public class RadioBoxGroup: Control, SelectorGroupHan } open override func updateView(viewModel: ModelType) { - func findSelectorView(id: UUID) -> ModelHandlerType? { - return selectorViews.first(where: { existingSelectorView in - return existingSelectorView.model.id == id - }) - } - for selectorModel in viewModel.selectors { //see if view is there for the model if let foundSelectorView = findSelectorView(id: selectorModel.id) { @@ -94,16 +101,7 @@ public class RadioBoxGroup: Control, SelectorGroupHan } else { //create view - let newSelectorView = ModelHandlerType(with: selectorModel) - - //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) - } - } - .store(in: &subscribers) + let newSelectorView = createModelHandler(selector: selectorModel) self.selectorViews.append(newSelectorView) mainStackView.addArrangedSubview(newSelectorView) diff --git a/VDS/Components/RadioBox/RadioBoxGroupModel.swift b/VDS/Components/RadioBox/RadioBoxGroupModel.swift index c6f9d441..bb784de7 100644 --- a/VDS/Components/RadioBox/RadioBoxGroupModel.swift +++ b/VDS/Components/RadioBox/RadioBoxGroupModel.swift @@ -7,9 +7,7 @@ import Foundation -public protocol RadioBoxGroupModel: SelectorGroupModelable where SelectorModelType: RadioBoxModel { - -} +public protocol RadioBoxGroupModel: SelectorGroupSelectedModelable where SelectorModelType: RadioBoxModel { } public struct DefaultRadioBoxGroupModel: RadioBoxGroupModel { public typealias SelectorModelType = DefaultRadioBoxModel @@ -19,6 +17,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 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 f05bc5a0..e056ceed 100644 --- a/VDS/Components/RadioButton/RadioButtonGroup.swift +++ b/VDS/Components/RadioButton/RadioButtonGroup.swift @@ -8,7 +8,7 @@ import Foundation import UIKit -public class RadioButtonGroup: Control, SelectorGroupHandlerable, Changable { +public class RadioButtonGroup: Control, SelectorGroupSelectedHandlerable, Changable { public typealias ModelHandlerType = RadioButton //-------------------------------------------------- @@ -95,12 +95,6 @@ public class RadioButtonGroup: Control, SelectorGr } open override func updateView(viewModel: ModelType) { - func findSelectorView(id: UUID) -> ModelHandlerType? { - return selectorViews.first(where: { existingSelectorView in - return existingSelectorView.model.id == id - }) - } - for selectorModel in viewModel.selectors { //see if view is there for the model if let foundSelectorView = findSelectorView(id: selectorModel.id) { @@ -108,46 +102,11 @@ public class RadioButtonGroup: Control, SelectorGr } else { //create view - let newSelectorView = ModelHandlerType(with: selectorModel) - - //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) - } - } - .store(in: &subscribers) + let newSelectorView = createModelHandler(selector: selectorModel) self.selectorViews.append(newSelectorView) mainStackView.addArrangedSubview(newSelectorView) } } } - - open func didSelect(selector: ModelHandlerType.ModelType) { - if var oldSelectedModel = selectedModel { - oldSelectedModel.selected = false - replace(viewModel: oldSelectedModel) - } - - //only select is selected - if selector.selected { - var newSelectedModel = selector - newSelectedModel.selected = true - replace(viewModel: newSelectedModel) - selectedModel = newSelectedModel - } else { - //ensure current value is removed - selectedModel = nil - } - } } diff --git a/VDS/Components/RadioButton/RadioButtonGroupModel.swift b/VDS/Components/RadioButton/RadioButtonGroupModel.swift index 6905b469..086e1b42 100644 --- a/VDS/Components/RadioButton/RadioButtonGroupModel.swift +++ b/VDS/Components/RadioButton/RadioButtonGroupModel.swift @@ -7,9 +7,7 @@ import Foundation -public protocol RadioButtonGroupModel: SelectorGroupModelable where SelectorModelType: RadioButtonModel { - var selectedModel: SelectorModelType? { get set } -} +public protocol RadioButtonGroupModel: SelectorGroupSelectedModelable where SelectorModelType: RadioButtonModel { } extension RadioButtonGroupModel { public var errorText: String? { return nil }