121 lines
4.4 KiB
Swift
121 lines
4.4 KiB
Swift
//
|
|
// Table.swift
|
|
// VDS
|
|
//
|
|
// Created by Nadigadda, Sumanth on 24/04/24.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import VDSTokens
|
|
|
|
@objc(VDSTable)
|
|
open class Table: View {
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Private Properties
|
|
//--------------------------------------------------
|
|
|
|
private lazy var matrixView = SelfSizingCollectionView(frame: .zero, collectionViewLayout: flowLayout).with {
|
|
$0.register(TableCellItem.self, forCellWithReuseIdentifier: TableCellItem.Identifier)
|
|
$0.dataSource = self
|
|
$0.delegate = self
|
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
|
$0.allowsSelection = false
|
|
$0.isScrollEnabled = false
|
|
$0.showsVerticalScrollIndicator = false
|
|
$0.showsHorizontalScrollIndicator = false
|
|
$0.isAccessibilityElement = true
|
|
$0.backgroundColor = .clear
|
|
}
|
|
|
|
private lazy var flowLayout = MatrixFlowLayout().with {
|
|
$0.delegate = self
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Enums
|
|
//--------------------------------------------------
|
|
|
|
public enum Padding: String, CaseIterable {
|
|
case standard, compact
|
|
|
|
func horizontalValue() -> CGFloat {
|
|
switch self {
|
|
case .standard:
|
|
return UIDevice.isIPad ? VDSLayout.space8X : VDSLayout.space6X
|
|
case .compact:
|
|
return UIDevice.isIPad ? VDSLayout.space8X : VDSLayout.space6X
|
|
}
|
|
}
|
|
|
|
func verticalValue() -> CGFloat {
|
|
switch self {
|
|
case .standard:
|
|
return UIDevice.isIPad ? VDSLayout.space8X : VDSLayout.space6X
|
|
case .compact:
|
|
return UIDevice.isIPad ? VDSLayout.space4X : VDSLayout.space3X
|
|
}
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Public Properties
|
|
//--------------------------------------------------
|
|
|
|
open var striped: Bool = false { didSet { setNeedsUpdate() } }
|
|
|
|
open var padding: Padding = .standard { didSet { setNeedsUpdate() } }
|
|
|
|
open var headerBottomLine: Bool = false { didSet { setNeedsUpdate() } }
|
|
|
|
open var rowBottomLine: Bool = false { didSet { setNeedsUpdate() } }
|
|
|
|
open var headerBottomLineType: Line.Style = .primary { didSet { setNeedsUpdate() } }
|
|
|
|
open var rowBottomLineType: Line.Style = .secondary { didSet { setNeedsUpdate() } }
|
|
|
|
open var tableData: [[TableCellModel]] = [] { didSet { setNeedsUpdate() } }
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Overrides
|
|
//--------------------------------------------------
|
|
|
|
open override func initialSetup() {
|
|
super.initialSetup()
|
|
addSubview(matrixView)
|
|
matrixView.pinToSuperView()
|
|
}
|
|
|
|
open override func updateView() {
|
|
super.updateView()
|
|
flowLayout.layoutPadding = padding
|
|
matrixView.reloadData()
|
|
matrixView.collectionViewLayout.invalidateLayout()
|
|
}
|
|
}
|
|
|
|
extension Table: UICollectionViewDelegate, UICollectionViewDataSource, TableCollectionViewLayoutDataDelegate {
|
|
|
|
public func numberOfSections(in collectionView: UICollectionView) -> Int {
|
|
return tableData.count
|
|
}
|
|
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
|
return tableData[section].count
|
|
}
|
|
|
|
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
|
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TableCellItem.Identifier, for: indexPath) as? TableCellItem else { return UICollectionViewCell() }
|
|
let currentItem = tableData[indexPath.section][indexPath.row]
|
|
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, padding: padding)
|
|
return cell
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, dataForItemAt indexPath: IndexPath) -> TableCellModel {
|
|
return tableData[indexPath.section][indexPath.row]
|
|
}
|
|
}
|