136 lines
5.4 KiB
Swift
136 lines
5.4 KiB
Swift
//
|
|
// RadioButton.swift
|
|
// MVMCoreUI
|
|
//
|
|
// Created by Suresh, Kamlesh on 4/9/19.
|
|
// Copyright © 2019 Verizon Wireless. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
@objcMembers open class RadioButton: ViewConstrainingView, FormValidationProtocol{
|
|
|
|
var moleculeJson: [AnyHashable: Any]?
|
|
var selectedRadioButton: MFRadioButton?
|
|
var selectedValue: String?
|
|
|
|
// 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 setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) {
|
|
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
|
|
self.moleculeJson = json
|
|
|
|
// Configure class properties with JSON values
|
|
guard let jsonDictionary = moleculeJson,
|
|
let optionsList = jsonDictionary.optionalArrayForKey("optionsList") else {
|
|
return
|
|
}
|
|
|
|
if let delegateObject = delegateObject as? MVMCoreUIDelegateObject {
|
|
FormValidator.setupValidation(molecule: self, delegate: delegateObject.formValidationProtocol)
|
|
}
|
|
|
|
var items:[UIView] = []
|
|
for option in optionsList {
|
|
if let itemUI = createOptionItem(option as? [AnyHashable: Any]) {
|
|
items.append(itemUI)
|
|
}
|
|
}
|
|
|
|
let verticalSpace = MFStyler.defaultVerticalPadding(forSize: MVMCoreUIUtility.getWidth())
|
|
StackableViewController.populateView(self,
|
|
withUIArray: items) { (item) -> UIEdgeInsets in
|
|
return UIEdgeInsets(top: verticalSpace,
|
|
left: 0,
|
|
bottom: 0,
|
|
right: 0)
|
|
}
|
|
}
|
|
|
|
func createOptionItem(_ optionJson: [AnyHashable: Any]?) -> UIView? {
|
|
|
|
guard let json = optionJson else {
|
|
return nil
|
|
}
|
|
|
|
let containerView = ViewConstrainingView.empty()
|
|
let radioButton = MFRadioButton()
|
|
radioButton.translatesAutoresizingMaskIntoConstraints = false
|
|
containerView.addSubview(radioButton)
|
|
|
|
let label = MFLabel.commonLabel()
|
|
label.setWithJSON(json.dictionaryForKey(KeyLabel), delegateObject: nil, additionalData: nil)
|
|
containerView.addSubview(label)
|
|
|
|
radioButton.leftAnchor.constraint(equalTo: containerView.leftAnchor, constant: 0).isActive = true
|
|
radioButton.topAnchor.constraint(greaterThanOrEqualTo: containerView.topAnchor, constant: PaddingOne).isActive = true
|
|
containerView.bottomAnchor.constraint(greaterThanOrEqualTo: radioButton.bottomAnchor, constant: PaddingOne).isActive = true
|
|
radioButton.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
|
|
|
|
label.leftAnchor.constraint(equalTo: radioButton.rightAnchor, constant: PaddingTwo).isActive = true
|
|
label.rightAnchor.constraint(equalTo: containerView.rightAnchor, constant: 0).isActive = true
|
|
label.topAnchor.constraint(greaterThanOrEqualTo: containerView.topAnchor, constant: PaddingOne).isActive = true
|
|
label.bottomAnchor.constraint(greaterThanOrEqualTo: containerView.bottomAnchor, constant: PaddingOne).isActive = true
|
|
label.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
|
|
|
|
addActionHandler(containerView, radioButton, json)
|
|
return containerView
|
|
}
|
|
|
|
func addActionHandler(_ containerView: UIView, _ radioButton: MFRadioButton, _ optionJson: [AnyHashable: Any]) {
|
|
let dummyButton = MFCustomButton(frame: .zero)
|
|
dummyButton.translatesAutoresizingMaskIntoConstraints = false
|
|
containerView.addSubview(dummyButton)
|
|
NSLayoutConstraint.constraintPinSubview(toSuperview: dummyButton)
|
|
containerView.bringSubviewToFront(dummyButton)
|
|
|
|
dummyButton.add({ (button) in
|
|
if let selectedbutton = self.selectedRadioButton {
|
|
selectedbutton.isSelected = false
|
|
}
|
|
self.selectedValue = optionJson.optionalStringForKey(KeyValue)
|
|
self.selectedRadioButton = radioButton
|
|
self.selectedRadioButton?.isSelected = true
|
|
}, for: .touchUpInside)
|
|
}
|
|
|
|
func getColor( _ json: [AnyHashable: Any], _ key: String) -> UIColor? {
|
|
if let colorHex = json.optionalStringForKey(key) {
|
|
return UIColor.mfGet(forHex: colorHex)
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
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 {
|
|
return selectedValue != nil
|
|
}
|
|
|
|
// The Field name key value pair for sending to server
|
|
@objc public func formFieldName() -> String? {
|
|
return moleculeJson?.stringForkey("fieldKey")
|
|
}
|
|
|
|
// The Feild value key value paid for sending to server
|
|
@objc public func formFieldValue() -> String? {
|
|
return selectedValue
|
|
}
|
|
}
|