update for changes discussed today

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-10-24 10:49:26 -05:00
parent 30530bc4e1
commit c075323ce7
2 changed files with 31 additions and 28 deletions

View File

@ -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<AnyCancellables>
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

View File

@ -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()
}