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
} }
} }
}
// Used to check the validity of the field, to enable/disable the primary button. // MARK: - MVMCoreUIMoleculeViewProtocol
@objc public func isValidField() -> Bool { extension RadioButton {
if !radioButton.isSelected {
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
}
label.setWithJSON(jsonDictionary.optionalDictionaryForKey(KeyLabel),
delegateObject: delegateObject,
additionalData: additionalData)
addActionHandler()
}
open override func needsToBeConstrained() -> Bool {
return true return true
} }
return formFieldValue() != 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 // The Field name key value pair for sending to server
@objc public func formFieldName() -> String? { @objc public func formFieldName() -> String? {
return radioButton.isSelected ? json?.stringForkey("fieldKey") : nil return json?.optionalStringForKey("fieldKey")
} }
// The Feild value key value paid for sending to server // The Field value key value paid for sending to server
@objc public func formFieldValue() -> String? { @objc public func formFieldValue() -> Any? {
return radioButton.isSelected ? json?.stringForkey(KeyValue) : nil 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)
@ -60,33 +96,11 @@ import UIKit
} }
} }
public func selected(_ radioButton: RadioButton) { open override func needsToBeConstrained() -> Bool {
selectedRadioButton?.radioButton.isSelected = false
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.
@objc public func isValidField() -> Bool {
if !(json?.boolForKey("required") ?? true) {
return true return true
} }
return selectedRadioButton?.isValidField() ?? false open override func moleculeAlignment() -> UIStackView.Alignment {
} return UIStackView.Alignment.leading;
// 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()
} }
} }