80 lines
2.1 KiB
Swift
80 lines
2.1 KiB
Swift
//
|
|
// ModelViewController.swift
|
|
// VDSSample
|
|
//
|
|
// Created by Matt Bruce on 8/15/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import Combine
|
|
import VDS
|
|
|
|
public class ModelViewController<ModelType: Modelable>: UIViewController, ModelHandlerable, Initable {
|
|
deinit {
|
|
print("\(Self.self) deinit")
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Combine Properties
|
|
//--------------------------------------------------
|
|
@Published public var model: ModelType = ModelType()
|
|
public var modelPublisher: Published<ModelType>.Publisher { $model }
|
|
public var subscribers = Set<AnyCancellable>()
|
|
|
|
//--------------------------------------------------
|
|
// 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() {
|
|
super.init(nibName: nil, bundle: nil)
|
|
initialSetup()
|
|
set(with: model)
|
|
}
|
|
|
|
public required init(with model: ModelType) {
|
|
super.init(nibName: nil, bundle: nil)
|
|
initialSetup()
|
|
set(with: model)
|
|
}
|
|
|
|
public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
|
|
super.init(nibName: nil, bundle: nil)
|
|
initialSetup()
|
|
}
|
|
|
|
public required init?(coder: NSCoder) {
|
|
super.init(coder: coder)
|
|
initialSetup()
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Setup
|
|
//--------------------------------------------------
|
|
|
|
public func initialSetup() {
|
|
if !initialSetupPerformed {
|
|
initialSetupPerformed = true
|
|
setupUpdateView()
|
|
setup()
|
|
}
|
|
}
|
|
|
|
open func setup() {}
|
|
|
|
open func shouldUpdateView(viewModel: ModelType) -> Bool { true }
|
|
|
|
open func updateView(viewModel: ModelType) {}
|
|
|
|
}
|