From d94c845eb3aee196f1cd5d940042f034b7eb1f56 Mon Sep 17 00:00:00 2001 From: Sumanth Nadigadda Date: Thu, 2 May 2024 18:47:30 +0530 Subject: [PATCH] Adding models for the tab cell items --- VDS.xcodeproj/project.pbxproj | 8 +++ VDS/Components/Table/Table.swift | 2 +- VDS/Components/Table/TableCellItem.swift | 60 ++++++++++++------- .../Table/TableCellLabelModel.swift | 39 ++++++++++++ VDS/Components/Table/TableCellModel.swift | 10 ++++ 5 files changed, 96 insertions(+), 23 deletions(-) create mode 100644 VDS/Components/Table/TableCellLabelModel.swift create mode 100644 VDS/Components/Table/TableCellModel.swift diff --git a/VDS.xcodeproj/project.pbxproj b/VDS.xcodeproj/project.pbxproj index f4a3f1ac..24239140 100644 --- a/VDS.xcodeproj/project.pbxproj +++ b/VDS.xcodeproj/project.pbxproj @@ -25,6 +25,8 @@ 445BA07829C07B3D0036A7C5 /* Notification.swift in Sources */ = {isa = PBXBuildFile; fileRef = 445BA07729C07B3D0036A7C5 /* Notification.swift */; }; 44604AD429CE186A00E62B51 /* NotificationButtonModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 44604AD329CE186A00E62B51 /* NotificationButtonModel.swift */; }; 44604AD729CE196600E62B51 /* Line.swift in Sources */ = {isa = PBXBuildFile; fileRef = 44604AD629CE196600E62B51 /* Line.swift */; }; + 44A952D92BE384C40009F874 /* TableCellModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 44A952D82BE384C40009F874 /* TableCellModel.swift */; }; + 44A952DB2BE3852E0009F874 /* TableCellLabelModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 44A952DA2BE3852E0009F874 /* TableCellLabelModel.swift */; }; 5F21D7BF28DCEB3D003E7CD6 /* Useable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F21D7BE28DCEB3D003E7CD6 /* Useable.swift */; }; 5FC35BE328D51405004EBEAC /* Button.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FC35BE228D51405004EBEAC /* Button.swift */; }; 710607952B91A99500F2863F /* TitleletChangeLog.txt in Resources */ = {isa = PBXBuildFile; fileRef = 710607942B91A99500F2863F /* TitleletChangeLog.txt */; }; @@ -216,6 +218,8 @@ 445BA07729C07B3D0036A7C5 /* Notification.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Notification.swift; sourceTree = ""; }; 44604AD329CE186A00E62B51 /* NotificationButtonModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationButtonModel.swift; sourceTree = ""; }; 44604AD629CE196600E62B51 /* Line.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Line.swift; sourceTree = ""; }; + 44A952D82BE384C40009F874 /* TableCellModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableCellModel.swift; sourceTree = ""; }; + 44A952DA2BE3852E0009F874 /* TableCellLabelModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableCellLabelModel.swift; sourceTree = ""; }; 5F21D7BE28DCEB3D003E7CD6 /* Useable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Useable.swift; sourceTree = ""; }; 5FC35BE228D51405004EBEAC /* Button.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Button.swift; sourceTree = ""; }; 710607942B91A99500F2863F /* TitleletChangeLog.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = TitleletChangeLog.txt; sourceTree = ""; }; @@ -436,6 +440,8 @@ children = ( 440B84C92BD8E0E9004A732A /* Table.swift */, 443DBAF92BDA303F0021497E /* TableCellItem.swift */, + 44A952D82BE384C40009F874 /* TableCellModel.swift */, + 44A952DA2BE3852E0009F874 /* TableCellLabelModel.swift */, ); path = Table; sourceTree = ""; @@ -1142,6 +1148,7 @@ EAACB89A2B927108006A3869 /* Valuing.swift in Sources */, EAE785312BA0A438009428EA /* UIImage+Helper.swift in Sources */, EAB5FEF5292D371F00998C17 /* ButtonBase.swift in Sources */, + 44A952D92BE384C40009F874 /* TableCellModel.swift in Sources */, EA978EC5291D6AFE00ACC883 /* AnyLabelAttribute.swift in Sources */, 71ACE89C2BA0451200FB6ADC /* PaginationContainer.swift in Sources */, EAC71A1F2A2E173D00E47A9F /* RadioButton.swift in Sources */, @@ -1211,6 +1218,7 @@ EA0B18052A9E2D2D00F2D0CD /* SelectorBase.swift in Sources */, EAC71A1D2A2E155A00E47A9F /* Checkbox.swift in Sources */, EAF7F0AB289B13FD00B287F5 /* TextStyleLabelAttribute.swift in Sources */, + 44A952DB2BE3852E0009F874 /* TableCellLabelModel.swift in Sources */, EAB1D29C28A5618900DAE764 /* RadioButtonGroup.swift in Sources */, EA81410B2A0E8E3C004F60D2 /* ButtonIcon.swift in Sources */, EA985BE629688F6A00F2FF2E /* TileletBadgeModel.swift in Sources */, diff --git a/VDS/Components/Table/Table.swift b/VDS/Components/Table/Table.swift index 41945763..32195467 100644 --- a/VDS/Components/Table/Table.swift +++ b/VDS/Components/Table/Table.swift @@ -77,7 +77,7 @@ open class Table: View { open var rowBottomLineType: Line.Style = .secondary { didSet { setNeedsUpdate() } } - open var tableData: [[Any]]? { didSet { setNeedsUpdate() } } + open var tableData: [[TableCellModel]]? { didSet { setNeedsUpdate() } } //-------------------------------------------------- // MARK: - Overrides diff --git a/VDS/Components/Table/TableCellItem.swift b/VDS/Components/Table/TableCellItem.swift index bfc5a430..7c2a1cdd 100644 --- a/VDS/Components/Table/TableCellItem.swift +++ b/VDS/Components/Table/TableCellItem.swift @@ -25,6 +25,10 @@ final class TableCellItem: UICollectionViewCell { $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) @@ -49,38 +53,50 @@ final class TableCellItem: UICollectionViewCell { 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 + 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()) + ]) - labelTopConstraint?.constant = padding.verticalValue() - labelBottomConstraint?.constant = padding.verticalValue() - labelTrailingConstraint?.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 { diff --git a/VDS/Components/Table/TableCellLabelModel.swift b/VDS/Components/Table/TableCellLabelModel.swift new file mode 100644 index 00000000..158c9bd4 --- /dev/null +++ b/VDS/Components/Table/TableCellLabelModel.swift @@ -0,0 +1,39 @@ +// +// TableCellLabelModel.swift +// VDS +// +// Created by Nadigadda, Sumanth on 02/05/24. +// + +import Foundation +extension Table { + public struct TableCellLabelModel: TableCellModel, Surfaceable { + + public var text: String + + public var accessibilityString: String? + + public var surface: Surface + + public init(text: String, accessibilityString: String? = "", surface: Surface = .light) { + self.text = text + self.accessibilityString = accessibilityString + self.surface = surface + } + } + + public struct TableCellImageModel: TableCellModel, Surfaceable { + + public var name: Icon.Name + + public var size: Icon.Size + + public var surface: Surface + + public init(name: Icon.Name, size: Icon.Size, surface: Surface = .light) { + self.name = name + self.size = size + self.surface = surface + } + } +} diff --git a/VDS/Components/Table/TableCellModel.swift b/VDS/Components/Table/TableCellModel.swift new file mode 100644 index 00000000..38ac8a3f --- /dev/null +++ b/VDS/Components/Table/TableCellModel.swift @@ -0,0 +1,10 @@ +// +// TableCellModel.swift +// VDS +// +// Created by Nadigadda, Sumanth on 02/05/24. +// + +import Foundation + +public protocol TableCellModel { }