session-ios/Session/Open Groups/OpenGroupSuggestionGrid.swift

180 lines
6.9 KiB
Swift
Raw Normal View History

2021-03-26 03:28:40 +01:00
import PromiseKit
import NVActivityIndicatorView
2021-03-25 06:16:08 +01:00
final class OpenGroupSuggestionGrid : UIView, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
private let maxWidth: CGFloat
2021-03-26 03:28:40 +01:00
private var rooms: [OpenGroupAPIV2.Info] = [] { didSet { update() } }
2021-03-25 06:16:08 +01:00
private var heightConstraint: NSLayoutConstraint!
2021-03-26 03:28:40 +01:00
var delegate: OpenGroupSuggestionGridDelegate?
2021-03-25 06:16:08 +01:00
// MARK: UI Components
private lazy var layout: UICollectionViewFlowLayout = {
let result = UICollectionViewFlowLayout()
2021-03-26 00:39:51 +01:00
result.minimumLineSpacing = 0
result.minimumInteritemSpacing = 0
2021-03-25 06:16:08 +01:00
return result
}()
private lazy var collectionView: UICollectionView = {
let result = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
result.register(Cell.self, forCellWithReuseIdentifier: Cell.identifier)
2021-03-26 00:39:51 +01:00
result.backgroundColor = .clear
2021-03-25 06:16:08 +01:00
result.isScrollEnabled = false
result.dataSource = self
result.delegate = self
return result
}()
2021-03-26 03:28:40 +01:00
private lazy var spinner: NVActivityIndicatorView = {
let result = NVActivityIndicatorView(frame: CGRect.zero, type: .circleStrokeSpin, color: Colors.text, padding: nil)
2021-03-26 03:35:12 +01:00
result.set(.width, to: 40)
result.set(.height, to: 40)
2021-03-26 03:28:40 +01:00
return result
}()
2021-03-25 06:16:08 +01:00
// MARK: Settings
private static let cellHeight: CGFloat = 40
private static let separatorWidth = 1 / UIScreen.main.scale
// MARK: Initialization
init(maxWidth: CGFloat) {
self.maxWidth = maxWidth
super.init(frame: CGRect.zero)
initialize()
}
override init(frame: CGRect) {
preconditionFailure("Use init(maxWidth:) instead.")
}
required init?(coder: NSCoder) {
preconditionFailure("Use init(maxWidth:) instead.")
}
private func initialize() {
addSubview(collectionView)
collectionView.pin(to: self)
2021-03-26 03:28:40 +01:00
addSubview(spinner)
spinner.pin([ UIView.HorizontalEdge.left, UIView.VerticalEdge.top ], to: self)
spinner.startAnimating()
2021-03-26 03:35:12 +01:00
heightConstraint = set(.height, to: 40)
widthAnchor.constraint(greaterThanOrEqualToConstant: 40).isActive = true
2021-03-29 07:14:14 +02:00
let _ = OpenGroupAPIV2.defaultRoomsPromise?.done { [weak self] rooms in
2021-03-26 03:28:40 +01:00
self?.rooms = rooms
}
2021-03-25 06:16:08 +01:00
}
// MARK: Updating
2021-03-26 03:28:40 +01:00
private func update() {
spinner.stopAnimating()
spinner.isHidden = true
let height = OpenGroupSuggestionGrid.cellHeight * ceil(CGFloat(rooms.count) / 2)
2021-03-25 06:16:08 +01:00
heightConstraint.constant = height
collectionView.reloadData()
}
// MARK: Layout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
2021-03-26 03:28:40 +01:00
return CGSize(width: maxWidth / 2, height: OpenGroupSuggestionGrid.cellHeight)
2021-03-25 06:16:08 +01:00
}
// MARK: Data Source
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
2021-03-26 03:28:40 +01:00
return min(rooms.count, 8) // Cap to a maximum of 8 (4 rows of 2)
2021-03-25 06:16:08 +01:00
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cell.identifier, for: indexPath) as! Cell
2021-03-26 03:28:40 +01:00
cell.showRightSeparator = (indexPath.row % 2 != 0) || (indexPath.row % 2 == 0 && indexPath.row == rooms.count - 1)
cell.showBottomSeparator = (indexPath.row >= rooms.count - 2)
2021-03-25 06:16:08 +01:00
cell.room = rooms[indexPath.item]
return cell
}
2021-03-26 03:28:40 +01:00
// MARK: Interaction
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let room = rooms[indexPath.item]
delegate?.join(room)
}
2021-03-25 06:16:08 +01:00
}
// MARK: Cell
extension OpenGroupSuggestionGrid {
fileprivate final class Cell : UICollectionViewCell {
2021-03-26 03:28:40 +01:00
var showRightSeparator = false
var showBottomSeparator = false
2021-03-25 06:16:08 +01:00
var room: OpenGroupAPIV2.Info? { didSet { update() } }
2021-03-26 03:28:40 +01:00
private var rightSeparator: UIView!
private var bottomSeparator: UIView!
2021-03-25 06:16:08 +01:00
static let identifier = "OpenGroupSuggestionGridCell"
private lazy var label: UILabel = {
let result = UILabel()
result.textColor = Colors.text
result.font = .systemFont(ofSize: Values.smallFontSize)
2021-03-26 00:39:51 +01:00
result.lineBreakMode = .byTruncatingTail
2021-03-25 06:16:08 +01:00
return result
}()
override init(frame: CGRect) {
super.init(frame: frame)
setUpViewHierarchy()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setUpViewHierarchy()
}
private func setUpViewHierarchy() {
addSubview(label)
label.center(in: self)
label.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor, constant: Values.smallSpacing).isActive = true
trailingAnchor.constraint(greaterThanOrEqualTo: label.trailingAnchor, constant: Values.smallSpacing).isActive = true
2021-03-26 03:28:40 +01:00
setUpSeparators()
}
private func setUpSeparators() {
func getVSeparator() -> UIView {
let separator = UIView()
separator.backgroundColor = Colors.separator
separator.set(.height, to: 1 / UIScreen.main.scale)
return separator
}
func getHSeparator() -> UIView {
let separator = UIView()
separator.backgroundColor = Colors.separator
separator.set(.width, to: 1 / UIScreen.main.scale)
return separator
}
let leftSeparator = getHSeparator()
addSubview(leftSeparator)
leftSeparator.pin([ UIView.HorizontalEdge.left, UIView.VerticalEdge.top, UIView.VerticalEdge.bottom ], to: self)
let topSeparator = getVSeparator()
addSubview(topSeparator)
topSeparator.pin([ UIView.HorizontalEdge.left, UIView.VerticalEdge.top, UIView.HorizontalEdge.right ], to: self)
rightSeparator = getHSeparator()
addSubview(rightSeparator)
rightSeparator.pin([ UIView.VerticalEdge.top, UIView.HorizontalEdge.right, UIView.VerticalEdge.bottom ], to: self)
bottomSeparator = getVSeparator()
addSubview(bottomSeparator)
bottomSeparator.pin([ UIView.HorizontalEdge.left, UIView.VerticalEdge.bottom, UIView.HorizontalEdge.right ], to: self)
2021-03-25 06:16:08 +01:00
}
private func update() {
guard let room = room else { return }
label.text = room.name
2021-03-26 03:28:40 +01:00
rightSeparator.alpha = showRightSeparator ? 1 :0
bottomSeparator.alpha = showBottomSeparator ? 1 :0
2021-03-25 06:16:08 +01:00
}
}
}
2021-03-26 03:28:40 +01:00
// MARK: Delegate
protocol OpenGroupSuggestionGridDelegate {
func join(_ room: OpenGroupAPIV2.Info)
}