// // 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().with { $0.translatesAutoresizingMaskIntoConstraints = false } private var cellLabel = Label().with { $0.translatesAutoresizingMaskIntoConstraints = false $0.setContentHuggingPriority(.defaultHigh, for: .horizontal) $0.setContentHuggingPriority(.defaultHigh, for:.vertical) $0.textAlignment = .left $0.lineBreakMode = .byWordWrapping } private let separator: Line = Line() private let backgroundColorConfiguration = SurfaceColorConfiguration(VDSColor.backgroundPrimaryLight, VDSColor.backgroundPrimaryDark) private let stripedColorConfiguration = SurfaceColorConfiguration(VDSColor.backgroundSecondaryLight, VDSColor.backgroundSecondaryDark) 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() containerView.addSubview(cellLabel) cellLabel.pinToSuperView() containerView.addSubview(separator) separator.pinLeading().pinTrailing().pinBottom() } func updateCell(content: Any, surface: Surface, separatorStyle: Line.Style, isHeader: Bool = false, hideSeparator: Bool = false, striped: Bool = false) { guard let info = content as? String else { return } cellLabel.textStyle = textStyle(for: isHeader) cellLabel.text = info cellLabel.surface = surface containerView.surface = surface containerView.backgroundColor = striped ? stripedColorConfiguration.getColor(surface) : backgroundColorConfiguration.getColor(surface) separator.isHidden = hideSeparator separator.style = separatorStyle separator.surface = surface } private func textStyle(for header:Bool) -> TextStyle { return header ? .boldTitleSmall : UIDevice.isIPad ? .bodyLarge : .bodySmall } override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes { let targetSize = CGSize(width: layoutAttributes.frame.width, height: 0) layoutAttributes.frame.size = contentView.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: .required, verticalFittingPriority: .fittingSizeLevel) return layoutAttributes } }