vds_ios/VDS/Components/Table/TableCellItem.swift

111 lines
4.4 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 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 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: TableCellModel, surface: Surface, separatorStyle: Line.Style, isHeader: Bool = false, hideSeparator: Bool = false, 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)
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 {
// let labelWidth = layoutAttributes.frame.size.width - (2 * padding.horizontalValue())
// let maxbounds = CGSize(width: labelWidth, height: CGFloat.greatestFiniteMagnitude)
// let labelSize = cellLabel.textRect(forBounds: CGRect(origin: .zero, size: maxbounds), limitedToNumberOfLines: cellLabel.numberOfLines)
// var labelHeight = max(labelSize.height, layoutAttributes.frame.size.height) + (2 * padding.horizontalValue())
// layoutAttributes.frame.size = CGSize(width: layoutAttributes.frame.size.width, height: labelHeight)
// return layoutAttributes
// }
}