mvm_core_ui/MVMCoreUI/Molecules/RadioButton.swift
Suresh, Kamlesh 6828f833e7 code review
2019-05-31 17:29:32 -04:00

161 lines
5.8 KiB
Swift

//
// RadioButton.swift
// MVMCoreUI
//
// Created by Suresh, Kamlesh on 4/25/19.
// Copyright © 2019 Verizon Wireless. All rights reserved.
//
import UIKit
@objcMembers open class RadioButton: ViewConstrainingView {
let radioButton = MFRadioButton()
var delegateObject: MVMCoreUIDelegateObject?
var dummyButton: MFCustomButton?
let label = Label()
var groupName: String?
var fieldKey: String?
var formValue: Bool?
var isRequired: Bool = false
var radioButtonModel: RadioButtonModel?
// MARK: - Inits
public init() {
super.init(frame: .zero)
}
public override init(frame: CGRect) {
super.init(frame: frame)
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
open override func setupView() {
super.setupView()
guard subviews.count == 0 else {
return
}
translatesAutoresizingMaskIntoConstraints = false
radioButton.translatesAutoresizingMaskIntoConstraints = false
addSubview(radioButton)
radioButton.leftAnchor.constraint(equalTo: leftAnchor, constant: 0).isActive = true
radioButton.topAnchor.constraint(greaterThanOrEqualTo: topAnchor, constant: PaddingOne).isActive = true
bottomAnchor.constraint(greaterThanOrEqualTo: radioButton.bottomAnchor, constant: PaddingOne).isActive = true
radioButton.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
if let rightView = createRightView() {
addSubview(rightView)
rightView.leftAnchor.constraint(equalTo: radioButton.rightAnchor, constant: PaddingHorizontalBetweenRelatedItems).isActive = true
rightView.rightAnchor.constraint(equalTo: rightAnchor, constant: 0).isActive = true
rightView.topAnchor.constraint(greaterThanOrEqualTo: topAnchor, constant: PaddingOne).isActive = true
rightView.bottomAnchor.constraint(greaterThanOrEqualTo: bottomAnchor, constant: PaddingOne).isActive = true
rightView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
}
addActionHandler()
addActionHandler()
}
func createRightView() -> ViewConstrainingView? {
let rightView = ViewConstrainingView(constrainingView: label)
return rightView
}
func addActionHandler() {
guard dummyButton == nil else {
return
}
let dummyButton = MFCustomButton(frame: .zero)
self.dummyButton = dummyButton
dummyButton.translatesAutoresizingMaskIntoConstraints = false
addSubview(dummyButton)
NSLayoutConstraint.constraintPinSubview(toSuperview: dummyButton)
bringSubviewToFront(dummyButton)
dummyButton.add({ [weak self] (button) in
self?.tapAction()
}, for: .touchUpInside)
}
func tapAction() {
if let radioButtonModel = radioButtonModel {
radioButtonModel.selected(self)
} else {
radioButton.isSelected = !radioButton.isSelected
}
FormValidator.enableByValidationWith(delegate: self.delegateObject?.formValidationProtocol)
}
}
// MARK: - MVMCoreUIMoleculeViewProtocol
extension RadioButton {
@objc open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
// Configure class properties with JSON values
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,
formValidator: self.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: self.delegateObject?.formValidationProtocol)
} else {
FormValidator.setupValidation(molecule: self, delegate: self.delegateObject?.formValidationProtocol)
}
label.setWithJSON(jsonDictionary.optionalDictionaryForKey(KeyLabel),
delegateObject: delegateObject,
additionalData: additionalData)
}
open override func needsToBeConstrained() -> Bool {
return true
}
open override func moleculeAlignment() -> UIStackView.Alignment {
return UIStackView.Alignment.leading;
}
}
// 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 Feild value key value paid for sending to server
@objc public func formFieldValue() -> Any? {
return radioButtonModel?.formFieldValue() ?? radioButton.isSelected
}
}