From 9f75f10d6ab12c3b2aa49440f84b1d4f354d102f Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Tue, 6 Sep 2022 13:01:34 +1000 Subject: [PATCH] centre align the last open group suggestion if there is only one of them --- .../Open Groups/OpenGroupSuggestionGrid.swift | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/Session/Open Groups/OpenGroupSuggestionGrid.swift b/Session/Open Groups/OpenGroupSuggestionGrid.swift index ee9e83ed7..eb7479af4 100644 --- a/Session/Open Groups/OpenGroupSuggestionGrid.swift +++ b/Session/Open Groups/OpenGroupSuggestionGrid.swift @@ -150,16 +150,34 @@ final class OpenGroupSuggestionGrid: UIView, UICollectionViewDataSource, UIColle // MARK: - Data Source + func numberOfSections(in collectionView: UICollectionView) -> Int { + return (min(rooms.count, 8) + 1) / 2 // Cap to a maximum of 4 (4 rows of 2) + } + func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { - return min(rooms.count, 8) // Cap to a maximum of 8 (4 rows of 2) + if (section + 1) * 2 <= min(rooms.count, 8) { + return 2 + } else { + return 1 + } } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cell.identifier, for: indexPath) as! Cell - cell.room = rooms[indexPath.item] + cell.room = rooms[indexPath.item + indexPath.section * 2] return cell } + func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { + if (section + 1) * 2 <= min(rooms.count, 8) { + return .zero + } else { + let cellWidth = UIDevice.current.isIPad ? maxWidth / 4 : maxWidth / 2 + let sideInset = (maxWidth - cellWidth) / 2 + return UIEdgeInsets(top: 0, left: sideInset, bottom: 0, right: sideInset) + } + } + // MARK: - Interaction func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {