IMConversationListViewController.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // IMConversationListViewController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2020/6/4.
  6. // Copyright © 2020 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. class IMConversationListViewController: UIViewController {
  11. fileprivate lazy var tableview: UITableView = {
  12. var tableview = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.width, height: self.view.height))
  13. tableview.delegate = self
  14. tableview.dataSource = self
  15. tableview.backgroundColor = UIColor(netHex: 0xe8edf3)
  16. tableview.register(UINib(nibName: "IMConversationItemCell", bundle: nil), forCellReuseIdentifier: "IMConversationItemCell")
  17. tableview.separatorStyle = .none
  18. return tableview
  19. }()
  20. fileprivate lazy var emptyView: UIView = {
  21. let view = UIView(frame: CGRect(x: 0, y: 36, width: self.view.width, height: self.view.height - 36))
  22. view.isHidden = true
  23. view.backgroundColor = .white
  24. let tips = UILabel()
  25. tips.text = "暂无会话"
  26. tips.textColor = UIColor(netHex: 0x666666)
  27. tips.sizeToFit()
  28. tips.center = CGPoint(x: view.centerX, y: view.height / 2 - 60)
  29. view.addSubview(tips)
  30. return view
  31. }()
  32. private lazy var viewModel: IMViewModel = {
  33. return IMViewModel()
  34. }()
  35. private var conversationList: [IMConversationInfo] = []
  36. override func viewDidLoad() {
  37. super.viewDidLoad()
  38. view.addSubview(tableview)
  39. view.addSubview(emptyView)
  40. NotificationCenter.default.addObserver(self, selector: #selector(receiveMessageFromWs(notice:)), name: OONotification.websocket.notificationName, object: nil)
  41. }
  42. override func viewWillAppear(_ animated: Bool) {
  43. getConversationList()
  44. }
  45. func getConversationList() {
  46. viewModel.myConversationList().then { (list) in
  47. self.conversationList = list
  48. DispatchQueue.main.async {
  49. if self.conversationList.count > 0 {
  50. self.emptyView.isHidden = true
  51. } else {
  52. self.emptyView.isHidden = false
  53. }
  54. self.tableview.reloadData()
  55. }
  56. }.catch { (err) in
  57. DispatchQueue.main.async { self.emptyView.isHidden = false }
  58. }
  59. }
  60. //接收websocket消息
  61. @objc private func receiveMessageFromWs(notice: Notification) {
  62. DDLogDebug("接收到websocket im 消息")
  63. if let message = notice.object as? IMMessageInfo {
  64. if self.conversationList.contains(where: { (info) -> Bool in
  65. return info.id == message.conversationId
  66. }) {
  67. DDLogDebug("有对应的会话 刷新列表")
  68. var newList: [IMConversationInfo] = []
  69. self.conversationList.forEach { (info) in
  70. if message.conversationId != nil && info.id == message.conversationId {
  71. info.lastMessage = message
  72. info.unreadNumber = (info.unreadNumber ?? 0) + 1
  73. }
  74. newList.append(info)
  75. }
  76. self.conversationList = newList
  77. DispatchQueue.main.async {
  78. self.tableview.reloadData()
  79. }
  80. }else {
  81. DDLogDebug("没有对应的会话 重新获取会话列表")
  82. self.getConversationList()
  83. }
  84. }else {
  85. DDLogError("不正确的消息类型。。。")
  86. }
  87. }
  88. }
  89. // MARK: - tableview delegate
  90. extension IMConversationListViewController: UITableViewDelegate, UITableViewDataSource {
  91. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  92. return self.conversationList.count
  93. }
  94. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  95. return tableView.dequeueReusableCell(withIdentifier: "IMConversationItemCell", for: indexPath)
  96. }
  97. func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  98. guard let c = cell as? IMConversationItemCell else {
  99. return
  100. }
  101. c.bindConversation(conversation: self.conversationList[indexPath.row])
  102. }
  103. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  104. return 64
  105. }
  106. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  107. DDLogDebug("点击了 row \(indexPath.row)")
  108. let chatView = IMChatViewController()
  109. chatView.conversation = self.conversationList[indexPath.row]
  110. self.navigationController?.pushViewController(chatView, animated: true)
  111. }
  112. //todo can edit
  113. }