|
|
|
|
@ -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(viewModel: ModelType) -> Bool
|
|
|
|
|
func updateView(viewModel: ModelType)
|
|
|
|
|
func updateView()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension ViewModelHandler {
|
|
|
|
|
@ -52,20 +55,14 @@ extension ViewModelHandler {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func set(with model: ModelType) {
|
|
|
|
|
if shouldUpdateView(viewModel: model){
|
|
|
|
|
viewModel.set(with: model)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func shouldUpdateView(viewModel: ModelType) -> Bool {
|
|
|
|
|
self.viewModel.model != viewModel
|
|
|
|
|
viewModel.set(with: model)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func setupUpdateView() {
|
|
|
|
|
handlerPublisher()
|
|
|
|
|
.subscribe(on: RunLoop.main)
|
|
|
|
|
.sink { [weak self] viewModel in
|
|
|
|
|
self?.updateView(viewModel: viewModel)
|
|
|
|
|
.sink { [weak self] _ in
|
|
|
|
|
self?.updateView()
|
|
|
|
|
}
|
|
|
|
|
.store(in: &subscribers)
|
|
|
|
|
}
|
|
|
|
|
@ -83,37 +80,45 @@ extension ViewModelHandler {
|
|
|
|
|
///-----------------------------------------------------------------------------
|
|
|
|
|
public protocol ViewModel<ModelType>: AnyObject, Surfaceable, Disabling {
|
|
|
|
|
associatedtype ModelType: Modelable
|
|
|
|
|
var model: ModelType { get set }
|
|
|
|
|
var model: ModelType { get }
|
|
|
|
|
var modelSubject: CurrentValueSubject<ModelType, Never> { get set }
|
|
|
|
|
var publisher: AnyPublisher<ModelType, Never> { get }
|
|
|
|
|
init(with model: ModelType)
|
|
|
|
|
func set(with model: ModelType)
|
|
|
|
|
func shouldUpdateView(model: ModelType) -> Bool
|
|
|
|
|
func modelDidUpdate()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///-----------------------------------------------------------------------------
|
|
|
|
|
///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 modelSubject = CurrentValueSubject<ModelType, Never>(ModelType())
|
|
|
|
|
public var publisher: AnyPublisher<ModelType, Never> { modelSubject.eraseToAnyPublisher() }
|
|
|
|
|
|
|
|
|
|
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(viewModel: 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,26 +379,26 @@ open class ToggleViewModelHandlerBase<ViewModelType: ToggleViewModel>: ControlVi
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
|
// MARK: - Toggle
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
|
private func updateToggle(_ viewModel: ModelType) {
|
|
|
|
|
private func updateToggle() {
|
|
|
|
|
//private func
|
|
|
|
|
func constrainKnob(){
|
|
|
|
|
self.knobLeadingConstraint?.isActive = false
|
|
|
|
|
self.knobTrailingConstraint?.isActive = false
|
|
|
|
|
if viewModel.on {
|
|
|
|
|
self.knobTrailingConstraint = self.toggleView.trailingAnchor.constraint(equalTo: self.knobView.trailingAnchor, constant: 2)
|
|
|
|
|
self.knobLeadingConstraint = self.knobView.leadingAnchor.constraint(greaterThanOrEqualTo: self.toggleView.leadingAnchor)
|
|
|
|
|
knobLeadingConstraint?.isActive = false
|
|
|
|
|
knobTrailingConstraint?.isActive = false
|
|
|
|
|
if viewModel.isOn {
|
|
|
|
|
knobTrailingConstraint = toggleView.trailingAnchor.constraint(equalTo: knobView.trailingAnchor, constant: 2)
|
|
|
|
|
knobLeadingConstraint = knobView.leadingAnchor.constraint(greaterThanOrEqualTo: toggleView.leadingAnchor)
|
|
|
|
|
} else {
|
|
|
|
|
self.knobTrailingConstraint = self.toggleView.trailingAnchor.constraint(greaterThanOrEqualTo: self.knobView.trailingAnchor)
|
|
|
|
|
self.knobLeadingConstraint = self.knobView.leadingAnchor.constraint(equalTo: self.toggleView.leadingAnchor, constant: 2)
|
|
|
|
|
knobTrailingConstraint = toggleView.trailingAnchor.constraint(greaterThanOrEqualTo: knobView.trailingAnchor)
|
|
|
|
|
knobLeadingConstraint = knobView.leadingAnchor.constraint(equalTo: toggleView.leadingAnchor, constant: 2)
|
|
|
|
|
}
|
|
|
|
|
self.knobTrailingConstraint?.isActive = true
|
|
|
|
|
self.knobLeadingConstraint?.isActive = true
|
|
|
|
|
self.knobWidthConstraint?.constant = self.knobSize.width
|
|
|
|
|
self.layoutIfNeeded()
|
|
|
|
|
knobTrailingConstraint?.isActive = true
|
|
|
|
|
knobLeadingConstraint?.isActive = true
|
|
|
|
|
knobWidthConstraint?.constant = knobSize.width
|
|
|
|
|
layoutIfNeeded()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let toggleColor = toggleColorConfiguration.getColor(viewModel)
|
|
|
|
|
let knobColor = knobColorConfiguration.getColor(viewModel)
|
|
|
|
|
let toggleColor = viewModel.toggleColor
|
|
|
|
|
let knobColor = viewModel.knobColor
|
|
|
|
|
|
|
|
|
|
if viewModel.disabled {
|
|
|
|
|
toggleView.backgroundColor = toggleColor
|
|
|
|
|
@ -404,7 +419,7 @@ open class ToggleViewModelHandlerBase<ViewModelType: ToggleViewModel>: ControlVi
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
|
// MARK: - Labels
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
|
private func updateLabel(_ viewModel: ModelType) {
|
|
|
|
|
private func updateLabel() {
|
|
|
|
|
let showText = viewModel.showText
|
|
|
|
|
stackView.spacing = showText ? 12 : 0
|
|
|
|
|
label.set(with: viewModel.labelModel)
|
|
|
|
|
@ -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,11 +486,11 @@ 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
|
|
|
|
|
stackView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
|
|
|
|
|
stackView.widthAnchor.constraint(greaterThanOrEqualToConstant: toggleContainerSize.width).isActive = true
|
|
|
|
|
stackView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
|
|
|
|
|
|
|
|
|
|
toggleView.centerXAnchor.constraint(equalTo: toggleContainerView.centerXAnchor).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,38 +513,13 @@ open class ToggleViewModelHandlerBase<ViewModelType: ToggleViewModel>: ControlVi
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
|
// MARK: - State
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
|
open override func updateView(viewModel: ModelType) {
|
|
|
|
|
updateLabel(viewModel)
|
|
|
|
|
updateToggle(viewModel)
|
|
|
|
|
open override func updateView() {
|
|
|
|
|
updateLabel()
|
|
|
|
|
updateToggle()
|
|
|
|
|
backgroundColor = viewModel.surface.color
|
|
|
|
|
setNeedsLayout()
|
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|