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 MVMCore
import Combine import Combine
open class PillButton: VDS.Button, MVMCoreUIViewConstrainingProtocol, MoleculeViewProtocol, MVMCoreViewProtocol, MFButtonProtocol { open class PillButton: VDS.Button, MVMCoreUIViewConstrainingProtocol, MoleculeViewProtocol, MVMCoreViewProtocol, MFButtonProtocol, VDSMoleculeViewProtocol {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Properties // MARK: - Properties
//-------------------------------------------------- //--------------------------------------------------
open var model: MoleculeModelProtocol? public var viewModel: ButtonModel!
public var buttonModel: ButtonModel? { public var delegateObject: MVMCoreUIDelegateObject?
get { model as? ButtonModel } public var additionalData: [AnyHashable: Any]?
}
internal var onClickCancellable: Cancellable? 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 // 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 /// The primary styling for a button. Should be used for main buttons
open func stylePrimary() { open func stylePrimary() {
model = ButtonModel(primaryButtonWith: "", action: ActionNoopModel()) viewModel = ButtonModel(primaryButtonWith: "", action: ActionNoopModel())
use = .primary use = .primary
} }
/// The secondary styling for a button. Should be used for secondary buttons /// The secondary styling for a button. Should be used for secondary buttons
open func styleSecondary() { open func styleSecondary() {
model = ButtonModel(secondaryButtonWith: "", action: ActionNoopModel()) viewModel = ButtonModel(secondaryButtonWith: "", action: ActionNoopModel())
use = .secondary use = .secondary
} }
open override func setup() {
super.setup()
setupView()
}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - MoleculeViewProtocol // MARK: - VDSMoleculeViewProtocol
//-------------------------------------------------- //--------------------------------------------------
open func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) { public func viewModelDidUpdate() {
guard let model = model as? ButtonModel else { return } if let accessibilityIdentifier = viewModel.accessibilityIdentifier {
self.model = model
if let accessibilityIdentifier = model.accessibilityIdentifier {
self.accessibilityIdentifier = accessibilityIdentifier self.accessibilityIdentifier = accessibilityIdentifier
} }
text = model.title text = viewModel.title
isEnabled = model.enabled isEnabled = viewModel.enabled
size = model.size size = viewModel.size
// if let backgroundColor = model.backgroundColor { use = viewModel.style ?? .primary
// self.backgroundColor = backgroundColor.uiColor if let accessibilityText = viewModel.accessibilityText {
// }
if let accessibilityText = model.accessibilityText {
accessibilityLabel = 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: { MVMCoreDispatchUtility.performBlock(onMainThread: {
self?.isEnabled = model.enabled self?.viewModelDidUpdate()
}) })
} }
} }
@ -106,11 +84,6 @@ open class PillButton: VDS.Button, MVMCoreUIViewConstrainingProtocol, MoleculeVi
// MARK: - MVMCoreViewProtocol // MARK: - MVMCoreViewProtocol
//-------------------------------------------------- //--------------------------------------------------
open override func reset() {
super.reset()
//backgroundColor = .clear
}
open class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? { open class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
return (model as? ButtonModel)?.size.height 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]?) { open func set(with actionModel: ActionModelProtocol?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
onClickCancellable = onClickSubscriber.publisher.sink { [weak self] _ in onClickSubscriber = publisher(for: .touchUpInside)
guard let self = self, .sink {[weak self] control in
let actionModel = self.buttonModel?.action else { return } guard let self = self else { return }
Task(priority: .userInitiated) { Task(priority: .userInitiated) {
try await Self.performButtonAction(with: actionModel, button: self, delegateObject: delegateObject, additionalData: additionalData, sourceModel: self.model) 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 { 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 container = try decoder.singleValueContainer()
let string = try container.decode(String.self) let string = try container.decode(String.self)
switch string { switch string {
case VDS.Button.Size.large.rawValue: case VDS.Button.Size.large.rawValue, "standard":
self = .large self = .large
case VDS.Button.Size.small.rawValue, "tiny": case VDS.Button.Size.small.rawValue, "tiny":
self = .small self = .small