فهرست منبع

Merge branch 'feature/ios_im_chat_load_more' into 'develop'

ios聊天窗口下拉加载更多消息内容

See merge request o2oa/o2oa!1071
楼国栋 5 سال پیش
والد
کامیت
d72c415cfc

+ 28 - 6
o2ios/O2Platform/App/IM-聊天/IMChatViewController.swift

@@ -62,7 +62,7 @@ class IMChatViewController: UIViewController {
     
     //private
     private var chatMessageList: [IMMessageInfo] = []
-    private var page = 1
+    private var page = 0
     private var isShowEmoji = false
     private var isShowAudioView = false
     private var bottomBarHeight = 64 //底部输入框 表情按钮 的高度
@@ -80,6 +80,10 @@ class IMChatViewController: UIViewController {
 //        self.tableView.rowHeight = UITableView.automaticDimension
 //        self.tableView.estimatedRowHeight = 144
         self.tableView.backgroundColor = UIColor(hex: "#f3f3f3")
+        self.tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
+           self.loadMsgList()
+        })
+        
         self.messageInputView.delegate = self
 
         //底部安全距离 老机型没有
@@ -111,7 +115,7 @@ class IMChatViewController: UIViewController {
         }
         
         //获取聊天数据
-        self.loadMsgList(page: page)
+        self.loadMsgList()
         //阅读
         self.viewModel.readConversation(conversationId: self.conversation?.id)
     }
@@ -204,11 +208,29 @@ class IMChatViewController: UIViewController {
     }
 
     //获取消息
-    private func loadMsgList(page: Int) {
+    private func loadMsgList() {
         if let c = self.conversation, let id = c.id {
-            self.viewModel.myMsgPageList(page: page, conversationId: id).then { (list) in
-                self.chatMessageList = list
-                self.scrollMessageToBottom()
+            self.viewModel.myMsgPageList(page: self.page + 1, conversationId: id).then { (list) in
+                if !list.isEmpty {
+                    self.page += 1
+                    self.chatMessageList.insert(contentsOf: list, at: 0)
+                    if self.page ==  1 {
+                        self.scrollMessageToBottom()
+                    }else {
+                        DispatchQueue.main.async {
+                            self.tableView.reloadData()
+                        }
+                    }
+                }
+                if self.tableView.mj_header.isRefreshing(){
+                    self.tableView.mj_header.endRefreshing()
+                }
+                
+            }.catch { (error) in
+                DDLogError(error.localizedDescription)
+                if self.tableView.mj_header.isRefreshing(){
+                    self.tableView.mj_header.endRefreshing()
+                }
             }
         } else {
             self.showError(title: "参数错误!!!")

+ 10 - 2
o2ios/O2Platform/App/IM-聊天/IMConversationListViewController.swift

@@ -59,6 +59,7 @@ class IMConversationListViewController: UIViewController {
     }
     
     func getInstantMsgList() {
+        self.showLoading()
         viewModel.getInstantMsgList().then { (list) in
             self.instantMsgList = list
             self.getConversationList()
@@ -77,7 +78,8 @@ class IMConversationListViewController: UIViewController {
                 }
             }
             DispatchQueue.main.async {
-                if self.conversationList.count > 0 || self.instantMsgList.count > 0{
+                self.hideLoading()
+                if self.conversationList.count > 0 || self.instantMsgList.count > 0 {
                     self.emptyView.isHidden = true
                 } else {
                     self.emptyView.isHidden = false
@@ -87,7 +89,13 @@ class IMConversationListViewController: UIViewController {
             }
 
         }.catch { (err) in
-            DispatchQueue.main.async { self.emptyView.isHidden = false }
+            DDLogError(err.localizedDescription)
+            DispatchQueue.main.async {
+                self.hideLoading()
+                if self.conversationList.count > 0 || self.instantMsgList.count > 0 {
+                    self.emptyView.isHidden = false
+                }
+            }
         }
     }
 

+ 14 - 3
o2ios/O2Platform/App/IM-聊天/IMViewModel.swift

@@ -137,7 +137,18 @@ extension IMViewModel {
                 let response = OOResult<BaseModelClass<[IMConversationInfo]>>(result)
                 if response.isResultSuccess() {
                     if let list = response.model?.data {
-                        fulfill(list)
+                        let rList = list.sorted { (f, s) -> Bool in
+                            let ft = f.lastMessage?.createTime ?? ""
+                            let st = s.lastMessage?.createTime ?? ""
+                            if ft == "" {
+                                return true
+                            }
+                            if st == "" {
+                                return false
+                            }
+                            return ft.toDate(formatter: "yyyy-MM-dd HH:mm:ss") > st.toDate(formatter: "yyyy-MM-dd HH:mm:ss")
+                        }
+                        fulfill(rList)
                     } else {
                         reject(OOAppError.apiEmptyResultError)
                     }
@@ -150,7 +161,7 @@ extension IMViewModel {
     //查询消息列表
     func myMsgPageList(page: Int, conversationId: String) -> Promise<[IMMessageInfo]> {
         return Promise { fulfill, reject in
-            self.communicateAPI.request(.msgListByPaging(page, 40, conversationId), completion: { result in
+            self.communicateAPI.request(.msgListByPaging(page, 15, conversationId), completion: { result in
                     let response = OOResult<BaseModelClass<[IMMessageInfo]>>(result)
                     if response.isResultSuccess() {
                         if let list = response.model?.data {
@@ -174,7 +185,7 @@ extension IMViewModel {
     
     func getInstantMsgList() -> Promise<[InstantMessage]> {
         return Promise { fulfill, reject in
-            self.communicateAPI.request(.instantMessageList(100), completion: { result in
+            self.communicateAPI.request(.instantMessageList(50), completion: { result in
                     let response = OOResult<BaseModelClass<[InstantMessage]>>(result)
                     if response.isResultSuccess() {
                         if let list = response.model?.data {

+ 0 - 1
o2ios/O2Platform/App/IM-聊天/View/IMConversationItemCell.swift

@@ -78,7 +78,6 @@ class IMConversationItemCell: UITableViewCell {
         }
         // message
         if let msgBody = conversation.lastMessage?.body, let body = parseJson(msg: msgBody) {
-            
             if body.type == o2_im_msg_type_text || body.type == o2_im_msg_type_image
                 || body.type == o2_im_msg_type_audio || body.type == o2_im_msg_type_location {
                 self.messageLabel.text = body.body