From 40883825b5b72eba5c7ed0c30db2982e850512de Mon Sep 17 00:00:00 2001 From: Krishna Kishore Bandaru Date: Sat, 2 Mar 2024 18:26:00 +0530 Subject: [PATCH 01/12] added new Pagination component --- VDS.xcodeproj/project.pbxproj | 12 + VDS/Components/Pagination/Pagination.swift | 228 ++++++++++++++++++ .../Contents.json | 12 + .../pagination-arrow-left.svg | 10 + .../Contents.json | 12 + .../pagination-arrow-right.svg | 3 + 6 files changed, 277 insertions(+) create mode 100644 VDS/Components/Pagination/Pagination.swift create mode 100644 VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-left.imageset/Contents.json create mode 100644 VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-left.imageset/pagination-arrow-left.svg create mode 100644 VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-right.imageset/Contents.json create mode 100644 VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-right.imageset/pagination-arrow-right.svg diff --git a/VDS.xcodeproj/project.pbxproj b/VDS.xcodeproj/project.pbxproj index 366fca71..79deb46b 100644 --- a/VDS.xcodeproj/project.pbxproj +++ b/VDS.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ 5F21D7BF28DCEB3D003E7CD6 /* Useable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F21D7BE28DCEB3D003E7CD6 /* Useable.swift */; }; 5FC35BE328D51405004EBEAC /* Button.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FC35BE228D51405004EBEAC /* Button.swift */; }; 7115BD3C2B84C0C200E0A610 /* TileContainerChangeLog.txt in Resources */ = {isa = PBXBuildFile; fileRef = 7115BD3B2B84C0C200E0A610 /* TileContainerChangeLog.txt */; }; + 71B23C2D2B91FA690027F7D9 /* Pagination.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71B23C2C2B91FA690027F7D9 /* Pagination.swift */; }; 71BFA70A2B7F70E6000DCE33 /* Dropshadowable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71BFA7092B7F70E6000DCE33 /* Dropshadowable.swift */; }; 71C02B382B7BD98F00E93E66 /* NotificationChangeLog.txt in Resources */ = {isa = PBXBuildFile; fileRef = 71C02B372B7BD98F00E93E66 /* NotificationChangeLog.txt */; }; EA0B18022A9E236900F2D0CD /* SelectorGroupBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA0B18012A9E236900F2D0CD /* SelectorGroupBase.swift */; }; @@ -184,6 +185,7 @@ 5F21D7BE28DCEB3D003E7CD6 /* Useable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Useable.swift; sourceTree = ""; }; 5FC35BE228D51405004EBEAC /* Button.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Button.swift; sourceTree = ""; }; 7115BD3B2B84C0C200E0A610 /* TileContainerChangeLog.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = TileContainerChangeLog.txt; sourceTree = ""; }; + 71B23C2C2B91FA690027F7D9 /* Pagination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pagination.swift; sourceTree = ""; }; 71BFA7092B7F70E6000DCE33 /* Dropshadowable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Dropshadowable.swift; sourceTree = ""; }; 71C02B372B7BD98F00E93E66 /* NotificationChangeLog.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = NotificationChangeLog.txt; sourceTree = ""; }; EA0B18012A9E236900F2D0CD /* SelectorGroupBase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectorGroupBase.swift; sourceTree = ""; }; @@ -384,6 +386,14 @@ path = Button; sourceTree = ""; }; + 71B23C2B2B91FA510027F7D9 /* Pagination */ = { + isa = PBXGroup; + children = ( + 71B23C2C2B91FA690027F7D9 /* Pagination.swift */, + ); + path = Pagination; + sourceTree = ""; + }; EA0B17FF2A9E21CA00F2D0CD /* Selector */ = { isa = PBXGroup; children = ( @@ -487,6 +497,7 @@ EA33619D288B1E330071C351 /* Components */ = { isa = PBXGroup; children = ( + 71B23C2B2B91FA510027F7D9 /* Pagination */, EA4DB2FE28DCBC1900103EE3 /* Badge */, EAD062AE2A3B87210015965D /* BadgeIndicator */, EA0FC2BE2912D18200DF80B4 /* Buttons */, @@ -1051,6 +1062,7 @@ EA8E40932A82889500934ED3 /* TooltipDialog.swift in Sources */, 44604AD429CE186A00E62B51 /* NotificationButtonModel.swift in Sources */, EAD8D2C128BFDE8B006EB6A6 /* UIGestureRecognizer+Publisher.swift in Sources */, + 71B23C2D2B91FA690027F7D9 /* Pagination.swift in Sources */, EA0D1C372A681CCE00E5C127 /* ToggleView.swift in Sources */, EAF7F0B9289C139800B287F5 /* ColorConfiguration.swift in Sources */, EA3361BD288B2C760071C351 /* TypeAlias.swift in Sources */, diff --git a/VDS/Components/Pagination/Pagination.swift b/VDS/Components/Pagination/Pagination.swift new file mode 100644 index 00000000..4df0a034 --- /dev/null +++ b/VDS/Components/Pagination/Pagination.swift @@ -0,0 +1,228 @@ +// +// Pagination.swift +// VDS +// +// Created by Bandaru, Krishna Kishore on 01/03/24. +// + +import Foundation +import VDSColorTokens +import Combine + +@objc(VDSPagination) +open class Pagination: View { + + @Published var onPreviousTapped: PassthroughSubject = PassthroughSubject() + @Published var onNextTapped: PassthroughSubject = PassthroughSubject() + @Published var onPageWillChange: PassthroughSubject = PassthroughSubject() + @Published var onPageChanged: PassthroughSubject = PassthroughSubject() + + public var total: Int = 0 { + didSet { + setNeedsUpdate() + } + } + public var selectedPage: Int = 0 { didSet { setNeedsUpdate() } } + private var numberOfRows: Int = 0 { + didSet { + collectionView.collectionViewLayout.invalidateLayout() + setNeedsUpdate() + } + } + private let pageItemCellSize: CGSize = .init(width: 20, height: 16) + private let spacingBetweenCell: CGFloat = VDSLayout.Spacing.space1X.value + private let buttonTintColorConfiguration = SurfaceColorConfiguration(VDSColor.paletteBlack, VDSColor.paletteWhite) + private let buttonTextColorConfiguration = SurfaceColorConfiguration(VDSColor.paletteBlack, VDSColor.paletteWhite) + + private lazy var collectionView: UICollectionView = { + let layout = UICollectionViewFlowLayout() + layout.itemSize = pageItemCellSize + layout.scrollDirection = .horizontal + layout.minimumInteritemSpacing = spacingBetweenCell + layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize + layout.minimumLineSpacing = spacingBetweenCell + layout.sectionInset = .zero + let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) + collectionView.isScrollEnabled = false + collectionView.translatesAutoresizingMaskIntoConstraints = false + collectionView.delegate = self + collectionView.dataSource = self + collectionView.showsHorizontalScrollIndicator = false + collectionView.showsVerticalScrollIndicator = false + collectionView.register(PaginationCellItem.self, forCellWithReuseIdentifier: PaginationCellItem.identifier) + collectionView.backgroundColor = .clear + return collectionView + }() + + //TODO: Need to check with textStyle with Matt as its getter only in ButtonBase + private lazy var previousButton: ButtonBase = { + let previousButton: ButtonBase + if #available(iOS 15.0, *) { + var configuration = ButtonBase.Configuration.plain() + configuration.imagePadding = VDSLayout.Spacing.space2X.value + configuration.attributedTitle = AttributedString("Previous", attributes: AttributeContainer([NSAttributedString.Key.font: TextStyle.boldBodySmall.font])) + configuration.titleAlignment = .leading + configuration.imagePlacement = .leading + configuration.contentInsets = .zero + previousButton = ButtonBase(configuration: configuration) + } else { + previousButton = ButtonBase() + previousButton.imageEdgeInsets = .init(top: 0, left: 0, bottom: 0, right: VDSLayout.Spacing.space2X.value) + previousButton.setTitle("Previous", for: .normal) + previousButton.titleLabel?.font = TextStyle.boldBodySmall.font + } + previousButton.contentHorizontalAlignment = .leading + previousButton.translatesAutoresizingMaskIntoConstraints = false + previousButton.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) + previousButton.setImage(BundleManager.shared.image(for: "pagination-arrow-left")?.withRenderingMode(.alwaysTemplate), for: .normal) + return previousButton + }() + + private let nextButton: ButtonBase = { + let nextButton: ButtonBase + if #available(iOS 15.0, *) { + var configuration = ButtonBase.Configuration.plain() + configuration.imagePadding = VDSLayout.Spacing.space2X.value + configuration.attributedTitle = AttributedString("Next", attributes: AttributeContainer([NSAttributedString.Key.font: TextStyle.boldBodySmall.font])) + configuration.imagePlacement = .trailing + configuration.titleAlignment = .trailing + configuration.contentInsets = .zero + nextButton = ButtonBase(configuration: configuration) + } else { + nextButton = ButtonBase() + nextButton.imageEdgeInsets = .init(top: 0, left: 0, bottom: 0, right: VDSLayout.Spacing.space2X.value) + nextButton.semanticContentAttribute = .forceRightToLeft + nextButton.titleLabel?.font = TextStyle.boldBodySmall.font + nextButton.setTitle("Next", for: .normal) + } + //nextButton.textStyle = .boldBodySmall + nextButton.translatesAutoresizingMaskIntoConstraints = false + nextButton.contentHorizontalAlignment = .trailing + nextButton.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) + nextButton.setImage(BundleManager.shared.image(for: "pagination-arrow-right")?.withRenderingMode(.alwaysTemplate), for: .normal) + return nextButton + }() + + private let containerView: View = View().with { + $0.translatesAutoresizingMaskIntoConstraints = false + } + + open override func initialSetup() { + super.initialSetup() + + addSubview(containerView) + containerView.pinToSuperView() + containerView.widthAnchor.constraint(greaterThanOrEqualToConstant: 288).activate() + containerView.heightAnchor.constraint(equalToConstant: 44).activate() + containerView.addSubview(previousButton) + containerView.addSubview(collectionView) + containerView.addSubview(nextButton) + + previousButton + .pinTop() + .pinBottom() + .pinLeading() + previousButton.trailingAnchor.constraint(greaterThanOrEqualTo: collectionView.leadingAnchor).activate() + collectionView.heightAnchor.constraint(equalToConstant: VDSLayout.Spacing.space4X.value).activate() + collectionView.centerYAnchor.constraint(equalTo: centerYAnchor).activate() + collectionView.centerXAnchor.constraint(equalTo: centerXAnchor).activate() + collectionView.trailingAnchor.constraint(greaterThanOrEqualTo: nextButton.leadingAnchor).activate() + collectionView.widthAnchor.constraint(equalToConstant: 92).activate() + nextButton + .pinTop() + .pinBottom() + .pinTrailing() + nextButton.onClick = onbuttonTapped + previousButton.onClick = onbuttonTapped + previousButton.isHidden = true + } + + open override func updateView() { + super.updateView() + + previousButton.tintColor = buttonTintColorConfiguration.getColor(surface) + nextButton.tintColor = buttonTintColorConfiguration.getColor(surface) + previousButton.setTitleColor(buttonTextColorConfiguration.getColor(surface), for: .normal) + nextButton.setTitleColor(buttonTextColorConfiguration.getColor(surface), for: .normal) + collectionView.reloadData() + } + + private func onbuttonTapped(_ sender: UIButton) { + let isNextAction = sender == nextButton + if isNextAction { + selectedPage += 1 + } else { + selectedPage -= 1 + } + updateSelection() + } + + private func updateSelection() { + collectionView.scrollToItem(at: IndexPath(row: max(selectedPage-1, 0), section: 0), at: .left, animated: false) + previousButton.isHidden = selectedPage == 0 + nextButton.isHidden = selectedPage == total - 1 + } +} + +extension Pagination: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { + + public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { total } + + public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { + guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PaginationCellItem.identifier, for: indexPath) as? PaginationCellItem else { return UICollectionViewCell() } + cell.update(selectedPage, currentIndex: indexPath.row, surface: surface) + return cell + } + + public func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool { + onPageWillChange.send(selectedPage) + return true + } + + public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { + selectedPage = indexPath.row + updateSelection() + onPageChanged.send(indexPath.row) + } +} + +internal final class PaginationCellItem: UICollectionViewCell { + + static let identifier: String = String(describing: PaginationCellItem.self) + private let textColorConfiguration = SurfaceColorConfiguration(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark) + + private var indexLabel: Label = Label().with { + $0.translatesAutoresizingMaskIntoConstraints = false + $0.textAlignment = .center + $0.numberOfLines = 1 + } + + override init(frame: CGRect) { + super.init(frame: frame) + setUp() + } + + required init?(coder: NSCoder) { + super.init(coder: coder) + setUp() + } + + private func setUp() { + let containerView = View() + containerView.translatesAutoresizingMaskIntoConstraints = false + containerView.addSubview(indexLabel) + contentView.addSubview(containerView) + containerView.pinToSuperView() + indexLabel.pinToSuperView() + indexLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: VDSLayout.Spacing.space5X.value).activate() + contentView.backgroundColor = .clear + containerView.backgroundColor = .clear + indexLabel.backgroundColor = .clear + } + + internal func update(_ selectedIndex: Int, currentIndex: Int, surface: Surface) { + indexLabel.textStyle = selectedIndex == currentIndex ? .boldBodySmall : .bodySmall + indexLabel.text = "\(currentIndex)" + indexLabel.textColor = textColorConfiguration.getColor(surface) + } +} diff --git a/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-left.imageset/Contents.json b/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-left.imageset/Contents.json new file mode 100644 index 00000000..6c37b40d --- /dev/null +++ b/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-left.imageset/Contents.json @@ -0,0 +1,12 @@ +{ + "images" : [ + { + "filename" : "pagination-arrow-left.svg", + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-left.imageset/pagination-arrow-left.svg b/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-left.imageset/pagination-arrow-left.svg new file mode 100644 index 00000000..a8c85c97 --- /dev/null +++ b/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-left.imageset/pagination-arrow-left.svg @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-right.imageset/Contents.json b/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-right.imageset/Contents.json new file mode 100644 index 00000000..d294d555 --- /dev/null +++ b/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-right.imageset/Contents.json @@ -0,0 +1,12 @@ +{ + "images" : [ + { + "filename" : "pagination-arrow-right.svg", + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-right.imageset/pagination-arrow-right.svg b/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-right.imageset/pagination-arrow-right.svg new file mode 100644 index 00000000..be02bedf --- /dev/null +++ b/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-right.imageset/pagination-arrow-right.svg @@ -0,0 +1,3 @@ + + + From 8ed924c8056f582430ff8b1b3f5b08ce911e34cb Mon Sep 17 00:00:00 2001 From: Krishna Kishore Bandaru Date: Tue, 5 Mar 2024 16:48:26 +0530 Subject: [PATCH 02/12] Created PaginationButton and changelog --- VDS.xcodeproj/project.pbxproj | 12 ++ VDS/Components/Pagination/Pagination.swift | 165 +++++------------- .../Pagination/PaginationButton.swift | 87 +++++++++ .../Pagination/PaginationCellItem.swift | 51 ++++++ .../Pagination/PaginationChangeLog.txt | 34 ++++ 5 files changed, 227 insertions(+), 122 deletions(-) create mode 100644 VDS/Components/Pagination/PaginationButton.swift create mode 100644 VDS/Components/Pagination/PaginationCellItem.swift create mode 100644 VDS/Components/Pagination/PaginationChangeLog.txt diff --git a/VDS.xcodeproj/project.pbxproj b/VDS.xcodeproj/project.pbxproj index 79deb46b..d37e3f8c 100644 --- a/VDS.xcodeproj/project.pbxproj +++ b/VDS.xcodeproj/project.pbxproj @@ -17,8 +17,11 @@ 5FC35BE328D51405004EBEAC /* Button.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FC35BE228D51405004EBEAC /* Button.swift */; }; 7115BD3C2B84C0C200E0A610 /* TileContainerChangeLog.txt in Resources */ = {isa = PBXBuildFile; fileRef = 7115BD3B2B84C0C200E0A610 /* TileContainerChangeLog.txt */; }; 71B23C2D2B91FA690027F7D9 /* Pagination.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71B23C2C2B91FA690027F7D9 /* Pagination.swift */; }; + 71B5FCBB2B95A0CA00269BCC /* PaginationChangeLog.txt in Resources */ = {isa = PBXBuildFile; fileRef = 71B5FCBA2B95A0CA00269BCC /* PaginationChangeLog.txt */; }; 71BFA70A2B7F70E6000DCE33 /* Dropshadowable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71BFA7092B7F70E6000DCE33 /* Dropshadowable.swift */; }; 71C02B382B7BD98F00E93E66 /* NotificationChangeLog.txt in Resources */ = {isa = PBXBuildFile; fileRef = 71C02B372B7BD98F00E93E66 /* NotificationChangeLog.txt */; }; + 71FC86DA2B96F44C00700965 /* PaginationButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71FC86D92B96F44C00700965 /* PaginationButton.swift */; }; + 71FC86DC2B96F4C800700965 /* PaginationCellItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71FC86DB2B96F4C800700965 /* PaginationCellItem.swift */; }; EA0B18022A9E236900F2D0CD /* SelectorGroupBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA0B18012A9E236900F2D0CD /* SelectorGroupBase.swift */; }; EA0B18052A9E2D2D00F2D0CD /* SelectorBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA0B18032A9E2D2D00F2D0CD /* SelectorBase.swift */; }; EA0B18062A9E2D2D00F2D0CD /* SelectorItemBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA0B18042A9E2D2D00F2D0CD /* SelectorItemBase.swift */; }; @@ -186,8 +189,11 @@ 5FC35BE228D51405004EBEAC /* Button.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Button.swift; sourceTree = ""; }; 7115BD3B2B84C0C200E0A610 /* TileContainerChangeLog.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = TileContainerChangeLog.txt; sourceTree = ""; }; 71B23C2C2B91FA690027F7D9 /* Pagination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pagination.swift; sourceTree = ""; }; + 71B5FCBA2B95A0CA00269BCC /* PaginationChangeLog.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = PaginationChangeLog.txt; sourceTree = ""; }; 71BFA7092B7F70E6000DCE33 /* Dropshadowable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Dropshadowable.swift; sourceTree = ""; }; 71C02B372B7BD98F00E93E66 /* NotificationChangeLog.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = NotificationChangeLog.txt; sourceTree = ""; }; + 71FC86D92B96F44C00700965 /* PaginationButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginationButton.swift; sourceTree = ""; }; + 71FC86DB2B96F4C800700965 /* PaginationCellItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginationCellItem.swift; sourceTree = ""; }; EA0B18012A9E236900F2D0CD /* SelectorGroupBase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectorGroupBase.swift; sourceTree = ""; }; EA0B18032A9E2D2D00F2D0CD /* SelectorBase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectorBase.swift; sourceTree = ""; }; EA0B18042A9E2D2D00F2D0CD /* SelectorItemBase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectorItemBase.swift; sourceTree = ""; }; @@ -390,6 +396,9 @@ isa = PBXGroup; children = ( 71B23C2C2B91FA690027F7D9 /* Pagination.swift */, + 71FC86D92B96F44C00700965 /* PaginationButton.swift */, + 71FC86DB2B96F4C800700965 /* PaginationCellItem.swift */, + 71B5FCBA2B95A0CA00269BCC /* PaginationChangeLog.txt */, ); path = Pagination; sourceTree = ""; @@ -981,6 +990,7 @@ EAEEECA92B1F969700531FC2 /* TooltipChangeLog.txt in Resources */, EAEEEC9C2B1F8F0700531FC2 /* TextLinkCaretChangeLog.txt in Resources */, EAA5EEE428F5B855003B3210 /* VerizonNHGDS-Light.otf in Resources */, + 71B5FCBB2B95A0CA00269BCC /* PaginationChangeLog.txt in Resources */, EAEEECAD2B1FC1A600531FC2 /* TitleLockupChangeLog.txt in Resources */, EAEEECAB2B1FBF2A00531FC2 /* ToggleChangeLog.txt in Resources */, ); @@ -1039,6 +1049,7 @@ EA985BEE2968A92400F2FF2E /* TitleLockupSubTitleModel.swift in Sources */, EA985BF22968B5BB00F2FF2E /* TitleLockupTextStyle.swift in Sources */, EAB1D2CD28ABE76100DAE764 /* Withable.swift in Sources */, + 71FC86DC2B96F4C800700965 /* PaginationCellItem.swift in Sources */, EAC846F3294B95CE00F685BA /* ButtonGroupCollectionViewCell.swift in Sources */, EAF7F0952899861000B287F5 /* CheckboxItem.swift in Sources */, EA985BE82968951C00F2FF2E /* TileletTitleModel.swift in Sources */, @@ -1055,6 +1066,7 @@ EAC9258F2911C9DE00091998 /* EntryFieldBase.swift in Sources */, EAB1D2EA28AE84AA00DAE764 /* UIControlPublisher.swift in Sources */, EAD068922A560B65002E3A2D /* LoaderViewController.swift in Sources */, + 71FC86DA2B96F44C00700965 /* PaginationButton.swift in Sources */, EABFEB642A26473700C4C106 /* NSAttributedString.swift in Sources */, EAF7F13328A2A16500B287F5 /* AttachmentLabelAttributeModel.swift in Sources */, EA0FC2C62914222900DF80B4 /* ButtonGroup.swift in Sources */, diff --git a/VDS/Components/Pagination/Pagination.swift b/VDS/Components/Pagination/Pagination.swift index 4df0a034..170e1935 100644 --- a/VDS/Components/Pagination/Pagination.swift +++ b/VDS/Components/Pagination/Pagination.swift @@ -12,37 +12,57 @@ import Combine @objc(VDSPagination) open class Pagination: View { - @Published var onPreviousTapped: PassthroughSubject = PassthroughSubject() - @Published var onNextTapped: PassthroughSubject = PassthroughSubject() - @Published var onPageWillChange: PassthroughSubject = PassthroughSubject() - @Published var onPageChanged: PassthroughSubject = PassthroughSubject() + open var onPageDidSelect: ((Int) -> Void)? + + public let previousButton: PaginationButton = .init(type: .previous) + public let nextButton: PaginationButton = .init(type: .next) public var total: Int = 0 { didSet { + previousButton.isHidden = true + nextButton.isHidden = total <= 1 + _selectedPage = 0 setNeedsUpdate() } } - public var selectedPage: Int = 0 { didSet { setNeedsUpdate() } } - private var numberOfRows: Int = 0 { - didSet { - collectionView.collectionViewLayout.invalidateLayout() + + public var selectedPage: Int { + set { + if newValue >= total { + _selectedPage = total - 1 + } else if newValue < 0 { + _selectedPage = 0 + } else { + _selectedPage = max(newValue - 1, 0) + } setNeedsUpdate() + updateSelection() + } + get { + _selectedPage } } + + private var _selectedPage: Int = 0 private let pageItemCellSize: CGSize = .init(width: 20, height: 16) private let spacingBetweenCell: CGFloat = VDSLayout.Spacing.space1X.value - private let buttonTintColorConfiguration = SurfaceColorConfiguration(VDSColor.paletteBlack, VDSColor.paletteWhite) - private let buttonTextColorConfiguration = SurfaceColorConfiguration(VDSColor.paletteBlack, VDSColor.paletteWhite) - private lazy var collectionView: UICollectionView = { + private let containerView: View = View().with { + $0.translatesAutoresizingMaskIntoConstraints = false + } + + private lazy var flowLayout: UICollectionViewFlowLayout = { let layout = UICollectionViewFlowLayout() - layout.itemSize = pageItemCellSize layout.scrollDirection = .horizontal layout.minimumInteritemSpacing = spacingBetweenCell - layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize layout.minimumLineSpacing = spacingBetweenCell layout.sectionInset = .zero - let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) + layout.estimatedItemSize = pageItemCellSize + return layout + }() + + private lazy var collectionView: UICollectionView = { + let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout) collectionView.isScrollEnabled = false collectionView.translatesAutoresizingMaskIntoConstraints = false collectionView.delegate = self @@ -54,59 +74,6 @@ open class Pagination: View { return collectionView }() - //TODO: Need to check with textStyle with Matt as its getter only in ButtonBase - private lazy var previousButton: ButtonBase = { - let previousButton: ButtonBase - if #available(iOS 15.0, *) { - var configuration = ButtonBase.Configuration.plain() - configuration.imagePadding = VDSLayout.Spacing.space2X.value - configuration.attributedTitle = AttributedString("Previous", attributes: AttributeContainer([NSAttributedString.Key.font: TextStyle.boldBodySmall.font])) - configuration.titleAlignment = .leading - configuration.imagePlacement = .leading - configuration.contentInsets = .zero - previousButton = ButtonBase(configuration: configuration) - } else { - previousButton = ButtonBase() - previousButton.imageEdgeInsets = .init(top: 0, left: 0, bottom: 0, right: VDSLayout.Spacing.space2X.value) - previousButton.setTitle("Previous", for: .normal) - previousButton.titleLabel?.font = TextStyle.boldBodySmall.font - } - previousButton.contentHorizontalAlignment = .leading - previousButton.translatesAutoresizingMaskIntoConstraints = false - previousButton.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) - previousButton.setImage(BundleManager.shared.image(for: "pagination-arrow-left")?.withRenderingMode(.alwaysTemplate), for: .normal) - return previousButton - }() - - private let nextButton: ButtonBase = { - let nextButton: ButtonBase - if #available(iOS 15.0, *) { - var configuration = ButtonBase.Configuration.plain() - configuration.imagePadding = VDSLayout.Spacing.space2X.value - configuration.attributedTitle = AttributedString("Next", attributes: AttributeContainer([NSAttributedString.Key.font: TextStyle.boldBodySmall.font])) - configuration.imagePlacement = .trailing - configuration.titleAlignment = .trailing - configuration.contentInsets = .zero - nextButton = ButtonBase(configuration: configuration) - } else { - nextButton = ButtonBase() - nextButton.imageEdgeInsets = .init(top: 0, left: 0, bottom: 0, right: VDSLayout.Spacing.space2X.value) - nextButton.semanticContentAttribute = .forceRightToLeft - nextButton.titleLabel?.font = TextStyle.boldBodySmall.font - nextButton.setTitle("Next", for: .normal) - } - //nextButton.textStyle = .boldBodySmall - nextButton.translatesAutoresizingMaskIntoConstraints = false - nextButton.contentHorizontalAlignment = .trailing - nextButton.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) - nextButton.setImage(BundleManager.shared.image(for: "pagination-arrow-right")?.withRenderingMode(.alwaysTemplate), for: .normal) - return nextButton - }() - - private let containerView: View = View().with { - $0.translatesAutoresizingMaskIntoConstraints = false - } - open override func initialSetup() { super.initialSetup() @@ -140,27 +107,27 @@ open class Pagination: View { open override func updateView() { super.updateView() - previousButton.tintColor = buttonTintColorConfiguration.getColor(surface) - nextButton.tintColor = buttonTintColorConfiguration.getColor(surface) - previousButton.setTitleColor(buttonTextColorConfiguration.getColor(surface), for: .normal) - nextButton.setTitleColor(buttonTextColorConfiguration.getColor(surface), for: .normal) + nextButton.surface = surface + previousButton.surface = surface collectionView.reloadData() } private func onbuttonTapped(_ sender: UIButton) { let isNextAction = sender == nextButton if isNextAction { - selectedPage += 1 + _selectedPage += 1 } else { - selectedPage -= 1 + _selectedPage -= 1 } updateSelection() } private func updateSelection() { - collectionView.scrollToItem(at: IndexPath(row: max(selectedPage-1, 0), section: 0), at: .left, animated: false) + let indexPath = IndexPath(row: selectedPage, section: 0) + collectionView.scrollToItem(at: indexPath, at: .left, animated: false) previousButton.isHidden = selectedPage == 0 nextButton.isHidden = selectedPage == total - 1 + collectionView.reloadData() } } @@ -174,55 +141,9 @@ extension Pagination: UICollectionViewDelegate, UICollectionViewDataSource, UICo return cell } - public func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool { - onPageWillChange.send(selectedPage) - return true - } - public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { - selectedPage = indexPath.row + _selectedPage = indexPath.row updateSelection() - onPageChanged.send(indexPath.row) - } -} - -internal final class PaginationCellItem: UICollectionViewCell { - - static let identifier: String = String(describing: PaginationCellItem.self) - private let textColorConfiguration = SurfaceColorConfiguration(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark) - - private var indexLabel: Label = Label().with { - $0.translatesAutoresizingMaskIntoConstraints = false - $0.textAlignment = .center - $0.numberOfLines = 1 - } - - override init(frame: CGRect) { - super.init(frame: frame) - setUp() - } - - required init?(coder: NSCoder) { - super.init(coder: coder) - setUp() - } - - private func setUp() { - let containerView = View() - containerView.translatesAutoresizingMaskIntoConstraints = false - containerView.addSubview(indexLabel) - contentView.addSubview(containerView) - containerView.pinToSuperView() - indexLabel.pinToSuperView() - indexLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: VDSLayout.Spacing.space5X.value).activate() - contentView.backgroundColor = .clear - containerView.backgroundColor = .clear - indexLabel.backgroundColor = .clear - } - - internal func update(_ selectedIndex: Int, currentIndex: Int, surface: Surface) { - indexLabel.textStyle = selectedIndex == currentIndex ? .boldBodySmall : .bodySmall - indexLabel.text = "\(currentIndex)" - indexLabel.textColor = textColorConfiguration.getColor(surface) + onPageDidSelect?(indexPath.row) } } diff --git a/VDS/Components/Pagination/PaginationButton.swift b/VDS/Components/Pagination/PaginationButton.swift new file mode 100644 index 00000000..6a1cd3fa --- /dev/null +++ b/VDS/Components/Pagination/PaginationButton.swift @@ -0,0 +1,87 @@ +// +// PaginationButton.swift +// VDS +// +// Created by Bandaru, Krishna Kishore on 05/03/24. +// + +import UIKit +import VDSColorTokens + +open class PaginationButton: ButtonBase { + + private let buttonTintColorConfiguration = SurfaceColorConfiguration(VDSColor.paletteBlack, VDSColor.paletteWhite) + private let buttonTextColorConfiguration = SurfaceColorConfiguration(VDSColor.paletteBlack, VDSColor.paletteWhite) + + @available(iOS 15.0, *) + var buttonConfiguration: Button.Configuration { + var configuration = ButtonBase.Configuration.plain() + configuration.imagePadding = VDSLayout.Spacing.space2X.value + configuration.imagePlacement = type == .next ? .trailing : .leading + configuration.titleAlignment = type == .next ? .trailing : .leading + configuration.contentInsets = .zero + return configuration + } + + open override var textStyle: TextStyle { TextStyle.boldBodySmall } + + open override var textColor: UIColor { buttonTextColorConfiguration.getColor(surface) } + + private var type: Type = .next + + init(type: Type) { + self.type = type + super.init() + } + + required public init() { + super.init() + } + + public required init?(coder: NSCoder) { + super.init(coder: coder) + } + + open override func initialSetup() { + super.initialSetup() + if #available(iOS 15.0, *) { + configuration = buttonConfiguration + } else { + semanticContentAttribute = type == .next ? .forceRightToLeft : .forceLeftToRight + imageEdgeInsets = .init(top: 0, left: 0, bottom: 0, right: VDSLayout.Spacing.space2X.value) + } + contentHorizontalAlignment = type == .next ? .trailing : .leading + } + + open override func updateView() { + text = type.title + setImage(type.image, for: .normal) + tintColor = buttonTintColorConfiguration.getColor(surface) + super.updateView() + } +} + +extension PaginationButton { + + enum `Type` { + case previous, next + + var title: String { + switch self { + case .next: + "Next" + case .previous: + "Previous" + } + } + + var image: UIImage? { + switch self { + case .previous: + BundleManager.shared.image(for: "pagination-arrow-left")?.withRenderingMode(.alwaysTemplate) + case .next: + BundleManager.shared.image(for: "pagination-arrow-right")?.withRenderingMode(.alwaysTemplate) + } + } + } +} diff --git a/VDS/Components/Pagination/PaginationCellItem.swift b/VDS/Components/Pagination/PaginationCellItem.swift new file mode 100644 index 00000000..1a546551 --- /dev/null +++ b/VDS/Components/Pagination/PaginationCellItem.swift @@ -0,0 +1,51 @@ +// +// PaginationCellItem.swift +// VDS +// +// Created by Bandaru, Krishna Kishore on 05/03/24. +// + +import UIKit +import VDSColorTokens + +final class PaginationCellItem: UICollectionViewCell { + + static let identifier: String = String(describing: PaginationCellItem.self) + + private let textColorConfiguration = SurfaceColorConfiguration(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark) + + private var indexLabel: Label = Label().with { + $0.translatesAutoresizingMaskIntoConstraints = false + $0.textAlignment = .center + $0.numberOfLines = 1 + } + + override init(frame: CGRect) { + super.init(frame: frame) + setUp() + } + + required init?(coder: NSCoder) { + super.init(coder: coder) + setUp() + } + + private func setUp() { + let containerView = View() + containerView.translatesAutoresizingMaskIntoConstraints = false + containerView.addSubview(indexLabel) + contentView.addSubview(containerView) + containerView.pinToSuperView() + indexLabel.pinToSuperView() + indexLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: VDSLayout.Spacing.space5X.value).activate() + contentView.backgroundColor = .clear + containerView.backgroundColor = .clear + indexLabel.backgroundColor = .clear + } + + func update(_ selectedIndex: Int, currentIndex: Int, surface: Surface) { + indexLabel.textStyle = selectedIndex == currentIndex ? .boldBodySmall : .bodySmall + indexLabel.text = "\(currentIndex + 1)" + indexLabel.textColor = textColorConfiguration.getColor(surface) + } +} diff --git a/VDS/Components/Pagination/PaginationChangeLog.txt b/VDS/Components/Pagination/PaginationChangeLog.txt new file mode 100644 index 00000000..66f0ac14 --- /dev/null +++ b/VDS/Components/Pagination/PaginationChangeLog.txt @@ -0,0 +1,34 @@ +MM/DD/YYYY +---------------- + +Initial Brand 3.0 handoff + +12/17/2021 +---------------- +- Replaced focusring colors (previously interactive/onlight/ondark) with accessibility/onlight/ondark colors +- Updated focus border name (previously interactive.focusring.onlight) with focusring.onlight/ondark + +02/28/2022 +---------------- +- Change Page Item Active to Page Item Selected. All Active references changed to Selected. + +03/01/2022 +---------------- +- Replaced Left and Right Arrow Non-Scaling icons with VDS Icon. +- Removed “weight” and “vector effect” from Anatomy frame. + +08/10/2022 +---------------- +- Updated default and inverted prop to light and dark surface. + +11/30/2022 +---------------- +- Added "(web only)" to any instance of "keyboard focus" + +12/13/2022 +---------------- +- Replaced focus border pixel and style & spacing values with tokens. + +01/12/2023 +---------------- +- Removed “Page Item Selected” from Anatomy. From cd85748a11732e632f0cb4e53e9641f42aac9f64 Mon Sep 17 00:00:00 2001 From: Krishna Kishore Bandaru Date: Wed, 6 Mar 2024 21:25:18 +0530 Subject: [PATCH 03/12] Fixed bugs, added comments and created new flow layout --- VDS.xcodeproj/project.pbxproj | 8 + VDS/Components/Pagination/Pagination.swift | 174 ++++++++++++------ .../Pagination/PaginationButton.swift | 35 +++- .../Pagination/PaginationCellItem.swift | 15 +- .../Pagination/PaginationFlowLayout.swift | 91 +++++++++ VDS/Utilities/Clamping.swift | 24 +++ 6 files changed, 276 insertions(+), 71 deletions(-) create mode 100644 VDS/Components/Pagination/PaginationFlowLayout.swift create mode 100644 VDS/Utilities/Clamping.swift diff --git a/VDS.xcodeproj/project.pbxproj b/VDS.xcodeproj/project.pbxproj index d37e3f8c..c70712cf 100644 --- a/VDS.xcodeproj/project.pbxproj +++ b/VDS.xcodeproj/project.pbxproj @@ -22,6 +22,8 @@ 71C02B382B7BD98F00E93E66 /* NotificationChangeLog.txt in Resources */ = {isa = PBXBuildFile; fileRef = 71C02B372B7BD98F00E93E66 /* NotificationChangeLog.txt */; }; 71FC86DA2B96F44C00700965 /* PaginationButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71FC86D92B96F44C00700965 /* PaginationButton.swift */; }; 71FC86DC2B96F4C800700965 /* PaginationCellItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71FC86DB2B96F4C800700965 /* PaginationCellItem.swift */; }; + 71FC86E22B97483000700965 /* Clamping.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71FC86E12B97483000700965 /* Clamping.swift */; }; + 71FC86E42B9841AC00700965 /* PaginationFlowLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71FC86E32B9841AC00700965 /* PaginationFlowLayout.swift */; }; EA0B18022A9E236900F2D0CD /* SelectorGroupBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA0B18012A9E236900F2D0CD /* SelectorGroupBase.swift */; }; EA0B18052A9E2D2D00F2D0CD /* SelectorBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA0B18032A9E2D2D00F2D0CD /* SelectorBase.swift */; }; EA0B18062A9E2D2D00F2D0CD /* SelectorItemBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA0B18042A9E2D2D00F2D0CD /* SelectorItemBase.swift */; }; @@ -194,6 +196,8 @@ 71C02B372B7BD98F00E93E66 /* NotificationChangeLog.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = NotificationChangeLog.txt; sourceTree = ""; }; 71FC86D92B96F44C00700965 /* PaginationButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginationButton.swift; sourceTree = ""; }; 71FC86DB2B96F4C800700965 /* PaginationCellItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginationCellItem.swift; sourceTree = ""; }; + 71FC86E12B97483000700965 /* Clamping.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Clamping.swift; sourceTree = ""; }; + 71FC86E32B9841AC00700965 /* PaginationFlowLayout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginationFlowLayout.swift; sourceTree = ""; }; EA0B18012A9E236900F2D0CD /* SelectorGroupBase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectorGroupBase.swift; sourceTree = ""; }; EA0B18032A9E2D2D00F2D0CD /* SelectorBase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectorBase.swift; sourceTree = ""; }; EA0B18042A9E2D2D00F2D0CD /* SelectorItemBase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectorItemBase.swift; sourceTree = ""; }; @@ -398,6 +402,7 @@ 71B23C2C2B91FA690027F7D9 /* Pagination.swift */, 71FC86D92B96F44C00700965 /* PaginationButton.swift */, 71FC86DB2B96F4C800700965 /* PaginationCellItem.swift */, + 71FC86E32B9841AC00700965 /* PaginationFlowLayout.swift */, 71B5FCBA2B95A0CA00269BCC /* PaginationChangeLog.txt */, ); path = Pagination; @@ -603,6 +608,7 @@ isa = PBXGroup; children = ( EA3361BC288B2C760071C351 /* TypeAlias.swift */, + 71FC86E12B97483000700965 /* Clamping.swift */, ); path = Utilities; sourceTree = ""; @@ -1022,6 +1028,7 @@ EA3361C328902D960071C351 /* Toggle.swift in Sources */, EAF7F0A0289AB7EC00B287F5 /* View.swift in Sources */, EA89201328B568D8006B9984 /* RadioBoxItem.swift in Sources */, + 71FC86E42B9841AC00700965 /* PaginationFlowLayout.swift in Sources */, EAC9258C2911C9DE00091998 /* InputField.swift in Sources */, EA3362402892EF6C0071C351 /* Label.swift in Sources */, EAB2376229E9880400AABE9A /* TrailingTooltipLabel.swift in Sources */, @@ -1030,6 +1037,7 @@ 71BFA70A2B7F70E6000DCE33 /* Dropshadowable.swift in Sources */, EA0D1C452A6AD73000E5C127 /* RawRepresentable.swift in Sources */, EA985C23296E033A00F2FF2E /* TextArea.swift in Sources */, + 71FC86E22B97483000700965 /* Clamping.swift in Sources */, EAF7F0B3289B1ADC00B287F5 /* ActionLabelAttribute.swift in Sources */, EAC925832911B35400091998 /* TextLinkCaret.swift in Sources */, EA33622E2891EA3C0071C351 /* DispatchQueue+Once.swift in Sources */, diff --git a/VDS/Components/Pagination/Pagination.swift b/VDS/Components/Pagination/Pagination.swift index 170e1935..f4bf8a60 100644 --- a/VDS/Components/Pagination/Pagination.swift +++ b/VDS/Components/Pagination/Pagination.swift @@ -9,58 +9,24 @@ import Foundation import VDSColorTokens import Combine +///Pagination is a control that enables customers to navigate multiple pages of content by selecting either a specific page or the next or previous set of four pages. @objc(VDSPagination) open class Pagination: View { - open var onPageDidSelect: ((Int) -> Void)? - - public let previousButton: PaginationButton = .init(type: .previous) - public let nextButton: PaginationButton = .init(type: .next) - - public var total: Int = 0 { - didSet { - previousButton.isHidden = true - nextButton.isHidden = total <= 1 - _selectedPage = 0 - setNeedsUpdate() - } - } - - public var selectedPage: Int { - set { - if newValue >= total { - _selectedPage = total - 1 - } else if newValue < 0 { - _selectedPage = 0 - } else { - _selectedPage = max(newValue - 1, 0) - } - setNeedsUpdate() - updateSelection() - } - get { - _selectedPage - } - } - - private var _selectedPage: Int = 0 - private let pageItemCellSize: CGSize = .init(width: 20, height: 16) - private let spacingBetweenCell: CGFloat = VDSLayout.Spacing.space1X.value - + //-------------------------------------------------- + // MARK: - Private Properties + //-------------------------------------------------- + ///Collectionview width anchor + private var collectionViewWidthAnchor: NSLayoutConstraint? + ///Selected page index + private var _selectedPageIndex: Int = 0 + ///Custom flow layout defined for the Pagination + private let flowLayout = PaginationFlowLayout() + ///A root view for the pagination private let containerView: View = View().with { $0.translatesAutoresizingMaskIntoConstraints = false } - - private lazy var flowLayout: UICollectionViewFlowLayout = { - let layout = UICollectionViewFlowLayout() - layout.scrollDirection = .horizontal - layout.minimumInteritemSpacing = spacingBetweenCell - layout.minimumLineSpacing = spacingBetweenCell - layout.sectionInset = .zero - layout.estimatedItemSize = pageItemCellSize - return layout - }() - + ///Collectionview to render pagination indexes private lazy var collectionView: UICollectionView = { let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout) collectionView.isScrollEnabled = false @@ -74,6 +40,48 @@ open class Pagination: View { return collectionView }() + //-------------------------------------------------- + // MARK: - Public Properties + //-------------------------------------------------- + ///Previous button to select previous page + public let previousButton: PaginationButton = .init(type: .previous) + ///Next button to select next page + public let nextButton: PaginationButton = .init(type: .next) + /// A callback when the page changes. Passes parameters (selectedPage). + public var onPageDidSelect: ((Int) -> Void)? + /// Total number of pages, allows limit ranging from 0 to 9999. + @Clamping(range: 0...9999) + public var total: Int { + didSet { + previousButton.isHidden = true + nextButton.isHidden = total <= 1 + _selectedPageIndex = 0 + setNeedsUpdate() + updateSelection() + } + } + ///Selected active page number and clips to total pages if selected index is greater than the total pages. + public var selectedPage: Int { + set { + if newValue >= total { + _selectedPageIndex = total - 1 + } else if newValue < 0 { + _selectedPageIndex = 0 + } else { + _selectedPageIndex = max(newValue - 1, 0) + } + setNeedsUpdate() + updateSelection() + } + get { + _selectedPageIndex + 1 //Returns selected page value not index + } + } + + //-------------------------------------------------- + // MARK: - Overrides + //-------------------------------------------------- + /// Executed on initialization for this View. open override func initialSetup() { super.initialSetup() @@ -94,56 +102,100 @@ open class Pagination: View { collectionView.centerYAnchor.constraint(equalTo: centerYAnchor).activate() collectionView.centerXAnchor.constraint(equalTo: centerXAnchor).activate() collectionView.trailingAnchor.constraint(greaterThanOrEqualTo: nextButton.leadingAnchor).activate() - collectionView.widthAnchor.constraint(equalToConstant: 92).activate() + collectionViewWidthAnchor = collectionView.widthAnchor.constraint(equalToConstant: 92) + collectionViewWidthAnchor?.activate() + nextButton .pinTop() .pinBottom() .pinTrailing() + nextButton.onClick = onbuttonTapped previousButton.onClick = onbuttonTapped previousButton.isHidden = true + + flowLayout.$collectionViewWidth + .receive(on: RunLoop.main) + .sink { [weak self] value in + self?.collectionViewWidthAnchor?.constant = value + }.store(in: &subscribers) } + /// Used to make changes to the View based off a change events or from local properties. open override func updateView() { super.updateView() - nextButton.surface = surface previousButton.surface = surface collectionView.reloadData() } + //-------------------------------------------------- + // MARK: - Private Methods + //-------------------------------------------------- + ///When previous/next button is tapped private func onbuttonTapped(_ sender: UIButton) { let isNextAction = sender == nextButton - if isNextAction { - _selectedPage += 1 - } else { - _selectedPage -= 1 - } + _selectedPageIndex = if isNextAction { _selectedPageIndex + 1 } else { _selectedPageIndex - 1 } updateSelection() } + ///Refreshing the UI based on the selected page private func updateSelection() { - let indexPath = IndexPath(row: selectedPage, section: 0) - collectionView.scrollToItem(at: indexPath, at: .left, animated: false) - previousButton.isHidden = selectedPage == 0 - nextButton.isHidden = selectedPage == total - 1 + guard _selectedPageIndex < total else { return } + collectionView.scrollToItem(at: IndexPath(row: _selectedPageIndex, section: 0), at: .left, animated: false) + previousButton.isHidden = _selectedPageIndex == 0 + nextButton.isHidden = _selectedPageIndex == total - 1 collectionView.reloadData() + verifyIfMaxDigitChanged() + } + + ///Identifying if there is any change in the digits of upcoming page + func verifyIfMaxDigitChanged() { + let upperLimitPage = _selectedPageIndex + flowLayout.maxNumberOfColumns + let upperLimitDigits = upperLimitPage.digitCount //future value digits + switch (flowLayout.numberOfColumns, upperLimitDigits) { + case (_, 3), (_, 4): + flowLayout.numberOfColumns = 3 + default: + flowLayout.numberOfColumns = 4 + } + if upperLimitDigits != flowLayout.upperLimitDigits { + flowLayout.upperLimitDigits = upperLimitDigits + flowLayout.invalidateLayout() + collectionView.reloadData() + collectionView.scrollToItem(at: IndexPath(row: self._selectedPageIndex, section: 0), at: .left, animated: false) + } } } extension Pagination: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { - + //-------------------------------------------------- + // MARK: - UICollectionView Delegate & Datasource + //-------------------------------------------------- public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { total } public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PaginationCellItem.identifier, for: indexPath) as? PaginationCellItem else { return UICollectionViewCell() } - cell.update(selectedPage, currentIndex: indexPath.row, surface: surface) + cell.update(_selectedPageIndex, currentIndex: indexPath.row, surface: surface) return cell } public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { - _selectedPage = indexPath.row + _selectedPageIndex = indexPath.row updateSelection() - onPageDidSelect?(indexPath.row) + onPageDidSelect?(selectedPage) + } +} + +fileprivate extension Int { + //-------------------------------------------------- + // MARK: - Extension on Int to identify number of digits in given number. + //-------------------------------------------------- + var digitCount: Int { + numberOfDigits(in: self) + } + + private func numberOfDigits(in number: Int) -> Int { + number < 10 && number >= 0 ? 1 : 1 + numberOfDigits(in: number/10) } } diff --git a/VDS/Components/Pagination/PaginationButton.swift b/VDS/Components/Pagination/PaginationButton.swift index 6a1cd3fa..3805051d 100644 --- a/VDS/Components/Pagination/PaginationButton.swift +++ b/VDS/Components/Pagination/PaginationButton.swift @@ -8,13 +8,20 @@ import UIKit import VDSColorTokens +///This is customised button for Pagination view open class PaginationButton: ButtonBase { - + //-------------------------------------------------- + // MARK: - Private Properties + //-------------------------------------------------- + /// Type of the PaginationButton + private var type: Type = .next + /// Button tint color configuration private let buttonTintColorConfiguration = SurfaceColorConfiguration(VDSColor.paletteBlack, VDSColor.paletteWhite) + /// Button title color configuration private let buttonTextColorConfiguration = SurfaceColorConfiguration(VDSColor.paletteBlack, VDSColor.paletteWhite) - + /// Button configuration for iOS 15+ @available(iOS 15.0, *) - var buttonConfiguration: Button.Configuration { + private var buttonConfiguration: Button.Configuration { var configuration = ButtonBase.Configuration.plain() configuration.imagePadding = VDSLayout.Spacing.space2X.value configuration.imagePlacement = type == .next ? .trailing : .leading @@ -23,12 +30,17 @@ open class PaginationButton: ButtonBase { return configuration } + //-------------------------------------------------- + // MARK: - Public Properties + //-------------------------------------------------- + /// TextStyle used on the titleLabel. open override var textStyle: TextStyle { TextStyle.boldBodySmall } - + /// UIColor used on the titleLabel text. open override var textColor: UIColor { buttonTextColorConfiguration.getColor(surface) } - private var type: Type = .next - + //-------------------------------------------------- + // MARK: - Initializers + //-------------------------------------------------- init(type: Type) { self.type = type super.init() @@ -42,6 +54,10 @@ open class PaginationButton: ButtonBase { super.init(coder: coder) } + //-------------------------------------------------- + // MARK: - Overrides + //-------------------------------------------------- + /// Executed on initialization for this View. open override func initialSetup() { super.initialSetup() if #available(iOS 15.0, *) { @@ -53,6 +69,7 @@ open class PaginationButton: ButtonBase { contentHorizontalAlignment = type == .next ? .trailing : .leading } + /// Used to make changes to the View based off a change events or from local properties. open override func updateView() { text = type.title setImage(type.image, for: .normal) @@ -62,7 +79,9 @@ open class PaginationButton: ButtonBase { } extension PaginationButton { - + //-------------------------------------------------- + // MARK: - Enum to configure PaginationButton + //-------------------------------------------------- enum `Type` { case previous, next @@ -74,7 +93,7 @@ extension PaginationButton { "Previous" } } - + ///Image for the configuration type var image: UIImage? { switch self { case .previous: diff --git a/VDS/Components/Pagination/PaginationCellItem.swift b/VDS/Components/Pagination/PaginationCellItem.swift index 1a546551..68fa7271 100644 --- a/VDS/Components/Pagination/PaginationCellItem.swift +++ b/VDS/Components/Pagination/PaginationCellItem.swift @@ -8,18 +8,27 @@ import UIKit import VDSColorTokens +///This is customised view for Pagination cell item final class PaginationCellItem: UICollectionViewCell { + ///Identifier for the PaginationCellItem static let identifier: String = String(describing: PaginationCellItem.self) - + + //-------------------------------------------------- + // MARK: - Private Properties + //-------------------------------------------------- + ///Text color configuration for the element private let textColorConfiguration = SurfaceColorConfiguration(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark) - + ///Pagination index label private var indexLabel: Label = Label().with { $0.translatesAutoresizingMaskIntoConstraints = false $0.textAlignment = .center $0.numberOfLines = 1 } + //-------------------------------------------------- + // MARK: - Initializers + //-------------------------------------------------- override init(frame: CGRect) { super.init(frame: frame) setUp() @@ -30,6 +39,7 @@ final class PaginationCellItem: UICollectionViewCell { setUp() } + ///Configuring the cell with default setup private func setUp() { let containerView = View() containerView.translatesAutoresizingMaskIntoConstraints = false @@ -43,6 +53,7 @@ final class PaginationCellItem: UICollectionViewCell { indexLabel.backgroundColor = .clear } + ///Updating UI based on selected index, current index along with surface func update(_ selectedIndex: Int, currentIndex: Int, surface: Surface) { indexLabel.textStyle = selectedIndex == currentIndex ? .boldBodySmall : .bodySmall indexLabel.text = "\(currentIndex + 1)" diff --git a/VDS/Components/Pagination/PaginationFlowLayout.swift b/VDS/Components/Pagination/PaginationFlowLayout.swift new file mode 100644 index 00000000..89debfe2 --- /dev/null +++ b/VDS/Components/Pagination/PaginationFlowLayout.swift @@ -0,0 +1,91 @@ +// +// PaginationFlowLayout.swift +// VDS +// +// Created by Bandaru, Krishna Kishore on 06/03/24. +// + +import Foundation +import UIKit + +///Customised flow layout for Pagination view +final class PaginationFlowLayout : UICollectionViewLayout { + //-------------------------------------------------- + // MARK: - Private Properties + //-------------------------------------------------- + ///Spacing between the pagination cells + private let spacingBetweenCell: CGFloat = VDSLayout.Spacing.space1X.value + ///Pre-defined sizes of the pagination cell based on number of digits. + private var upperLimitSize: CGSize { + switch upperLimitDigits { + case 3: .init(width: 28, height: 16) + case 4: .init(width: 34, height: 16) + default: .init(width: 20, height: 16) + } + } + ///Property to store the defined layout attributes. + private var itemCache : [UICollectionViewLayoutAttributes] = [] + + //-------------------------------------------------- + // MARK: - Internal Properties + //-------------------------------------------------- + ///Maximum number of page indexes shown on UI + let maxNumberOfColumns: Int = 4 + ///Number of digits of the maximum page index. + var upperLimitDigits: Int = 0 + ///Number of page indexes shown on UI. + var numberOfColumns: Int = 4 + ///A property that publishes when there is change in collection view width. + @Published var collectionViewWidth: CGFloat = 0 + + //-------------------------------------------------- + // MARK: - Overrides + //-------------------------------------------------- + ///Preparing the layout collection attributes for pagination and updating the collectionview width. + override func prepare() { + + guard let collectionView else { return } + + itemCache.removeAll() + var xPos : CGFloat = 0 + for item in 0.. [UICollectionViewLayoutAttributes]? { + var visibleLayoutAttributes: [UICollectionViewLayoutAttributes] = [] + for attributes in itemCache { + if attributes.frame.intersects(rect) { + visibleLayoutAttributes.append(attributes) + } + } + return visibleLayoutAttributes + } + + ///This will return the layout attributes at particular indexPath + override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { + return itemCache[indexPath.row] + } + + ///Returns the collectionview content size + override var collectionViewContentSize: CGSize { + guard let lastAttribute = itemCache.last else { return super.collectionViewContentSize } + return .init(width: lastAttribute.frame.width + lastAttribute.frame.origin.x, height: 16) + } +} diff --git a/VDS/Utilities/Clamping.swift b/VDS/Utilities/Clamping.swift new file mode 100644 index 00000000..c8213828 --- /dev/null +++ b/VDS/Utilities/Clamping.swift @@ -0,0 +1,24 @@ +// +// Clamping.swift +// VDS +// +// Created by Bandaru, Krishna Kishore on 05/03/24. +// + +import Foundation + +@propertyWrapper public struct Clamping { + + private var value: Value + private let range: ClosedRange + + public init(range: ClosedRange) { + self.value = range.lowerBound + self.range = range + } + + public var wrappedValue: Value { + get { value } + set { value = min(max(range.lowerBound, newValue), range.upperBound) } + } +} From ae12db4a2497738a2f2dc6c07fb3438fa6ad1382 Mon Sep 17 00:00:00 2001 From: Krishna Kishore Bandaru Date: Mon, 11 Mar 2024 16:25:32 +0530 Subject: [PATCH 04/12] reordering the Pagination folder & made selected index as second element --- VDS.xcodeproj/project.pbxproj | 18 +++++++----------- VDS/Components/Pagination/Pagination.swift | 7 +++++-- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/VDS.xcodeproj/project.pbxproj b/VDS.xcodeproj/project.pbxproj index 82643d0f..0b263712 100644 --- a/VDS.xcodeproj/project.pbxproj +++ b/VDS.xcodeproj/project.pbxproj @@ -18,16 +18,14 @@ 7115BD3C2B84C0C200E0A610 /* TileContainerChangeLog.txt in Resources */ = {isa = PBXBuildFile; fileRef = 7115BD3B2B84C0C200E0A610 /* TileContainerChangeLog.txt */; }; 71B23C2D2B91FA690027F7D9 /* Pagination.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71B23C2C2B91FA690027F7D9 /* Pagination.swift */; }; 71B5FCBB2B95A0CA00269BCC /* PaginationChangeLog.txt in Resources */ = {isa = PBXBuildFile; fileRef = 71B5FCBA2B95A0CA00269BCC /* PaginationChangeLog.txt */; }; - 71BFA70A2B7F70E6000DCE33 /* Dropshadowable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71BFA7092B7F70E6000DCE33 /* Dropshadowable.swift */; }; + 71BFA70A2B7F70E6000DCE33 /* DropShadowable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71BFA7092B7F70E6000DCE33 /* DropShadowable.swift */; }; 71C02B382B7BD98F00E93E66 /* NotificationChangeLog.txt in Resources */ = {isa = PBXBuildFile; fileRef = 71C02B372B7BD98F00E93E66 /* NotificationChangeLog.txt */; }; 71FC86DA2B96F44C00700965 /* PaginationButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71FC86D92B96F44C00700965 /* PaginationButton.swift */; }; 71FC86DC2B96F4C800700965 /* PaginationCellItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71FC86DB2B96F4C800700965 /* PaginationCellItem.swift */; }; - 71FC86E22B97483000700965 /* Clamping.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71FC86E12B97483000700965 /* Clamping.swift */; }; - 71FC86E42B9841AC00700965 /* PaginationFlowLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71FC86E32B9841AC00700965 /* PaginationFlowLayout.swift */; }; - 71BFA70A2B7F70E6000DCE33 /* DropShadowable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71BFA7092B7F70E6000DCE33 /* DropShadowable.swift */; }; - 71C02B382B7BD98F00E93E66 /* NotificationChangeLog.txt in Resources */ = {isa = PBXBuildFile; fileRef = 71C02B372B7BD98F00E93E66 /* NotificationChangeLog.txt */; }; 71FC86DE2B9738B900700965 /* SurfaceConfigurationValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71FC86DD2B9738B900700965 /* SurfaceConfigurationValue.swift */; }; 71FC86E02B973AE500700965 /* DropShadowConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71FC86DF2B973AE500700965 /* DropShadowConfiguration.swift */; }; + 71FC86E22B97483000700965 /* Clamping.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71FC86E12B97483000700965 /* Clamping.swift */; }; + 71FC86E42B9841AC00700965 /* PaginationFlowLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71FC86E32B9841AC00700965 /* PaginationFlowLayout.swift */; }; EA0B18022A9E236900F2D0CD /* SelectorGroupBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA0B18012A9E236900F2D0CD /* SelectorGroupBase.swift */; }; EA0B18052A9E2D2D00F2D0CD /* SelectorBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA0B18032A9E2D2D00F2D0CD /* SelectorBase.swift */; }; EA0B18062A9E2D2D00F2D0CD /* SelectorItemBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA0B18042A9E2D2D00F2D0CD /* SelectorItemBase.swift */; }; @@ -196,16 +194,14 @@ 7115BD3B2B84C0C200E0A610 /* TileContainerChangeLog.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = TileContainerChangeLog.txt; sourceTree = ""; }; 71B23C2C2B91FA690027F7D9 /* Pagination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pagination.swift; sourceTree = ""; }; 71B5FCBA2B95A0CA00269BCC /* PaginationChangeLog.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = PaginationChangeLog.txt; sourceTree = ""; }; - 71BFA7092B7F70E6000DCE33 /* Dropshadowable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Dropshadowable.swift; sourceTree = ""; }; + 71BFA7092B7F70E6000DCE33 /* DropShadowable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DropShadowable.swift; sourceTree = ""; }; 71C02B372B7BD98F00E93E66 /* NotificationChangeLog.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = NotificationChangeLog.txt; sourceTree = ""; }; 71FC86D92B96F44C00700965 /* PaginationButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginationButton.swift; sourceTree = ""; }; 71FC86DB2B96F4C800700965 /* PaginationCellItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginationCellItem.swift; sourceTree = ""; }; - 71FC86E12B97483000700965 /* Clamping.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Clamping.swift; sourceTree = ""; }; - 71FC86E32B9841AC00700965 /* PaginationFlowLayout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginationFlowLayout.swift; sourceTree = ""; }; - 71BFA7092B7F70E6000DCE33 /* DropShadowable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DropShadowable.swift; sourceTree = ""; }; - 71C02B372B7BD98F00E93E66 /* NotificationChangeLog.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = NotificationChangeLog.txt; sourceTree = ""; }; 71FC86DD2B9738B900700965 /* SurfaceConfigurationValue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SurfaceConfigurationValue.swift; sourceTree = ""; }; 71FC86DF2B973AE500700965 /* DropShadowConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DropShadowConfiguration.swift; sourceTree = ""; }; + 71FC86E12B97483000700965 /* Clamping.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Clamping.swift; sourceTree = ""; }; + 71FC86E32B9841AC00700965 /* PaginationFlowLayout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginationFlowLayout.swift; sourceTree = ""; }; EA0B18012A9E236900F2D0CD /* SelectorGroupBase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectorGroupBase.swift; sourceTree = ""; }; EA0B18032A9E2D2D00F2D0CD /* SelectorBase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectorBase.swift; sourceTree = ""; }; EA0B18042A9E2D2D00F2D0CD /* SelectorItemBase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectorItemBase.swift; sourceTree = ""; }; @@ -519,7 +515,6 @@ EA33619D288B1E330071C351 /* Components */ = { isa = PBXGroup; children = ( - 71B23C2B2B91FA510027F7D9 /* Pagination */, EA4DB2FE28DCBC1900103EE3 /* Badge */, EAD062AE2A3B87210015965D /* BadgeIndicator */, EA0FC2BE2912D18200DF80B4 /* Buttons */, @@ -529,6 +524,7 @@ 44604AD529CE195300E62B51 /* Line */, EAD0688C2A55F801002E3A2D /* Loader */, 445BA07629C07ABA0036A7C5 /* Notification */, + 71B23C2B2B91FA510027F7D9 /* Pagination */, EA89200B28B530F0006B9984 /* RadioBox */, EAF7F11428A1470D00B287F5 /* RadioButton */, EA596ABB2A16B4D500300C4B /* Tabs */, diff --git a/VDS/Components/Pagination/Pagination.swift b/VDS/Components/Pagination/Pagination.swift index f4bf8a60..1f00b949 100644 --- a/VDS/Components/Pagination/Pagination.swift +++ b/VDS/Components/Pagination/Pagination.swift @@ -142,7 +142,8 @@ open class Pagination: View { ///Refreshing the UI based on the selected page private func updateSelection() { guard _selectedPageIndex < total else { return } - collectionView.scrollToItem(at: IndexPath(row: _selectedPageIndex, section: 0), at: .left, animated: false) + //Need to make selected page as second element so scrolling previous index of the selected page to left + collectionView.scrollToItem(at: IndexPath(row: max(_selectedPageIndex - 1, 0), section: 0), at: .left, animated: false) previousButton.isHidden = _selectedPageIndex == 0 nextButton.isHidden = _selectedPageIndex == total - 1 collectionView.reloadData() @@ -163,7 +164,8 @@ open class Pagination: View { flowLayout.upperLimitDigits = upperLimitDigits flowLayout.invalidateLayout() collectionView.reloadData() - collectionView.scrollToItem(at: IndexPath(row: self._selectedPageIndex, section: 0), at: .left, animated: false) + //Need to make selected page as second element so scrolling previous index of the selected page to left + collectionView.scrollToItem(at: IndexPath(row: max(_selectedPageIndex - 1, 0), section: 0), at: .left, animated: false) } } } @@ -181,6 +183,7 @@ extension Pagination: UICollectionViewDelegate, UICollectionViewDataSource, UICo } public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { + guard _selectedPageIndex != indexPath.row else { return } _selectedPageIndex = indexPath.row updateSelection() onPageDidSelect?(selectedPage) From 3a064c914ce016a43c6e723ac8fbe18931c2f137 Mon Sep 17 00:00:00 2001 From: Krishna Kishore Bandaru Date: Mon, 11 Mar 2024 18:08:33 +0530 Subject: [PATCH 05/12] added Pagination in VDS.md --- VDS/VDS.docc/VDS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/VDS/VDS.docc/VDS.md b/VDS/VDS.docc/VDS.md index 8beb9255..dae2c72a 100755 --- a/VDS/VDS.docc/VDS.md +++ b/VDS/VDS.docc/VDS.md @@ -33,6 +33,7 @@ Using the system allows designers and developers to collaborate more easily and - ``Line`` - ``Loader`` - ``Notification`` +- ``Pagination`` - ``RadioBoxItem`` - ``RadioBoxGroup`` - ``RadioButton`` From 024194e7e56a9c8a6bff0ebb75a9d6d9003febbb Mon Sep 17 00:00:00 2001 From: Krishna Kishore Bandaru Date: Tue, 12 Mar 2024 18:24:48 +0530 Subject: [PATCH 06/12] Updated accessibility, add addressed review comments --- VDS.xcodeproj/project.pbxproj | 4 + VDS/Components/Pagination/Pagination.swift | 82 ++++++++++++------- .../Pagination/PaginationCellItem.swift | 1 + .../Pagination/PaginationCollectionView.swift | 66 +++++++++++++++ 4 files changed, 122 insertions(+), 31 deletions(-) create mode 100644 VDS/Components/Pagination/PaginationCollectionView.swift diff --git a/VDS.xcodeproj/project.pbxproj b/VDS.xcodeproj/project.pbxproj index 0b263712..38a79f1e 100644 --- a/VDS.xcodeproj/project.pbxproj +++ b/VDS.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ 5F21D7BF28DCEB3D003E7CD6 /* Useable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F21D7BE28DCEB3D003E7CD6 /* Useable.swift */; }; 5FC35BE328D51405004EBEAC /* Button.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FC35BE228D51405004EBEAC /* Button.swift */; }; 7115BD3C2B84C0C200E0A610 /* TileContainerChangeLog.txt in Resources */ = {isa = PBXBuildFile; fileRef = 7115BD3B2B84C0C200E0A610 /* TileContainerChangeLog.txt */; }; + 71ACE89C2BA0451200FB6ADC /* PaginationCollectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71ACE89B2BA0451200FB6ADC /* PaginationCollectionView.swift */; }; 71B23C2D2B91FA690027F7D9 /* Pagination.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71B23C2C2B91FA690027F7D9 /* Pagination.swift */; }; 71B5FCBB2B95A0CA00269BCC /* PaginationChangeLog.txt in Resources */ = {isa = PBXBuildFile; fileRef = 71B5FCBA2B95A0CA00269BCC /* PaginationChangeLog.txt */; }; 71BFA70A2B7F70E6000DCE33 /* DropShadowable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71BFA7092B7F70E6000DCE33 /* DropShadowable.swift */; }; @@ -192,6 +193,7 @@ 5F21D7BE28DCEB3D003E7CD6 /* Useable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Useable.swift; sourceTree = ""; }; 5FC35BE228D51405004EBEAC /* Button.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Button.swift; sourceTree = ""; }; 7115BD3B2B84C0C200E0A610 /* TileContainerChangeLog.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = TileContainerChangeLog.txt; sourceTree = ""; }; + 71ACE89B2BA0451200FB6ADC /* PaginationCollectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginationCollectionView.swift; sourceTree = ""; }; 71B23C2C2B91FA690027F7D9 /* Pagination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pagination.swift; sourceTree = ""; }; 71B5FCBA2B95A0CA00269BCC /* PaginationChangeLog.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = PaginationChangeLog.txt; sourceTree = ""; }; 71BFA7092B7F70E6000DCE33 /* DropShadowable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DropShadowable.swift; sourceTree = ""; }; @@ -404,6 +406,7 @@ isa = PBXGroup; children = ( 71B23C2C2B91FA690027F7D9 /* Pagination.swift */, + 71ACE89B2BA0451200FB6ADC /* PaginationCollectionView.swift */, 71FC86D92B96F44C00700965 /* PaginationButton.swift */, 71FC86DB2B96F4C800700965 /* PaginationCellItem.swift */, 71FC86E32B9841AC00700965 /* PaginationFlowLayout.swift */, @@ -1056,6 +1059,7 @@ EAC925842911C63100091998 /* Colorable.swift in Sources */, EAB5FEF5292D371F00998C17 /* ButtonBase.swift in Sources */, EA978EC5291D6AFE00ACC883 /* AnyLabelAttribute.swift in Sources */, + 71ACE89C2BA0451200FB6ADC /* PaginationCollectionView.swift in Sources */, EAC71A1F2A2E173D00E47A9F /* RadioButton.swift in Sources */, EA33622C2891E73B0071C351 /* FontProtocol.swift in Sources */, EA596ABD2A16B4EC00300C4B /* Tab.swift in Sources */, diff --git a/VDS/Components/Pagination/Pagination.swift b/VDS/Components/Pagination/Pagination.swift index 1f00b949..754935b7 100644 --- a/VDS/Components/Pagination/Pagination.swift +++ b/VDS/Components/Pagination/Pagination.swift @@ -21,24 +21,16 @@ open class Pagination: View { ///Selected page index private var _selectedPageIndex: Int = 0 ///Custom flow layout defined for the Pagination - private let flowLayout = PaginationFlowLayout() + private var flowLayout: PaginationFlowLayout { + guard let flowLayout = collectionContainerView.collectionView.collectionViewLayout as? PaginationFlowLayout else { fatalError("Flow layout should be PaginationFlowLayout class") } + return flowLayout + } ///A root view for the pagination private let containerView: View = View().with { $0.translatesAutoresizingMaskIntoConstraints = false } - ///Collectionview to render pagination indexes - private lazy var collectionView: UICollectionView = { - let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout) - collectionView.isScrollEnabled = false - collectionView.translatesAutoresizingMaskIntoConstraints = false - collectionView.delegate = self - collectionView.dataSource = self - collectionView.showsHorizontalScrollIndicator = false - collectionView.showsVerticalScrollIndicator = false - collectionView.register(PaginationCellItem.self, forCellWithReuseIdentifier: PaginationCellItem.identifier) - collectionView.backgroundColor = .clear - return collectionView - }() + ///Container view to hold collectionview to render pagination indexes + private let collectionContainerView = PaginationCollectionView() //-------------------------------------------------- // MARK: - Public Properties @@ -86,24 +78,32 @@ open class Pagination: View { super.initialSetup() addSubview(containerView) - containerView.pinToSuperView() - containerView.widthAnchor.constraint(greaterThanOrEqualToConstant: 288).activate() + containerView + .pinTop() + .pinBottom() + containerView.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor).activate() + trailingAnchor.constraint(greaterThanOrEqualTo: containerView.trailingAnchor).activate() + containerView.centerXAnchor.constraint(equalTo: centerXAnchor).activate() + containerView.widthAnchor.constraint(equalToConstant: 288).activate() containerView.heightAnchor.constraint(equalToConstant: 44).activate() containerView.addSubview(previousButton) - containerView.addSubview(collectionView) + containerView.addSubview(collectionContainerView) containerView.addSubview(nextButton) previousButton .pinTop() .pinBottom() .pinLeading() - previousButton.trailingAnchor.constraint(greaterThanOrEqualTo: collectionView.leadingAnchor).activate() - collectionView.heightAnchor.constraint(equalToConstant: VDSLayout.Spacing.space4X.value).activate() - collectionView.centerYAnchor.constraint(equalTo: centerYAnchor).activate() - collectionView.centerXAnchor.constraint(equalTo: centerXAnchor).activate() - collectionView.trailingAnchor.constraint(greaterThanOrEqualTo: nextButton.leadingAnchor).activate() - collectionViewWidthAnchor = collectionView.widthAnchor.constraint(equalToConstant: 92) + + previousButton.trailingAnchor.constraint(greaterThanOrEqualTo: collectionContainerView.leadingAnchor).activate() + collectionContainerView.heightAnchor.constraint(equalToConstant: VDSLayout.Spacing.space4X.value).activate() + collectionContainerView.centerYAnchor.constraint(equalTo: centerYAnchor).activate() + collectionContainerView.centerXAnchor.constraint(equalTo: centerXAnchor).activate() + collectionContainerView.trailingAnchor.constraint(greaterThanOrEqualTo: nextButton.leadingAnchor).activate() + collectionViewWidthAnchor = collectionContainerView.widthAnchor.constraint(equalToConstant: 92) collectionViewWidthAnchor?.activate() + collectionContainerView.collectionView.delegate = self + collectionContainerView.collectionView.dataSource = self nextButton .pinTop() @@ -117,8 +117,24 @@ open class Pagination: View { flowLayout.$collectionViewWidth .receive(on: RunLoop.main) .sink { [weak self] value in - self?.collectionViewWidthAnchor?.constant = value - }.store(in: &subscribers) + self?.collectionViewWidthAnchor?.constant = value //As cell width is dynamic i.e cell may contain 2 or 3 or 4 charcters. Make sure that all the visible cells are displayed. + }.store(in: &subscribers) + collectionContainerView.onAccessibilityIncrement = { [weak self] in + guard let self else { return } + self.selectedPage = max(0, self.selectedPage + 1) + } + collectionContainerView.onAccessibilityDecrement = { [weak self] in + guard let self else { return } + self.selectedPage = max(0, self.selectedPage - 1) + } + } + + ///Updating the accessiblity values i.e elements, label, value other items for the component. + open override func updateAccessibility() { + super.updateAccessibility() + accessibilityElements = [previousButton, collectionContainerView, nextButton] + collectionContainerView.accessibilityLabel = "Pagination containing \(total) pages" + collectionContainerView.accessibilityValue = "Page \(selectedPage) of \(total) selected" } /// Used to make changes to the View based off a change events or from local properties. @@ -126,7 +142,7 @@ open class Pagination: View { super.updateView() nextButton.surface = surface previousButton.surface = surface - collectionView.reloadData() + collectionContainerView.collectionView.reloadData() } //-------------------------------------------------- @@ -137,21 +153,25 @@ open class Pagination: View { let isNextAction = sender == nextButton _selectedPageIndex = if isNextAction { _selectedPageIndex + 1 } else { _selectedPageIndex - 1 } updateSelection() + DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) { [weak self] in + guard let self else { return } + UIAccessibility.post(notification: .announcement, argument: "Page \(self.selectedPage) of \(self.total) selected") + } } ///Refreshing the UI based on the selected page private func updateSelection() { guard _selectedPageIndex < total else { return } //Need to make selected page as second element so scrolling previous index of the selected page to left - collectionView.scrollToItem(at: IndexPath(row: max(_selectedPageIndex - 1, 0), section: 0), at: .left, animated: false) + collectionContainerView.collectionView.scrollToItem(at: IndexPath(row: max(_selectedPageIndex - 1, 0), section: 0), at: .left, animated: false) previousButton.isHidden = _selectedPageIndex == 0 nextButton.isHidden = _selectedPageIndex == total - 1 - collectionView.reloadData() + collectionContainerView.collectionView.reloadData() verifyIfMaxDigitChanged() } ///Identifying if there is any change in the digits of upcoming page - func verifyIfMaxDigitChanged() { + private func verifyIfMaxDigitChanged() { let upperLimitPage = _selectedPageIndex + flowLayout.maxNumberOfColumns let upperLimitDigits = upperLimitPage.digitCount //future value digits switch (flowLayout.numberOfColumns, upperLimitDigits) { @@ -163,9 +183,9 @@ open class Pagination: View { if upperLimitDigits != flowLayout.upperLimitDigits { flowLayout.upperLimitDigits = upperLimitDigits flowLayout.invalidateLayout() - collectionView.reloadData() + collectionContainerView.collectionView.reloadData() //Need to make selected page as second element so scrolling previous index of the selected page to left - collectionView.scrollToItem(at: IndexPath(row: max(_selectedPageIndex - 1, 0), section: 0), at: .left, animated: false) + collectionContainerView.collectionView.scrollToItem(at: IndexPath(row: max(_selectedPageIndex - 1, 0), section: 0), at: .left, animated: false) } } } diff --git a/VDS/Components/Pagination/PaginationCellItem.swift b/VDS/Components/Pagination/PaginationCellItem.swift index 68fa7271..68f51142 100644 --- a/VDS/Components/Pagination/PaginationCellItem.swift +++ b/VDS/Components/Pagination/PaginationCellItem.swift @@ -23,6 +23,7 @@ final class PaginationCellItem: UICollectionViewCell { private var indexLabel: Label = Label().with { $0.translatesAutoresizingMaskIntoConstraints = false $0.textAlignment = .center + $0.isAccessibilityElement = false $0.numberOfLines = 1 } diff --git a/VDS/Components/Pagination/PaginationCollectionView.swift b/VDS/Components/Pagination/PaginationCollectionView.swift new file mode 100644 index 00000000..a41c65ef --- /dev/null +++ b/VDS/Components/Pagination/PaginationCollectionView.swift @@ -0,0 +1,66 @@ +// +// PaginationCollectionView.swift +// VDS +// +// Created by Bandaru, Krishna Kishore on 12/03/24. +// + +import UIKit + +///PaginationCollectionView is a container view that holds collectionview for displaying page indexes +final class PaginationCollectionView: View { + + //-------------------------------------------------- + // MARK: - Internal Properties + //-------------------------------------------------- + ///Notifies when accessibility increment is happend when user swipes up + var onAccessibilityIncrement: (() -> Void)? + ///Notifies when accessibility decrement is happend when user swipes down + var onAccessibilityDecrement: (() -> Void)? + ///Collectionview to render pagination indexes + lazy var collectionView: UICollectionView = { + let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout) + collectionView.isScrollEnabled = false + collectionView.translatesAutoresizingMaskIntoConstraints = false + collectionView.showsHorizontalScrollIndicator = false + collectionView.showsVerticalScrollIndicator = false + collectionView.isAccessibilityElement = true + collectionView.register(PaginationCellItem.self, forCellWithReuseIdentifier: PaginationCellItem.identifier) + collectionView.backgroundColor = .clear + return collectionView + }() + + //-------------------------------------------------- + // MARK: - Private Properties + //-------------------------------------------------- + ///Custom flow layout defined for the Pagination + private let flowLayout = PaginationFlowLayout() + + //-------------------------------------------------- + // MARK: - Overrides + //-------------------------------------------------- + ///Accessibilty traits for the Pagination view + override var accessibilityTraits: UIAccessibilityTraits { + get { [.adjustable] } + set { } + } + + ///Accessibilty increment + override func accessibilityIncrement() { + onAccessibilityIncrement?() + } + + ///Accessibilty decrement + override func accessibilityDecrement() { + onAccessibilityDecrement?() + } + + /// Executed on initialization for this View. + override func setup() { + super.setup() + addSubview(collectionView) + collectionView.pinToSuperView() + isAccessibilityElement = true + accessibilityElements = [collectionView] + } +} From 4cc1d5287d70815089dfdbf238946d2ceb0a63ac Mon Sep 17 00:00:00 2001 From: Krishna Kishore Bandaru Date: Wed, 13 Mar 2024 12:59:05 +0530 Subject: [PATCH 07/12] updated cell size for 3 & 4 digits --- VDS/Components/Pagination/PaginationFlowLayout.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/VDS/Components/Pagination/PaginationFlowLayout.swift b/VDS/Components/Pagination/PaginationFlowLayout.swift index 89debfe2..542e4eff 100644 --- a/VDS/Components/Pagination/PaginationFlowLayout.swift +++ b/VDS/Components/Pagination/PaginationFlowLayout.swift @@ -18,8 +18,7 @@ final class PaginationFlowLayout : UICollectionViewLayout { ///Pre-defined sizes of the pagination cell based on number of digits. private var upperLimitSize: CGSize { switch upperLimitDigits { - case 3: .init(width: 28, height: 16) - case 4: .init(width: 34, height: 16) + case 3, 4: .init(width: 34, height: 16) default: .init(width: 20, height: 16) } } From 9c8437fe6c1dc482fad0419716f611a47d952fb2 Mon Sep 17 00:00:00 2001 From: Krishna Kishore Bandaru Date: Wed, 13 Mar 2024 22:28:52 +0530 Subject: [PATCH 08/12] Fixed layout issues --- VDS.xcodeproj/project.pbxproj | 8 +-- VDS/Components/Pagination/Pagination.swift | 62 ++++++++++++------- ...ew.swift => PaginationContainerView.swift} | 25 +------- .../Pagination/PaginationFlowLayout.swift | 5 +- 4 files changed, 48 insertions(+), 52 deletions(-) rename VDS/Components/Pagination/{PaginationCollectionView.swift => PaginationContainerView.swift} (52%) diff --git a/VDS.xcodeproj/project.pbxproj b/VDS.xcodeproj/project.pbxproj index 38a79f1e..f57edb0f 100644 --- a/VDS.xcodeproj/project.pbxproj +++ b/VDS.xcodeproj/project.pbxproj @@ -16,7 +16,7 @@ 5F21D7BF28DCEB3D003E7CD6 /* Useable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F21D7BE28DCEB3D003E7CD6 /* Useable.swift */; }; 5FC35BE328D51405004EBEAC /* Button.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FC35BE228D51405004EBEAC /* Button.swift */; }; 7115BD3C2B84C0C200E0A610 /* TileContainerChangeLog.txt in Resources */ = {isa = PBXBuildFile; fileRef = 7115BD3B2B84C0C200E0A610 /* TileContainerChangeLog.txt */; }; - 71ACE89C2BA0451200FB6ADC /* PaginationCollectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71ACE89B2BA0451200FB6ADC /* PaginationCollectionView.swift */; }; + 71ACE89C2BA0451200FB6ADC /* PaginationContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71ACE89B2BA0451200FB6ADC /* PaginationContainerView.swift */; }; 71B23C2D2B91FA690027F7D9 /* Pagination.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71B23C2C2B91FA690027F7D9 /* Pagination.swift */; }; 71B5FCBB2B95A0CA00269BCC /* PaginationChangeLog.txt in Resources */ = {isa = PBXBuildFile; fileRef = 71B5FCBA2B95A0CA00269BCC /* PaginationChangeLog.txt */; }; 71BFA70A2B7F70E6000DCE33 /* DropShadowable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71BFA7092B7F70E6000DCE33 /* DropShadowable.swift */; }; @@ -193,7 +193,7 @@ 5F21D7BE28DCEB3D003E7CD6 /* Useable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Useable.swift; sourceTree = ""; }; 5FC35BE228D51405004EBEAC /* Button.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Button.swift; sourceTree = ""; }; 7115BD3B2B84C0C200E0A610 /* TileContainerChangeLog.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = TileContainerChangeLog.txt; sourceTree = ""; }; - 71ACE89B2BA0451200FB6ADC /* PaginationCollectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginationCollectionView.swift; sourceTree = ""; }; + 71ACE89B2BA0451200FB6ADC /* PaginationContainerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginationContainerView.swift; sourceTree = ""; }; 71B23C2C2B91FA690027F7D9 /* Pagination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pagination.swift; sourceTree = ""; }; 71B5FCBA2B95A0CA00269BCC /* PaginationChangeLog.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = PaginationChangeLog.txt; sourceTree = ""; }; 71BFA7092B7F70E6000DCE33 /* DropShadowable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DropShadowable.swift; sourceTree = ""; }; @@ -406,7 +406,7 @@ isa = PBXGroup; children = ( 71B23C2C2B91FA690027F7D9 /* Pagination.swift */, - 71ACE89B2BA0451200FB6ADC /* PaginationCollectionView.swift */, + 71ACE89B2BA0451200FB6ADC /* PaginationContainerView.swift */, 71FC86D92B96F44C00700965 /* PaginationButton.swift */, 71FC86DB2B96F4C800700965 /* PaginationCellItem.swift */, 71FC86E32B9841AC00700965 /* PaginationFlowLayout.swift */, @@ -1059,7 +1059,7 @@ EAC925842911C63100091998 /* Colorable.swift in Sources */, EAB5FEF5292D371F00998C17 /* ButtonBase.swift in Sources */, EA978EC5291D6AFE00ACC883 /* AnyLabelAttribute.swift in Sources */, - 71ACE89C2BA0451200FB6ADC /* PaginationCollectionView.swift in Sources */, + 71ACE89C2BA0451200FB6ADC /* PaginationContainerView.swift in Sources */, EAC71A1F2A2E173D00E47A9F /* RadioButton.swift in Sources */, EA33622C2891E73B0071C351 /* FontProtocol.swift in Sources */, EA596ABD2A16B4EC00300C4B /* Tab.swift in Sources */, diff --git a/VDS/Components/Pagination/Pagination.swift b/VDS/Components/Pagination/Pagination.swift index 754935b7..7728d6dc 100644 --- a/VDS/Components/Pagination/Pagination.swift +++ b/VDS/Components/Pagination/Pagination.swift @@ -16,21 +16,36 @@ open class Pagination: View { //-------------------------------------------------- // MARK: - Private Properties //-------------------------------------------------- + ///Maximum component width + private let maxWidth: CGFloat = 288.0 ///Collectionview width anchor private var collectionViewWidthAnchor: NSLayoutConstraint? + ///Collectionview container Center X constraint + private var collectionContainerViewCenterXConstraint: NSLayoutConstraint? ///Selected page index private var _selectedPageIndex: Int = 0 ///Custom flow layout defined for the Pagination - private var flowLayout: PaginationFlowLayout { - guard let flowLayout = collectionContainerView.collectionView.collectionViewLayout as? PaginationFlowLayout else { fatalError("Flow layout should be PaginationFlowLayout class") } - return flowLayout - } + private let flowLayout = PaginationFlowLayout() ///A root view for the pagination - private let containerView: View = View().with { + public let containerView: View = View().with { $0.translatesAutoresizingMaskIntoConstraints = false } - ///Container view to hold collectionview to render pagination indexes - private let collectionContainerView = PaginationCollectionView() + ///Collectionview to render pagination indexes + private lazy var collectionView: UICollectionView = { + let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout) + collectionView.isScrollEnabled = false + collectionView.translatesAutoresizingMaskIntoConstraints = false + collectionView.showsHorizontalScrollIndicator = false + collectionView.showsVerticalScrollIndicator = false + collectionView.isAccessibilityElement = true + collectionView.register(PaginationCellItem.self, forCellWithReuseIdentifier: PaginationCellItem.identifier) + collectionView.backgroundColor = .clear + collectionView.delegate = self + collectionView.dataSource = self + return collectionView + }() + ///Container view to hold collectionview to render pagination indexes and to handler accessibility. + private let collectionContainerView = PaginationContainerView() //-------------------------------------------------- // MARK: - Public Properties @@ -84,26 +99,27 @@ open class Pagination: View { containerView.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor).activate() trailingAnchor.constraint(greaterThanOrEqualTo: containerView.trailingAnchor).activate() containerView.centerXAnchor.constraint(equalTo: centerXAnchor).activate() - containerView.widthAnchor.constraint(equalToConstant: 288).activate() + containerView.widthAnchor.constraint(equalToConstant: maxWidth).activate() containerView.heightAnchor.constraint(equalToConstant: 44).activate() containerView.addSubview(previousButton) containerView.addSubview(collectionContainerView) containerView.addSubview(nextButton) - + collectionContainerView.addSubview(collectionView) previousButton .pinTop() .pinBottom() .pinLeading() previousButton.trailingAnchor.constraint(greaterThanOrEqualTo: collectionContainerView.leadingAnchor).activate() - collectionContainerView.heightAnchor.constraint(equalToConstant: VDSLayout.Spacing.space4X.value).activate() - collectionContainerView.centerYAnchor.constraint(equalTo: centerYAnchor).activate() - collectionContainerView.centerXAnchor.constraint(equalTo: centerXAnchor).activate() collectionContainerView.trailingAnchor.constraint(greaterThanOrEqualTo: nextButton.leadingAnchor).activate() - collectionViewWidthAnchor = collectionContainerView.widthAnchor.constraint(equalToConstant: 92) + collectionContainerView + .pinTop() + .pinBottom() + collectionView.heightAnchor.constraint(equalToConstant: VDSLayout.Spacing.space4X.value).activate() + collectionView.centerYAnchor.constraint(equalTo: centerYAnchor).activate() + collectionView.centerXAnchor.constraint(equalTo: collectionContainerView.centerXAnchor).activate() + collectionViewWidthAnchor = collectionView.widthAnchor.constraint(equalToConstant: 92) collectionViewWidthAnchor?.activate() - collectionContainerView.collectionView.delegate = self - collectionContainerView.collectionView.dataSource = self nextButton .pinTop() @@ -142,7 +158,7 @@ open class Pagination: View { super.updateView() nextButton.surface = surface previousButton.surface = surface - collectionContainerView.collectionView.reloadData() + collectionView.reloadData() } //-------------------------------------------------- @@ -163,10 +179,10 @@ open class Pagination: View { private func updateSelection() { guard _selectedPageIndex < total else { return } //Need to make selected page as second element so scrolling previous index of the selected page to left - collectionContainerView.collectionView.scrollToItem(at: IndexPath(row: max(_selectedPageIndex - 1, 0), section: 0), at: .left, animated: false) + collectionView.scrollToItem(at: IndexPath(row: max(_selectedPageIndex - 1, 0), section: 0), at: .left, animated: false) previousButton.isHidden = _selectedPageIndex == 0 nextButton.isHidden = _selectedPageIndex == total - 1 - collectionContainerView.collectionView.reloadData() + collectionView.reloadData() verifyIfMaxDigitChanged() } @@ -175,17 +191,17 @@ open class Pagination: View { let upperLimitPage = _selectedPageIndex + flowLayout.maxNumberOfColumns let upperLimitDigits = upperLimitPage.digitCount //future value digits switch (flowLayout.numberOfColumns, upperLimitDigits) { - case (_, 3), (_, 4): - flowLayout.numberOfColumns = 3 - default: + case (_, 1), (_, 2): flowLayout.numberOfColumns = 4 + default: + flowLayout.numberOfColumns = 3 } if upperLimitDigits != flowLayout.upperLimitDigits { flowLayout.upperLimitDigits = upperLimitDigits flowLayout.invalidateLayout() - collectionContainerView.collectionView.reloadData() + collectionView.reloadData() //Need to make selected page as second element so scrolling previous index of the selected page to left - collectionContainerView.collectionView.scrollToItem(at: IndexPath(row: max(_selectedPageIndex - 1, 0), section: 0), at: .left, animated: false) + collectionView.scrollToItem(at: IndexPath(row: max(_selectedPageIndex - 1, 0), section: 0), at: .left, animated: false) } } } diff --git a/VDS/Components/Pagination/PaginationCollectionView.swift b/VDS/Components/Pagination/PaginationContainerView.swift similarity index 52% rename from VDS/Components/Pagination/PaginationCollectionView.swift rename to VDS/Components/Pagination/PaginationContainerView.swift index a41c65ef..b79a2fe2 100644 --- a/VDS/Components/Pagination/PaginationCollectionView.swift +++ b/VDS/Components/Pagination/PaginationContainerView.swift @@ -1,5 +1,5 @@ // -// PaginationCollectionView.swift +// PaginationContainerView.swift // VDS // // Created by Bandaru, Krishna Kishore on 12/03/24. @@ -8,7 +8,7 @@ import UIKit ///PaginationCollectionView is a container view that holds collectionview for displaying page indexes -final class PaginationCollectionView: View { +final class PaginationContainerView: View { //-------------------------------------------------- // MARK: - Internal Properties @@ -17,24 +17,6 @@ final class PaginationCollectionView: View { var onAccessibilityIncrement: (() -> Void)? ///Notifies when accessibility decrement is happend when user swipes down var onAccessibilityDecrement: (() -> Void)? - ///Collectionview to render pagination indexes - lazy var collectionView: UICollectionView = { - let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout) - collectionView.isScrollEnabled = false - collectionView.translatesAutoresizingMaskIntoConstraints = false - collectionView.showsHorizontalScrollIndicator = false - collectionView.showsVerticalScrollIndicator = false - collectionView.isAccessibilityElement = true - collectionView.register(PaginationCellItem.self, forCellWithReuseIdentifier: PaginationCellItem.identifier) - collectionView.backgroundColor = .clear - return collectionView - }() - - //-------------------------------------------------- - // MARK: - Private Properties - //-------------------------------------------------- - ///Custom flow layout defined for the Pagination - private let flowLayout = PaginationFlowLayout() //-------------------------------------------------- // MARK: - Overrides @@ -58,9 +40,6 @@ final class PaginationCollectionView: View { /// Executed on initialization for this View. override func setup() { super.setup() - addSubview(collectionView) - collectionView.pinToSuperView() isAccessibilityElement = true - accessibilityElements = [collectionView] } } diff --git a/VDS/Components/Pagination/PaginationFlowLayout.swift b/VDS/Components/Pagination/PaginationFlowLayout.swift index 542e4eff..c6bd02c3 100644 --- a/VDS/Components/Pagination/PaginationFlowLayout.swift +++ b/VDS/Components/Pagination/PaginationFlowLayout.swift @@ -18,8 +18,9 @@ final class PaginationFlowLayout : UICollectionViewLayout { ///Pre-defined sizes of the pagination cell based on number of digits. private var upperLimitSize: CGSize { switch upperLimitDigits { - case 3, 4: .init(width: 34, height: 16) - default: .init(width: 20, height: 16) + case 1, 2: .init(width: 20, height: 16) + case 3: .init(width: 28, height: 16) + default: .init(width: 34, height: 16) } } ///Property to store the defined layout attributes. From 86e91c3ac7398ff4ed92abecc6b04f7c9c18c865 Mon Sep 17 00:00:00 2001 From: Krishna Kishore Bandaru Date: Wed, 20 Mar 2024 16:50:47 +0530 Subject: [PATCH 09/12] Added Center X & Y constraints and modified code based on constraints --- VDS/Components/Pagination/Pagination.swift | 38 +++-- .../Pagination/PaginationCellItem.swift | 2 +- VDS/Protocols/LayoutConstraintable.swift | 152 ++++++++++++++++++ 3 files changed, 174 insertions(+), 18 deletions(-) diff --git a/VDS/Components/Pagination/Pagination.swift b/VDS/Components/Pagination/Pagination.swift index 7728d6dc..72cb3224 100644 --- a/VDS/Components/Pagination/Pagination.swift +++ b/VDS/Components/Pagination/Pagination.swift @@ -92,34 +92,38 @@ open class Pagination: View { open override func initialSetup() { super.initialSetup() - addSubview(containerView) - containerView - .pinTop() - .pinBottom() - containerView.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor).activate() - trailingAnchor.constraint(greaterThanOrEqualTo: containerView.trailingAnchor).activate() - containerView.centerXAnchor.constraint(equalTo: centerXAnchor).activate() - containerView.widthAnchor.constraint(equalToConstant: maxWidth).activate() - containerView.heightAnchor.constraint(equalToConstant: 44).activate() + collectionContainerView.addSubview(collectionView) containerView.addSubview(previousButton) containerView.addSubview(collectionContainerView) containerView.addSubview(nextButton) - collectionContainerView.addSubview(collectionView) + addSubview(containerView) + + containerView + .pinTop() + .pinBottom() + .pinLeadingGreaterThanOrEqualTo() + .pinTrailingLessThanOrEqualTo() + .pinCenterX() + .width(maxWidth) + .height(44) + previousButton .pinTop() .pinBottom() .pinLeading() + .pinTrailingGreaterThanOrEqualTo(collectionContainerView.leadingAnchor) - previousButton.trailingAnchor.constraint(greaterThanOrEqualTo: collectionContainerView.leadingAnchor).activate() - collectionContainerView.trailingAnchor.constraint(greaterThanOrEqualTo: nextButton.leadingAnchor).activate() collectionContainerView + .pinTrailingGreaterThanOrEqualTo(nextButton.leadingAnchor) .pinTop() .pinBottom() - collectionView.heightAnchor.constraint(equalToConstant: VDSLayout.Spacing.space4X.value).activate() - collectionView.centerYAnchor.constraint(equalTo: centerYAnchor).activate() - collectionView.centerXAnchor.constraint(equalTo: collectionContainerView.centerXAnchor).activate() - collectionViewWidthAnchor = collectionView.widthAnchor.constraint(equalToConstant: 92) - collectionViewWidthAnchor?.activate() + + collectionView + .height(VDSLayout.Spacing.space4X.value) + .pinCenterY() + .pinCenterX() + + collectionViewWidthAnchor = collectionView.width(constant: 92) nextButton .pinTop() diff --git a/VDS/Components/Pagination/PaginationCellItem.swift b/VDS/Components/Pagination/PaginationCellItem.swift index 68f51142..24edc3a4 100644 --- a/VDS/Components/Pagination/PaginationCellItem.swift +++ b/VDS/Components/Pagination/PaginationCellItem.swift @@ -48,7 +48,7 @@ final class PaginationCellItem: UICollectionViewCell { contentView.addSubview(containerView) containerView.pinToSuperView() indexLabel.pinToSuperView() - indexLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: VDSLayout.Spacing.space5X.value).activate() + indexLabel.widthGreaterThanEqualTo(VDSLayout.Spacing.space5X.value) contentView.backgroundColor = .clear containerView.backgroundColor = .clear indexLabel.backgroundColor = .clear diff --git a/VDS/Protocols/LayoutConstraintable.swift b/VDS/Protocols/LayoutConstraintable.swift index 3c05bbc7..457b5fa5 100644 --- a/VDS/Protocols/LayoutConstraintable.swift +++ b/VDS/Protocols/LayoutConstraintable.swift @@ -478,7 +478,159 @@ extension LayoutConstraintable { } } +//-------------------------------------------------- +// MARK: - Center X Constraints +//-------------------------------------------------- +extension LayoutConstraintable { + + @discardableResult + /// Adds a centerXAnchor. + /// - Parameter constant: Constant size. + /// - Returns: Yourself. + public func pinCenterX(_ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self { + pinCenterX(nil, constant) + } + @discardableResult + /// Adds a centerXAnchor to a specific XAxisAnchor. + /// - Parameter anchor:The anchor in which to attach the centerXAnchor. + /// - constant: Constant size. + /// - Returns: Yourself. + public func pinCenterX(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self { + pinCenterX(anchor: anchor, constant: constant) + return self + } + + @discardableResult + /// Adds a centerXAnchor to a specific XAxisAnchor passed in using a lessThanOrEqualTo Constraint + /// - Parameter anchor:The anchor in which to attach the centerXAnchor + /// - constant: Constant size. + /// - Returns: Yourself. + public func pinCenterXLessThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self { + pinCenterXLessThanOrEqualTo(anchor: anchor, constant: constant) + return self + } + + @discardableResult + /// Adds a centerXAnchor to a specific XAxisAnchor passed in using a greaterThanOrEqualTo Constraint + /// - Parameter anchor:The anchor in which to attach the centerXAnchor + /// - constant: Constant size. + /// - Returns: Yourself. + public func pinCenterXGreaterThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self { + pinCenterXGreaterThanOrEqualTo(anchor: anchor, constant: constant) + return self + } + + @discardableResult + /// Adds a centerXAnchor for the constant passed into the method. + /// - Parameter anchor:The anchor in which to attach the centerXAnchor + /// - constant: Constant size. + /// - Returns: The Constraint that was created. + public func pinCenterX(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { + let found: NSLayoutXAxisAnchor? = anchor ?? superview?.centerXAnchor + guard let found else { return nil } + return centerXAnchor.constraint(equalTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true } + } + + @discardableResult + /// Adds a centerXAnchor with the constant passed in using a lessThanOrEqualTo Constraint. + /// - Parameter anchor:The anchor in which to attach the centerXAnchor + /// - constant: Constant size. + /// - Returns: The Constraint that was created. + public func pinCenterXLessThanOrEqualTo(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { + let found: NSLayoutXAxisAnchor? = anchor ?? superview?.centerXAnchor + guard let found else { return nil } + return centerXAnchor.constraint(lessThanOrEqualTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true } + } + + @discardableResult + /// Adds a centerXAnchor with the constant passed in using a greaterThanOrEqualTo Constraint. + /// - Parameter anchor:The anchor in which to attach the centerXAnchor + /// - constant: Constant size. + /// - Returns: The Constraint that was created. + public func pinCenterXGreaterThanOrEqualTo(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { + let found: NSLayoutXAxisAnchor? = anchor ?? superview?.centerXAnchor + guard let found else { return nil } + return centerXAnchor.constraint(greaterThanOrEqualTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true } + } +} + +//-------------------------------------------------- +// MARK: - Center Y Constraints +//-------------------------------------------------- +extension LayoutConstraintable { + + @discardableResult + /// Adds a centerYAnchor. + /// - Parameter constant: Constant size. + /// - Returns: Yourself. + public func pinCenterY(_ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self { + pinCenterY(nil, constant) + } + + @discardableResult + /// Adds a centerYAnchor to a specific YAxisAnchor. + /// - Parameter anchor:The anchor in which to attach the centerYAnchor. + /// - constant: Constant size. + /// - Returns: Yourself. + public func pinCenterY(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self { + pinCenterY(anchor: anchor, constant: constant) + return self + } + + @discardableResult + /// Adds a centerYAnchor to a specific YAxisAnchor passed in using a lessThanOrEqualTo Constraint + /// - Parameter anchor:The anchor in which to attach the centerYAnchor + /// - constant: Constant size. + /// - Returns: Yourself. + public func pinCenterYLessThanOrEqualTo(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0, _ priority: UILayoutPriority = .required) -> Self { + pinCenterYLessThanOrEqualTo(anchor: anchor, constant: constant) + return self + } + + @discardableResult + /// Adds a centerYAnchor to a specific YAxisAnchor passed in using a greaterThanOrEqualTo Constraint + /// - Parameter anchor:The anchor in which to attach the centerYAnchor + /// - constant: Constant size. + /// - Returns: Yourself. + public func pinCenterYGreaterThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self { + pinCenterXGreaterThanOrEqualTo(anchor: anchor, constant: constant) + return self + } + + @discardableResult + /// Adds a centerYAnchor for the constant passed into the method. + /// - Parameter anchor:The anchor in which to attach the centerYAnchor + /// - constant: Constant size. + /// - Returns: The Constraint that was created. + public func pinCenterY(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { + let found: NSLayoutYAxisAnchor? = anchor ?? superview?.centerYAnchor + guard let found else { return nil } + return centerYAnchor.constraint(equalTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true } + } + + @discardableResult + /// Adds a centerYAnchor with the constant passed in using a lessThanOrEqualTo Constraint. + /// - Parameter anchor:The anchor in which to attach the centerYAnchor + /// - constant: Constant size. + /// - Returns: The Constraint that was created. + public func pinCenterYLessThanOrEqualTo(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { + let found: NSLayoutYAxisAnchor? = anchor ?? superview?.centerYAnchor + guard let found else { return nil } + return centerYAnchor.constraint(lessThanOrEqualTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true } + } + + @discardableResult + /// Adds a centerYAnchor with the constant passed in using a greaterThanOrEqualTo Constraint. + /// - Parameter anchor:The anchor in which to attach the centerYAnchor + /// - constant: Constant size. + /// - Returns: The Constraint that was created. + public func pinCenterYGreaterThanOrEqualTo(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? { + let found: NSLayoutYAxisAnchor? = anchor ?? superview?.centerYAnchor + guard let found else { return nil } + return centerYAnchor.constraint(greaterThanOrEqualTo: found, constant: -constant).with { $0.priority = priority; $0.isActive = true } + } +} //-------------------------------------------------- // MARK: - Implementations //-------------------------------------------------- From bbcdbebbb0b9290abb0addfef11bfbe0ac4f8051 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Thu, 21 Mar 2024 14:31:57 -0500 Subject: [PATCH 10/12] refactored some naming to keep in line with others Signed-off-by: Matt Bruce --- VDS.xcodeproj/project.pbxproj | 8 ++++---- VDS/Components/Pagination/Pagination.swift | 2 +- VDS/Components/Pagination/PaginationButton.swift | 1 + ...ationContainerView.swift => PaginationContainer.swift} | 6 +++--- 4 files changed, 9 insertions(+), 8 deletions(-) rename VDS/Components/Pagination/{PaginationContainerView.swift => PaginationContainer.swift} (96%) diff --git a/VDS.xcodeproj/project.pbxproj b/VDS.xcodeproj/project.pbxproj index 0dca7215..419b82cd 100644 --- a/VDS.xcodeproj/project.pbxproj +++ b/VDS.xcodeproj/project.pbxproj @@ -16,7 +16,7 @@ 5F21D7BF28DCEB3D003E7CD6 /* Useable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F21D7BE28DCEB3D003E7CD6 /* Useable.swift */; }; 5FC35BE328D51405004EBEAC /* Button.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FC35BE228D51405004EBEAC /* Button.swift */; }; 7115BD3C2B84C0C200E0A610 /* TileContainerChangeLog.txt in Resources */ = {isa = PBXBuildFile; fileRef = 7115BD3B2B84C0C200E0A610 /* TileContainerChangeLog.txt */; }; - 71ACE89C2BA0451200FB6ADC /* PaginationContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71ACE89B2BA0451200FB6ADC /* PaginationContainerView.swift */; }; + 71ACE89C2BA0451200FB6ADC /* PaginationContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71ACE89B2BA0451200FB6ADC /* PaginationContainer.swift */; }; 71B23C2D2B91FA690027F7D9 /* Pagination.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71B23C2C2B91FA690027F7D9 /* Pagination.swift */; }; 71B5FCBB2B95A0CA00269BCC /* PaginationChangeLog.txt in Resources */ = {isa = PBXBuildFile; fileRef = 71B5FCBA2B95A0CA00269BCC /* PaginationChangeLog.txt */; }; 71BFA70A2B7F70E6000DCE33 /* DropShadowable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71BFA7092B7F70E6000DCE33 /* DropShadowable.swift */; }; @@ -194,7 +194,7 @@ 5F21D7BE28DCEB3D003E7CD6 /* Useable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Useable.swift; sourceTree = ""; }; 5FC35BE228D51405004EBEAC /* Button.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Button.swift; sourceTree = ""; }; 7115BD3B2B84C0C200E0A610 /* TileContainerChangeLog.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = TileContainerChangeLog.txt; sourceTree = ""; }; - 71ACE89B2BA0451200FB6ADC /* PaginationContainerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginationContainerView.swift; sourceTree = ""; }; + 71ACE89B2BA0451200FB6ADC /* PaginationContainer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginationContainer.swift; sourceTree = ""; }; 71B23C2C2B91FA690027F7D9 /* Pagination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pagination.swift; sourceTree = ""; }; 71B5FCBA2B95A0CA00269BCC /* PaginationChangeLog.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = PaginationChangeLog.txt; sourceTree = ""; }; 71BFA7092B7F70E6000DCE33 /* DropShadowable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DropShadowable.swift; sourceTree = ""; }; @@ -408,7 +408,7 @@ isa = PBXGroup; children = ( 71B23C2C2B91FA690027F7D9 /* Pagination.swift */, - 71ACE89B2BA0451200FB6ADC /* PaginationContainerView.swift */, + 71ACE89B2BA0451200FB6ADC /* PaginationContainer.swift */, 71FC86D92B96F44C00700965 /* PaginationButton.swift */, 71FC86DB2B96F4C800700965 /* PaginationCellItem.swift */, 71FC86E32B9841AC00700965 /* PaginationFlowLayout.swift */, @@ -1063,7 +1063,7 @@ EAE785312BA0A438009428EA /* UIImage+Helper.swift in Sources */, EAB5FEF5292D371F00998C17 /* ButtonBase.swift in Sources */, EA978EC5291D6AFE00ACC883 /* AnyLabelAttribute.swift in Sources */, - 71ACE89C2BA0451200FB6ADC /* PaginationContainerView.swift in Sources */, + 71ACE89C2BA0451200FB6ADC /* PaginationContainer.swift in Sources */, EAC71A1F2A2E173D00E47A9F /* RadioButton.swift in Sources */, EA33622C2891E73B0071C351 /* FontProtocol.swift in Sources */, EA596ABD2A16B4EC00300C4B /* Tab.swift in Sources */, diff --git a/VDS/Components/Pagination/Pagination.swift b/VDS/Components/Pagination/Pagination.swift index 72cb3224..b7adcab3 100644 --- a/VDS/Components/Pagination/Pagination.swift +++ b/VDS/Components/Pagination/Pagination.swift @@ -45,7 +45,7 @@ open class Pagination: View { return collectionView }() ///Container view to hold collectionview to render pagination indexes and to handler accessibility. - private let collectionContainerView = PaginationContainerView() + private let collectionContainerView = PaginationContainer() //-------------------------------------------------- // MARK: - Public Properties diff --git a/VDS/Components/Pagination/PaginationButton.swift b/VDS/Components/Pagination/PaginationButton.swift index 3805051d..4ed851b5 100644 --- a/VDS/Components/Pagination/PaginationButton.swift +++ b/VDS/Components/Pagination/PaginationButton.swift @@ -9,6 +9,7 @@ import UIKit import VDSColorTokens ///This is customised button for Pagination view +@objc(PaginationButton) open class PaginationButton: ButtonBase { //-------------------------------------------------- // MARK: - Private Properties diff --git a/VDS/Components/Pagination/PaginationContainerView.swift b/VDS/Components/Pagination/PaginationContainer.swift similarity index 96% rename from VDS/Components/Pagination/PaginationContainerView.swift rename to VDS/Components/Pagination/PaginationContainer.swift index b79a2fe2..2172e32a 100644 --- a/VDS/Components/Pagination/PaginationContainerView.swift +++ b/VDS/Components/Pagination/PaginationContainer.swift @@ -6,9 +6,9 @@ // import UIKit - + ///PaginationCollectionView is a container view that holds collectionview for displaying page indexes -final class PaginationContainerView: View { +final class PaginationContainer: View { //-------------------------------------------------- // MARK: - Internal Properties @@ -17,7 +17,7 @@ final class PaginationContainerView: View { var onAccessibilityIncrement: (() -> Void)? ///Notifies when accessibility decrement is happend when user swipes down var onAccessibilityDecrement: (() -> Void)? - + //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- From 67e42b9730711884df56871d23e6579c1cdb48d5 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Thu, 21 Mar 2024 14:32:04 -0500 Subject: [PATCH 11/12] removed dumb code Signed-off-by: Matt Bruce --- VDS/Components/Tabs/Tabs.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/VDS/Components/Tabs/Tabs.swift b/VDS/Components/Tabs/Tabs.swift index 0aa81bdf..647a74a2 100644 --- a/VDS/Components/Tabs/Tabs.swift +++ b/VDS/Components/Tabs/Tabs.swift @@ -264,7 +264,6 @@ open class Tabs: View { model.onClick?(tab.index) self.selectedIndex = tab.index self.onTabDidSelect?(tab.index) - let t = tabViews[tab.index] } } } From d1006f12664735048026fa331d3977e497617d65 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Thu, 21 Mar 2024 15:11:38 -0500 Subject: [PATCH 12/12] removed images that already existed added resizer and refactored code Signed-off-by: Matt Bruce --- VDS/Components/Icon/IconName.swift | 1 - VDS/Components/Pagination/PaginationButton.swift | 14 +++++++++----- VDS/Extensions/UIImage+Helper.swift | 13 +++++++++++++ .../pagination-arrow-left.imageset/Contents.json | 12 ------------ .../pagination-arrow-left.svg | 10 ---------- .../pagination-arrow-right.imageset/Contents.json | 12 ------------ .../pagination-arrow-right.svg | 3 --- 7 files changed, 22 insertions(+), 43 deletions(-) delete mode 100644 VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-left.imageset/Contents.json delete mode 100644 VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-left.imageset/pagination-arrow-left.svg delete mode 100644 VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-right.imageset/Contents.json delete mode 100644 VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-right.imageset/pagination-arrow-right.svg diff --git a/VDS/Components/Icon/IconName.swift b/VDS/Components/Icon/IconName.swift index c2d5076b..1234df63 100644 --- a/VDS/Components/Icon/IconName.swift +++ b/VDS/Components/Icon/IconName.swift @@ -48,7 +48,6 @@ extension Icon { internal static let paginationRightCaret = Name(name: "pagination-right-caret") internal static let verizonUp = Name(name: "verizon-up") internal static let warningBold = Name(name: "warning-bold") - public static let checkmark = Name(name: "checkmark") public static let checkmarkAlt = Name(name: "checkmark-alt") public static let close = Name(name: "close") diff --git a/VDS/Components/Pagination/PaginationButton.swift b/VDS/Components/Pagination/PaginationButton.swift index 4ed851b5..290bbef5 100644 --- a/VDS/Components/Pagination/PaginationButton.swift +++ b/VDS/Components/Pagination/PaginationButton.swift @@ -73,8 +73,9 @@ open class PaginationButton: ButtonBase { /// Used to make changes to the View based off a change events or from local properties. open override func updateView() { text = type.title - setImage(type.image, for: .normal) - tintColor = buttonTintColorConfiguration.getColor(surface) + let color = buttonTintColorConfiguration.getColor(surface) + setImage(type.image(color), for: .normal) + tintColor = color super.updateView() } } @@ -94,13 +95,16 @@ extension PaginationButton { "Previous" } } + + private var imageSize: CGSize { Icon.Size.xsmall.dimensions } + ///Image for the configuration type - var image: UIImage? { + func image(_ color: UIColor) -> UIImage? { switch self { case .previous: - BundleManager.shared.image(for: "pagination-arrow-left")?.withRenderingMode(.alwaysTemplate) + UIImage.image(for: .paginationLeftArrow, color: color, renderingMode: .alwaysTemplate)?.resized(to: imageSize) case .next: - BundleManager.shared.image(for: "pagination-arrow-right")?.withRenderingMode(.alwaysTemplate) + UIImage.image(for: .paginationRightArrow, color: color, renderingMode: .alwaysTemplate)?.resized(to: imageSize) } } } diff --git a/VDS/Extensions/UIImage+Helper.swift b/VDS/Extensions/UIImage+Helper.swift index c8f103dc..41f25a42 100644 --- a/VDS/Extensions/UIImage+Helper.swift +++ b/VDS/Extensions/UIImage+Helper.swift @@ -23,4 +23,17 @@ extension UIImage { return image.withTintColor(color, renderingMode: renderingMode) } + + /// Resizes image to a specific Size + /// - Parameter size: Size to resize + /// - Returns: Image that is resized + public func resized(to size: CGSize) -> UIImage? { + UIGraphicsBeginImageContextWithOptions(size, false, 0.0) + + defer { UIGraphicsEndImageContext() } + draw(in: .init(origin: .zero, size: size)) + + return UIGraphicsGetImageFromCurrentImageContext() + } + } diff --git a/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-left.imageset/Contents.json b/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-left.imageset/Contents.json deleted file mode 100644 index 6c37b40d..00000000 --- a/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-left.imageset/Contents.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "images" : [ - { - "filename" : "pagination-arrow-left.svg", - "idiom" : "universal" - } - ], - "info" : { - "author" : "xcode", - "version" : 1 - } -} diff --git a/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-left.imageset/pagination-arrow-left.svg b/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-left.imageset/pagination-arrow-left.svg deleted file mode 100644 index a8c85c97..00000000 --- a/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-left.imageset/pagination-arrow-left.svg +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - diff --git a/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-right.imageset/Contents.json b/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-right.imageset/Contents.json deleted file mode 100644 index d294d555..00000000 --- a/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-right.imageset/Contents.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "images" : [ - { - "filename" : "pagination-arrow-right.svg", - "idiom" : "universal" - } - ], - "info" : { - "author" : "xcode", - "version" : 1 - } -} diff --git a/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-right.imageset/pagination-arrow-right.svg b/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-right.imageset/pagination-arrow-right.svg deleted file mode 100644 index be02bedf..00000000 --- a/VDS/SupportingFiles/Icons.xcassets/Restricted/pagination-arrow-right.imageset/pagination-arrow-right.svg +++ /dev/null @@ -1,3 +0,0 @@ - - -