LNPotentialUserViewController.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // LNPotentialUserViewController.swift
  3. // Gami
  4. //
  5. // Created by OneeChan on 2026/3/10.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. import MJRefresh
  11. extension UIView {
  12. func pushToPotentialUsers() {
  13. let vc = LNPotentialUserViewController()
  14. navigationController?.pushViewController(vc, animated: true)
  15. }
  16. }
  17. class LNPotentialUserViewController: LNViewController {
  18. private let emptyView = LNNoMoreDataView()
  19. private let tableView = UITableView()
  20. private var users: [LNPotentialUserVO] = []
  21. private var isLoading = false
  22. override func viewDidLoad() {
  23. super.viewDidLoad()
  24. setupViews()
  25. tableView.mj_header?.beginRefreshing()
  26. }
  27. }
  28. extension LNPotentialUserViewController: UITableViewDataSource {
  29. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  30. users.count
  31. }
  32. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  33. let cell = tableView.dequeueReusableCell(
  34. withIdentifier: LNPotentialUserItemCell.className,
  35. for: indexPath
  36. ) as! LNPotentialUserItemCell
  37. let item = users[indexPath.row]
  38. cell.update(item)
  39. return cell
  40. }
  41. }
  42. extension LNPotentialUserViewController {
  43. private func setupViews() {
  44. title = .init(key: "B00116")
  45. emptyView.isHidden = true
  46. view.addSubview(emptyView)
  47. emptyView.snp.makeConstraints { make in
  48. make.centerX.equalToSuperview()
  49. make.centerY.equalToSuperview().multipliedBy(0.6)
  50. }
  51. let header = MJRefreshNormalHeader { [weak self] in
  52. guard let self else { return }
  53. self.loadList()
  54. }
  55. header.lastUpdatedTimeLabel?.isHidden = true
  56. header.stateLabel?.isHidden = true
  57. tableView.mj_header = header
  58. tableView.dataSource = self
  59. tableView.separatorStyle = .none
  60. tableView.showsVerticalScrollIndicator = false
  61. tableView.backgroundColor = .clear
  62. tableView.allowsSelection = false
  63. tableView.register(LNPotentialUserItemCell.self, forCellReuseIdentifier: LNPotentialUserItemCell.className)
  64. view.addSubview(tableView)
  65. tableView.snp.makeConstraints { make in
  66. make.edges.equalToSuperview()
  67. }
  68. }
  69. private func loadList() {
  70. guard !isLoading else { return }
  71. isLoading = true
  72. LNGameMateManager.shared.getPotentialUsers { [weak self] res in
  73. guard let self else { return }
  74. isLoading = false
  75. if let list = res?.list {
  76. users = list
  77. tableView.reloadData()
  78. if users.isEmpty {
  79. emptyView.showNoData(tips: .init(key: "B00124"))
  80. } else {
  81. emptyView.hide()
  82. }
  83. } else {
  84. if users.isEmpty {
  85. emptyView.showNetworkError()
  86. }
  87. }
  88. tableView.mj_header?.endRefreshing()
  89. }
  90. }
  91. }
  92. private class LNPotentialUserItemCell: UITableViewCell {
  93. private let avatar = UIImageView()
  94. private let onlineView = LNOnlineView()
  95. private let nameLabel = UILabel()
  96. private let genderView = LNGenderView()
  97. private let newTag = UIView()
  98. private let chatButton = UIButton()
  99. private var curItem: LNPotentialUserVO?
  100. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  101. super.init(style: style, reuseIdentifier: reuseIdentifier)
  102. setupViews()
  103. }
  104. required init?(coder: NSCoder) {
  105. fatalError("init(coder:) has not been implemented")
  106. }
  107. func update(_ item: LNPotentialUserVO) {
  108. avatar.showAvatar(item.avatar)
  109. nameLabel.text = item.nickname
  110. genderView.update(item.gender, item.age)
  111. newTag.isHidden = !item.newRegister
  112. onlineView.isHidden = !item.online
  113. if item.contacted {
  114. chatButton.setTitle(.init(key: "B00117"), for: .normal)
  115. } else {
  116. chatButton.setTitle(.init(key: "B00123"), for: .normal)
  117. }
  118. curItem = item
  119. }
  120. }
  121. private extension LNPotentialUserItemCell {
  122. func setupViews() {
  123. backgroundColor = .clear
  124. selectionStyle = .none
  125. let container = UIView()
  126. container.onTap { [weak self] in
  127. guard let self else { return }
  128. guard let curItem else { return }
  129. pushToProfile(uid: curItem.userNo)
  130. }
  131. contentView.addSubview(container)
  132. container.snp.makeConstraints { make in
  133. make.horizontalEdges.equalToSuperview().inset(16)
  134. make.top.equalToSuperview().offset(16)
  135. make.bottom.equalToSuperview().offset(4)
  136. }
  137. avatar.layer.cornerRadius = 20
  138. avatar.clipsToBounds = true
  139. container.addSubview(avatar)
  140. avatar.snp.makeConstraints { make in
  141. make.verticalEdges.equalToSuperview().inset(10)
  142. make.leading.equalToSuperview().offset(5)
  143. make.width.height.equalTo(40)
  144. }
  145. onlineView.offset = 5
  146. container.addSubview(onlineView)
  147. onlineView.snp.makeConstraints { make in
  148. make.edges.equalTo(avatar).inset(-1)
  149. }
  150. let infoView = UIStackView()
  151. infoView.axis = .vertical
  152. infoView.spacing = 4
  153. infoView.isUserInteractionEnabled = false
  154. infoView.alignment = .leading
  155. container.addSubview(infoView)
  156. infoView.snp.makeConstraints { make in
  157. make.leading.equalTo(avatar.snp.trailing).offset(13)
  158. make.centerY.equalToSuperview()
  159. }
  160. let nameContainer = UIView()
  161. infoView.addArrangedSubview(nameContainer)
  162. nameLabel.font = .heading_h4
  163. nameLabel.textColor = .text_5
  164. nameContainer.addSubview(nameLabel)
  165. nameLabel.snp.makeConstraints { make in
  166. make.leading.verticalEdges.equalToSuperview()
  167. }
  168. nameContainer.addSubview(genderView)
  169. genderView.snp.makeConstraints { make in
  170. make.leading.equalTo(nameLabel.snp.trailing).offset(4)
  171. make.centerY.equalTo(nameLabel)
  172. make.trailing.lessThanOrEqualToSuperview()
  173. }
  174. newTag.layer.cornerRadius = 7.5
  175. newTag.clipsToBounds = true
  176. newTag.backgroundColor = .primary_3
  177. infoView.addArrangedSubview(newTag)
  178. let newLabel = UILabel()
  179. newLabel.text = "New"
  180. newLabel.font = .body_xs
  181. newLabel.textColor = .text_1
  182. newTag.addSubview(newLabel)
  183. newLabel.snp.makeConstraints { make in
  184. make.verticalEdges.equalToSuperview().inset(0.5)
  185. make.horizontalEdges.equalToSuperview().inset(6)
  186. }
  187. chatButton.setBackgroundImage(.primary_7, for: .normal)
  188. chatButton.setTitle(.init(key: "B00123"), for: .normal)
  189. chatButton.titleLabel?.font = .heading_h5
  190. chatButton.setTitleColor(.text_1, for: .normal)
  191. chatButton.layer.cornerRadius = 11
  192. chatButton.clipsToBounds = true
  193. chatButton.backgroundColor = .fill_2
  194. chatButton.contentEdgeInsets = .init(top: 0, left: 16, bottom: 0, right: 16)
  195. chatButton.addAction(UIAction(handler: { [weak self] _ in
  196. guard let self else { return }
  197. guard let curItem else { return }
  198. pushToChat(uid: curItem.userNo)
  199. curItem.contacted = true
  200. update(curItem)
  201. }), for: .touchUpInside)
  202. container.addSubview(chatButton)
  203. chatButton.snp.makeConstraints { make in
  204. make.trailing.equalToSuperview()
  205. make.centerY.equalToSuperview()
  206. make.height.equalTo(22)
  207. }
  208. }
  209. }