From 24ab8f5ece8daac56851e0bc90aca84820812b7a Mon Sep 17 00:00:00 2001 From: "Suresh, Kamlesh" Date: Wed, 1 May 2019 10:41:04 -0400 Subject: [PATCH] cleanup --- MVMCoreUI/Molecules/RadioButton.swift | 67 +++++++++++--------- MVMCoreUI/Molecules/RadioButtonList.swift | 76 ++++++++++++++--------- 2 files changed, 83 insertions(+), 60 deletions(-) diff --git a/MVMCoreUI/Molecules/RadioButton.swift b/MVMCoreUI/Molecules/RadioButton.swift index 4fb469fd..c66bdedd 100644 --- a/MVMCoreUI/Molecules/RadioButton.swift +++ b/MVMCoreUI/Molecules/RadioButton.swift @@ -9,7 +9,7 @@ import UIKit -@objcMembers open class RadioButton: ViewConstrainingView, FormValidationProtocol{ +@objcMembers open class RadioButton: ViewConstrainingView { var selectedRadioButton: MFRadioButton? let radioButton = MFRadioButton() @@ -36,11 +36,13 @@ import UIKit open override func setupView() { super.setupView() - self.translatesAutoresizingMaskIntoConstraints = false + guard subviews.count == 0 else { + return + } + translatesAutoresizingMaskIntoConstraints = false radioButton.translatesAutoresizingMaskIntoConstraints = false addSubview(radioButton) - addSubview(label) radioButton.leftAnchor.constraint(equalTo: leftAnchor, constant: 0).isActive = true @@ -55,19 +57,6 @@ import UIKit label.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true } - open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) { - super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) - - // Configure class properties with JSON values - guard let jsonDictionary = json else { - return - } - - setupFormValidation(delegateObject: delegateObject) - label.setWithJSON(jsonDictionary.dictionaryForKey(KeyLabel), delegateObject: nil, additionalData: nil) - addActionHandler() - } - func addActionHandler() { let dummyButton = MFCustomButton(frame: .zero) @@ -76,8 +65,8 @@ import UIKit NSLayoutConstraint.constraintPinSubview(toSuperview: dummyButton) bringSubviewToFront(dummyButton) - dummyButton.add({ (button) in - self.tapAction() + dummyButton.add({ [weak self] (button) in + self?.tapAction() }, for: .touchUpInside) } @@ -97,23 +86,43 @@ import UIKit return nil } } +} + +// MARK: - MVMCoreUIMoleculeViewProtocol +extension RadioButton { - // Used to check the validity of the field, to enable/disable the primary button. - @objc public func isValidField() -> Bool { - if !radioButton.isSelected { - return true + open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) { + super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) + + // Configure class properties with JSON values + guard let jsonDictionary = json else { + return } - return formFieldValue() != nil + + label.setWithJSON(jsonDictionary.optionalDictionaryForKey(KeyLabel), + delegateObject: delegateObject, + additionalData: additionalData) + addActionHandler() } - // The Field name key value pair for sending to server - @objc public func formFieldName() -> String? { - return radioButton.isSelected ? json?.stringForkey("fieldKey") : nil + open override func needsToBeConstrained() -> Bool { + return true } - // The Feild value key value paid for sending to server - @objc public func formFieldValue() -> String? { - return radioButton.isSelected ? json?.stringForkey(KeyValue) : nil + open override func moleculeAlignment() -> UIStackView.Alignment { + return UIStackView.Alignment.leading; } } +// MARK: - FormValidationProtocol +extension RadioButton: FormValidationProtocol { + // The Field name key value pair for sending to server + @objc public func formFieldName() -> String? { + return json?.optionalStringForKey("fieldKey") + } + + // The Field value key value paid for sending to server + @objc public func formFieldValue() -> Any? { + return json?.optionalStringForKey(KeyValue) + } +} diff --git a/MVMCoreUI/Molecules/RadioButtonList.swift b/MVMCoreUI/Molecules/RadioButtonList.swift index e0393927..fdfbd00a 100644 --- a/MVMCoreUI/Molecules/RadioButtonList.swift +++ b/MVMCoreUI/Molecules/RadioButtonList.swift @@ -13,8 +13,7 @@ import UIKit @objc optional func selected(_ radioButton: RadioButton) } - -@objcMembers open class RadioButtonList: ViewConstrainingView, FormValidationProtocol, RadioButtonListProtocol { +@objcMembers open class RadioButtonList: ViewConstrainingView, RadioButtonListProtocol { var selectedRadioButton: RadioButton? var selectedValue: String? @@ -32,6 +31,43 @@ import UIKit super.init(coder: aDecoder) } + public func selected(_ radioButton: RadioButton) { + selectedRadioButton?.radioButton.isSelected = false + selectedRadioButton = radioButton + selectedRadioButton?.radioButton.isSelected = true + formValidator?.enableByValidation() + } + + open override func setupView() { + super.setupView() + self.translatesAutoresizingMaskIntoConstraints = false + } +} + +// MARK: - FormValidationProtocol +extension RadioButtonList: FormValidationProtocol { + // Used to check the validity of the field, to enable/disable the primary button. + @objc public func isValidField() -> Bool { + if !(json?.boolForKey("required") ?? true) { + return true + } + return selectedRadioButton != nil + } + + // The Field name key value pair for sending to server + @objc public func formFieldName() -> String? { + return selectedRadioButton?.formFieldName() + } + + // The Feild value key value paid for sending to server + @objc public func formFieldValue() -> Any? { + return selectedRadioButton?.formFieldValue() + } +} + +// MARK: - MVMCoreUIMoleculeViewProtocol +extension RadioButtonList { + open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) { super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) @@ -39,9 +75,9 @@ import UIKit guard let jsonDictionary = json, let optionsList = jsonDictionary.optionalArrayForKey("optionsList") as? [[AnyHashable: Any]], let delegateObject = delegateObject as? MVMCoreUIDelegateObject else { - return + return } - + setupFormValidation(delegateObject: delegateObject) var items:[UIView] = [] for option in optionsList { @@ -49,7 +85,7 @@ import UIKit radioButton.setWithJSON(option, delegateObject: delegateObject, additionalData: nil) items.append(radioButton) } - + let verticalSpace = MFStyler.defaultVerticalPadding(forSize: MVMCoreUIUtility.getWidth()) StackableViewController.populateView(self, withUIArray: items) { (item) -> UIEdgeInsets in @@ -60,33 +96,11 @@ import UIKit } } - public func selected(_ radioButton: RadioButton) { - selectedRadioButton?.radioButton.isSelected = false - selectedRadioButton = radioButton - selectedRadioButton?.radioButton.isSelected = true - } - - open override func setupView() { - super.setupView() - self.translatesAutoresizingMaskIntoConstraints = false + open override func needsToBeConstrained() -> Bool { + return true } - // Used to check the validity of the field, to enable/disable the primary button. - @objc public func isValidField() -> Bool { - if !(json?.boolForKey("required") ?? true) { - return true - } - - return selectedRadioButton?.isValidField() ?? false - } - - // The Field name key value pair for sending to server - @objc public func formFieldName() -> String? { - return selectedRadioButton?.formFieldName() - } - - // The Feild value key value paid for sending to server - @objc public func formFieldValue() -> String? { - return selectedRadioButton?.formFieldValue() + open override func moleculeAlignment() -> UIStackView.Alignment { + return UIStackView.Alignment.leading; } }