77 lines
2.1 KiB
Swift
77 lines
2.1 KiB
Swift
//
|
|
// CollectionViewCell.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 8/25/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import Combine
|
|
|
|
open class CollectionViewCell<ModelHandlerType: ModelHandlerable & UIView>: UICollectionViewCell, ViewProtocol {
|
|
//--------------------------------------------------
|
|
// MARK: - Properties
|
|
//--------------------------------------------------
|
|
private var initialSetupPerformed = false
|
|
|
|
public var modelHandler: ModelHandlerType = ModelHandlerType()
|
|
|
|
@Proxy(\.modelHandler.surface)
|
|
open var surface: Surface
|
|
|
|
@Proxy(\.modelHandler.disabled)
|
|
open var disabled: Bool
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Initializers
|
|
//--------------------------------------------------
|
|
required public init() {
|
|
super.init(frame: .zero)
|
|
initialSetup()
|
|
}
|
|
|
|
public override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
initialSetup()
|
|
}
|
|
|
|
public required init?(coder: NSCoder) {
|
|
super.init(coder: coder)
|
|
initialSetup()
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Setup
|
|
//--------------------------------------------------
|
|
|
|
open func initialSetup() {
|
|
if !initialSetupPerformed {
|
|
initialSetupPerformed = true
|
|
setup()
|
|
modelHandler.updateView()
|
|
}
|
|
}
|
|
|
|
// MARK: - ViewProtocol
|
|
/// Will be called only once.
|
|
open func setup() {
|
|
translatesAutoresizingMaskIntoConstraints = false
|
|
insetsLayoutMarginsFromSafeArea = false
|
|
|
|
addSubview(modelHandler)
|
|
|
|
modelHandler.topAnchor.constraint(equalTo: topAnchor).isActive = true
|
|
modelHandler.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
|
|
modelHandler.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
|
|
modelHandler.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
|
|
}
|
|
|
|
open override func prepareForReuse() {
|
|
super.prepareForReuse()
|
|
if let handler = modelHandler as? Resettable {
|
|
handler.reset()
|
|
}
|
|
}
|
|
}
|