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

111 lines
4.2 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().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 var icon = Icon().with {
$0.size = UIDevice.isIPad ? .medium : .small
}
private let separator: Line = Line()
private let backgroundColorConfiguration = SurfaceColorConfiguration(VDSColor.backgroundPrimaryLight, VDSColor.backgroundPrimaryDark)
private let stripedColorConfiguration = SurfaceColorConfiguration(VDSColor.backgroundSecondaryLight, VDSColor.backgroundSecondaryDark)
private var labelTopConstraint: NSLayoutConstraint?
private var labelBottomConstraint: NSLayoutConstraint?
private var labelTrailingConstraint: NSLayoutConstraint?
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: TableCellModel, surface: Surface, separatorStyle: Line.Style, isHeader: Bool = false, hideSeparator: Bool = false, striped: Bool = false, padding: Table.Padding = .standard) {
containerView.subviews.forEach({ $0.removeFromSuperview() })
containerView.surface = surface
containerView.backgroundColor = striped ? stripedColorConfiguration.getColor(surface) : backgroundColorConfiguration.getColor(surface)
if let model = content as? Table.TableCellLabelModel {
addLabel(model: model, surface: surface, isHeader: isHeader, padding: padding)
} else if let model = content as? Table.TableCellImageModel {
addImage(model: model, surface: surface)
}
containerView.addSubview(separator)
separator.pinLeading().pinTrailing().pinBottom()
separator.isHidden = hideSeparator
separator.style = separatorStyle
separator.surface = surface
}
private func addLabel(model: Table.TableCellLabelModel, surface: Surface, isHeader: Bool, padding: Table.Padding) {
containerView.addSubview(cellLabel)
cellLabel.pinLeading(VDSLayout.space1X)
NSLayoutConstraint.activate([
cellLabel.topAnchor.constraint(equalTo: containerView.topAnchor, constant: padding.verticalValue()),
containerView.bottomAnchor.constraint(equalTo: cellLabel.bottomAnchor, constant: padding.verticalValue()),
containerView.trailingAnchor.constraint(equalTo: cellLabel.trailingAnchor, constant: padding.horizontalValue())
])
cellLabel.textStyle = textStyle(for: isHeader)
cellLabel.text = model.text
cellLabel.surface = surface
}
private func addImage(model: Table.TableCellImageModel, surface: Surface) {
containerView.addSubview(icon)
icon.pinLeading().pinCenterY()
icon.name = model.name
icon.surface = surface
}
private func textStyle(for header:Bool) -> TextStyle {
return header ? .boldTitleSmall : UIDevice.isIPad ? .bodyLarge : .bodySmall
}
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
layoutAttributes.frame.size = contentView.systemLayoutSizeFitting(layoutAttributes.frame.size, withHorizontalFittingPriority: .required, verticalFittingPriority: .required)
return layoutAttributes
}
}