vds_ios/VDS/Components/Table/TableCellItem.swift
2024-05-02 13:36:29 +05:30

95 lines
3.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().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)
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()
containerView.addSubview(cellLabel)
cellLabel.pinLeading()
labelTopConstraint = cellLabel.pinTop(anchor: containerView.topAnchor, constant: 0)
labelBottomConstraint = containerView.pinBottom(anchor: cellLabel.bottomAnchor, constant: 0)
labelTrailingConstraint = containerView.pinTrailing(anchor: cellLabel.trailingAnchor, constant: 0)
labelTopConstraint?.activate()
labelBottomConstraint?.activate()
labelTrailingConstraint?.activate()
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, padding: Table.Padding = .standard) {
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
labelTopConstraint?.constant = padding.verticalValue()
labelBottomConstraint?.constant = padding.verticalValue()
labelTrailingConstraint?.constant = padding.horizontalValue()
}
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
}
}