vds_ios/VDS/Components/Table/TableCellItem.swift
2024-05-09 18:24:02 +05:30

71 lines
2.6 KiB
Swift

//
// TableCellItem.swift
// VDS
//
// Created by Nadigadda, Sumanth on 25/04/24.
//
import Foundation
import UIKit
import VDSTokens
final class TableCellItem: UICollectionViewCell {
static let Identifier: String = String(describing: TableCellItem.self)
private let containerView = View()
private let separator: Line = Line()
private let backgroundColorConfiguration = SurfaceColorConfiguration(VDSColor.backgroundPrimaryLight, VDSColor.backgroundPrimaryDark)
private let stripedColorConfiguration = SurfaceColorConfiguration(VDSColor.backgroundSecondaryLight, VDSColor.backgroundSecondaryDark)
private var padding: Table.Padding = .standard
override init(frame: CGRect) {
super.init(frame: frame)
setupCell()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupCell()
}
private func setupCell() {
contentView.backgroundColor = .clear
addSubview(containerView)
containerView.pinToSuperView()
}
func updateCell(content: TableItemModel, surface: Surface, striped: Bool = false, padding: Table.Padding = .standard) {
containerView.subviews.forEach({ $0.removeFromSuperview() })
self.padding = padding
containerView.surface = surface
containerView.backgroundColor = striped ? stripedColorConfiguration.getColor(surface) : backgroundColorConfiguration.getColor(surface)
containerView.addSubview(content.component)
if var surfacedView = content.component as? Surfaceable {
surfacedView.surface = surface
}
NSLayoutConstraint.activate([
content.component.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: VDSLayout.space1X),
content.component.topAnchor.constraint(greaterThanOrEqualTo: containerView.topAnchor, constant: padding.verticalValue()),
containerView.bottomAnchor.constraint(greaterThanOrEqualTo: content.component.bottomAnchor, constant: padding.verticalValue()),
containerView.trailingAnchor.constraint(greaterThanOrEqualTo: content.component.trailingAnchor, constant: padding.horizontalValue()),
containerView.centerYAnchor.constraint(equalTo: content.component.centerYAnchor)
])
containerView.addSubview(separator)
separator.pinLeading().pinTrailing().pinBottom()
separator.style = content.bottomLine ?? .primary
separator.isHidden = content.bottomLine == nil
separator.surface = surface
}
}