diff --git a/MVMCoreUI/Atomic/Atoms/Selectors/Toggle.swift b/MVMCoreUI/Atomic/Atoms/Selectors/Toggle.swift index 9a977b40..16072b89 100644 --- a/MVMCoreUI/Atomic/Atoms/Selectors/Toggle.swift +++ b/MVMCoreUI/Atomic/Atoms/Selectors/Toggle.swift @@ -9,6 +9,7 @@ import MVMCore import UIKit import VDS +import Combine /** A custom implementation of Apple's UISwitch. @@ -25,8 +26,8 @@ open class Toggle: ToggleBase, VDSMoleculeViewProtocol { public var viewModel: ToggleModel! public var delegateObject: MVMCoreUIDelegateObject? public var additionalData: [AnyHashable: Any]? - public var didToggleAction: ActionBlock? - + public var valueChangedCancellable: AnyCancellable? + //-------------------------------------------------- // MARK: - Initializers //-------------------------------------------------- @@ -34,22 +35,37 @@ open class Toggle: ToggleBase, VDSMoleculeViewProtocol { self.init(frame: .zero) self.isOn = isOn } - + open override func initialSetup() { super.initialSetup() - + publisher(for: .touchUpInside) .sink {[weak self] toggle in guard let self = self else { return } self.toggle() }.store(in: &subscribers) + //this is logic that will always need to be run + //and is added into the array of Set publisher(for: .valueChanged) .sink {[weak self] _ in - guard let self = self else { return } - self.valueChanged() + guard let self = self, let viewModel = self.viewModel else { return } + //sync the value on the viewModel + viewModel.selected = self.isOn + + //tell the form you changed + _ = FormValidator.validate(delegate: self.delegateObject?.formHolderDelegate) + }.store(in: &subscribers) - + + //register the defaultActionExecuter + //this can then be overwritten by a subclass + valueChangedCancellable = publisher(for: .valueChanged) + .sink {[weak self] _ in + guard let self = self else { return } + self.executeDefaultAction() + } + accessibilityLabelEnabled = MVMCoreUIUtility.hardcodedString(withKey: "Toggle_buttonlabel") accessibilityLabelDisabled = MVMCoreUIUtility.hardcodedString(withKey: "Toggle_buttonlabel") accessibilityHintEnabled = MVMCoreUIUtility.hardcodedString(withKey: "AccToggleHint") @@ -57,9 +73,9 @@ open class Toggle: ToggleBase, VDSMoleculeViewProtocol { accessibilityValueEnabled = MVMCoreUIUtility.hardcodedString(withKey: "AccOn") accessibilityValueDisabled = MVMCoreUIUtility.hardcodedString(withKey: "AccOff") } - + open func updateView(_ size: CGFloat) {} - + open override func updateView() { super.updateView() backgroundColor = .clear @@ -67,20 +83,13 @@ open class Toggle: ToggleBase, VDSMoleculeViewProtocol { open func viewModelDidUpdate() { guard let viewModel else { return } + + //send toggle.model to the Form FormValidator.setupValidation(for: viewModel, delegate: delegateObject?.formHolderDelegate) - additionalData = additionalData.dictionaryAdding(key: KeySourceModel, value: viewModel) } - private func valueChanged(){ + public func executeDefaultAction() { guard let viewModel else { return } - //sync the value on the viewModel - viewModel.selected = isOn - - //tell the form you changed - _ = FormValidator.validate(delegate: self.delegateObject?.formHolderDelegate) - - didToggleAction?() - if viewModel.action != nil || viewModel.alternateAction != nil { var action: ActionModelProtocol? if isOn { @@ -97,19 +106,12 @@ open class Toggle: ToggleBase, VDSMoleculeViewProtocol { } } + //Return the same height as the internal ToggleBase.toggleContainerSize.height + //since this is a class func, we can't reference it directly public static func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? { return 44 } - private typealias ActionDefinition = (model: ActionModelProtocol, - sourceModel: MoleculeModelProtocol?) - - private func performActionUnstructured(definition: ActionDefinition) { - MVMCoreUIActionHandler.performActionUnstructured(with: definition.model, - sourceModel: definition.sourceModel, - additionalData: additionalData, - delegateObject: delegateObject) - } } // MARK: - MVMCoreUIViewConstrainingProtocol diff --git a/MVMCoreUI/Atomic/Protocols/VDSMoleculeViewProtocol.swift b/MVMCoreUI/Atomic/Protocols/VDSMoleculeViewProtocol.swift index 0a6f3179..41349f2c 100644 --- a/MVMCoreUI/Atomic/Protocols/VDSMoleculeViewProtocol.swift +++ b/MVMCoreUI/Atomic/Protocols/VDSMoleculeViewProtocol.swift @@ -24,6 +24,7 @@ extension VDSMoleculeViewProtocol { public func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) { guard let castedModel = model as? ViewModel else { return } self.delegateObject = delegateObject + self.additionalData = additionalData viewModel = castedModel viewModelDidUpdate() }