This commit is contained in:
Kevin G Christiano 2020-03-18 14:05:01 -04:00
parent ac5f7ca25c
commit 9baf3948ad
2 changed files with 70 additions and 23 deletions

View File

@ -8,31 +8,30 @@
import UIKit
@objcMembers public class RadioButton: Control {
@objcMembers open class RadioButton: Control {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
var diameter: CGFloat = 30 {
public var diameter: CGFloat = 30 {
didSet {
widthConstraint?.constant = diameter
}
}
var enabledColor = UIColor.black
var disabledColor = UIColor.mfSilver()
var delegateObject: MVMCoreUIDelegateObject?
var widthConstraint: NSLayoutConstraint?
var heightConstraint: NSLayoutConstraint?
var radioModel: RadioButtonModel? {
public var enabledColor: UIColor = .black
public var disabledColor: UIColor = .mfSilver()
public var delegateObject: MVMCoreUIDelegateObject?
public var radioModel: RadioButtonModel? {
return model as? RadioButtonModel
}
lazy var radioGroupName: String? = {
lazy public var radioGroupName: String? = {
[unowned self] in return radioModel?.fieldKey
}()
lazy var radioButtonSelectionHelper: RadioButtonSelectionHelper? = {
[unowned self] in
lazy public var radioButtonSelectionHelper: RadioButtonSelectionHelper? = {
if let radioGroupName = radioGroupName,
let radioButtonModel = delegateObject?.formHolderDelegate?.formValidator?.radioButtonsModelByGroup[radioGroupName] {
return radioButtonModel
@ -41,20 +40,40 @@ import UIKit
}
}()
//--------------------------------------------------
// MARK: - Constraints
//--------------------------------------------------
public var widthConstraint: NSLayoutConstraint?
public var heightConstraint: NSLayoutConstraint?
//--------------------------------------------------
// MARK: - Lifecycle
//--------------------------------------------------
open override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else { return }
let color = isEnabled ? enabledColor.cgColor : disabledColor.cgColor
layer.cornerRadius = bounds.width * 0.5
layer.borderColor = color
layer.borderWidth = bounds.width * 0.0333
if isSelected {
// Space around inner circle is 1/5 the size
context.addEllipse(in: CGRect(x: bounds.width*0.2, y: bounds.height*0.2, width: bounds.width*0.6, height: bounds.height*0.6))
context.addEllipse(in: CGRect(x: bounds.width * 0.2,
y: bounds.height * 0.2,
width: bounds.width * 0.6,
height: bounds.height * 0.6))
context.setFillColor(color)
context.fillPath()
}
}
//--------------------------------------------------
// MARK: - Methods
//--------------------------------------------------
/// The action performed when tapped.
func tapAction() {
if let radioButtonModel = radioButtonSelectionHelper {
@ -82,9 +101,13 @@ import UIKit
return radioModel?.fieldValue
}
// MARK: - MVMViewProtocol
//--------------------------------------------------
// MARK: - MVMViewProtocol
//--------------------------------------------------
open override func setupView() {
super.setupView()
backgroundColor = .white
clipsToBounds = true
widthConstraint = widthAnchor.constraint(equalToConstant: 30)
@ -98,12 +121,13 @@ import UIKit
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)
guard let model = model as? RadioButtonModel else {
return
}
guard let model = model as? RadioButtonModel else { return }
self.delegateObject = delegateObject
isSelected = model.state
let radioButtonModel = RadioButtonSelectionHelper.setupForRadioButtonGroup(model,
formValidator: delegateObject?.formHolderDelegate?.formValidator)
FormValidator.setupValidation(molecule: radioButtonModel, delegate: delegateObject?.formHolderDelegate)

View File

@ -9,18 +9,26 @@
import Foundation
import MVMCore
public class RadioButtonModel: MoleculeModelProtocol, FormFieldProtocol {
open class RadioButtonModel: MoleculeModelProtocol, FormFieldProtocol {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
public static var identifier: String = "radioButton"
public var backgroundColor: Color?
public var state: Bool = false
public var enabled: Bool = true
public var baseValue: AnyHashable?
public var groupName: String?
public var fieldKey: String?
public var fieldValue: String?
//--------------------------------------------------
// MARK: - Keys
//--------------------------------------------------
private enum CodingKeys: String, CodingKey {
case moleculeName
case backgroundColor
@ -31,22 +39,37 @@ public class RadioButtonModel: MoleculeModelProtocol, FormFieldProtocol {
case groupName
}
//--------------------------------------------------
// MARK: - Initializer
//--------------------------------------------------
public init(_ state: Bool) {
self.state = state
}
//--------------------------------------------------
// MARK: - Method
//--------------------------------------------------
public func formFieldValue() -> AnyHashable? {
return state
}
//--------------------------------------------------
// MARK: - Codec
//--------------------------------------------------
required public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
if let state = try typeContainer.decodeIfPresent(Bool.self, forKey: .state) {
self.state = state
}
if let enabled = try typeContainer.decodeIfPresent(Bool.self, forKey: .enabled) {
self.enabled = enabled
}
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
fieldKey = try typeContainer.decodeIfPresent(String.self, forKey: .fieldKey)
groupName = try typeContainer.decodeIfPresent(String.self, forKey: .groupName)