LNRoomListViewController.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // LNRoomListViewController.swift
  3. // Gami
  4. //
  5. // Created by OneeChan on 2026/3/26.
  6. //
  7. import Foundation
  8. import UIKit
  9. import MJRefresh
  10. import SnapKit
  11. class LNRoomListViewController: UIViewController {
  12. private let emptyView = LNNoMoreDataView()
  13. private var items: [LNRoomItemVO] = []
  14. private var nextTag: String? = nil
  15. private let collectionView: UICollectionView
  16. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  17. let width = (UIScreen.main.bounds.width - 16 * 2 - 13) * 0.5
  18. let layout = UICollectionViewFlowLayout()
  19. layout.scrollDirection = .vertical
  20. layout.minimumLineSpacing = 16
  21. layout.minimumInteritemSpacing = 13
  22. layout.itemSize = .init(width: width, height: width * 204 / 165)
  23. self.collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
  24. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  25. }
  26. override func viewDidLoad() {
  27. super.viewDidLoad()
  28. setupViews()
  29. nextTag = nil
  30. items.removeAll()
  31. collectionView.reloadData()
  32. emptyView.hide()
  33. collectionView.mj_footer?.resetNoMoreData()
  34. collectionView.mj_header?.beginRefreshing()
  35. }
  36. required init?(coder: NSCoder) {
  37. fatalError("init(coder:) has not been implemented")
  38. }
  39. }
  40. extension LNRoomListViewController {
  41. private func loadList() {
  42. LNRoomManager.shared.getRoomList(next: nextTag) { [weak self] res in
  43. guard let self else { return }
  44. guard let list = res?.list else {
  45. collectionView.mj_header?.endRefreshing()
  46. collectionView.mj_footer?.endRefreshingWithNoMoreData()
  47. if items.isEmpty {
  48. emptyView.showNetworkError()
  49. }
  50. return
  51. }
  52. if nextTag?.isEmpty != false {
  53. items = list
  54. } else {
  55. items.append(contentsOf: list)
  56. }
  57. nextTag = res?.next
  58. collectionView.reloadData()
  59. if items.isEmpty {
  60. emptyView.showNoData(tips: .init(key: "A00244"))
  61. } else {
  62. emptyView.hide()
  63. }
  64. collectionView.mj_header?.endRefreshing()
  65. if res?.next.isEmpty != false {
  66. collectionView.mj_footer?.endRefreshingWithNoMoreData()
  67. } else {
  68. collectionView.mj_footer?.endRefreshing()
  69. }
  70. }
  71. }
  72. }
  73. extension LNRoomListViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  74. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  75. items.count
  76. }
  77. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  78. let cell = collectionView.dequeueReusableCell(
  79. withReuseIdentifier: LNRoomListCardCell.className,
  80. for: indexPath
  81. ) as! LNRoomListCardCell
  82. cell.update(items[indexPath.item])
  83. return cell
  84. }
  85. }
  86. extension LNRoomListViewController {
  87. private func setupViews() {
  88. view.backgroundColor = .primary_1
  89. let fakeNavBar = LNFakeNaviBar()
  90. fakeNavBar.isUserInteractionEnabled = false // 因为是空白
  91. view.addSubview(fakeNavBar)
  92. fakeNavBar.snp.makeConstraints { make in
  93. make.horizontalEdges.equalToSuperview()
  94. make.top.equalToSuperview()
  95. }
  96. let titleLabel = UILabel()
  97. titleLabel.text = .init(key: "A00370")
  98. titleLabel.font = .heading_h3
  99. titleLabel.textColor = .text_5
  100. fakeNavBar.actionView.addSubview(titleLabel)
  101. titleLabel.snp.makeConstraints { make in
  102. make.center.equalToSuperview()
  103. }
  104. let header = MJRefreshNormalHeader { [weak self] in
  105. guard let self else { return }
  106. nextTag = nil
  107. loadList()
  108. }
  109. header.lastUpdatedTimeLabel?.isHidden = true
  110. header.stateLabel?.isHidden = true
  111. collectionView.mj_header = header
  112. let footer = MJRefreshAutoNormalFooter { [weak self] in
  113. guard let self else { return }
  114. loadList()
  115. }
  116. footer.setTitle("", for: .noMoreData)
  117. footer.setTitle(.init(key: "A00046"), for: .idle)
  118. collectionView.mj_footer = footer
  119. collectionView.backgroundColor = .clear
  120. collectionView.showsVerticalScrollIndicator = false
  121. collectionView.showsHorizontalScrollIndicator = false
  122. collectionView.allowsSelection = false
  123. collectionView.dataSource = self
  124. collectionView.delegate = self
  125. collectionView.register(LNRoomListCardCell.self, forCellWithReuseIdentifier: LNRoomListCardCell.className)
  126. collectionView.contentInset = .init(top: 6, left: 0, bottom: -view.commonBottomInset, right: 0)
  127. view.addSubview(collectionView)
  128. collectionView.snp.makeConstraints { make in
  129. make.horizontalEdges.equalToSuperview().inset(16)
  130. make.top.equalTo(fakeNavBar.snp.bottom)
  131. make.bottom.equalToSuperview()
  132. }
  133. collectionView.addSubview(emptyView)
  134. emptyView.snp.makeConstraints { make in
  135. make.centerX.equalToSuperview()
  136. make.centerY.equalToSuperview().multipliedBy(0.6)
  137. }
  138. }
  139. }
  140. private final class LNRoomListCardCell: UICollectionViewCell {
  141. private let cardView = LNUserSearchRoomCardView()
  142. override init(frame: CGRect) {
  143. super.init(frame: frame)
  144. contentView.addSubview(cardView)
  145. cardView.snp.makeConstraints { make in
  146. make.edges.equalToSuperview()
  147. }
  148. }
  149. required init?(coder: NSCoder) {
  150. fatalError("init(coder:) has not been implemented")
  151. }
  152. func update(_ item: LNRoomItemVO) {
  153. cardView.update(item)
  154. }
  155. }