QSLRequestController.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // QSLRequestController.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by Destiny on 2024/12/5.
  6. //
  7. import UIKit
  8. import CRRefresh
  9. class QSLRequestController: QSLBaseController {
  10. lazy var requestTableView: UITableView = {
  11. let tableView = UITableView(frame: .zero, style: .grouped)
  12. tableView.backgroundColor = .clear
  13. tableView.separatorStyle = .none
  14. tableView.showsVerticalScrollIndicator = false
  15. tableView.delegate = self
  16. tableView.dataSource = self
  17. tableView.tableViewNeverAdjustContentInset()
  18. tableView.bounces = true
  19. tableView.isUserInteractionEnabled = true
  20. tableView.isScrollEnabled = false
  21. tableView.contentInsetAdjustmentBehavior = .never
  22. tableView.register(cellClass: QSLRequestCell.self)
  23. tableView.cr.addHeadRefresh(animator: NormalHeaderAnimator()) { [weak self] in
  24. self?.requestRequestList()
  25. }
  26. return tableView
  27. }()
  28. var requestList: [QSLRequestModel] = [QSLRequestModel]()
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. initializeView()
  32. requestTableView.cr.beginHeaderRefresh()
  33. NotificationCenter.default.addObserver(self, selector: #selector(requestRequestList), name: QSLNotification.QSLRefreshRequest, object: nil)
  34. gravityInstance?.track(QSLGravityConst.message_request_show)
  35. }
  36. override func viewWillAppear(_ animated:Bool) {
  37. super.viewWillAppear(animated)
  38. self.navigationController?.setNavigationBarHidden(false, animated: animated)
  39. }
  40. override func viewDidDisappear(_ animated: Bool) {
  41. super.viewDidDisappear(animated)
  42. self.navigationController?.setNavigationBarHidden(true, animated: animated)
  43. }
  44. }
  45. extension QSLRequestController {
  46. // 请求好友请求列表
  47. @objc func requestRequestList() {
  48. self.requestList.removeAll()
  49. QSLNetwork().request(.requestList(dict: [:])) { response in
  50. self.requestTableView.cr.endHeaderRefresh()
  51. let requestList = response.mapArray(QSLRequestModel.self, modelKey: "data>list")
  52. self.requestList = requestList
  53. self.requestTableView.reloadData()
  54. } fail: { code, error in
  55. self.requestTableView.cr.endHeaderRefresh()
  56. self.requestTableView.reloadData()
  57. }
  58. }
  59. }
  60. extension QSLRequestController: QSLRequestCellDelegate {
  61. // 拒绝点击
  62. func refuseBtnAction(model: QSLRequestModel) {
  63. gravityInstance?.track(QSLGravityConst.message_request_disagree)
  64. QSLNetwork().request(.requestRefuse(dict: ["id": model.requestId])) { response in
  65. // 发送通知
  66. NotificationCenter.default.post(name: QSLNotification.QSLRefreshRequest, object: nil)
  67. } fail: { code, error in
  68. self.view.toast(text: error)
  69. }
  70. }
  71. // 同意点击
  72. func accpetBtnAction(model: QSLRequestModel) {
  73. QSLNetwork().request(.requestAccept(dict: ["id": model.requestId])) { response in
  74. // 发送通知
  75. NotificationCenter.default.post(name: QSLNotification.QSLRefreshRequest, object: nil)
  76. } fail: { code, error in
  77. self.view.toast(text: error)
  78. }
  79. }
  80. }
  81. // MARK: - 设置Tableview
  82. extension QSLRequestController: UITableViewDelegate, UITableViewDataSource {
  83. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  84. return self.requestList.count
  85. }
  86. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  87. let cell = tableView.dequeueReusableCell(cellType: QSLRequestCell.self, cellForRowAt: indexPath)
  88. cell.selectionStyle = .none
  89. let model = self.requestList[indexPath.row]
  90. cell.config(model: model)
  91. cell.delegate = self
  92. return cell
  93. }
  94. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  95. return 117.rpx
  96. }
  97. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  98. return 1.rpx
  99. }
  100. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  101. return 0.0001
  102. }
  103. }
  104. extension QSLRequestController {
  105. func initializeView() {
  106. self.view.addSubview(requestTableView)
  107. requestTableView.snp.makeConstraints { make in
  108. make.left.right.bottom.equalTo(0)
  109. make.top.equalTo(0)
  110. }
  111. }
  112. }