diff --git a/MVMCoreUI/Atomic/Atoms/Views/Tilelet.swift b/MVMCoreUI/Atomic/Atoms/Views/Tilelet.swift index 838f3d4f..72b3a07a 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/Tilelet.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/Tilelet.swift @@ -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 + } } //--------------------------------------------------