This commit is contained in:
Suresh, Kamlesh 2019-05-01 10:41:04 -04:00
parent f1b46cb8bf
commit 24ab8f5ece
2 changed files with 83 additions and 60 deletions

View File

@ -9,7 +9,7 @@
import UIKit import UIKit
@objcMembers open class RadioButton: ViewConstrainingView, FormValidationProtocol{ @objcMembers open class RadioButton: ViewConstrainingView {
var selectedRadioButton: MFRadioButton? var selectedRadioButton: MFRadioButton?
let radioButton = MFRadioButton() let radioButton = MFRadioButton()
@ -36,11 +36,13 @@ import UIKit
open override func setupView() { open override func setupView() {
super.setupView() super.setupView()
self.translatesAutoresizingMaskIntoConstraints = false guard subviews.count == 0 else {
return
}
translatesAutoresizingMaskIntoConstraints = false
radioButton.translatesAutoresizingMaskIntoConstraints = false radioButton.translatesAutoresizingMaskIntoConstraints = false
addSubview(radioButton) addSubview(radioButton)
addSubview(label) addSubview(label)
radioButton.leftAnchor.constraint(equalTo: leftAnchor, constant: 0).isActive = true radioButton.leftAnchor.constraint(equalTo: leftAnchor, constant: 0).isActive = true
@ -55,19 +57,6 @@ import UIKit
label.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true 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() { func addActionHandler() {
let dummyButton = MFCustomButton(frame: .zero) let dummyButton = MFCustomButton(frame: .zero)
@ -76,8 +65,8 @@ import UIKit
NSLayoutConstraint.constraintPinSubview(toSuperview: dummyButton) NSLayoutConstraint.constraintPinSubview(toSuperview: dummyButton)
bringSubviewToFront(dummyButton) bringSubviewToFront(dummyButton)
dummyButton.add({ (button) in dummyButton.add({ [weak self] (button) in
self.tapAction() self?.tapAction()
}, for: .touchUpInside) }, for: .touchUpInside)
} }
@ -97,23 +86,43 @@ import UIKit
return nil return nil
} }
} }
}
// MARK: - MVMCoreUIMoleculeViewProtocol
extension RadioButton {
// Used to check the validity of the field, to enable/disable the primary button. open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) {
@objc public func isValidField() -> Bool { super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
if !radioButton.isSelected {
return true // 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 open override func needsToBeConstrained() -> Bool {
@objc public func formFieldName() -> String? { return true
return radioButton.isSelected ? json?.stringForkey("fieldKey") : nil
} }
// The Feild value key value paid for sending to server open override func moleculeAlignment() -> UIStackView.Alignment {
@objc public func formFieldValue() -> String? { return UIStackView.Alignment.leading;
return radioButton.isSelected ? json?.stringForkey(KeyValue) : nil
} }
} }
// 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)
}
}

View File

@ -13,8 +13,7 @@ import UIKit
@objc optional func selected(_ radioButton: RadioButton) @objc optional func selected(_ radioButton: RadioButton)
} }
@objcMembers open class RadioButtonList: ViewConstrainingView, RadioButtonListProtocol {
@objcMembers open class RadioButtonList: ViewConstrainingView, FormValidationProtocol, RadioButtonListProtocol {
var selectedRadioButton: RadioButton? var selectedRadioButton: RadioButton?
var selectedValue: String? var selectedValue: String?
@ -32,6 +31,43 @@ import UIKit
super.init(coder: aDecoder) 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]?) { open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) {
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
@ -39,9 +75,9 @@ import UIKit
guard let jsonDictionary = json, guard let jsonDictionary = json,
let optionsList = jsonDictionary.optionalArrayForKey("optionsList") as? [[AnyHashable: Any]], let optionsList = jsonDictionary.optionalArrayForKey("optionsList") as? [[AnyHashable: Any]],
let delegateObject = delegateObject as? MVMCoreUIDelegateObject else { let delegateObject = delegateObject as? MVMCoreUIDelegateObject else {
return return
} }
setupFormValidation(delegateObject: delegateObject) setupFormValidation(delegateObject: delegateObject)
var items:[UIView] = [] var items:[UIView] = []
for option in optionsList { for option in optionsList {
@ -49,7 +85,7 @@ import UIKit
radioButton.setWithJSON(option, delegateObject: delegateObject, additionalData: nil) radioButton.setWithJSON(option, delegateObject: delegateObject, additionalData: nil)
items.append(radioButton) items.append(radioButton)
} }
let verticalSpace = MFStyler.defaultVerticalPadding(forSize: MVMCoreUIUtility.getWidth()) let verticalSpace = MFStyler.defaultVerticalPadding(forSize: MVMCoreUIUtility.getWidth())
StackableViewController.populateView(self, StackableViewController.populateView(self,
withUIArray: items) { (item) -> UIEdgeInsets in withUIArray: items) { (item) -> UIEdgeInsets in
@ -60,33 +96,11 @@ import UIKit
} }
} }
public func selected(_ radioButton: RadioButton) { open override func needsToBeConstrained() -> Bool {
selectedRadioButton?.radioButton.isSelected = false return true
selectedRadioButton = radioButton
selectedRadioButton?.radioButton.isSelected = true
}
open override func setupView() {
super.setupView()
self.translatesAutoresizingMaskIntoConstraints = false
} }
// Used to check the validity of the field, to enable/disable the primary button. open override func moleculeAlignment() -> UIStackView.Alignment {
@objc public func isValidField() -> Bool { return UIStackView.Alignment.leading;
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()
} }
} }