vds_ios/VDS/Classes/CollectionView.swift
Matt Bruce 9e2d5fff47 added Equality to Modelable
added AnyEquality to classes that have an a property with 'any'
added default implementation on shouldUpdateView() to check equality, this can be re-implemented in classes
removed all shouldUpdateView methods everywhere since above was done

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-09-16 09:14:27 -05:00

111 lines
3.1 KiB
Swift

//
// CollectionView.swift
// VDS
//
// Created by Matt Bruce on 8/25/22.
//
import Foundation
import UIKit
import Combine
open class CollectionView<ModelType: Modelable>: UICollectionView, ModelHandlerable, ViewProtocol, Resettable {
//--------------------------------------------------
// 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 {
didSet {
self.isEnabled = !disabled
}
}
open var isEnabled: Bool {
get { !model.disabled }
set {
//create local vars for clear coding
let disabled = !newValue
if model.disabled != disabled {
model.disabled = disabled
}
}
}
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
required public init() {
super.init(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
initialSetup()
}
public required init(with model: ModelType) {
super.init(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
initialSetup()
set(with: model)
}
public required init(with model: ModelType, collectionViewLayout layout: UICollectionViewLayout) {
super.init(frame: .zero, collectionViewLayout: layout)
initialSetup()
set(with: model)
}
public override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
super.init(frame: frame, collectionViewLayout: layout)
initialSetup()
set(with: model)
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
initialSetup()
}
//--------------------------------------------------
// MARK: - Setup
//--------------------------------------------------
public func initialSetup() {
if !initialSetupPerformed {
initialSetupPerformed = true
setupUpdateView()
setup()
}
}
//--------------------------------------------------
// MARK: - Overrides
//--------------------------------------------------
open func updateView(viewModel: ModelType) {
fatalError("Implement updateView")
}
open func reset() {
backgroundColor = .clear
if let model = model as? Resettable {
model.reset()
}
}
// MARK: - ViewProtocol
/// Will be called only once.
open func setup() {
translatesAutoresizingMaskIntoConstraints = false
insetsLayoutMarginsFromSafeArea = false
}
}