From 6b71115eb03ce61cf1123cfa0e5bf4473884f77a Mon Sep 17 00:00:00 2001 From: "Suresh, Kamlesh" Date: Tue, 27 Aug 2019 20:25:07 -0400 Subject: [PATCH] fixes --- MVMCoreUI/Molecules/RadioButton.swift | 43 +++++----------------- MVMCoreUI/Molecules/RadioButtonModel.swift | 4 +- 2 files changed, 11 insertions(+), 36 deletions(-) diff --git a/MVMCoreUI/Molecules/RadioButton.swift b/MVMCoreUI/Molecules/RadioButton.swift index 0fc89c1b..acd5f016 100644 --- a/MVMCoreUI/Molecules/RadioButton.swift +++ b/MVMCoreUI/Molecules/RadioButton.swift @@ -16,12 +16,16 @@ import UIKit var dummyButton: MFCustomButton? let label = Label() - var groupName: String? var fieldKey: String? var formValue: Bool? var isRequired: Bool = false var radioButtonModel: RadioButtonModel? + lazy var groupName: String? = { + [unowned self] in + return json?.optionalStringForKey("groupName") ?? json?.optionalStringForKey("fieldKey") + }() + // MARK: - Inits public init() { super.init(frame: .zero) @@ -115,22 +119,15 @@ extension RadioButton { guard let jsonDictionary = json else { return } - groupName = jsonDictionary.optionalStringForKey("groupName") + fieldKey = jsonDictionary.optionalStringForKey("fieldKey") isRequired = jsonDictionary.boolForKey("required") self.delegateObject = delegateObject - radioButtonModel = RadioButtonModel.setupForRadioButtonGroup(radioButton: self, + let radioButtonModel = RadioButtonModel.setupForRadioButtonGroup(radioButton: self, formValidator: delegateObject?.formValidationProtocol?.formValidatorModel?()) - - /* If the radio button has a group, it will have RadioButtonModel. - In this case the RadioButtonModel should be the validator - */ - if let radioButtonModel = radioButtonModel { - FormValidator.setupValidation(molecule: radioButtonModel, delegate: delegateObject?.formValidationProtocol) - } else { - FormValidator.setupValidation(molecule: self, delegate: delegateObject?.formValidationProtocol) - } + FormValidator.setupValidation(molecule: radioButtonModel, delegate: delegateObject?.formValidationProtocol) + self.radioButtonModel = radioButtonModel label.setWithJSON(jsonDictionary.optionalDictionaryForKey(KeyLabel), delegateObject: delegateObject, @@ -139,28 +136,6 @@ extension RadioButton { } } - -// MARK: - FormValidationProtocol -extension RadioButton: FormValidationProtocol { - // Used to check the validity of the field, to enable/disable the primary button. - @objc public func isValidField() -> Bool { - guard isRequired else { - return true - } - return radioButtonModel?.isValidField() ?? false - } - - // The Field name key value pair for sending to server - @objc public func formFieldName() -> String? { - return radioButtonModel?.formFieldName() ?? json?.optionalStringForKey("fieldKey") - } - - // The Field value key value pair for sending to server - @objc public func formFieldValue() -> Any? { - return radioButtonModel?.formFieldValue() ?? radioButton.isSelected - } -} - // MARK: Accessibility extension RadioButton { func changeAccessibilityLabel() { diff --git a/MVMCoreUI/Molecules/RadioButtonModel.swift b/MVMCoreUI/Molecules/RadioButtonModel.swift index 2063fd83..40238307 100644 --- a/MVMCoreUI/Molecules/RadioButtonModel.swift +++ b/MVMCoreUI/Molecules/RadioButtonModel.swift @@ -13,10 +13,10 @@ import UIKit private var selectedRadioButton: RadioButton? - public static func setupForRadioButtonGroup(radioButton: RadioButton, formValidator: FormValidator?) -> RadioButtonModel? { + public static func setupForRadioButtonGroup(radioButton: RadioButton, formValidator: FormValidator?) -> RadioButtonModel { guard let groupName = radioButton.groupName, let formValidator = formValidator else { - return nil + return RadioButtonModel() } let radioButtonModel = formValidator.radioButtonsModelByGroup[groupName] ?? RadioButtonModel()