vds_ios/VDS/Classes/View.swift
Matt Bruce 85b0b81da9 added resetable
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-08-03 15:26:25 -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 View<ModelType: Initable>: UIView, ModelHandlerable, ViewProtocol, Resettable {
@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
cancellable = $model.debounce(for: .seconds(Constants.ModelStateDebounce), scheduler: RunLoop.main).sink { [weak self] viewModel in
self?.onStateChange(viewModel: viewModel)
}
setup()
}
}
open func reset() {
backgroundColor = .clear
}
// MARK: - ViewProtocol
open func updateView(_ size: CGFloat) { }
/// Will be called only once.
open func setup() {
translatesAutoresizingMaskIntoConstraints = false
insetsLayoutMarginsFromSafeArea = false
}
}