Compare commits

...

3 Commits

Author SHA1 Message Date
Matt Bruce
9c5f30b886 Signed-off-by: Matt Bruce <matt.bruce@verizon.com> 2022-10-20 15:58:40 -05:00
Matt Bruce
627a1eddac comments
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-10-13 10:24:39 -05:00
Matt Bruce
9791426c18 name change
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-10-13 09:46:26 -05:00
4 changed files with 98 additions and 126 deletions

View File

@ -22,18 +22,19 @@
{ {
"moleculeName": "stackItem", "moleculeName": "stackItem",
"molecule": { "molecule": {
"moleculeName": "testToggle3" "moleculeName": "toggle"
} }
}, },
{ {
"moleculeName": "stackItem", "moleculeName": "stackItem",
"molecule": { "molecule": {
"moleculeName": "testLabelToggle", "moleculeName": "labelToggle",
"label": { "label": {
"moleculeName": "label",
"text": "isActive" "text": "isActive"
}, },
"toggle": { "toggle": {
"moleculeName": "testToggle1", "moleculeName": "toggle",
"fieldKey": "isActive" "fieldKey": "isActive"
} }
} }

View File

@ -11,26 +11,6 @@ import MVMCoreUI
import UIKit import UIKit
import VDS import VDS
///-----------------------------------------------------------------------------
///MARK: -- VDSMoleculeViewProtocol (Contract between VDS -> Atomic)
///-----------------------------------------------------------------------------
public protocol VDSMoleculeViewProtocol: MoleculeViewProtocol, MVMCoreViewProtocol {
associatedtype ViewModel: MoleculeModelProtocol
var viewModel: ViewModel! { get set }
var delegateObject: MVMCoreUIDelegateObject? { get set }
var additionalData: [AnyHashable: Any]? { get set }
func viewModelDidUpdate()
}
extension VDSMoleculeViewProtocol {
public func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
guard let castedModel = model as? ViewModel else { return }
self.delegateObject = delegateObject
viewModel = castedModel
viewModelDidUpdate()
}
}
///----------------------------------------------------------------------------- ///-----------------------------------------------------------------------------
///MARK: -- TestToggleModel Extension ///MARK: -- TestToggleModel Extension
///----------------------------------------------------------------------------- ///-----------------------------------------------------------------------------
@ -96,7 +76,6 @@ open class TestToggle: ToggleBase<DefaultToggleModel>, VDSMoleculeViewProtocol {
open func viewModelDidUpdate() { open func viewModelDidUpdate() {
guard let viewModel else { return } guard let viewModel else { return }
FormValidator.setupValidation(for: viewModel, delegate: delegateObject?.formHolderDelegate)
additionalData = additionalData.dictionaryAdding(key: KeySourceModel, value: viewModel) additionalData = additionalData.dictionaryAdding(key: KeySourceModel, value: viewModel)
set(with: viewModel.getVDSModel()) set(with: viewModel.getVDSModel())
} }

View File

@ -11,6 +11,9 @@ import MVMCore
import MVMCoreUI import MVMCoreUI
import VDS import VDS
///-----------------------------------------------------------------------------
///File contains update Atomic Toggle / Model
///-----------------------------------------------------------------------------
/// This is a mixed model of Atomic + VDS.ToggleModel that will be used with the Control /// This is a mixed model of Atomic + VDS.ToggleModel that will be used with the Control
/// There is no requirement for syncing since it is 1 model /// There is no requirement for syncing since it is 1 model
@ -195,7 +198,6 @@ open class TestToggle3: ToggleViewModelHandlerBase<ToggleViewModelBase<TestToggl
open func updateView(_ size: CGFloat) {} open func updateView(_ size: CGFloat) {}
open func viewModelDidSet() { open func viewModelDidSet() {
FormValidator.setupValidation(for: viewModel.model, delegate: delegateObject?.formHolderDelegate)
additionalData = additionalData.dictionaryAdding(key: KeySourceModel, value: viewModel) additionalData = additionalData.dictionaryAdding(key: KeySourceModel, value: viewModel)
} }

View File

@ -13,6 +13,10 @@ import VDS
import MVMCore import MVMCore
import MVMCoreUI import MVMCoreUI
///-----------------------------------------------------------------------------
///File contains update VDS Protocols / Control / ToggleModel / Toggle
///-----------------------------------------------------------------------------
///----------------------------------------------------------------------------- ///-----------------------------------------------------------------------------
///MARK: -- VDSVMMoleculeViewProtocol (Contract between VDS -> Atomic ///MARK: -- VDSVMMoleculeViewProtocol (Contract between VDS -> Atomic
///----------------------------------------------------------------------------- ///-----------------------------------------------------------------------------
@ -41,8 +45,7 @@ public protocol ViewModelHandler: AnyObject, Initable {
var subscribers: Set<AnyCancellable> { get set } var subscribers: Set<AnyCancellable> { get set }
init(with model: ModelType) init(with model: ModelType)
func set(with model: ModelType) func set(with model: ModelType)
func shouldUpdateView(viewModel: ModelType) -> Bool func updateView()
func updateView(viewModel: ModelType)
} }
extension ViewModelHandler { extension ViewModelHandler {
@ -52,20 +55,14 @@ extension ViewModelHandler {
} }
public func set(with model: ModelType) { public func set(with model: ModelType) {
if shouldUpdateView(viewModel: model){
viewModel.set(with: model) viewModel.set(with: model)
} }
}
public func shouldUpdateView(viewModel: ModelType) -> Bool {
self.viewModel.model != viewModel
}
public func setupUpdateView() { public func setupUpdateView() {
handlerPublisher() handlerPublisher()
.subscribe(on: RunLoop.main) .subscribe(on: RunLoop.main)
.sink { [weak self] viewModel in .sink { [weak self] _ in
self?.updateView(viewModel: viewModel) self?.updateView()
} }
.store(in: &subscribers) .store(in: &subscribers)
} }
@ -83,37 +80,45 @@ extension ViewModelHandler {
///----------------------------------------------------------------------------- ///-----------------------------------------------------------------------------
public protocol ViewModel<ModelType>: AnyObject, Surfaceable, Disabling { public protocol ViewModel<ModelType>: AnyObject, Surfaceable, Disabling {
associatedtype ModelType: Modelable associatedtype ModelType: Modelable
var model: ModelType { get set } var model: ModelType { get }
var modelSubject: CurrentValueSubject<ModelType, Never> { get set } var modelSubject: CurrentValueSubject<ModelType, Never> { get set }
var publisher: AnyPublisher<ModelType, Never> { get } var publisher: AnyPublisher<ModelType, Never> { get }
init(with model: ModelType) init(with model: ModelType)
func set(with model: ModelType) func set(with model: ModelType)
func shouldUpdateView(model: ModelType) -> Bool
func modelDidUpdate()
} }
///----------------------------------------------------------------------------- ///-----------------------------------------------------------------------------
///MARK: -- ViewModel Generic Base Class ///MARK: -- ViewModel Generic Base Class
///----------------------------------------------------------------------------- ///-----------------------------------------------------------------------------
public class ViewModelBase<ModelType: Modelable>: NSObject, ViewModel, ObservableObject { public class ViewModelBase<ModelType: Modelable>: NSObject, ViewModel {
public var model: ModelType public var model: ModelType
public var modelSubject = CurrentValueSubject<ModelType, Never>(ModelType()) public var modelSubject = CurrentValueSubject<ModelType, Never>(ModelType())
public var publisher: AnyPublisher<ModelType, Never> { modelSubject.eraseToAnyPublisher() } public var publisher: AnyPublisher<ModelType, Never> { modelSubject.eraseToAnyPublisher() }
required public init(with model: ModelType) { required public init(with model: ModelType) {
self.model = model self.model = model
modelSubject.send(model) super.init()
self.modelDidUpdate()
} }
public func set(with model: ModelType){ public func set(with model: ModelType){
if shouldUpdateView(model: model) {
self.model = model self.model = model
modelSubject.send(model) modelDidUpdate()
}
} }
@Proxy(\.model.surface) @Proxy(\.model.surface)
open var surface: Surface { didSet { modelSubject.send(model) }} open var surface: Surface { didSet { modelDidUpdate() }}
@Proxy(\.model.disabled) @Proxy(\.model.disabled)
open var disabled: Bool { didSet { modelSubject.send(model) }} open var disabled: Bool { didSet { modelDidUpdate() }}
open func shouldUpdateView(model: ModelType) -> Bool { self.model != model }
open func modelDidUpdate() { modelSubject.send(model) }
} }
///----------------------------------------------------------------------------- ///-----------------------------------------------------------------------------
@ -201,15 +206,15 @@ open class ControlViewModelHandler<ViewModelType: ViewModel>: UIControl, ViewMod
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Overrides // MARK: - Overrides
//-------------------------------------------------- //--------------------------------------------------
open func updateView(viewModel: ModelType) { open func updateView() {
fatalError("Implement updateView") fatalError("Implement updateView")
} }
open func reset() { open func reset() {
backgroundColor = .clear backgroundColor = .clear
// if let model = model as? Resettable { if let resetable = viewModel as? Resettable {
// model.reset() resetable.reset()
// } }
} }
// MARK: - ViewProtocol // MARK: - ViewProtocol
@ -236,6 +241,10 @@ public protocol ToggleViewModel: ViewModel where ModelType: VDS.ToggleModel {
var textSize: ToggleTextSize { get set } var textSize: ToggleTextSize { get set }
var textWeight: ToggleTextWeight { get set } var textWeight: ToggleTextWeight { get set }
var textPosition: ToggleTextPosition { get set } var textPosition: ToggleTextPosition { get set }
var labelModel: DefaultLabelModel { get }
var toggleColor: UIColor { get }
var knobColor: UIColor { get }
} }
///----------------------------------------------------------------------------- ///-----------------------------------------------------------------------------
@ -244,25 +253,53 @@ public protocol ToggleViewModel: ViewModel where ModelType: VDS.ToggleModel {
public class ToggleViewModelBase<ModelType: VDS.ToggleModel>: ViewModelBase<ModelType>, ToggleViewModel { public class ToggleViewModelBase<ModelType: VDS.ToggleModel>: ViewModelBase<ModelType>, ToggleViewModel {
@Proxy(\.model.on) @Proxy(\.model.on)
open var isOn: Bool { didSet { modelSubject.send(model) }} open var isOn: Bool { didSet { modelDidUpdate() }}
@Proxy(\.model.showText) @Proxy(\.model.showText)
public var showText: Bool { didSet { modelSubject.send(model) }} public var showText: Bool { didSet { modelDidUpdate() }}
@Proxy(\.model.onText) @Proxy(\.model.onText)
public var onText: String { didSet { modelSubject.send(model) }} public var onText: String { didSet { modelDidUpdate() }}
@Proxy(\.model.offText) @Proxy(\.model.offText)
public var offText: String { didSet { modelSubject.send(model) }} public var offText: String { didSet { modelDidUpdate() }}
@Proxy(\.model.textSize) @Proxy(\.model.textSize)
public var textSize: ToggleTextSize { didSet { modelSubject.send(model) }} public var textSize: ToggleTextSize { didSet { modelDidUpdate() }}
@Proxy(\.model.textWeight) @Proxy(\.model.textWeight)
public var textWeight: ToggleTextWeight { didSet { modelSubject.send(model) }} public var textWeight: ToggleTextWeight { didSet { modelDidUpdate() }}
@Proxy(\.model.textPosition) @Proxy(\.model.textPosition)
public var textPosition: ToggleTextPosition { didSet { modelSubject.send(model) }} public var textPosition: ToggleTextPosition { didSet { modelDidUpdate() }}
public var labelModel: DefaultLabelModel { model.labelModel }
public var toggleColor: UIColor { toggleColorConfiguration.getColor(model) }
public var knobColor: UIColor { knobColorConfiguration.getColor(model) }
private var toggleColorConfiguration = BinaryDisabledSurfaceColorConfiguration().with {
$0.forTrue.enabled.lightColor = VDSColor.paletteGreen26
$0.forTrue.enabled.darkColor = VDSColor.paletteGreen34
$0.forTrue.disabled.lightColor = VDSColor.interactiveDisabledOnlight
$0.forTrue.disabled.darkColor = VDSColor.interactiveDisabledOndark
$0.forFalse.enabled.lightColor = VDSColor.elementsSecondaryOnlight
$0.forFalse.enabled.darkColor = VDSColor.paletteGray44
$0.forFalse.disabled.lightColor = VDSColor.interactiveDisabledOnlight
$0.forFalse.disabled.darkColor = VDSColor.interactiveDisabledOndark
}
private var knobColorConfiguration = BinaryDisabledSurfaceColorConfiguration().with {
$0.forTrue.enabled.lightColor = VDSColor.elementsPrimaryOndark
$0.forTrue.enabled.darkColor = VDSColor.elementsPrimaryOndark
$0.forTrue.disabled.lightColor = VDSColor.paletteGray95
$0.forTrue.disabled.darkColor = VDSColor.paletteGray44
$0.forFalse.enabled.lightColor = VDSColor.elementsPrimaryOndark
$0.forFalse.enabled.darkColor = VDSColor.elementsPrimaryOndark
$0.forFalse.disabled.lightColor = VDSColor.paletteGray95
$0.forFalse.disabled.darkColor = VDSColor.paletteGray44
}
} }
///----------------------------------------------------------------------------- ///-----------------------------------------------------------------------------
@ -304,28 +341,6 @@ open class ToggleViewModelHandlerBase<ViewModelType: ToggleViewModel>: ControlVi
public let toggleContainerSize = CGSize(width: 52, height: 44) public let toggleContainerSize = CGSize(width: 52, height: 44)
public let knobSize = CGSize(width: 20, height: 20) public let knobSize = CGSize(width: 20, height: 20)
private var toggleColorConfiguration = BinaryDisabledSurfaceColorConfiguration().with {
$0.forTrue.enabled.lightColor = VDSColor.paletteGreen26
$0.forTrue.enabled.darkColor = VDSColor.paletteGreen34
$0.forTrue.disabled.lightColor = VDSColor.interactiveDisabledOnlight
$0.forTrue.disabled.darkColor = VDSColor.interactiveDisabledOndark
$0.forFalse.enabled.lightColor = VDSColor.elementsSecondaryOnlight
$0.forFalse.enabled.darkColor = VDSColor.paletteGray44
$0.forFalse.disabled.lightColor = VDSColor.interactiveDisabledOnlight
$0.forFalse.disabled.darkColor = VDSColor.interactiveDisabledOndark
}
private var knobColorConfiguration = BinaryDisabledSurfaceColorConfiguration().with {
$0.forTrue.enabled.lightColor = VDSColor.elementsPrimaryOndark
$0.forTrue.enabled.darkColor = VDSColor.elementsPrimaryOndark
$0.forTrue.disabled.lightColor = VDSColor.paletteGray95
$0.forTrue.disabled.darkColor = VDSColor.paletteGray44
$0.forFalse.enabled.lightColor = VDSColor.elementsPrimaryOndark
$0.forFalse.enabled.darkColor = VDSColor.elementsPrimaryOndark
$0.forFalse.disabled.lightColor = VDSColor.paletteGray95
$0.forFalse.disabled.darkColor = VDSColor.paletteGray44
}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Public Properties // MARK: - Public Properties
//-------------------------------------------------- //--------------------------------------------------
@ -364,26 +379,26 @@ open class ToggleViewModelHandlerBase<ViewModelType: ToggleViewModel>: ControlVi
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Toggle // MARK: - Toggle
//-------------------------------------------------- //--------------------------------------------------
private func updateToggle(_ viewModel: ModelType) { private func updateToggle() {
//private func //private func
func constrainKnob(){ func constrainKnob(){
self.knobLeadingConstraint?.isActive = false knobLeadingConstraint?.isActive = false
self.knobTrailingConstraint?.isActive = false knobTrailingConstraint?.isActive = false
if viewModel.on { if viewModel.isOn {
self.knobTrailingConstraint = self.toggleView.trailingAnchor.constraint(equalTo: self.knobView.trailingAnchor, constant: 2) knobTrailingConstraint = toggleView.trailingAnchor.constraint(equalTo: knobView.trailingAnchor, constant: 2)
self.knobLeadingConstraint = self.knobView.leadingAnchor.constraint(greaterThanOrEqualTo: self.toggleView.leadingAnchor) knobLeadingConstraint = knobView.leadingAnchor.constraint(greaterThanOrEqualTo: toggleView.leadingAnchor)
} else { } else {
self.knobTrailingConstraint = self.toggleView.trailingAnchor.constraint(greaterThanOrEqualTo: self.knobView.trailingAnchor) knobTrailingConstraint = toggleView.trailingAnchor.constraint(greaterThanOrEqualTo: knobView.trailingAnchor)
self.knobLeadingConstraint = self.knobView.leadingAnchor.constraint(equalTo: self.toggleView.leadingAnchor, constant: 2) knobLeadingConstraint = knobView.leadingAnchor.constraint(equalTo: toggleView.leadingAnchor, constant: 2)
} }
self.knobTrailingConstraint?.isActive = true knobTrailingConstraint?.isActive = true
self.knobLeadingConstraint?.isActive = true knobLeadingConstraint?.isActive = true
self.knobWidthConstraint?.constant = self.knobSize.width knobWidthConstraint?.constant = knobSize.width
self.layoutIfNeeded() layoutIfNeeded()
} }
let toggleColor = toggleColorConfiguration.getColor(viewModel) let toggleColor = viewModel.toggleColor
let knobColor = knobColorConfiguration.getColor(viewModel) let knobColor = viewModel.knobColor
if viewModel.disabled { if viewModel.disabled {
toggleView.backgroundColor = toggleColor toggleView.backgroundColor = toggleColor
@ -404,7 +419,7 @@ open class ToggleViewModelHandlerBase<ViewModelType: ToggleViewModel>: ControlVi
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Labels // MARK: - Labels
//-------------------------------------------------- //--------------------------------------------------
private func updateLabel(_ viewModel: ModelType) { private func updateLabel() {
let showText = viewModel.showText let showText = viewModel.showText
stackView.spacing = showText ? 12 : 0 stackView.spacing = showText ? 12 : 0
label.set(with: viewModel.labelModel) label.set(with: viewModel.labelModel)
@ -456,7 +471,7 @@ open class ToggleViewModelHandlerBase<ViewModelType: ToggleViewModel>: ControlVi
toggleView.layer.cornerRadius = toggleSize.height / 2.0 toggleView.layer.cornerRadius = toggleSize.height / 2.0
knobView.layer.cornerRadius = knobSize.height / 2.0 knobView.layer.cornerRadius = knobSize.height / 2.0
toggleView.backgroundColor = toggleColorConfiguration.getColor(viewModel.model) toggleView.backgroundColor = viewModel.toggleColor
toggleContainerView.addSubview(toggleView) toggleContainerView.addSubview(toggleView)
toggleView.addSubview(knobView) toggleView.addSubview(knobView)
@ -471,11 +486,11 @@ open class ToggleViewModelHandlerBase<ViewModelType: ToggleViewModel>: ControlVi
toggleView.bottomAnchor.constraint(greaterThanOrEqualTo: knobView.bottomAnchor).isActive = true toggleView.bottomAnchor.constraint(greaterThanOrEqualTo: knobView.bottomAnchor).isActive = true
updateLabel(viewModel.model) updateLabel()
stackView.addArrangedSubview(toggleContainerView) stackView.addArrangedSubview(toggleContainerView)
stackView.topAnchor.constraint(equalTo: topAnchor).isActive = true stackView.topAnchor.constraint(equalTo: topAnchor).isActive = true
stackView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true stackView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
stackView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true stackView.widthAnchor.constraint(greaterThanOrEqualToConstant: toggleContainerSize.width).isActive = true
stackView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true stackView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
toggleView.centerXAnchor.constraint(equalTo: toggleContainerView.centerXAnchor).isActive = true toggleView.centerXAnchor.constraint(equalTo: toggleContainerView.centerXAnchor).isActive = true
@ -485,8 +500,8 @@ open class ToggleViewModelHandlerBase<ViewModelType: ToggleViewModel>: ControlVi
public override func reset() { public override func reset() {
super.reset() super.reset()
toggleView.backgroundColor = toggleColorConfiguration.getColor(viewModel.model) toggleView.backgroundColor = viewModel.toggleColor
knobView.backgroundColor = knobColorConfiguration.getColor(viewModel.model) knobView.backgroundColor = viewModel.knobColor
} }
/// This will toggle the state of the Toggle and execute the actionBlock if provided. /// This will toggle the state of the Toggle and execute the actionBlock if provided.
@ -498,38 +513,13 @@ open class ToggleViewModelHandlerBase<ViewModelType: ToggleViewModel>: ControlVi
//-------------------------------------------------- //--------------------------------------------------
// MARK: - State // MARK: - State
//-------------------------------------------------- //--------------------------------------------------
open override func updateView(viewModel: ModelType) { open override func updateView() {
updateLabel(viewModel) updateLabel()
updateToggle(viewModel) updateToggle()
backgroundColor = viewModel.surface.color backgroundColor = viewModel.surface.color
setNeedsLayout() setNeedsLayout()
layoutIfNeeded() layoutIfNeeded()
} //viewModel.model = DefaultToggleModel()
//viewModel.model.disabled = true
public func set(with model: ModelType) {
if shouldUpdateView(viewModel: model){
viewModel.set(with: model)
} }
} }
public func shouldUpdateView(viewModel: ModelType) -> Bool {
self.viewModel.model != viewModel
}
public func setupUpdateView() {
handlerPublisher()
.subscribe(on: RunLoop.main)
.sink { [weak self] viewModel in
self?.updateView(viewModel: viewModel)
}
.store(in: &subscribers)
}
public func handlerPublisher() -> AnyPublisher<ModelType, Never> {
viewModel
.publisher
.debounce(for: .seconds(Constants.ModelStateDebounce), scheduler: RunLoop.main)
.eraseToAnyPublisher()
}
}