From 646dfd6a10df871831ad0dfe2f20255bfda3c544 Mon Sep 17 00:00:00 2001 From: "Suresh, Kamlesh" Date: Tue, 7 Apr 2020 15:49:58 -0400 Subject: [PATCH 1/7] selction fixes and reuse fixes --- MVMCoreUI/Atomic/Atoms/Buttons/RadioButton.swift | 9 +++++++++ MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonModel.swift | 4 +--- .../Atoms/Buttons/RadioButtonSelectionHelper.swift | 9 ++++++--- MVMCoreUI/FormUIHelpers/FormValidator.swift | 3 ++- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/MVMCoreUI/Atomic/Atoms/Buttons/RadioButton.swift b/MVMCoreUI/Atomic/Atoms/Buttons/RadioButton.swift index 98532549..3d52e63b 100644 --- a/MVMCoreUI/Atomic/Atoms/Buttons/RadioButton.swift +++ b/MVMCoreUI/Atomic/Atoms/Buttons/RadioButton.swift @@ -19,6 +19,12 @@ import UIKit } } + public override var isSelected: Bool { + didSet { + radioModel?.state = isSelected + } + } + public var enabledColor: UIColor = .black public var disabledColor: UIColor = .mfSilver() public var delegateObject: MVMCoreUIDelegateObject? @@ -128,6 +134,9 @@ import UIKit self.delegateObject = delegateObject isSelected = model.state + if model.state { + print("log") + } RadioButtonSelectionHelper.setupForRadioButtonGroup(model, self, delegateObject: delegateObject) } diff --git a/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonModel.swift b/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonModel.swift index 579c0ed9..99e15280 100644 --- a/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonModel.swift +++ b/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonModel.swift @@ -47,7 +47,6 @@ open class RadioButtonModel: MoleculeModelProtocol, FormFieldProtocol { public init(_ state: Bool) { self.state = state - baseValue = state } //-------------------------------------------------- @@ -55,7 +54,7 @@ open class RadioButtonModel: MoleculeModelProtocol, FormFieldProtocol { //-------------------------------------------------- public func formFieldValue() -> AnyHashable? { - return state + return fieldValue } //-------------------------------------------------- @@ -75,7 +74,6 @@ open class RadioButtonModel: MoleculeModelProtocol, FormFieldProtocol { backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) - baseValue = state fieldKey = try typeContainer.decodeIfPresent(String.self, forKey: .fieldKey) if let groupName = try typeContainer.decodeIfPresent(String.self, forKey: .groupName) { self.groupName = groupName diff --git a/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift b/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift index 3decadb5..dbc3f7fe 100644 --- a/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift +++ b/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift @@ -16,8 +16,11 @@ import UIKit private var fieldGroupName: String? public var baseValue: AnyHashable? - init(_ fieldKey: String?) { - self.fieldKey = fieldKey + init(_ radioButtonModel: RadioButtonModel?) { + self.fieldKey = radioButtonModel?.fieldKey + if radioButtonModel?.state ?? false { + self.baseValue = radioButtonModel?.formFieldValue() + } } public static func setupForRadioButtonGroup(_ radioButtonModel: RadioButtonModel, _ radioButton: RadioButton, delegateObject: MVMCoreUIDelegateObject?) { @@ -26,7 +29,7 @@ import UIKit return } - let radioButtonSelectionHelper = formValidator.radioButtonsModelByGroup[groupName] ?? RadioButtonSelectionHelper(radioButtonModel.fieldKey) + let radioButtonSelectionHelper = formValidator.radioButtonsModelByGroup[groupName] ?? RadioButtonSelectionHelper(radioButtonModel) radioButtonSelectionHelper.fieldGroupName = radioButtonModel.fieldKey formValidator.radioButtonsModelByGroup[groupName] = radioButtonSelectionHelper diff --git a/MVMCoreUI/FormUIHelpers/FormValidator.swift b/MVMCoreUI/FormUIHelpers/FormValidator.swift index 592132f5..661036d8 100644 --- a/MVMCoreUI/FormUIHelpers/FormValidator.swift +++ b/MVMCoreUI/FormUIHelpers/FormValidator.swift @@ -66,7 +66,8 @@ import MVMCore return valid } for group in formRules { - valid = valid && validateGroup(group) + let groupValid = validateGroup(group) + valid = valid && groupValid } return valid } From d66da2d2b831e92cc85d2e208125d3cf52a9fef5 Mon Sep 17 00:00:00 2001 From: "Suresh, Kamlesh" Date: Wed, 8 Apr 2020 11:16:41 -0400 Subject: [PATCH 2/7] fix --- MVMCoreUI/Atomic/Atoms/Buttons/RadioButton.swift | 3 --- MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonModel.swift | 2 ++ 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/MVMCoreUI/Atomic/Atoms/Buttons/RadioButton.swift b/MVMCoreUI/Atomic/Atoms/Buttons/RadioButton.swift index 3d52e63b..3ee1a9a2 100644 --- a/MVMCoreUI/Atomic/Atoms/Buttons/RadioButton.swift +++ b/MVMCoreUI/Atomic/Atoms/Buttons/RadioButton.swift @@ -134,9 +134,6 @@ import UIKit self.delegateObject = delegateObject isSelected = model.state - if model.state { - print("log") - } RadioButtonSelectionHelper.setupForRadioButtonGroup(model, self, delegateObject: delegateObject) } diff --git a/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonModel.swift b/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonModel.swift index 99e15280..8863e9fa 100644 --- a/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonModel.swift +++ b/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonModel.swift @@ -47,6 +47,7 @@ open class RadioButtonModel: MoleculeModelProtocol, FormFieldProtocol { public init(_ state: Bool) { self.state = state + baseValue = state } //-------------------------------------------------- @@ -74,6 +75,7 @@ open class RadioButtonModel: MoleculeModelProtocol, FormFieldProtocol { backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) + baseValue = state fieldKey = try typeContainer.decodeIfPresent(String.self, forKey: .fieldKey) if let groupName = try typeContainer.decodeIfPresent(String.self, forKey: .groupName) { self.groupName = groupName From 03438a116587d0ebfd7f6e7b7e269f2e4a5fccb4 Mon Sep 17 00:00:00 2001 From: "Suresh, Kamlesh" Date: Wed, 8 Apr 2020 22:33:14 -0400 Subject: [PATCH 3/7] reuse fixes --- .../Buttons/RadioButtonSelectionHelper.swift | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift b/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift index dbc3f7fe..2273785f 100644 --- a/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift +++ b/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift @@ -13,46 +13,50 @@ import UIKit public var fieldKey: String? public var groupName: String = FormValidator.defaultGroupName private var selectedRadioButton: RadioButton? - private var fieldGroupName: String? + private var selectedRadioButtonModel: RadioButtonModel? public var baseValue: AnyHashable? - init(_ radioButtonModel: RadioButtonModel?) { - self.fieldKey = radioButtonModel?.fieldKey - if radioButtonModel?.state ?? false { - self.baseValue = radioButtonModel?.formFieldValue() + public func set(_ radioButtonModel: RadioButtonModel, _ radioButton: RadioButton) { + self.fieldKey = radioButtonModel.fieldKey + if radioButtonModel.state { + if self.baseValue == nil { + self.baseValue = radioButtonModel.fieldValue + } + selectedRadioButtonModel = radioButtonModel + + // Below code is needed for cell resuse scenario. + radioButton.isSelected = true + selectedRadioButton = radioButton + } else { + radioButton.isSelected = false } } - + public static func setupForRadioButtonGroup(_ radioButtonModel: RadioButtonModel, _ radioButton: RadioButton, delegateObject: MVMCoreUIDelegateObject?) { guard let groupName = radioButtonModel.fieldKey, - let formValidator = delegateObject?.formHolderDelegate?.formValidator else { - return + let formValidator = delegateObject?.formHolderDelegate?.formValidator else { + return } - - let radioButtonSelectionHelper = formValidator.radioButtonsModelByGroup[groupName] ?? RadioButtonSelectionHelper(radioButtonModel) - radioButtonSelectionHelper.fieldGroupName = radioButtonModel.fieldKey - formValidator.radioButtonsModelByGroup[groupName] = radioButtonSelectionHelper - if radioButtonModel.state { - radioButtonSelectionHelper.selectedRadioButton = radioButton - } + let radioButtonSelectionHelper = formValidator.radioButtonsModelByGroup[groupName] ?? RadioButtonSelectionHelper() + radioButtonSelectionHelper.set(radioButtonModel, radioButton) + formValidator.radioButtonsModelByGroup[groupName] = radioButtonSelectionHelper FormValidator.setupValidation(for: radioButtonSelectionHelper, delegate: delegateObject?.formHolderDelegate) } public func selected(_ radioButton: RadioButton) { selectedRadioButton?.isSelected = false + selectedRadioButtonModel?.state = false selectedRadioButton = radioButton + selectedRadioButton?.isSelected = true + selectedRadioButtonModel = selectedRadioButton?.radioModel } } // MARK: - FormValidationFormFieldProtocol -extension RadioButtonSelectionHelper { - public func formFieldGroupName() -> String? { - return selectedRadioButton?.formFieldGroupName() ?? self.fieldGroupName - } - +extension RadioButtonSelectionHelper { public func formFieldValue() -> AnyHashable? { - return selectedRadioButton?.formFieldValue() + return selectedRadioButtonModel?.fieldValue } } From db0b953bad1fc5af048cd939fb85f548e3584a54 Mon Sep 17 00:00:00 2001 From: "Suresh, Kamlesh" Date: Thu, 9 Apr 2020 10:35:58 -0400 Subject: [PATCH 4/7] fixes --- .../Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift b/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift index 2273785f..60c79465 100644 --- a/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift +++ b/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift @@ -19,7 +19,8 @@ import UIKit public func set(_ radioButtonModel: RadioButtonModel, _ radioButton: RadioButton) { self.fieldKey = radioButtonModel.fieldKey if radioButtonModel.state { - if self.baseValue == nil { + if self.baseValue == nil, + let selected = radioButtonModel.baseValue as? Bool, selected { self.baseValue = radioButtonModel.fieldValue } selectedRadioButtonModel = radioButtonModel From 5715d7eb237ab818980a7b96a327131be5b2bde8 Mon Sep 17 00:00:00 2001 From: "Suresh, Kamlesh" Date: Thu, 9 Apr 2020 15:56:00 -0400 Subject: [PATCH 5/7] need groupName --- .../Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift b/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift index 60c79465..09936d87 100644 --- a/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift +++ b/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift @@ -18,6 +18,8 @@ import UIKit public func set(_ radioButtonModel: RadioButtonModel, _ radioButton: RadioButton) { self.fieldKey = radioButtonModel.fieldKey + self.groupName = radioButtonModel.groupName + if radioButtonModel.state { if self.baseValue == nil, let selected = radioButtonModel.baseValue as? Bool, selected { @@ -56,7 +58,7 @@ import UIKit } // MARK: - FormValidationFormFieldProtocol -extension RadioButtonSelectionHelper { +extension RadioButtonSelectionHelper { public func formFieldValue() -> AnyHashable? { return selectedRadioButtonModel?.fieldValue } From 072951f91269c6c1bb74f8086058de537db37b06 Mon Sep 17 00:00:00 2001 From: "Suresh, Kamlesh" Date: Thu, 9 Apr 2020 17:22:43 -0400 Subject: [PATCH 6/7] fixes --- .../Atoms/Buttons/RadioButtonSelectionHelper.swift | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift b/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift index 09936d87..6c1da830 100644 --- a/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift +++ b/MVMCoreUI/Atomic/Atoms/Buttons/RadioButtonSelectionHelper.swift @@ -19,7 +19,7 @@ import UIKit public func set(_ radioButtonModel: RadioButtonModel, _ radioButton: RadioButton) { self.fieldKey = radioButtonModel.fieldKey self.groupName = radioButtonModel.groupName - + if radioButtonModel.state { if self.baseValue == nil, let selected = radioButtonModel.baseValue as? Bool, selected { @@ -46,12 +46,16 @@ import UIKit formValidator.radioButtonsModelByGroup[groupName] = radioButtonSelectionHelper FormValidator.setupValidation(for: radioButtonSelectionHelper, delegate: delegateObject?.formHolderDelegate) } - + public func selected(_ radioButton: RadioButton) { - selectedRadioButton?.isSelected = false - selectedRadioButtonModel?.state = false + // Checks because the view could be reused + if selectedRadioButton?.radioModel === selectedRadioButtonModel { + selectedRadioButton?.isSelected = false + } else { + selectedRadioButtonModel?.state = false + } + selectedRadioButton = radioButton - selectedRadioButton?.isSelected = true selectedRadioButtonModel = selectedRadioButton?.radioModel } From 15911265a7d7b25fb4a498cb0568309fe60a4dbd Mon Sep 17 00:00:00 2001 From: Kevin G Christiano Date: Fri, 10 Apr 2020 14:29:40 -0400 Subject: [PATCH 7/7] comments and formatting --- MVMCoreUI/FormUIHelpers/FormValidator.swift | 9 ++++++--- .../Rules/Rules/FormGroupRule.swift | 16 ++++++++++++++++ .../Rules/Rules/RuleAllValueChangedModel.swift | 11 +++++++++-- .../Rules/Rules/RuleEqualsModel.swift | 18 ++++++++++++++---- .../Rules/Rules/RuleRegexModel.swift | 11 +++++++++++ .../Rules/Rules/RuleRequiredModel.swift | 17 +++++++++++++---- 6 files changed, 69 insertions(+), 13 deletions(-) diff --git a/MVMCoreUI/FormUIHelpers/FormValidator.swift b/MVMCoreUI/FormUIHelpers/FormValidator.swift index e4c20365..29690ae5 100644 --- a/MVMCoreUI/FormUIHelpers/FormValidator.swift +++ b/MVMCoreUI/FormUIHelpers/FormValidator.swift @@ -74,13 +74,13 @@ import MVMCore /// Validates all rule groups. Returns if valid public func validate() -> Bool { var valid = true - guard let formRules = formRules else { - return valid - } + guard let formRules = formRules else { return valid } + for group in formRules { let groupValid = validateGroup(group) valid = valid && groupValid } + return valid } @@ -138,13 +138,16 @@ import MVMCore // TODO: Temporary hacks, rewrite architecture to support this. public extension FormValidator { + func getGroupName(forPageType pageType: String?) -> String? { + for actionItem in groupWatchers { if let buttonModel = actionItem as? ButtonModel, pageType == (buttonModel.action as? ActionOpenPageModel)?.pageType { return buttonModel.groupName } } + return nil } } diff --git a/MVMCoreUI/FormUIHelpers/Rules/Rules/FormGroupRule.swift b/MVMCoreUI/FormUIHelpers/Rules/Rules/FormGroupRule.swift index b40d0460..56a5a2cf 100644 --- a/MVMCoreUI/FormUIHelpers/Rules/Rules/FormGroupRule.swift +++ b/MVMCoreUI/FormUIHelpers/Rules/Rules/FormGroupRule.swift @@ -11,19 +11,35 @@ import Foundation open class FormGroupRule: Codable { + //-------------------------------------------------- + // MARK: - Properties + //-------------------------------------------------- + var groupName: String var rules: [RulesProtocol] + //-------------------------------------------------- + // MARK: - Initializer + //-------------------------------------------------- + init(_ groupName: String, _ rules: [RulesProtocol]) { self.groupName = groupName self.rules = rules } + //-------------------------------------------------- + // MARK: - Keys + //-------------------------------------------------- + private enum CodingKeys: String, CodingKey { case groupName case rules } + //-------------------------------------------------- + // MARK: - Codec + //-------------------------------------------------- + required public init(from decoder: Decoder) throws { let typeContainer = try decoder.container(keyedBy: CodingKeys.self) self.groupName = try typeContainer.decode(String.self, forKey: .groupName) diff --git a/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleAllValueChangedModel.swift b/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleAllValueChangedModel.swift index 7535c6dd..1f50bcc7 100644 --- a/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleAllValueChangedModel.swift +++ b/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleAllValueChangedModel.swift @@ -9,11 +9,18 @@ import Foundation public class RuleAllValueChangedModel: RulesProtocol { - + //-------------------------------------------------- + // MARK: - Properties + //-------------------------------------------------- + public static var identifier: String = "allValueChanged" public var type: String = RuleAllValueChangedModel.identifier public var fields: [String] - + + //-------------------------------------------------- + // MARK: - Validation + //-------------------------------------------------- + public func isValid(_ formField: FormFieldProtocol) -> Bool { return formField.baseValue != formField.formFieldValue() } diff --git a/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleEqualsModel.swift b/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleEqualsModel.swift index e6b338a4..05e7ad32 100644 --- a/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleEqualsModel.swift +++ b/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleEqualsModel.swift @@ -10,31 +10,41 @@ import Foundation public class RuleEqualsModel: RulesProtocol { + //-------------------------------------------------- + // MARK: - Properties + //-------------------------------------------------- + public static var identifier: String = "equals" public var type: String = RuleEqualsModel.identifier public var fields: [String] + + //-------------------------------------------------- + // MARK: - Validation + //-------------------------------------------------- public func isValid(_ formField: FormFieldProtocol) -> Bool { return false } public func isValid(_ fieldMolecules: [String: FormFieldProtocol]) -> Bool { + var valid = true var compareValue: AnyHashable? + for formKey in fields { - guard let formField = fieldMolecules[formKey] else { - continue - } + guard let formField = fieldMolecules[formKey] else { continue } if compareValue == nil { compareValue = formField.formFieldValue() continue } - if compareValue != formField.formFieldValue(){ + + if compareValue != formField.formFieldValue() { valid = false break } } + return valid } } diff --git a/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleRegexModel.swift b/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleRegexModel.swift index db1c4859..68eea7e5 100644 --- a/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleRegexModel.swift +++ b/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleRegexModel.swift @@ -8,16 +8,27 @@ import Foundation + public class RuleRegexModel: RulesProtocol { + //-------------------------------------------------- + // MARK: - Properties + //-------------------------------------------------- + public static var identifier: String = "regex" public var type: String = RuleRegexModel.identifier public var fields: [String] public var regex: String + //-------------------------------------------------- + // MARK: - Properties + //-------------------------------------------------- + public func isValid(_ formField: FormFieldProtocol) -> Bool { + if let stringToValidate = formField.formFieldValue() as? String { return MVMCoreUIUtility.validate(stringToValidate, withRegularExpression: regex) } + return false } } diff --git a/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleRequiredModel.swift b/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleRequiredModel.swift index cfff35eb..b34a24df 100644 --- a/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleRequiredModel.swift +++ b/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleRequiredModel.swift @@ -10,21 +10,30 @@ import Foundation public class RuleRequiredModel: RulesProtocol { - + //-------------------------------------------------- + // MARK: - Properties + //-------------------------------------------------- + public static var identifier: String = "allRequired" public var type: String = RuleRequiredModel.identifier public var fields: [String] + //-------------------------------------------------- + // MARK: - Validation + //-------------------------------------------------- + public func isValid(_ formField: FormFieldProtocol) -> Bool { - guard let value = formField.formFieldValue() else { - return false - } + guard let value = formField.formFieldValue() else { return false } + var valid = true + if let valueString = value as? String { valid = valueString.count > 0 + } else if let valueBool = value as? Bool { valid = valueBool } + return valid } }