From 9e2d5fff477226f7a142781772cf4c50019db042 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Fri, 16 Sep 2022 09:14:27 -0500 Subject: [PATCH] added Equality to Modelable added AnyEquality to classes that have an a property with 'any' added default implementation on shouldUpdateView() to check equality, this can be re-implemented in classes removed all shouldUpdateView methods everywhere since above was done Signed-off-by: Matt Bruce --- VDS/Classes/CollectionView.swift | 4 --- VDS/Classes/Control.swift | 4 --- VDS/Classes/View.swift | 6 +--- VDS/Components/Checkbox/Checkbox.swift | 14 +------- VDS/Components/Checkbox/CheckboxGroup.swift | 10 +----- VDS/Components/Checkbox/CheckboxModel.swift | 29 ++++++++++++++++ VDS/Components/Label/Label.swift | 12 +------ VDS/Components/Label/LabelModel.swift | 4 +-- VDS/Components/RadioBox/RadioBox.swift | 14 +------- VDS/Components/RadioBox/RadioBoxGroup.swift | 8 ----- VDS/Components/RadioBox/RadioBoxModel.swift | 33 +++++++++++++++++++ VDS/Components/RadioButton/RadioButton.swift | 14 +------- .../RadioButton/RadioButtonGroup.swift | 9 ----- .../RadioButton/RadioButtonModel.swift | 33 +++++++++++++++++++ VDS/Components/RadioSwatch/RadioSwatch.swift | 9 +---- .../RadioSwatch/RadioSwatchGroup.swift | 4 --- VDS/Components/Toggle/Toggle.swift | 8 +---- VDS/Protocols/ModelHandlerable.swift | 4 +++ VDS/Protocols/Modelable.swift | 2 +- 19 files changed, 110 insertions(+), 111 deletions(-) diff --git a/VDS/Classes/CollectionView.swift b/VDS/Classes/CollectionView.swift index eca2ddf4..e0b1ab57 100644 --- a/VDS/Classes/CollectionView.swift +++ b/VDS/Classes/CollectionView.swift @@ -90,10 +90,6 @@ open class CollectionView: UICollectionView, ModelHandlera //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- - open func shouldUpdateView(viewModel: ModelType) -> Bool { - fatalError("Implement shouldUpdateView") - } - open func updateView(viewModel: ModelType) { fatalError("Implement updateView") } diff --git a/VDS/Classes/Control.swift b/VDS/Classes/Control.swift index 457ceaa7..50283184 100644 --- a/VDS/Classes/Control.swift +++ b/VDS/Classes/Control.swift @@ -96,10 +96,6 @@ open class Control: UIControl, ModelHandlerable, ViewProto //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- - open func shouldUpdateView(viewModel: ModelType) -> Bool { - fatalError("Implement shouldUpdateView") - } - open func updateView(viewModel: ModelType) { fatalError("Implement updateView") } diff --git a/VDS/Classes/View.swift b/VDS/Classes/View.swift index a0ed38b2..5883930f 100644 --- a/VDS/Classes/View.swift +++ b/VDS/Classes/View.swift @@ -84,11 +84,7 @@ open class View: UIView, ModelHandlerable, ViewProtocol, R //-------------------------------------------------- // MARK: - Overrides - //-------------------------------------------------- - open func shouldUpdateView(viewModel: ModelType) -> Bool { - fatalError("Implement shouldUpdateView") - } - + //-------------------------------------------------- open func updateView(viewModel: ModelType) { fatalError("Implement updateView") } diff --git a/VDS/Components/Checkbox/Checkbox.swift b/VDS/Components/Checkbox/Checkbox.swift index 213f4f35..2d24d803 100644 --- a/VDS/Components/Checkbox/Checkbox.swift +++ b/VDS/Components/Checkbox/Checkbox.swift @@ -228,19 +228,7 @@ open class CheckboxBase: Control { //-------------------------------------------------- // MARK: - State - //-------------------------------------------------- - /// Follow the SwiftUI View paradigm - /// - Parameter viewModel: state - open override func shouldUpdateView(viewModel: ModelType) -> Bool { - let update = viewModel.selected != model.selected - || viewModel.labelText != model.labelText - || viewModel.childText != model.childText - || viewModel.hasError != model.hasError - || viewModel.surface != model.surface - || viewModel.disabled != model.disabled - return update - } - + //-------------------------------------------------- open override func updateView(viewModel: ModelType) { let enabled = !viewModel.disabled diff --git a/VDS/Components/Checkbox/CheckboxGroup.swift b/VDS/Components/Checkbox/CheckboxGroup.swift index 548ef212..bdad7d36 100644 --- a/VDS/Components/Checkbox/CheckboxGroup.swift +++ b/VDS/Components/Checkbox/CheckboxGroup.swift @@ -67,15 +67,7 @@ public class CheckboxGroupBase Bool { - let update = viewModel.selectors.count != model.selectors.count - || viewModel.hasError != model.hasError - || viewModel.surface != model.surface - || viewModel.disabled != model.disabled - return update - } - + open override func updateView(viewModel: ModelType) { for selectorModel in viewModel.selectors { //see if view is there for the model diff --git a/VDS/Components/Checkbox/CheckboxModel.swift b/VDS/Components/Checkbox/CheckboxModel.swift index 3ca18beb..160aab95 100644 --- a/VDS/Components/Checkbox/CheckboxModel.swift +++ b/VDS/Components/Checkbox/CheckboxModel.swift @@ -91,4 +91,33 @@ public struct DefaultCheckboxModel: CheckboxModel { public var accessibilityLabelDisabled: String? public init() {} + + public static func == (lhs: DefaultCheckboxModel, rhs: DefaultCheckboxModel) -> Bool { + lhs.isEqual(rhs) + } + + public func isEqual(_ equatable: DefaultCheckboxModel) -> Bool { + return id == equatable.id + && selected == equatable.selected + && labelText == equatable.labelText + && labelTextAttributes == equatable.labelTextAttributes + && childText == equatable.childText + && childTextAttributes == equatable.childTextAttributes + && hasError == equatable.hasError + && errorText == equatable.errorText + && inputId == equatable.inputId + && value == equatable.value + && surface == equatable.surface + && disabled == equatable.disabled + && dataAnalyticsTrack == equatable.dataAnalyticsTrack + && dataClickStream == equatable.dataClickStream + && dataTrack == equatable.dataTrack + && accessibilityHintEnabled == equatable.accessibilityHintEnabled + && accessibilityHintDisabled == equatable.accessibilityHintDisabled + && accessibilityValueEnabled == equatable.accessibilityValueEnabled + && accessibilityValueDisabled == equatable.accessibilityValueDisabled + && accessibilityLabelEnabled == equatable.accessibilityLabelEnabled + && accessibilityLabelDisabled == equatable.accessibilityLabelDisabled + } + } diff --git a/VDS/Components/Label/Label.swift b/VDS/Components/Label/Label.swift index 2b9d01a9..0e6c037d 100644 --- a/VDS/Components/Label/Label.swift +++ b/VDS/Components/Label/Label.swift @@ -130,17 +130,7 @@ open class LabelBase: UILabel, ModelHandlerable, ViewProt //-------------------------------------------------- // MARK: - Overrides - //-------------------------------------------------- - /// Follow the SwiftUI View paradigm - /// - Parameter viewModel: state - open func shouldUpdateView(viewModel: ModelType) -> Bool { - return viewModel.text != model.text - || viewModel.disabled != model.disabled - || viewModel.surface != model.surface - || viewModel.font != model.font - || viewModel.textPosition != model.textPosition - } - + //-------------------------------------------------- open func updateView(viewModel: ModelType) { textAlignment = viewModel.textPosition.textAlignment textColor = textColorConfiguration.getColor(viewModel) diff --git a/VDS/Components/Label/LabelModel.swift b/VDS/Components/Label/LabelModel.swift index 94c270ee..a8e8b659 100644 --- a/VDS/Components/Label/LabelModel.swift +++ b/VDS/Components/Label/LabelModel.swift @@ -8,12 +8,12 @@ import Foundation import UIKit -public protocol LabelModel: Modelable, Labelable { +public protocol LabelModel: Modelable, Labelable, Equatable, AnyEquatable { var text: String? { get set } var attributes: [any LabelAttributeModel]? { get set } } -public struct DefaultLabelModel: LabelModel, AnyEquatable, Equatable { +public struct DefaultLabelModel: LabelModel { public static func == (lhs: DefaultLabelModel, rhs: DefaultLabelModel) -> Bool { lhs.isEqual(rhs) } diff --git a/VDS/Components/RadioBox/RadioBox.swift b/VDS/Components/RadioBox/RadioBox.swift index c249ada6..6b860403 100644 --- a/VDS/Components/RadioBox/RadioBox.swift +++ b/VDS/Components/RadioBox/RadioBox.swift @@ -205,19 +205,7 @@ open class RadioBoxBase: Control { //-------------------------------------------------- // MARK: - State - //-------------------------------------------------- - /// Follow the SwiftUI View paradigm - /// - Parameter viewModel: state - open override func shouldUpdateView(viewModel: ModelType) -> Bool { - let update = viewModel.selected != model.selected - || viewModel.text != model.text - || viewModel.subText != model.subText - || viewModel.subTextRight != model.subTextRight - || viewModel.surface != model.surface - || viewModel.disabled != model.disabled - return update - } - + //-------------------------------------------------- open override func updateView(viewModel: ModelType) { let enabled = !viewModel.disabled diff --git a/VDS/Components/RadioBox/RadioBoxGroup.swift b/VDS/Components/RadioBox/RadioBoxGroup.swift index 6143670f..74059739 100644 --- a/VDS/Components/RadioBox/RadioBoxGroup.swift +++ b/VDS/Components/RadioBox/RadioBoxGroup.swift @@ -68,14 +68,6 @@ public class RadioBoxGroupBase Bool { - let update = viewModel.selectedModel?.inputId != model.selectedModel?.inputId - || viewModel.selectors.count != model.selectors.count - || viewModel.surface != model.surface - || viewModel.disabled != model.disabled - return update - } open override func updateView(viewModel: ModelType) { for selectorModel in viewModel.selectors { diff --git a/VDS/Components/RadioBox/RadioBoxModel.swift b/VDS/Components/RadioBox/RadioBoxModel.swift index 097fa91f..7c6884fa 100644 --- a/VDS/Components/RadioBox/RadioBoxModel.swift +++ b/VDS/Components/RadioBox/RadioBoxModel.swift @@ -86,4 +86,37 @@ public struct DefaultRadioBoxModel: RadioBoxModel { public var accessibilityLabelDisabled: String? public init() {} + + public static func == (lhs: DefaultRadioBoxModel, rhs: DefaultRadioBoxModel) -> Bool { + lhs.isEqual(rhs) + } + + public func isEqual(_ equatable: DefaultRadioBoxModel) -> Bool { + return id == equatable.id + && selected == equatable.selected + && text == equatable.text + && textAttributes == equatable.textAttributes + && subText == equatable.subText + && subTextAttributes == equatable.subTextAttributes + && subTextRight == equatable.subTextRight + && subTextRightAttributes == equatable.subTextRightAttributes + && selectedAccentColor == equatable.selectedAccentColor + && strikethrough == equatable.strikethrough + + && inputId == equatable.inputId + && value == equatable.value + + && surface == equatable.surface + && disabled == equatable.disabled + + && dataAnalyticsTrack == equatable.dataAnalyticsTrack + && dataClickStream == equatable.dataClickStream + && dataTrack == equatable.dataTrack + && accessibilityHintEnabled == equatable.accessibilityHintEnabled + && accessibilityHintDisabled == equatable.accessibilityHintDisabled + && accessibilityValueEnabled == equatable.accessibilityValueEnabled + && accessibilityValueDisabled == equatable.accessibilityValueDisabled + && accessibilityLabelEnabled == equatable.accessibilityLabelEnabled + && accessibilityLabelDisabled == equatable.accessibilityLabelDisabled + } } diff --git a/VDS/Components/RadioButton/RadioButton.swift b/VDS/Components/RadioButton/RadioButton.swift index 76e432f6..d0e3100f 100644 --- a/VDS/Components/RadioButton/RadioButton.swift +++ b/VDS/Components/RadioButton/RadioButton.swift @@ -238,19 +238,7 @@ open class RadioButtonBase: Control { //-------------------------------------------------- // MARK: - State - //-------------------------------------------------- - /// Follow the SwiftUI View paradigm - /// - Parameter viewModel: state - open override func shouldUpdateView(viewModel: ModelType) -> Bool { - let update = viewModel.selected != model.selected - || viewModel.labelText != model.labelText - || viewModel.childText != model.childText - || viewModel.hasError != model.hasError - || viewModel.surface != model.surface - || viewModel.disabled != model.disabled - return update - } - + //-------------------------------------------------- open override func updateView(viewModel: ModelType) { let enabled = !viewModel.disabled diff --git a/VDS/Components/RadioButton/RadioButtonGroup.swift b/VDS/Components/RadioButton/RadioButtonGroup.swift index f910a7aa..8b499f73 100644 --- a/VDS/Components/RadioButton/RadioButtonGroup.swift +++ b/VDS/Components/RadioButton/RadioButtonGroup.swift @@ -68,15 +68,6 @@ public class RadioButtonGroupBase Bool { - let update = viewModel.selectedModel?.inputId != model.selectedModel?.inputId - || viewModel.selectors.count != model.selectors.count - || viewModel.hasError != model.hasError - || viewModel.surface != model.surface - || viewModel.disabled != model.disabled - return update - } open override func updateView(viewModel: ModelType) { for selectorModel in viewModel.selectors { diff --git a/VDS/Components/RadioButton/RadioButtonModel.swift b/VDS/Components/RadioButton/RadioButtonModel.swift index e7ff67e3..5bafcf02 100644 --- a/VDS/Components/RadioButton/RadioButtonModel.swift +++ b/VDS/Components/RadioButton/RadioButtonModel.swift @@ -92,4 +92,37 @@ public struct DefaultRadioButtonModel: RadioButtonModel { public var accessibilityLabelDisabled: String? public init() {} + + public static func == (lhs: DefaultRadioButtonModel, rhs: DefaultRadioButtonModel) -> Bool { + lhs.isEqual(rhs) + } + + public func isEqual(_ equatable: DefaultRadioButtonModel) -> Bool { + return id == equatable.id + && selected == equatable.selected + + && labelText == equatable.labelText + && labelTextAttributes == equatable.labelTextAttributes + && childText == equatable.childText + && childTextAttributes == equatable.childTextAttributes + + && hasError == equatable.hasError + && errorText == equatable.errorText + + && inputId == equatable.inputId + && value == equatable.value + + && surface == equatable.surface + && disabled == equatable.disabled + + && dataAnalyticsTrack == equatable.dataAnalyticsTrack + && dataClickStream == equatable.dataClickStream + && dataTrack == equatable.dataTrack + && accessibilityHintEnabled == equatable.accessibilityHintEnabled + && accessibilityHintDisabled == equatable.accessibilityHintDisabled + && accessibilityValueEnabled == equatable.accessibilityValueEnabled + && accessibilityValueDisabled == equatable.accessibilityValueDisabled + && accessibilityLabelEnabled == equatable.accessibilityLabelEnabled + && accessibilityLabelDisabled == equatable.accessibilityLabelDisabled + } } diff --git a/VDS/Components/RadioSwatch/RadioSwatch.swift b/VDS/Components/RadioSwatch/RadioSwatch.swift index 9d7d1c35..235b28bd 100644 --- a/VDS/Components/RadioSwatch/RadioSwatch.swift +++ b/VDS/Components/RadioSwatch/RadioSwatch.swift @@ -139,14 +139,7 @@ open class RadioSwatchBase: Control { //-------------------------------------------------- // MARK: - State - //-------------------------------------------------- - /// Follow the SwiftUI View paradigm - /// - Parameter viewModel: state - open override func shouldUpdateView(viewModel: ModelType) -> Bool { - let should = viewModel != model - return should - } - + //-------------------------------------------------- open override func updateView(viewModel: ModelType) { let enabled = !viewModel.disabled diff --git a/VDS/Components/RadioSwatch/RadioSwatchGroup.swift b/VDS/Components/RadioSwatch/RadioSwatchGroup.swift index cc400e72..1769a89a 100644 --- a/VDS/Components/RadioSwatch/RadioSwatchGroup.swift +++ b/VDS/Components/RadioSwatch/RadioSwatchGroup.swift @@ -124,10 +124,6 @@ public class RadioSwatchGroupBase Bool { - return viewModel != model - } - public override func initialSetup() { super.initialSetup() collectionView.delegate = self diff --git a/VDS/Components/Toggle/Toggle.swift b/VDS/Components/Toggle/Toggle.swift index 8fc4c38d..b66d4eef 100644 --- a/VDS/Components/Toggle/Toggle.swift +++ b/VDS/Components/Toggle/Toggle.swift @@ -328,13 +328,7 @@ open class ToggleBase: Control { //-------------------------------------------------- // MARK: - State - //-------------------------------------------------- - /// Follow the SwiftUI View paradigm - /// - Parameter viewModel: state - open override func shouldUpdateView(viewModel: ModelType) -> Bool { - return true - } - + //-------------------------------------------------- open override func updateView(viewModel: ModelType) { label.set(with: viewModel.label) updateLabel(viewModel) diff --git a/VDS/Protocols/ModelHandlerable.swift b/VDS/Protocols/ModelHandlerable.swift index 82046c0c..0b3c684e 100644 --- a/VDS/Protocols/ModelHandlerable.swift +++ b/VDS/Protocols/ModelHandlerable.swift @@ -32,6 +32,10 @@ extension ModelHandlerable { } } + public func shouldUpdateView(viewModel: ModelType) -> Bool { + model != viewModel + } + public func setupUpdateView() { handlerPublisher() .subscribe(on: RunLoop.main) diff --git a/VDS/Protocols/Modelable.swift b/VDS/Protocols/Modelable.swift index dc49a3fe..78c5ab6c 100644 --- a/VDS/Protocols/Modelable.swift +++ b/VDS/Protocols/Modelable.swift @@ -7,7 +7,7 @@ import Foundation -public protocol Modelable: Surfaceable, Disabling, Initable, Withable, Identifiable, CustomDebugStringConvertible where ID == UUID { +public protocol Modelable: Surfaceable, Disabling, Initable, Withable, Identifiable, Equatable, CustomDebugStringConvertible where ID == UUID { }