166 lines
5.9 KiB
Swift
166 lines
5.9 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, FormValidationFormFieldProtocol {
|
|
|
|
public let radioButton = MFRadioButton()
|
|
var delegateObject: MVMCoreUIDelegateObject?
|
|
var dummyButton: MFCustomButton?
|
|
let label = Label()
|
|
|
|
var fieldKey: String?
|
|
var formValue: Bool?
|
|
var isRequired: Bool = false
|
|
var radioButtonModel: RadioButtonModel?
|
|
|
|
|
|
lazy var radioGroupName: String? = {
|
|
[unowned self] in
|
|
return json?.optionalStringForKey("radioGroupName") ?? json?.optionalStringForKey("fieldKey")
|
|
}()
|
|
|
|
// 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)
|
|
|
|
isAccessibilityElement = true
|
|
accessibilityTraits = .none
|
|
accessibilityHint = MVMCoreUIUtility.hardcodedString(withKey: "radio_action_hint")
|
|
|
|
radioButton.leftAnchor.constraint(equalTo: layoutMarginsGuide.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: layoutMarginsGuide.rightAnchor, constant: 0).isActive = true
|
|
|
|
var constraint = rightView.topAnchor.constraint(equalTo: topAnchor, constant: PaddingOne)
|
|
constraint.priority = .defaultHigh
|
|
constraint.isActive = true
|
|
|
|
constraint = bottomAnchor.constraint(equalTo: rightView.bottomAnchor, constant: PaddingOne)
|
|
constraint.priority = .defaultHigh
|
|
constraint.isActive = true
|
|
}
|
|
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: delegateObject?.formValidationProtocol)
|
|
changeAccessibilityLabel()
|
|
}
|
|
|
|
public func isValidField() -> Bool {
|
|
return radioButton.isSelected
|
|
}
|
|
|
|
public func formFieldName() -> String? {
|
|
return json?.optionalStringForKey("fieldKey")
|
|
}
|
|
|
|
public func formFieldGroupName() -> String? {
|
|
return json?.optionalStringForKey("groupName")
|
|
}
|
|
|
|
public func formFieldValue() -> Any? {
|
|
return radioButton.isSelected
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
fieldKey = jsonDictionary.optionalStringForKey("fieldKey")
|
|
isRequired = jsonDictionary.boolForKey("required")
|
|
self.delegateObject = delegateObject
|
|
|
|
let radioButtonModel = RadioButtonModel.setupForRadioButtonGroup(radioButton: self,
|
|
formValidator: delegateObject?.formValidationProtocol?.formValidatorModel?())
|
|
FormValidator.setupValidation(molecule: radioButtonModel, delegate: delegateObject?.formValidationProtocol)
|
|
self.radioButtonModel = radioButtonModel
|
|
|
|
label.setWithJSON(jsonDictionary.optionalDictionaryForKey(KeyLabel),
|
|
delegateObject: delegateObject,
|
|
additionalData: additionalData)
|
|
changeAccessibilityLabel()
|
|
}
|
|
}
|
|
|
|
// MARK: Accessibility
|
|
extension RadioButton {
|
|
func changeAccessibilityLabel() {
|
|
let stateString = radioButton.isSelected ? "radio_selected_state" : "radio_not_selected_state"
|
|
let localizedStringState = MVMCoreUIUtility.hardcodedString(withKey: stateString) ?? ""
|
|
let accebilityString = (label.accessibilityLabel ?? (json?.optionalStringForKey("accessibilityText") ?? ""))
|
|
+ (MVMCoreUIUtility.hardcodedString(withKey: "radio_desc_state") ?? "") + localizedStringState
|
|
accessibilityLabel = accebilityString
|
|
}
|
|
}
|