Make PillButton VDSMoleculeViewProtocol

This commit is contained in:
Scott Pfeil 2023-09-14 14:49:07 -04:00
parent 741bf0501b
commit c82a0cd4d6
2 changed files with 32 additions and 59 deletions

View File

@ -12,40 +12,18 @@ import VDS
import MVMCore
import Combine
open class PillButton: VDS.Button, MVMCoreUIViewConstrainingProtocol, MoleculeViewProtocol, MVMCoreViewProtocol, MFButtonProtocol {
open class PillButton: VDS.Button, MVMCoreUIViewConstrainingProtocol, MoleculeViewProtocol, MVMCoreViewProtocol, MFButtonProtocol, VDSMoleculeViewProtocol {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
open var model: MoleculeModelProtocol?
public var buttonModel: ButtonModel? {
get { model as? ButtonModel }
}
public var viewModel: ButtonModel!
public var delegateObject: MVMCoreUIDelegateObject?
public var additionalData: [AnyHashable: Any]?
internal var onClickCancellable: Cancellable?
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
required public init() {
super.init(frame: .zero)
}
public override init(frame: CGRect) {
super.init(frame: .zero)
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
}
open override func setup() {
super.setup()
setupView()
}
//--------------------------------------------------
// MARK: - Convenience
//--------------------------------------------------
@ -59,45 +37,45 @@ open class PillButton: VDS.Button, MVMCoreUIViewConstrainingProtocol, MoleculeVi
/// The primary styling for a button. Should be used for main buttons
open func stylePrimary() {
model = ButtonModel(primaryButtonWith: "", action: ActionNoopModel())
viewModel = ButtonModel(primaryButtonWith: "", action: ActionNoopModel())
use = .primary
}
/// The secondary styling for a button. Should be used for secondary buttons
open func styleSecondary() {
model = ButtonModel(secondaryButtonWith: "", action: ActionNoopModel())
viewModel = ButtonModel(secondaryButtonWith: "", action: ActionNoopModel())
use = .secondary
}
open override func setup() {
super.setup()
setupView()
}
//--------------------------------------------------
// MARK: - MoleculeViewProtocol
// MARK: - VDSMoleculeViewProtocol
//--------------------------------------------------
open func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
guard let model = model as? ButtonModel else { return }
self.model = model
if let accessibilityIdentifier = model.accessibilityIdentifier {
public func viewModelDidUpdate() {
if let accessibilityIdentifier = viewModel.accessibilityIdentifier {
self.accessibilityIdentifier = accessibilityIdentifier
}
text = model.title
isEnabled = model.enabled
size = model.size
// if let backgroundColor = model.backgroundColor {
// self.backgroundColor = backgroundColor.uiColor
// }
if let accessibilityText = model.accessibilityText {
text = viewModel.title
isEnabled = viewModel.enabled
size = viewModel.size
use = viewModel.style ?? .primary
if let accessibilityText = viewModel.accessibilityText {
accessibilityLabel = accessibilityText
}
FormValidator.setupValidation(for: model, delegate: delegateObject?.formHolderDelegate)
FormValidator.setupValidation(for: viewModel, delegate: delegateObject?.formHolderDelegate)
set(with: model.action, delegateObject: delegateObject, additionalData: additionalData)
set(with: viewModel.action, delegateObject: delegateObject, additionalData: additionalData)
model.updateUI = { [weak self] in
viewModel.updateUI = { [weak self] in
MVMCoreDispatchUtility.performBlock(onMainThread: {
self?.isEnabled = model.enabled
self?.viewModelDidUpdate()
})
}
}
@ -106,11 +84,6 @@ open class PillButton: VDS.Button, MVMCoreUIViewConstrainingProtocol, MoleculeVi
// MARK: - MVMCoreViewProtocol
//--------------------------------------------------
open override func reset() {
super.reset()
//backgroundColor = .clear
}
open class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
return (model as? ButtonModel)?.size.height
}
@ -130,13 +103,13 @@ open class PillButton: VDS.Button, MVMCoreUIViewConstrainingProtocol, MoleculeVi
//--------------------------------------------------
open func set(with actionModel: ActionModelProtocol?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
onClickCancellable = onClickSubscriber.publisher.sink { [weak self] _ in
guard let self = self,
let actionModel = self.buttonModel?.action else { return }
Task(priority: .userInitiated) {
try await Self.performButtonAction(with: actionModel, button: self, delegateObject: delegateObject, additionalData: additionalData, sourceModel: self.model)
onClickSubscriber = publisher(for: .touchUpInside)
.sink {[weak self] control in
guard let self = self else { return }
Task(priority: .userInitiated) {
try await Self.performButtonAction(with: self.viewModel.action, button: self, delegateObject: delegateObject, additionalData: additionalData, sourceModel: self.viewModel)
}
}
}
}
open class func performButtonAction(with model: ActionModelProtocol, button: MFButtonProtocol, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?, sourceModel: MoleculeModelProtocol? = nil) async throws {

View File

@ -22,7 +22,7 @@ extension VDS.Button.Size: Codable {
let container = try decoder.singleValueContainer()
let string = try container.decode(String.self)
switch string {
case VDS.Button.Size.large.rawValue:
case VDS.Button.Size.large.rawValue, "standard":
self = .large
case VDS.Button.Size.small.rawValue, "tiny":
self = .small