fix community suggestion grid issues.

pull/689/head
ryanzhao 2 years ago
parent b4d29c58c4
commit df669030ac

@ -4,6 +4,7 @@ import SessionMessagingKit
import SessionUIKit import SessionUIKit
final class OpenGroupSuggestionGrid: UIView, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { final class OpenGroupSuggestionGrid: UIView, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
private let itemsPerSection: Int = (UIDevice.current.isIPad ? 4 : 2)
private let maxWidth: CGFloat private let maxWidth: CGFloat
private var rooms: [OpenGroupAPI.Room] = [] { didSet { update() } } private var rooms: [OpenGroupAPI.Room] = [] { didSet { update() } }
private var heightConstraint: NSLayoutConstraint! private var heightConstraint: NSLayoutConstraint!
@ -144,35 +145,35 @@ final class OpenGroupSuggestionGrid: UIView, UICollectionViewDataSource, UIColle
// MARK: - Layout // MARK: - Layout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let cellWidth = UIDevice.current.isIPad ? maxWidth / 4 : maxWidth / 2 let cellWidth = maxWidth / CGFloat(itemsPerSection)
return CGSize(width: cellWidth, height: OpenGroupSuggestionGrid.cellHeight) return CGSize(width: cellWidth, height: OpenGroupSuggestionGrid.cellHeight)
} }
// MARK: - Data Source // MARK: - Data Source
func numberOfSections(in collectionView: UICollectionView) -> Int { func numberOfSections(in collectionView: UICollectionView) -> Int {
return (min(rooms.count, 8) + 1) / 2 // Cap to a maximum of 4 (4 rows of 2) return Int(ceil(Double(min(rooms.count, 8)) / Double(itemsPerSection))) // Cap to a maximum of 4 (4 rows of 2)
} }
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if (section + 1) * 2 <= min(rooms.count, 8) { if (section + 1) * itemsPerSection <= min(rooms.count, 8) {
return 2 return itemsPerSection
} else { } else {
return 1 return min(rooms.count, 8) - itemsPerSection * section
} }
} }
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cell.identifier, for: indexPath) as! Cell let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cell.identifier, for: indexPath) as! Cell
cell.room = rooms[indexPath.item + indexPath.section * 2] cell.room = rooms[indexPath.item + indexPath.section * itemsPerSection]
return cell return cell
} }
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
if (section + 1) * 2 <= min(rooms.count, 8) { if (section + 1) * itemsPerSection <= min(rooms.count, 8) {
return .zero return .zero
} else { } else {
let cellWidth = UIDevice.current.isIPad ? maxWidth / 4 : maxWidth / 2 let cellWidth = maxWidth / CGFloat(itemsPerSection)
let sideInset = (maxWidth - cellWidth) / 2 let sideInset = (maxWidth - cellWidth) / 2
return UIEdgeInsets(top: 0, left: sideInset, bottom: 0, right: sideInset) return UIEdgeInsets(top: 0, left: sideInset, bottom: 0, right: sideInset)
} }
@ -181,7 +182,7 @@ final class OpenGroupSuggestionGrid: UIView, UICollectionViewDataSource, UIColle
// MARK: - Interaction // MARK: - Interaction
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let room = rooms[indexPath.item] let room = rooms[indexPath.section * itemsPerSection + indexPath.item]
delegate?.join(room) delegate?.join(room)
} }
} }

Loading…
Cancel
Save