mvm_core_ui/MVMCoreUI/Molecules/RadioButtonModel.swift
2019-10-28 16:25:25 -04:00

55 lines
1.9 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 RadioButtonModel: NSObject {
private var selectedRadioButton: RadioButton?
private var fieldGroupName: String?
public static func setupForRadioButtonGroup(radioButton: RadioButton, formValidator: FormValidator?) -> RadioButtonModel {
guard let groupName = radioButton.radioGroupName,
let formValidator = formValidator else {
return RadioButtonModel()
}
let radioButtonModel = formValidator.radioButtonsModelByGroup[groupName] ?? RadioButtonModel()
radioButtonModel.fieldGroupName = radioButton.formFieldGroupName()
formValidator.radioButtonsModelByGroup[groupName] = radioButtonModel
return radioButtonModel
}
public func selected(_ radioButton: RadioButton) {
selectedRadioButton?.isSelected = false
selectedRadioButton = radioButton
selectedRadioButton?.isSelected = true
}
}
// MARK: - FormValidationFormFieldProtocol
extension RadioButtonModel: FormValidationFormFieldProtocol {
public func formFieldGroupName() -> String? {
return selectedRadioButton?.formFieldGroupName() ?? self.fieldGroupName
}
// Used to check the validity of the field, to enable/disable the primary button.
@objc public func isValidField() -> Bool {
return selectedRadioButton != nil ? true : false
}
// Name of the field to send to server
@objc public func formFieldName() -> String? {
return selectedRadioButton?.fieldKey
}
// The field value key value pair for sending to server
@objc public func formFieldValue() -> Any? {
return selectedRadioButton != nil ? true : false
}
}