mvm_core_ui/MVMCoreUI/Molecules/RadioButtonModel.swift
Suresh, Kamlesh 132b810a66 refactoring
2019-05-14 21:52:32 -04:00

58 lines
1.8 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 radioButtons: [RadioButton] = []
public static func setupForRadioButtonGroup(radioButton: RadioButton, formValidator: FormValidator?) -> RadioButtonModel? {
guard let groupName = radioButton.groupName,
let formValidator = formValidator else {
return nil
}
let radioButtonModel = formValidator.radioButtonsModelByGroup[groupName] ?? RadioButtonModel()
formValidator.radioButtonsModelByGroup[groupName] = radioButtonModel
radioButtonModel.radioButtons.append(radioButton)
return radioButtonModel
}
public func selected(_ radioButton: RadioButton) {
selectedRadioButton?.radioButton.isSelected = false
selectedRadioButton = radioButton
selectedRadioButton?.radioButton.isSelected = true
}
}
// MARK: - FormValidationProtocol
extension RadioButtonModel: FormValidationProtocol {
// Used to check the validity of the field, to enable/disable the primary button.
@objc public func isValidField() -> Bool {
if selectedRadioButton != nil {
return true
}
return false
}
// The Field name key value pair for sending to server
@objc public func formFieldName() -> String? {
return selectedRadioButton?.fieldKey
}
// The Feild value key value paid for sending to server
@objc public func formFieldValue() -> Any? {
return selectedRadioButton != nil ? true : false
}
}