converted to vds
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
10c55b12be
commit
724130a827
@ -7,54 +7,121 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
|
import VDS
|
||||||
|
|
||||||
|
@objcMembers public class RadioButtonLabel: VDS.RadioButtonItem, RadioButtonSelectionHelperProtocol, VDSMoleculeViewProtocol, MFButtonProtocol {
|
||||||
|
|
||||||
@objcMembers public class RadioButtonLabel: View {
|
//------------------------------------------------------
|
||||||
|
// MARK: - Properties
|
||||||
public let radioButton = RadioButton()
|
//------------------------------------------------------
|
||||||
var delegateObject: MVMCoreUIDelegateObject?
|
open var viewModel: RadioButtonLabelModel!
|
||||||
let label = Label()
|
open var delegateObject: MVMCoreUIDelegateObject?
|
||||||
|
open var additionalData: [AnyHashable : Any]?
|
||||||
public override func updateView(_ size: CGFloat) {
|
|
||||||
super.updateView(size)
|
|
||||||
radioButton.updateView(size)
|
|
||||||
label.updateView(size)
|
|
||||||
}
|
|
||||||
|
|
||||||
open override func setupView() {
|
open var radioButtonModel: RadioButtonModel {
|
||||||
super.setupView()
|
viewModel.radioButton
|
||||||
|
}
|
||||||
|
|
||||||
addSubview(radioButton)
|
// Form Validation
|
||||||
radioButton.leftAnchor.constraint(equalTo: layoutMarginsGuide.leftAnchor, constant: 0).isActive = true
|
var fieldKey: String?
|
||||||
radioButton.topAnchor.constraint(greaterThanOrEqualTo: layoutMarginsGuide.topAnchor, constant: PaddingOne).isActive = true
|
var fieldValue: JSONValue?
|
||||||
layoutMarginsGuide.bottomAnchor.constraint(greaterThanOrEqualTo: radioButton.bottomAnchor, constant: PaddingOne).isActive = true
|
var groupName: String?
|
||||||
radioButton.centerYAnchor.constraint(equalTo: layoutMarginsGuide.centerYAnchor).isActive = true
|
|
||||||
|
|
||||||
if let rightView = createRightView() {
|
open override var isSelected: Bool {
|
||||||
addSubview(rightView)
|
didSet {
|
||||||
rightView.leftAnchor.constraint(equalTo: radioButton.rightAnchor, constant: Padding.Component.gutterForApplicationWidth).isActive = true
|
radioButtonModel.state = isSelected
|
||||||
rightView.rightAnchor.constraint(equalTo: layoutMarginsGuide.rightAnchor, constant: 0).isActive = true
|
if oldValue != isSelected {
|
||||||
|
sendActions(for: .valueChanged)
|
||||||
var constraint = rightView.topAnchor.constraint(greaterThanOrEqualTo: layoutMarginsGuide.topAnchor, constant: PaddingOne)
|
}
|
||||||
constraint.priority = .defaultHigh
|
|
||||||
constraint.isActive = true
|
|
||||||
|
|
||||||
constraint = layoutMarginsGuide.bottomAnchor.constraint(greaterThanOrEqualTo: rightView.bottomAnchor, constant: PaddingOne)
|
|
||||||
constraint.priority = .defaultHigh
|
|
||||||
constraint.isActive = true
|
|
||||||
layoutMarginsGuide.centerYAnchor.constraint(equalTo: rightView.centerYAnchor).isActive = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createRightView() -> Container? {
|
lazy public var radioGroupName: String? = { viewModel.radioButton.fieldKey }()
|
||||||
let rightView = Container(andContain: label)
|
|
||||||
return rightView
|
lazy public var radioButtonSelectionHelper: RadioButtonSelectionHelper? = {
|
||||||
|
|
||||||
|
guard let radioGroupName = radioGroupName,
|
||||||
|
let radioButtonModel = delegateObject?.formHolderDelegate?.formValidator?.radioButtonsModelByGroup[radioGroupName]
|
||||||
|
else { return nil }
|
||||||
|
|
||||||
|
return radioButtonModel
|
||||||
|
}()
|
||||||
|
|
||||||
|
//--------------------------------------------------
|
||||||
|
// MARK: - Life Cycle
|
||||||
|
//--------------------------------------------------
|
||||||
|
@objc open func updateView(_ size: CGFloat) {}
|
||||||
|
|
||||||
|
open override func toggle() {
|
||||||
|
guard !isSelected, isEnabled else { return }
|
||||||
|
|
||||||
|
//removed error
|
||||||
|
if showError && isSelected == false {
|
||||||
|
showError.toggle()
|
||||||
|
}
|
||||||
|
|
||||||
|
let wasPreviouslySelected = isSelected
|
||||||
|
if let radioButtonSelectionHelper {
|
||||||
|
radioButtonSelectionHelper.selected(self)
|
||||||
|
} else {
|
||||||
|
isSelected = !isSelected
|
||||||
|
}
|
||||||
|
|
||||||
|
if let actionModel = viewModel.radioButton.action, isSelected, !wasPreviouslySelected {
|
||||||
|
Task(priority: .userInitiated) {
|
||||||
|
try await Button.performButtonAction(with: actionModel, button: self, delegateObject: delegateObject, additionalData: additionalData, sourceModel: viewModel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ = FormValidator.validate(delegate: delegateObject?.formHolderDelegate)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
|
//--------------------------------------------------
|
||||||
guard let radioButtonLabelModel = model as? RadioButtonLabelModel else { return }
|
// MARK: - Atomic
|
||||||
|
//--------------------------------------------------
|
||||||
radioButton.set(with: radioButtonLabelModel.radioButton, delegateObject, additionalData)
|
public func viewModelDidUpdate() {
|
||||||
label.set(with: radioButtonLabelModel.label, delegateObject, additionalData)
|
surface = viewModel.surface
|
||||||
|
|
||||||
|
updateRadioButton()
|
||||||
|
|
||||||
|
//primary label
|
||||||
|
labelText = viewModel.label.text
|
||||||
|
if let attributes = viewModel.label.attributes?.toVDSLabelAttributeModel(delegateObject: delegateObject, additionalData: additionalData) {
|
||||||
|
labelTextAttributes = attributes
|
||||||
|
}
|
||||||
|
|
||||||
|
//secondary label
|
||||||
|
if let subTitleModel = viewModel.subTitle {
|
||||||
|
childText = subTitleModel.text
|
||||||
|
if let attributes = subTitleModel.attributes?.toVDSLabelAttributeModel(delegateObject: delegateObject, additionalData: additionalData) {
|
||||||
|
childTextAttributes = attributes
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func updateRadioButton() {
|
||||||
|
|
||||||
|
if let fieldKey = viewModel.radioButton.fieldKey {
|
||||||
|
self.fieldKey = fieldKey
|
||||||
|
}
|
||||||
|
|
||||||
|
//properties
|
||||||
|
isEnabled = viewModel.radioButton.enabled && !viewModel.radioButton.readOnly
|
||||||
|
isSelected = viewModel.radioButton.state
|
||||||
|
|
||||||
|
//forms
|
||||||
|
RadioButtonSelectionHelper.setupForRadioButtonGroup(viewModel.radioButton, self, delegateObject: delegateObject)
|
||||||
|
|
||||||
|
//events
|
||||||
|
viewModel.radioButton.updateUI = {
|
||||||
|
MVMCoreDispatchUtility.performBlock(onMainThread: { [weak self] in
|
||||||
|
guard let self = self else { return }
|
||||||
|
let isValid = viewModel.radioButton.isValid ?? true
|
||||||
|
showError = !isValid
|
||||||
|
errorText = viewModel.radioButton.errorMessage
|
||||||
|
isEnabled = viewModel.radioButton.enabled
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,8 +8,9 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import MVMCore
|
import MVMCore
|
||||||
|
import VDS
|
||||||
|
|
||||||
@objcMembers public class RadioButtonLabelModel: MoleculeModelProtocol {
|
@objcMembers public class RadioButtonLabelModel: MoleculeModelProtocol, ParentMoleculeModelProtocol {
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Properties
|
// MARK: - Properties
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -21,14 +22,23 @@ import MVMCore
|
|||||||
public var moleculeName: String = RadioButtonLabelModel.identifier
|
public var moleculeName: String = RadioButtonLabelModel.identifier
|
||||||
public var radioButton: RadioButtonModel
|
public var radioButton: RadioButtonModel
|
||||||
public var label: LabelModel
|
public var label: LabelModel
|
||||||
|
public var subTitle: LabelModel?
|
||||||
|
public var inverted: Bool? = false
|
||||||
|
public var surface: Surface { inverted ?? false ? .dark : .light }
|
||||||
|
|
||||||
|
public var children: [MoleculeModelProtocol] {
|
||||||
|
guard let subTitle else { return [radioButton, label] }
|
||||||
|
return [radioButton, label, subTitle]
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Initializer
|
// MARK: - Initializer
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
|
|
||||||
public init(radioButton: RadioButtonModel, label: LabelModel) {
|
public init(radioButton: RadioButtonModel, label: LabelModel, subTitle: LabelModel?) {
|
||||||
self.radioButton = radioButton
|
self.radioButton = radioButton
|
||||||
self.label = label
|
self.label = label
|
||||||
|
self.subTitle = subTitle
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user