80 lines
2.0 KiB
Swift
80 lines
2.0 KiB
Swift
//
|
|
// Control.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 7/22/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import Combine
|
|
|
|
|
|
open class View<ModelType: Modelable>: 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
|
|
|
|
@Proxy(\.model.surface)
|
|
open var surface: Surface
|
|
|
|
@Proxy(\.model.disabled)
|
|
open var disabled: Bool
|
|
//--------------------------------------------------
|
|
// 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
|
|
}
|
|
}
|