vds_ios/VDS/Classes/VDSControl.swift
Matt Bruce 7fef4aadb7 refactored constants
updated constant references
fixed bug in checkbox

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-08-03 08:46:51 -05:00

75 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, ModelHandlerable, 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(VDSConstants.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
}
}