|
|
|
@ -1,8 +1,11 @@
|
|
|
|
|
import PromiseKit
|
|
|
|
|
import NVActivityIndicatorView
|
|
|
|
|
|
|
|
|
|
final class OpenGroupSuggestionGrid : UIView, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
|
|
|
|
|
private let maxWidth: CGFloat
|
|
|
|
|
private var rooms: [OpenGroupAPIV2.Info] = [] { didSet { reload() } }
|
|
|
|
|
private var rooms: [OpenGroupAPIV2.Info] = [] { didSet { update() } }
|
|
|
|
|
private var heightConstraint: NSLayoutConstraint!
|
|
|
|
|
var delegate: OpenGroupSuggestionGridDelegate?
|
|
|
|
|
|
|
|
|
|
// MARK: UI Components
|
|
|
|
|
private lazy var layout: UICollectionViewFlowLayout = {
|
|
|
|
@ -22,6 +25,13 @@ final class OpenGroupSuggestionGrid : UIView, UICollectionViewDataSource, UIColl
|
|
|
|
|
return result
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
private lazy var spinner: NVActivityIndicatorView = {
|
|
|
|
|
let result = NVActivityIndicatorView(frame: CGRect.zero, type: .circleStrokeSpin, color: Colors.text, padding: nil)
|
|
|
|
|
result.set(.width, to: 64)
|
|
|
|
|
result.set(.height, to: 64)
|
|
|
|
|
return result
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
// MARK: Settings
|
|
|
|
|
private static let cellHeight: CGFloat = 40
|
|
|
|
|
private static let separatorWidth = 1 / UIScreen.main.scale
|
|
|
|
@ -44,43 +54,59 @@ final class OpenGroupSuggestionGrid : UIView, UICollectionViewDataSource, UIColl
|
|
|
|
|
private func initialize() {
|
|
|
|
|
addSubview(collectionView)
|
|
|
|
|
collectionView.pin(to: self)
|
|
|
|
|
heightConstraint = set(.height, to: 0)
|
|
|
|
|
attempt(maxRetryCount: 8, recoveringOn: DispatchQueue.main) {
|
|
|
|
|
return OpenGroupAPIV2.getAllRooms(from: "https://sessionopengroup.com").done { [weak self] rooms in
|
|
|
|
|
addSubview(spinner)
|
|
|
|
|
spinner.pin([ UIView.HorizontalEdge.left, UIView.VerticalEdge.top ], to: self)
|
|
|
|
|
spinner.startAnimating()
|
|
|
|
|
heightConstraint = set(.height, to: 64)
|
|
|
|
|
widthAnchor.constraint(greaterThanOrEqualToConstant: 64).isActive = true
|
|
|
|
|
let _ = OpenGroupAPIV2.getDefaultRoomsPromise?.done { [weak self] rooms in
|
|
|
|
|
self?.rooms = rooms
|
|
|
|
|
}
|
|
|
|
|
}.retainUntilComplete()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Updating
|
|
|
|
|
private func reload() {
|
|
|
|
|
let height = OpenGroupSuggestionGrid.cellHeight * ceil(CGFloat(rooms.count) / 3)
|
|
|
|
|
private func update() {
|
|
|
|
|
spinner.stopAnimating()
|
|
|
|
|
spinner.isHidden = true
|
|
|
|
|
let height = OpenGroupSuggestionGrid.cellHeight * ceil(CGFloat(rooms.count) / 2)
|
|
|
|
|
heightConstraint.constant = height
|
|
|
|
|
collectionView.reloadData()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Layout
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
|
|
|
|
return CGSize(width: maxWidth / 3, height: OpenGroupSuggestionGrid.cellHeight)
|
|
|
|
|
return CGSize(width: maxWidth / 2, height: OpenGroupSuggestionGrid.cellHeight)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Data Source
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
|
|
|
|
return min(rooms.count, 12) // Cap to a maximum of 12 (4 rows of 3)
|
|
|
|
|
return min(rooms.count, 8) // Cap to a maximum of 8 (4 rows of 2)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
|
|
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cell.identifier, for: indexPath) as! Cell
|
|
|
|
|
cell.showRightSeparator = (indexPath.row % 2 != 0) || (indexPath.row % 2 == 0 && indexPath.row == rooms.count - 1)
|
|
|
|
|
cell.showBottomSeparator = (indexPath.row >= rooms.count - 2)
|
|
|
|
|
cell.room = rooms[indexPath.item]
|
|
|
|
|
return cell
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Interaction
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
|
|
|
let room = rooms[indexPath.item]
|
|
|
|
|
delegate?.join(room)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Cell
|
|
|
|
|
extension OpenGroupSuggestionGrid {
|
|
|
|
|
|
|
|
|
|
fileprivate final class Cell : UICollectionViewCell {
|
|
|
|
|
var showRightSeparator = false
|
|
|
|
|
var showBottomSeparator = false
|
|
|
|
|
var room: OpenGroupAPIV2.Info? { didSet { update() } }
|
|
|
|
|
private var rightSeparator: UIView!
|
|
|
|
|
private var bottomSeparator: UIView!
|
|
|
|
|
|
|
|
|
|
static let identifier = "OpenGroupSuggestionGridCell"
|
|
|
|
|
|
|
|
|
@ -107,11 +133,47 @@ extension OpenGroupSuggestionGrid {
|
|
|
|
|
label.center(in: self)
|
|
|
|
|
label.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor, constant: Values.smallSpacing).isActive = true
|
|
|
|
|
trailingAnchor.constraint(greaterThanOrEqualTo: label.trailingAnchor, constant: Values.smallSpacing).isActive = true
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func update() {
|
|
|
|
|
guard let room = room else { return }
|
|
|
|
|
label.text = room.name
|
|
|
|
|
rightSeparator.alpha = showRightSeparator ? 1 :0
|
|
|
|
|
bottomSeparator.alpha = showBottomSeparator ? 1 :0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Delegate
|
|
|
|
|
protocol OpenGroupSuggestionGridDelegate {
|
|
|
|
|
|
|
|
|
|
func join(_ room: OpenGroupAPIV2.Info)
|
|
|
|
|
}
|
|
|
|
|