refactored to only add actions when the model has them

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-02-14 09:17:50 -06:00
parent fdabe5eb86
commit 2bc62fb268

View File

@ -9,6 +9,7 @@
import Foundation
import MVMCore
import VDS
import Combine
/**
This class expects its height and width to be equal.
@ -21,6 +22,11 @@ open class Tilelet: VDS.Tilelet, VDSMoleculeViewProtocol{
public var delegateObject: MVMCoreUIDelegateObject?
public var additionalData: [AnyHashable: Any]?
//even though we have a local set, others could be
//subscribing to other events, we use this one specically
//for the action so that we can ensure there is only 1 being used
private var actionSubscriber: AnyCancellable?
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
@ -28,23 +34,6 @@ open class Tilelet: VDS.Tilelet, VDSMoleculeViewProtocol{
self.init(frame: .zero)
}
open override func initialSetup() {
super.initialSetup()
publisher(for: .touchUpInside)
.sink {[weak self] control in
self?.executeAction()
}.store(in: &subscribers)
}
//--------------------------------------------------
// MARK: - Private
//--------------------------------------------------
private func executeAction(){
if let action = viewModel.action {
MVMCoreUIActionHandler.performActionUnstructured(with: action, sourceModel: viewModel, additionalData: additionalData, delegateObject: delegateObject)
}
}
//--------------------------------------------------
// MARK: - Public
//--------------------------------------------------
@ -60,6 +49,23 @@ open class Tilelet: VDS.Tilelet, VDSMoleculeViewProtocol{
badgeModel = viewModel.badge
descriptiveIconModel = viewModel.descriptiveIcon
directionalIconModel = viewModel.directionalIcon
//setup action
if let action = viewModel.action {
//add the subscriber
actionSubscriber = publisher(for: .touchUpInside)
.sink {[weak self] control in
guard let self else { return }
MVMCoreUIActionHandler.performActionUnstructured(with: action,
sourceModel: self.viewModel,
additionalData: self.additionalData,
delegateObject: self.delegateObject)
}
}//if there is no action but there was a subscriber, kill it
else if let actionSubscriber {
actionSubscriber.cancel()
self.actionSubscriber = nil
}
}
//--------------------------------------------------