mvm_core_ui/MVMCoreUI/Molecules/RadioButtonSelectionHelper.swift
2020-03-20 19:33:53 -04:00

58 lines
2.0 KiB
Swift

//
// RadioButtonModel.swift
// MVMCoreUI
//
// Created by Suresh, Kamlesh on 5/14/19.
// Copyright © 2019 Verizon Wireless. All rights reserved.
//
import Foundation
import UIKit
@objcMembers public class RadioButtonSelectionHelper: FormFieldProtocol {
public var fieldKey: String?
public var groupName: String? = FormValidator.defaultGroupName
var selectedRadioButton: RadioButton?
private var fieldGroupName: String?
public var baseValue: AnyHashable?
init(_ fieldKey: String?) {
self.fieldKey = fieldKey
}
public static func setupForRadioButtonGroup(_ radioButtonModel: RadioButtonModel, _ radioButton: RadioButton, delegateObject: MVMCoreUIDelegateObject?) {
guard let groupName = radioButtonModel.fieldKey,
let formValidator = delegateObject?.formHolderDelegate?.formValidator else {
return
}
let radioButtonSelectionHelper = formValidator.radioButtonsModelByGroup[groupName] ?? RadioButtonSelectionHelper(radioButtonModel.fieldKey)
radioButtonSelectionHelper.fieldGroupName = radioButtonModel.fieldKey
formValidator.radioButtonsModelByGroup[groupName] = radioButtonSelectionHelper
if radioButtonModel.state {
radioButtonSelectionHelper.selectedRadioButton = radioButton
}
FormValidator.setupValidation(molecule: radioButtonSelectionHelper, delegate: delegateObject?.formHolderDelegate)
}
public func selected(_ radioButton: RadioButton) {
selectedRadioButton?.isSelected = false
selectedRadioButton = radioButton
selectedRadioButton?.isSelected = true
}
}
// MARK: - FormValidationFormFieldProtocol
extension RadioButtonSelectionHelper {
public func formFieldGroupName() -> String? {
return selectedRadioButton?.formFieldGroupName() ?? self.fieldGroupName
}
public func formFieldValue() -> AnyHashable? {
return selectedRadioButton?.formFieldValue()
}
}