120 lines
4.0 KiB
Swift
120 lines
4.0 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, FormValidationProtocol{
|
|
|
|
var selectedRadioButton: MFRadioButton?
|
|
let radioButton = MFRadioButton()
|
|
let label = Label()
|
|
var target: RadioButtonListProtocol?
|
|
|
|
// 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)
|
|
}
|
|
|
|
public init(target: RadioButtonListProtocol) {
|
|
super.init(frame: .zero)
|
|
self.target = target
|
|
}
|
|
|
|
open override func setupView() {
|
|
super.setupView()
|
|
self.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
radioButton.translatesAutoresizingMaskIntoConstraints = false
|
|
addSubview(radioButton)
|
|
|
|
addSubview(label)
|
|
|
|
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
|
|
|
|
label.leftAnchor.constraint(equalTo: radioButton.rightAnchor, constant: PaddingTwo).isActive = true
|
|
label.rightAnchor.constraint(equalTo: rightAnchor, constant: 0).isActive = true
|
|
label.topAnchor.constraint(greaterThanOrEqualTo: topAnchor, constant: PaddingOne).isActive = true
|
|
label.bottomAnchor.constraint(greaterThanOrEqualTo: bottomAnchor, constant: PaddingOne).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() {
|
|
|
|
let dummyButton = MFCustomButton(frame: .zero)
|
|
dummyButton.translatesAutoresizingMaskIntoConstraints = false
|
|
addSubview(dummyButton)
|
|
NSLayoutConstraint.constraintPinSubview(toSuperview: dummyButton)
|
|
bringSubviewToFront(dummyButton)
|
|
|
|
dummyButton.add({ (button) in
|
|
self.tapAction()
|
|
}, for: .touchUpInside)
|
|
}
|
|
|
|
func tapAction() {
|
|
if let target = target {
|
|
target.selected?(self)
|
|
} else {
|
|
radioButton.isSelected = !radioButton.isSelected
|
|
}
|
|
formValidator?.enableByValidation()
|
|
}
|
|
|
|
func getColor( _ json: [AnyHashable: Any], _ key: String) -> UIColor? {
|
|
if let colorHex = json.optionalStringForKey(key) {
|
|
return UIColor.mfGet(forHex: colorHex)
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// Used to check the validity of the field, to enable/disable the primary button.
|
|
@objc public func isValidField() -> Bool {
|
|
if !radioButton.isSelected {
|
|
return true
|
|
}
|
|
return formFieldValue() != nil
|
|
}
|
|
|
|
// The Field name key value pair for sending to server
|
|
@objc public func formFieldName() -> String? {
|
|
return radioButton.isSelected ? json?.stringForkey("fieldKey") : nil
|
|
}
|
|
|
|
// The Feild value key value paid for sending to server
|
|
@objc public func formFieldValue() -> String? {
|
|
return radioButton.isSelected ? json?.stringForkey(KeyValue) : nil
|
|
}
|
|
}
|
|
|