From 39ef411559c7c5ea1b04d4ec5ad9b92f74280a61 Mon Sep 17 00:00:00 2001 From: Sumanth Nadigadda Date: Thu, 2 May 2024 13:36:29 +0530 Subject: [PATCH] Adding padding related changes to table --- VDS/Components/Table/Table.swift | 17 ++++------------- VDS/Components/Table/TableCellItem.swift | 22 ++++++++++++++++++---- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/VDS/Components/Table/Table.swift b/VDS/Components/Table/Table.swift index ded02e63..41945763 100644 --- a/VDS/Components/Table/Table.swift +++ b/VDS/Components/Table/Table.swift @@ -31,6 +31,8 @@ open class Table: View { private let flowLayout = MatrixFlowLayout().with { $0.estimatedItemSize = UICollectionViewFlowLayout.automaticSize + $0.minimumLineSpacing = 0 + $0.minimumInteritemSpacing = 0 } //-------------------------------------------------- @@ -109,7 +111,7 @@ extension Table : UICollectionViewDelegate, UICollectionViewDataSource { let shouldStrip = striped ? (indexPath.section % 2 != 0) : false let style = indexPath.section == 0 ? headerBottomLineType : rowBottomLineType let hideSeparator = indexPath.section == 0 ? headerBottomLine : rowBottomLine - cell.updateCell(content: currentItem, surface: surface, separatorStyle: style, isHeader: indexPath.section == 0, hideSeparator: hideSeparator, striped: shouldStrip) + cell.updateCell(content: currentItem, surface: surface, separatorStyle: style, isHeader: indexPath.section == 0, hideSeparator: hideSeparator, striped: shouldStrip, padding: padding) return cell } } @@ -119,19 +121,8 @@ extension Table: UICollectionViewDelegateFlowLayout { public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { guard let sectionCount = tableData?[indexPath.section].count else { return CGSize.zero } let width = Int(collectionView.frame.width) / sectionCount - return CGSize(width: width, height: 50) + return CGSize(width: width, height: 100) } - - public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { - //return padding.verticalValue() - return 0 - } - - public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { - //return padding.horizontalValue() - return 0 - } - } final class MatrixFlowLayout : UICollectionViewFlowLayout { diff --git a/VDS/Components/Table/TableCellItem.swift b/VDS/Components/Table/TableCellItem.swift index da5a6ee1..bfc5a430 100644 --- a/VDS/Components/Table/TableCellItem.swift +++ b/VDS/Components/Table/TableCellItem.swift @@ -30,6 +30,9 @@ final class TableCellItem: UICollectionViewCell { 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) @@ -48,13 +51,21 @@ final class TableCellItem: UICollectionViewCell { containerView.pinToSuperView() containerView.addSubview(cellLabel) - cellLabel.pinToSuperView() + 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) { + 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 @@ -66,6 +77,10 @@ final class TableCellItem: UICollectionViewCell { 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 { @@ -73,8 +88,7 @@ final class TableCellItem: UICollectionViewCell { } 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) + layoutAttributes.frame.size = contentView.systemLayoutSizeFitting(layoutAttributes.frame.size, withHorizontalFittingPriority: .required, verticalFittingPriority: .required) return layoutAttributes } }