// // 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() let label = Label() var target: RadioButtonListProtocol? var dummyButton: MFCustomButton? // 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() guard subviews.count == 0 else { return } 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 } func addActionHandler() { if dummyButton != nil { 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 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 } } } // MARK: - MVMCoreUIMoleculeViewProtocol extension RadioButton { 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 } 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 @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) } }