IMConversationListViewController.swift 8.8 KB

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