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

View File

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