IMConversationListViewController.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 - TAB_BAR_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. private var instantMsgList: [InstantMessage] = []
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. self.navigationItem.rightBarButtonItems = [UIBarButtonItem(image: UIImage(named: "add"), style: .plain, target: self, action: #selector(addConversation))]
  40. view.addSubview(tableview)
  41. view.addSubview(emptyView)
  42. NotificationCenter.default.addObserver(self, selector: #selector(receiveMessageFromWs(notice:)), name: OONotification.websocket.notificationName, object: nil)
  43. }
  44. override func viewWillAppear(_ animated: Bool) {
  45. getInstantMsgList()
  46. }
  47. func getInstantMsgList() {
  48. self.showLoading()
  49. viewModel.getInstantMsgList().then { (list) in
  50. self.instantMsgList = list
  51. self.getConversationList()
  52. }
  53. }
  54. func getConversationList() {
  55. viewModel.myConversationList().then { (list) in
  56. self.conversationList = list
  57. var n = 0
  58. if !self.conversationList.isEmpty {
  59. for item in self.conversationList {
  60. if let number = item.unreadNumber {
  61. n += number
  62. }
  63. }
  64. }
  65. DispatchQueue.main.async {
  66. self.hideLoading()
  67. if self.conversationList.count > 0 || self.instantMsgList.count > 0 {
  68. self.emptyView.isHidden = true
  69. } else {
  70. self.emptyView.isHidden = false
  71. }
  72. self.tableview.reloadData()
  73. self.refreshRedPoint(number: n)
  74. }
  75. }.catch { (err) in
  76. DDLogError(err.localizedDescription)
  77. DispatchQueue.main.async {
  78. self.hideLoading()
  79. if self.conversationList.count > 0 || self.instantMsgList.count > 0 {
  80. self.emptyView.isHidden = false
  81. }
  82. }
  83. }
  84. }
  85. //接收websocket消息
  86. @objc private func receiveMessageFromWs(notice: Notification) {
  87. DDLogDebug("接收到websocket im 消息")
  88. if let message = notice.object as? IMMessageInfo {
  89. if self.conversationList.contains(where: { (info) -> Bool in
  90. return info.id == message.conversationId
  91. }) {
  92. DDLogDebug("有对应的会话 刷新列表")
  93. var newList: [IMConversationInfo] = []
  94. self.conversationList.forEach { (info) in
  95. if message.conversationId != nil && info.id == message.conversationId {
  96. info.lastMessage = message
  97. info.unreadNumber = (info.unreadNumber ?? 0) + 1
  98. }
  99. newList.append(info)
  100. }
  101. self.conversationList = newList
  102. DispatchQueue.main.async {
  103. self.tableview.reloadData()
  104. }
  105. } else {
  106. DDLogDebug("没有对应的会话 重新获取会话列表")
  107. self.getInstantMsgList()
  108. }
  109. } else {
  110. DDLogError("不正确的消息类型。。。")
  111. }
  112. }
  113. private func refreshRedPoint(number: Int) {
  114. if number > 0 && number < 100 {
  115. self.navigationController?.tabBarItem.badgeValue = "\(number)"
  116. } else if number >= 100 {
  117. self.navigationController?.tabBarItem.badgeValue = "99.."
  118. }else {
  119. self.navigationController?.tabBarItem.badgeValue = nil
  120. }
  121. }
  122. @objc private func addConversation() {
  123. self.showSheetAction(title: nil, message: nil, actions: [
  124. UIAlertAction(title: "创建单聊", style: .default, handler: { (action) in
  125. self.createSingleConversation()
  126. }),
  127. UIAlertAction(title: "创建群聊", style: .default, handler: { (action) in
  128. self.createGroupConversation()
  129. })
  130. ])
  131. }
  132. private func createSingleConversation() {
  133. self.showContactPicker(modes: [.person], callback: { (result) in
  134. if let users = result.users, users.count > 0 {
  135. self.viewModel.createConversation(type: o2_im_conversation_type_single, users: [users[0].distinguishedName!]).then { (con) in
  136. self.createConversationSuccess(conv: con)
  137. }.catch { (err) in
  138. self.showError(title: "创建单聊失败, \(err.localizedDescription)")
  139. }
  140. }
  141. }, multiple: false)
  142. }
  143. private func createGroupConversation() {
  144. self.showContactPicker(modes: [.person], callback: { (result) in
  145. if let users = result.users, users.count > 0 {
  146. let array = users.map { (item) -> String in
  147. item.distinguishedName!
  148. }
  149. self.viewModel.createConversation(type: o2_im_conversation_type_group, users: array).then { (conv) in
  150. self.createConversationSuccess(conv: conv)
  151. }.catch { (err) in
  152. self.showError(title: "创建群聊失败, \(err.localizedDescription)")
  153. }
  154. }
  155. })
  156. }
  157. //创建会话成功 打开聊天界面
  158. private func createConversationSuccess(conv: IMConversationInfo) {
  159. if !self.conversationList.contains(where: { (info) -> Bool in
  160. return info.id == conv.id
  161. }) {
  162. self.conversationList.append(conv)
  163. DispatchQueue.main.async {
  164. self.tableview.reloadData()
  165. }
  166. }
  167. let chatView = IMChatViewController()
  168. chatView.conversation = conv
  169. self.navigationController?.pushViewController(chatView, animated: true)
  170. }
  171. }
  172. // MARK: - tableview delegate
  173. extension IMConversationListViewController: UITableViewDelegate, UITableViewDataSource {
  174. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  175. if self.instantMsgList.count > 0 {
  176. return self.conversationList.count + 1
  177. }
  178. return self.conversationList.count
  179. }
  180. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  181. if let cell = tableView.dequeueReusableCell(withIdentifier: "IMConversationItemCell", for: indexPath) as? IMConversationItemCell {
  182. if self.instantMsgList.count > 0 {
  183. if indexPath.row == 0 {
  184. cell.setInstantContent(item: self.instantMsgList.last!)
  185. }else {
  186. cell.bindConversation(conversation: self.conversationList[indexPath.row - 1])
  187. }
  188. }else {
  189. cell.bindConversation(conversation: self.conversationList[indexPath.row])
  190. }
  191. return cell
  192. }
  193. return UITableViewCell()
  194. }
  195. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  196. return 64
  197. }
  198. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  199. DDLogDebug("点击了 row \(indexPath.row)")
  200. if self.instantMsgList.count > 0 {
  201. if indexPath.row == 0 {
  202. let instantView = IMInstantMessageViewController()
  203. instantView.instantMsgList = self.instantMsgList
  204. self.navigationController?.pushViewController(instantView, animated: true)
  205. }else {
  206. gotoChatView(row: indexPath.row-1)
  207. }
  208. }else {
  209. gotoChatView(row: indexPath.row)
  210. }
  211. }
  212. private func gotoChatView(row: Int) {
  213. let chatView = IMChatViewController()
  214. chatView.conversation = self.conversationList[row]
  215. self.navigationController?.pushViewController(chatView, animated: true)
  216. }
  217. }