organized radio

This commit is contained in:
Kevin G Christiano 2020-04-07 14:33:57 -04:00
parent 752ff76e79
commit 8072d8d4aa
2 changed files with 33 additions and 18 deletions

View File

@ -8,6 +8,7 @@
import UIKit import UIKit
@objcMembers open class RadioButton: Control { @objcMembers open class RadioButton: Control {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Properties // MARK: - Properties
@ -18,19 +19,19 @@ import UIKit
widthConstraint?.constant = diameter widthConstraint?.constant = diameter
} }
} }
public var enabledColor: UIColor = .black public var enabledColor: UIColor = .mvmBlack
public var disabledColor: UIColor = .mfSilver() public var disabledColor: UIColor = .mvmCoolGray6
public var delegateObject: MVMCoreUIDelegateObject? public var delegateObject: MVMCoreUIDelegateObject?
public var radioModel: RadioButtonModel? { public var radioModel: RadioButtonModel? {
return model as? RadioButtonModel return model as? RadioButtonModel
} }
lazy public var radioGroupName: String? = { lazy public var radioGroupName: String? = {
return radioModel?.fieldKey return radioModel?.fieldKey
}() }()
lazy public var radioButtonSelectionHelper: RadioButtonSelectionHelper? = { lazy public var radioButtonSelectionHelper: RadioButtonSelectionHelper? = {
if let radioGroupName = radioGroupName, if let radioGroupName = radioGroupName,
let radioButtonModel = delegateObject?.formHolderDelegate?.formValidator?.radioButtonsModelByGroup[radioGroupName] { let radioButtonModel = delegateObject?.formHolderDelegate?.formValidator?.radioButtonsModelByGroup[radioGroupName] {
@ -100,7 +101,7 @@ import UIKit
public func formFieldValue() -> AnyHashable? { public func formFieldValue() -> AnyHashable? {
return radioModel?.fieldValue return radioModel?.fieldValue
} }
//-------------------------------------------------- //--------------------------------------------------
// MARK: - MVMViewProtocol // MARK: - MVMViewProtocol
//-------------------------------------------------- //--------------------------------------------------
@ -108,7 +109,7 @@ import UIKit
open override func setupView() { open override func setupView() {
super.setupView() super.setupView()
backgroundColor = .white backgroundColor = .mvmWhite
clipsToBounds = true clipsToBounds = true
widthConstraint = widthAnchor.constraint(equalToConstant: 30) widthConstraint = widthAnchor.constraint(equalToConstant: 30)
widthConstraint?.isActive = true widthConstraint?.isActive = true
@ -120,7 +121,7 @@ import UIKit
accessibilityTraits = .button accessibilityTraits = .button
accessibilityHint = MVMCoreUIUtility.hardcodedString(withKey: "radio_action_hint") accessibilityHint = MVMCoreUIUtility.hardcodedString(withKey: "radio_action_hint")
} }
public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) { public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
super.set(with: model, delegateObject, additionalData) super.set(with: model, delegateObject, additionalData)
@ -130,9 +131,9 @@ import UIKit
isSelected = model.state isSelected = model.state
RadioButtonSelectionHelper.setupForRadioButtonGroup(model, self, delegateObject: delegateObject) RadioButtonSelectionHelper.setupForRadioButtonGroup(model, self, delegateObject: delegateObject)
} }
public override func reset() { public override func reset() {
super.reset() super.reset()
backgroundColor = .white backgroundColor = .white
} }
} }

View File

@ -7,29 +7,41 @@
// //
import Foundation import Foundation
import UIKit
@objcMembers public class RadioButtonSelectionHelper: FormFieldProtocol { @objcMembers public class RadioButtonSelectionHelper: FormFieldProtocol {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
public var fieldKey: String? public var fieldKey: String?
public var groupName: String = FormValidator.defaultGroupName public var groupName: String = FormValidator.defaultGroupName
private var selectedRadioButton: RadioButton? private var selectedRadioButton: RadioButton?
private var fieldGroupName: String? private var fieldGroupName: String?
public var baseValue: AnyHashable? public var baseValue: AnyHashable?
//--------------------------------------------------
// MARK: - Initializer
//--------------------------------------------------
init(_ fieldKey: String?) { init(_ fieldKey: String?) {
self.fieldKey = fieldKey self.fieldKey = fieldKey
} }
//--------------------------------------------------
// MARK: - Methods
//--------------------------------------------------
public static func setupForRadioButtonGroup(_ radioButtonModel: RadioButtonModel, _ radioButton: RadioButton, delegateObject: MVMCoreUIDelegateObject?) { public static func setupForRadioButtonGroup(_ radioButtonModel: RadioButtonModel, _ radioButton: RadioButton, delegateObject: MVMCoreUIDelegateObject?) {
guard let groupName = radioButtonModel.fieldKey, guard let groupName = radioButtonModel.fieldKey,
let formValidator = delegateObject?.formHolderDelegate?.formValidator else { let formValidator = delegateObject?.formHolderDelegate?.formValidator
return else { return }
}
let radioButtonSelectionHelper = formValidator.radioButtonsModelByGroup[groupName] ?? RadioButtonSelectionHelper(radioButtonModel.fieldKey) let radioButtonSelectionHelper = formValidator.radioButtonsModelByGroup[groupName] ?? RadioButtonSelectionHelper(radioButtonModel.fieldKey)
radioButtonSelectionHelper.fieldGroupName = radioButtonModel.fieldKey radioButtonSelectionHelper.fieldGroupName = radioButtonModel.fieldKey
formValidator.radioButtonsModelByGroup[groupName] = radioButtonSelectionHelper formValidator.radioButtonsModelByGroup[groupName] = radioButtonSelectionHelper
if radioButtonModel.state { if radioButtonModel.state {
radioButtonSelectionHelper.selectedRadioButton = radioButton radioButtonSelectionHelper.selectedRadioButton = radioButton
} }
@ -37,6 +49,7 @@ import UIKit
} }
public func selected(_ radioButton: RadioButton) { public func selected(_ radioButton: RadioButton) {
selectedRadioButton?.isSelected = false selectedRadioButton?.isSelected = false
selectedRadioButton = radioButton selectedRadioButton = radioButton
selectedRadioButton?.isSelected = true selectedRadioButton?.isSelected = true
@ -45,8 +58,9 @@ import UIKit
// MARK: - FormValidationFormFieldProtocol // MARK: - FormValidationFormFieldProtocol
extension RadioButtonSelectionHelper { extension RadioButtonSelectionHelper {
public func formFieldGroupName() -> String? { public func formFieldGroupName() -> String? {
return selectedRadioButton?.formFieldGroupName() ?? self.fieldGroupName return selectedRadioButton?.formFieldGroupName() ?? fieldGroupName
} }
public func formFieldValue() -> AnyHashable? { public func formFieldValue() -> AnyHashable? {