comments
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
9791426c18
commit
627a1eddac
@ -11,6 +11,9 @@ import MVMCore
|
||||
import MVMCoreUI
|
||||
import VDS
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///File contains update Atomic Toggle / Model
|
||||
///-----------------------------------------------------------------------------
|
||||
|
||||
/// 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
|
||||
|
||||
@ -13,6 +13,10 @@ import VDS
|
||||
import MVMCore
|
||||
import MVMCoreUI
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///File contains update VDS Protocols / Control / ToggleModel / Toggle
|
||||
///-----------------------------------------------------------------------------
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///MARK: -- VDSVMMoleculeViewProtocol (Contract between VDS -> Atomic
|
||||
///-----------------------------------------------------------------------------
|
||||
@ -41,8 +45,7 @@ public protocol ViewModelHandler: AnyObject, Initable {
|
||||
var subscribers: Set<AnyCancellable> { get set }
|
||||
init(with model: ModelType)
|
||||
func set(with model: ModelType)
|
||||
func shouldUpdateView(model: ModelType) -> Bool
|
||||
func updateView(model: ModelType)
|
||||
func updateView()
|
||||
}
|
||||
|
||||
extension ViewModelHandler {
|
||||
@ -52,20 +55,14 @@ extension ViewModelHandler {
|
||||
}
|
||||
|
||||
public func set(with model: ModelType) {
|
||||
if shouldUpdateView(model: model){
|
||||
viewModel.set(with: model)
|
||||
}
|
||||
}
|
||||
|
||||
public func shouldUpdateView(model: ModelType) -> Bool {
|
||||
self.viewModel.model != model
|
||||
viewModel.set(with: model)
|
||||
}
|
||||
|
||||
public func setupUpdateView() {
|
||||
handlerPublisher()
|
||||
.subscribe(on: RunLoop.main)
|
||||
.sink { [weak self] model in
|
||||
self?.updateView(model: model)
|
||||
.sink { [weak self] _ in
|
||||
self?.updateView()
|
||||
}
|
||||
.store(in: &subscribers)
|
||||
}
|
||||
@ -88,6 +85,8 @@ public protocol ViewModel<ModelType>: AnyObject, Surfaceable, Disabling {
|
||||
var publisher: AnyPublisher<ModelType, Never> { get }
|
||||
init(with model: ModelType)
|
||||
func set(with model: ModelType)
|
||||
func shouldUpdateView(model: ModelType) -> Bool
|
||||
func modelDidUpdate()
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
@ -100,20 +99,26 @@ public class ViewModelBase<ModelType: Modelable>: NSObject, ViewModel, Observabl
|
||||
|
||||
required public init(with model: ModelType) {
|
||||
self.model = model
|
||||
modelSubject.send(model)
|
||||
super.init()
|
||||
self.modelDidUpdate()
|
||||
}
|
||||
|
||||
public func set(with model: ModelType){
|
||||
self.model = model
|
||||
modelSubject.send(model)
|
||||
if shouldUpdateView(model: model) {
|
||||
self.model = model
|
||||
modelDidUpdate()
|
||||
}
|
||||
}
|
||||
|
||||
@Proxy(\.model.surface)
|
||||
open var surface: Surface { didSet { modelSubject.send(model) }}
|
||||
open var surface: Surface { didSet { modelDidUpdate() }}
|
||||
|
||||
@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
|
||||
//--------------------------------------------------
|
||||
open func updateView(model: ModelType) {
|
||||
open func updateView() {
|
||||
fatalError("Implement updateView")
|
||||
}
|
||||
|
||||
open func reset() {
|
||||
backgroundColor = .clear
|
||||
// if let model = model as? Resettable {
|
||||
// model.reset()
|
||||
// }
|
||||
if let resetable = viewModel as? Resettable {
|
||||
resetable.reset()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - ViewProtocol
|
||||
@ -236,6 +241,10 @@ public protocol ToggleViewModel: ViewModel where ModelType: VDS.ToggleModel {
|
||||
var textSize: ToggleTextSize { get set }
|
||||
var textWeight: ToggleTextWeight { 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 {
|
||||
|
||||
@Proxy(\.model.on)
|
||||
open var isOn: Bool { didSet { modelSubject.send(model) }}
|
||||
open var isOn: Bool { didSet { modelDidUpdate() }}
|
||||
|
||||
@Proxy(\.model.showText)
|
||||
public var showText: Bool { didSet { modelSubject.send(model) }}
|
||||
public var showText: Bool { didSet { modelDidUpdate() }}
|
||||
|
||||
@Proxy(\.model.onText)
|
||||
public var onText: String { didSet { modelSubject.send(model) }}
|
||||
public var onText: String { didSet { modelDidUpdate() }}
|
||||
|
||||
@Proxy(\.model.offText)
|
||||
public var offText: String { didSet { modelSubject.send(model) }}
|
||||
public var offText: String { didSet { modelDidUpdate() }}
|
||||
|
||||
@Proxy(\.model.textSize)
|
||||
public var textSize: ToggleTextSize { didSet { modelSubject.send(model) }}
|
||||
public var textSize: ToggleTextSize { didSet { modelDidUpdate() }}
|
||||
|
||||
@Proxy(\.model.textWeight)
|
||||
public var textWeight: ToggleTextWeight { didSet { modelSubject.send(model) }}
|
||||
public var textWeight: ToggleTextWeight { didSet { modelDidUpdate() }}
|
||||
|
||||
@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 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
|
||||
//--------------------------------------------------
|
||||
@ -364,12 +379,12 @@ open class ToggleViewModelHandlerBase<ViewModelType: ToggleViewModel>: ControlVi
|
||||
//--------------------------------------------------
|
||||
// MARK: - Toggle
|
||||
//--------------------------------------------------
|
||||
private func updateToggle(_ model: ModelType) {
|
||||
private func updateToggle() {
|
||||
//private func
|
||||
func constrainKnob(){
|
||||
knobLeadingConstraint?.isActive = false
|
||||
knobTrailingConstraint?.isActive = false
|
||||
if model.on {
|
||||
if viewModel.isOn {
|
||||
knobTrailingConstraint = toggleView.trailingAnchor.constraint(equalTo: knobView.trailingAnchor, constant: 2)
|
||||
knobLeadingConstraint = knobView.leadingAnchor.constraint(greaterThanOrEqualTo: toggleView.leadingAnchor)
|
||||
} else {
|
||||
@ -382,10 +397,10 @@ open class ToggleViewModelHandlerBase<ViewModelType: ToggleViewModel>: ControlVi
|
||||
layoutIfNeeded()
|
||||
}
|
||||
|
||||
let toggleColor = toggleColorConfiguration.getColor(model)
|
||||
let knobColor = knobColorConfiguration.getColor(model)
|
||||
let toggleColor = viewModel.toggleColor
|
||||
let knobColor = viewModel.knobColor
|
||||
|
||||
if model.disabled {
|
||||
if viewModel.disabled {
|
||||
toggleView.backgroundColor = toggleColor
|
||||
knobView.backgroundColor = knobColor
|
||||
constrainKnob()
|
||||
@ -404,10 +419,10 @@ open class ToggleViewModelHandlerBase<ViewModelType: ToggleViewModel>: ControlVi
|
||||
//--------------------------------------------------
|
||||
// MARK: - Labels
|
||||
//--------------------------------------------------
|
||||
private func updateLabel(_ model: ModelType) {
|
||||
let showText = model.showText
|
||||
private func updateLabel() {
|
||||
let showText = viewModel.showText
|
||||
stackView.spacing = showText ? 12 : 0
|
||||
label.set(with: model.labelModel)
|
||||
label.set(with: viewModel.labelModel)
|
||||
|
||||
if stackView.subviews.contains(label) {
|
||||
label.removeFromSuperview()
|
||||
@ -456,7 +471,7 @@ open class ToggleViewModelHandlerBase<ViewModelType: ToggleViewModel>: ControlVi
|
||||
toggleView.layer.cornerRadius = toggleSize.height / 2.0
|
||||
knobView.layer.cornerRadius = knobSize.height / 2.0
|
||||
|
||||
toggleView.backgroundColor = toggleColorConfiguration.getColor(viewModel.model)
|
||||
toggleView.backgroundColor = viewModel.toggleColor
|
||||
|
||||
toggleContainerView.addSubview(toggleView)
|
||||
toggleView.addSubview(knobView)
|
||||
@ -471,7 +486,7 @@ open class ToggleViewModelHandlerBase<ViewModelType: ToggleViewModel>: ControlVi
|
||||
|
||||
toggleView.bottomAnchor.constraint(greaterThanOrEqualTo: knobView.bottomAnchor).isActive = true
|
||||
|
||||
updateLabel(viewModel.model)
|
||||
updateLabel()
|
||||
stackView.addArrangedSubview(toggleContainerView)
|
||||
stackView.topAnchor.constraint(equalTo: topAnchor).isActive = true
|
||||
stackView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
|
||||
@ -485,8 +500,8 @@ open class ToggleViewModelHandlerBase<ViewModelType: ToggleViewModel>: ControlVi
|
||||
|
||||
public override func reset() {
|
||||
super.reset()
|
||||
toggleView.backgroundColor = toggleColorConfiguration.getColor(viewModel.model)
|
||||
knobView.backgroundColor = knobColorConfiguration.getColor(viewModel.model)
|
||||
toggleView.backgroundColor = viewModel.toggleColor
|
||||
knobView.backgroundColor = viewModel.knobColor
|
||||
}
|
||||
|
||||
/// This will toggle the state of the Toggle and execute the actionBlock if provided.
|
||||
@ -498,10 +513,10 @@ open class ToggleViewModelHandlerBase<ViewModelType: ToggleViewModel>: ControlVi
|
||||
//--------------------------------------------------
|
||||
// MARK: - State
|
||||
//--------------------------------------------------
|
||||
open override func updateView(model: ModelType) {
|
||||
updateLabel(model)
|
||||
updateToggle(model)
|
||||
backgroundColor = model.surface.color
|
||||
open override func updateView() {
|
||||
updateLabel()
|
||||
updateToggle()
|
||||
backgroundColor = viewModel.surface.color
|
||||
setNeedsLayout()
|
||||
layoutIfNeeded()
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user