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 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
|
||||||
|
|||||||
@ -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(model: ModelType) -> Bool
|
func updateView()
|
||||||
func updateView(model: 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(model: model){
|
viewModel.set(with: model)
|
||||||
viewModel.set(with: model)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public func shouldUpdateView(model: ModelType) -> Bool {
|
|
||||||
self.viewModel.model != model
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func setupUpdateView() {
|
public func setupUpdateView() {
|
||||||
handlerPublisher()
|
handlerPublisher()
|
||||||
.subscribe(on: RunLoop.main)
|
.subscribe(on: RunLoop.main)
|
||||||
.sink { [weak self] model in
|
.sink { [weak self] _ in
|
||||||
self?.updateView(model: model)
|
self?.updateView()
|
||||||
}
|
}
|
||||||
.store(in: &subscribers)
|
.store(in: &subscribers)
|
||||||
}
|
}
|
||||||
@ -88,6 +85,8 @@ public protocol ViewModel<ModelType>: AnyObject, Surfaceable, Disabling {
|
|||||||
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()
|
||||||
}
|
}
|
||||||
|
|
||||||
///-----------------------------------------------------------------------------
|
///-----------------------------------------------------------------------------
|
||||||
@ -100,20 +99,26 @@ public class ViewModelBase<ModelType: Modelable>: NSObject, ViewModel, Observabl
|
|||||||
|
|
||||||
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){
|
||||||
self.model = model
|
if shouldUpdateView(model: model) {
|
||||||
modelSubject.send(model)
|
self.model = 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(model: 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,12 +379,12 @@ open class ToggleViewModelHandlerBase<ViewModelType: ToggleViewModel>: ControlVi
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Toggle
|
// MARK: - Toggle
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
private func updateToggle(_ model: ModelType) {
|
private func updateToggle() {
|
||||||
//private func
|
//private func
|
||||||
func constrainKnob(){
|
func constrainKnob(){
|
||||||
knobLeadingConstraint?.isActive = false
|
knobLeadingConstraint?.isActive = false
|
||||||
knobTrailingConstraint?.isActive = false
|
knobTrailingConstraint?.isActive = false
|
||||||
if model.on {
|
if viewModel.isOn {
|
||||||
knobTrailingConstraint = toggleView.trailingAnchor.constraint(equalTo: knobView.trailingAnchor, constant: 2)
|
knobTrailingConstraint = toggleView.trailingAnchor.constraint(equalTo: knobView.trailingAnchor, constant: 2)
|
||||||
knobLeadingConstraint = knobView.leadingAnchor.constraint(greaterThanOrEqualTo: toggleView.leadingAnchor)
|
knobLeadingConstraint = knobView.leadingAnchor.constraint(greaterThanOrEqualTo: toggleView.leadingAnchor)
|
||||||
} else {
|
} else {
|
||||||
@ -382,10 +397,10 @@ open class ToggleViewModelHandlerBase<ViewModelType: ToggleViewModel>: ControlVi
|
|||||||
layoutIfNeeded()
|
layoutIfNeeded()
|
||||||
}
|
}
|
||||||
|
|
||||||
let toggleColor = toggleColorConfiguration.getColor(model)
|
let toggleColor = viewModel.toggleColor
|
||||||
let knobColor = knobColorConfiguration.getColor(model)
|
let knobColor = viewModel.knobColor
|
||||||
|
|
||||||
if model.disabled {
|
if viewModel.disabled {
|
||||||
toggleView.backgroundColor = toggleColor
|
toggleView.backgroundColor = toggleColor
|
||||||
knobView.backgroundColor = knobColor
|
knobView.backgroundColor = knobColor
|
||||||
constrainKnob()
|
constrainKnob()
|
||||||
@ -404,10 +419,10 @@ open class ToggleViewModelHandlerBase<ViewModelType: ToggleViewModel>: ControlVi
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Labels
|
// MARK: - Labels
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
private func updateLabel(_ model: ModelType) {
|
private func updateLabel() {
|
||||||
let showText = model.showText
|
let showText = viewModel.showText
|
||||||
stackView.spacing = showText ? 12 : 0
|
stackView.spacing = showText ? 12 : 0
|
||||||
label.set(with: model.labelModel)
|
label.set(with: viewModel.labelModel)
|
||||||
|
|
||||||
if stackView.subviews.contains(label) {
|
if stackView.subviews.contains(label) {
|
||||||
label.removeFromSuperview()
|
label.removeFromSuperview()
|
||||||
@ -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,7 +486,7 @@ 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
|
||||||
@ -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,10 +513,10 @@ open class ToggleViewModelHandlerBase<ViewModelType: ToggleViewModel>: ControlVi
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - State
|
// MARK: - State
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
open override func updateView(model: ModelType) {
|
open override func updateView() {
|
||||||
updateLabel(model)
|
updateLabel()
|
||||||
updateToggle(model)
|
updateToggle()
|
||||||
backgroundColor = model.surface.color
|
backgroundColor = viewModel.surface.color
|
||||||
setNeedsLayout()
|
setNeedsLayout()
|
||||||
layoutIfNeeded()
|
layoutIfNeeded()
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user