IMChatViewController.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // IMChatViewController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2020/6/8.
  6. // Copyright © 2020 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. import O2OA_Auth_SDK
  11. class IMChatViewController: UIViewController {
  12. // MARK: - IBOutlet
  13. //消息列表
  14. @IBOutlet weak var tableView: UITableView!
  15. //消息输入框
  16. @IBOutlet weak var messageInputView: UITextField!
  17. //底部工具栏的高度约束
  18. @IBOutlet weak var bottomBarHeightConstraint: NSLayoutConstraint!
  19. //底部工具栏
  20. @IBOutlet weak var bottomBar: UIView!
  21. private lazy var viewModel: IMViewModel = {
  22. return IMViewModel()
  23. }()
  24. // MARK: - properties
  25. var conversation: IMConversationInfo? = nil
  26. private var chatMessageList: [IMMessageInfo] = []
  27. private var page = 1
  28. private var isShowEmoji = false
  29. private var bottomBarHeight = 64
  30. // MARK: - functions
  31. override func viewDidLoad() {
  32. super.viewDidLoad()
  33. self.tableView.delegate = self
  34. self.tableView.dataSource = self
  35. self.tableView.register(UINib(nibName: "IMChatMessageViewCell", bundle: nil), forCellReuseIdentifier: "IMChatMessageViewCell")
  36. self.tableView.register(UINib(nibName: "IMChatMessageSendViewCell", bundle: nil), forCellReuseIdentifier: "IMChatMessageSendViewCell")
  37. self.tableView.separatorStyle = .none
  38. self.tableView.rowHeight = UITableView.automaticDimension
  39. self.tableView.estimatedRowHeight = 144
  40. self.messageInputView.delegate = self
  41. //底部安全距离 老机型没有
  42. self.bottomBarHeight = Int(iPhoneX ? 64 + IPHONEX_BOTTOM_SAFE_HEIGHT: 64)
  43. self.bottomBarHeightConstraint.constant = self.bottomBarHeight.toCGFloat
  44. self.bottomBar.topBorder(width: 1, borderColor: base_gray_color.alpha(0.5))
  45. self.messageInputView.backgroundColor = base_gray_color
  46. //标题
  47. if let c = self.conversation {
  48. var person = ""
  49. c.personList?.forEach({ (p) in
  50. if p != O2AuthSDK.shared.myInfo()?.distinguishedName {
  51. person = p
  52. }
  53. })
  54. if !person.isEmpty {
  55. self.title = person.split("@").first ?? ""
  56. }
  57. }
  58. //获取聊天数据
  59. self.loadMsgList(page: page)
  60. }
  61. //获取消息
  62. private func loadMsgList(page: Int) {
  63. if let c = self.conversation, let id = c.id {
  64. self.viewModel.myMsgPageList(page: page, conversationId: id).then { (list) in
  65. self.chatMessageList = list
  66. DispatchQueue.main.async {
  67. self.tableView.reloadData()
  68. if self.chatMessageList.count > 0 {
  69. self.tableView.scrollToRow(at: IndexPath(row: self.chatMessageList.count-1, section: 0), at: .bottom, animated: true)
  70. }
  71. }
  72. }
  73. } else {
  74. self.showError(title: "参数错误!!!")
  75. }
  76. }
  77. // MARK: - IBAction
  78. //点击表情按钮
  79. @IBAction func clickEmojiBtn(_ sender: UIButton) {
  80. self.isShowEmoji.toggle()
  81. self.view.endEditing(true)
  82. if self.isShowEmoji {
  83. self.bottomBarHeightConstraint.constant = self.bottomBarHeight.toCGFloat + 128
  84. } else {
  85. self.bottomBarHeightConstraint.constant = self.bottomBarHeight.toCGFloat
  86. }
  87. self.view.layoutIfNeeded()
  88. }
  89. }
  90. // MARK: - tableview delegate
  91. extension IMChatViewController: UITableViewDelegate, UITableViewDataSource {
  92. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  93. return self.chatMessageList.count
  94. }
  95. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  96. let msg = self.chatMessageList[indexPath.row]
  97. if msg.createPerson == O2AuthSDK.shared.myInfo()?.distinguishedName { //发送者
  98. if let cell = tableView.dequeueReusableCell(withIdentifier: "IMChatMessageSendViewCell", for: indexPath) as? IMChatMessageSendViewCell {
  99. cell.setContent(item: self.chatMessageList[indexPath.row])
  100. return cell
  101. }
  102. }else {
  103. if let cell = tableView.dequeueReusableCell(withIdentifier: "IMChatMessageViewCell", for: indexPath) as? IMChatMessageViewCell {
  104. cell.setContent(item: self.chatMessageList[indexPath.row])
  105. return cell
  106. }
  107. }
  108. return UITableViewCell()
  109. }
  110. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  111. tableView.deselectRow(at: indexPath, animated: false)
  112. }
  113. // func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  114. // guard let c = cell as? IMChatMessageViewCell else {
  115. // return
  116. // }
  117. // //todo
  118. // c.setContent(item: self.chatMessageList[indexPath.row])
  119. // }
  120. }
  121. // MARK: - textField delegate
  122. extension IMChatViewController: UITextFieldDelegate {
  123. func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
  124. DDLogDebug("准备开始输入......")
  125. closeEmoji()
  126. return true
  127. }
  128. private func closeEmoji() {
  129. self.isShowEmoji = false
  130. self.bottomBarHeightConstraint.constant = 64
  131. self.view.layoutIfNeeded()
  132. }
  133. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  134. DDLogDebug("回车。。。。")
  135. return true
  136. }
  137. }