# Conflicts: # VDS/Classes/VDSControl.swift # VDS/Components/Toggle/VDSToggle.swift Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
74 lines
1.9 KiB
Swift
74 lines
1.9 KiB
Swift
//
|
|
// Control.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 7/22/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import Combine
|
|
|
|
open class VDSControl<ModelType: Initable>: UIControl, Modelable, ViewProtocol {
|
|
|
|
@Published public var model: ModelType
|
|
private var cancellable: AnyCancellable?
|
|
|
|
open func set(with model: ModelType) {
|
|
self.model = model
|
|
}
|
|
|
|
open func onStateChange(viewModel: ModelType) {
|
|
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Properties
|
|
//--------------------------------------------------
|
|
private var initialSetupPerformed = false
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Initializers
|
|
//--------------------------------------------------
|
|
|
|
required public init(with model: ModelType) {
|
|
self.model = model
|
|
super.init(frame: .zero)
|
|
initialSetup()
|
|
set(with: model)
|
|
}
|
|
|
|
public required init?(coder: NSCoder) {
|
|
self.model = ModelType.init()
|
|
super.init(coder: coder)
|
|
initialSetup()
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Setup
|
|
//--------------------------------------------------
|
|
|
|
public func initialSetup() {
|
|
if !initialSetupPerformed {
|
|
initialSetupPerformed = true
|
|
setupView()
|
|
}
|
|
cancellable = $model.debounce(for: .seconds(ModelStateDebounce), scheduler: RunLoop.main).sink { [weak self] viewModel in
|
|
self?.onStateChange(viewModel: viewModel)
|
|
}
|
|
}
|
|
|
|
open func reset() {
|
|
backgroundColor = .clear
|
|
}
|
|
|
|
// MARK: - ViewProtocol
|
|
open func updateView(_ size: CGFloat) { }
|
|
|
|
/// Will be called only once.
|
|
open func setupView() {
|
|
translatesAutoresizingMaskIntoConstraints = false
|
|
insetsLayoutMarginsFromSafeArea = false
|
|
}
|
|
}
|