Commit 47a4f7b9 authored by Steven杜宇's avatar Steven杜宇

// AI

parent fef975de
...@@ -111,19 +111,21 @@ class YHAIMainChatViewController: YHBaseViewController { ...@@ -111,19 +111,21 @@ class YHAIMainChatViewController: YHBaseViewController {
self.bottomInputView.showKeyBoard(false) self.bottomInputView.showKeyBoard(false)
} }
func scrollToBottom() { func reloadAndScrollToBottom() {
self.tableView.reloadData()
if !canTriggerProgrammaticScroll() { if !canTriggerProgrammaticScroll() {
return return
} }
self.tableView.reloadData {
if self.messages.count > 0 { DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
self.tableView.setContentOffset(.zero, animated: true) self.tableView.setContentOffset(.zero, animated: true)
self.tableView.setContentOffset(CGPointMake(0, 90000), animated: true) self.tableView.setContentOffset(CGPointMake(0, 90000), animated: true)
// 使用 performBatchUpdates 来确保所有布局更新完成后再滚动 // 使用 performBatchUpdates 来确保所有布局更新完成后再滚动
self.tableView.performBatchUpdates(nil) { _ in self.tableView.performBatchUpdates(nil) { _ in
// 在布局更新完成后执行滚动 // 在布局更新完成后执行滚动
if self.messages.count > 0 {
let lastIndexPath = IndexPath(row: self.messages.count-1, section: 0) let lastIndexPath = IndexPath(row: self.messages.count-1, section: 0)
self.tableView.scrollToRow(at: lastIndexPath, at: .bottom, animated: true) self.tableView.scrollToRow(at: lastIndexPath, at: .bottom, animated: true)
} }
...@@ -139,7 +141,7 @@ class YHAIMainChatViewController: YHBaseViewController { ...@@ -139,7 +141,7 @@ class YHAIMainChatViewController: YHBaseViewController {
let question = YHAIChatMessage.createQuestionMessage(text) let question = YHAIChatMessage.createQuestionMessage(text)
self.messages.append(question) self.messages.append(question)
self.messages.append(self.thinkingMessage) self.messages.append(self.thinkingMessage)
self.scrollToBottom() self.reloadAndScrollToBottom()
self.bottomInputView.status = .loading self.bottomInputView.status = .loading
self.chatConfig.disableHandleMessage = false self.chatConfig.disableHandleMessage = false
self.manager.requestAI(botId: self.robotId, conversationId: self.conversationId, question:text) { self.manager.requestAI(botId: self.robotId, conversationId: self.conversationId, question:text) {
...@@ -150,7 +152,7 @@ class YHAIMainChatViewController: YHBaseViewController { ...@@ -150,7 +152,7 @@ class YHAIMainChatViewController: YHBaseViewController {
print("RESPONSE-DONE") print("RESPONSE-DONE")
self.bottomInputView.status = self.bottomInputView.textView.text.isEmpty ? .disableSend : .enableSend self.bottomInputView.status = self.bottomInputView.textView.text.isEmpty ? .disableSend : .enableSend
} }
self.scrollToBottom() self.reloadAndScrollToBottom()
} }
} }
} }
...@@ -178,9 +180,7 @@ class YHAIMainChatViewController: YHBaseViewController { ...@@ -178,9 +180,7 @@ class YHAIMainChatViewController: YHBaseViewController {
return !uuids.contains($0.messageId) return !uuids.contains($0.messageId)
} }
messages.insert(contentsOf: results, at: 0) messages.insert(contentsOf: results, at: 0)
self.tableView.reloadData { self.reloadAndScrollToBottom()
self.scrollToBottom()
}
} }
} }
...@@ -190,9 +190,7 @@ class YHAIMainChatViewController: YHBaseViewController { ...@@ -190,9 +190,7 @@ class YHAIMainChatViewController: YHBaseViewController {
let previewUrl = "https://prod-cdn-pub.galaxy-immi.com/5000000/chats/mask_group.jpg" let previewUrl = "https://prod-cdn-pub.galaxy-immi.com/5000000/chats/mask_group.jpg"
let msg = self.chatConfig.createRobotResponseLocalPictureMessage("ai_auto_chat_img", previewUrl:previewUrl) let msg = self.chatConfig.createRobotResponseLocalPictureMessage("ai_auto_chat_img", previewUrl:previewUrl)
messages.append(msg) messages.append(msg)
self.tableView.reloadData { self.reloadAndScrollToBottom()
self.scrollToBottom()
}
} }
} }
...@@ -201,7 +199,7 @@ class YHAIMainChatViewController: YHBaseViewController { ...@@ -201,7 +199,7 @@ class YHAIMainChatViewController: YHBaseViewController {
self.manager.stopChat(chatId: self.manager.chatId, conversationId: self.conversationId) { success, error in self.manager.stopChat(chatId: self.manager.chatId, conversationId: self.conversationId) { success, error in
self.bottomInputView.status = self.bottomInputView.textView.text.isEmpty ? .disableSend : .enableSend self.bottomInputView.status = self.bottomInputView.textView.text.isEmpty ? .disableSend : .enableSend
if success { if success {
self.scrollToBottom() self.reloadAndScrollToBottom()
self.manager.request?.cancel() self.manager.request?.cancel()
self.chatConfig.disableHandleMessage = true self.chatConfig.disableHandleMessage = true
DispatchQueue.main.asyncAfter(deadline: .now()+0.5) { DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
...@@ -320,9 +318,7 @@ extension YHAIMainChatViewController: UITableViewDelegate, UITableViewDataSource ...@@ -320,9 +318,7 @@ extension YHAIMainChatViewController: UITableViewDelegate, UITableViewDataSource
if arr.count > 0 { if arr.count > 0 {
messages.append(contentsOf: arr) messages.append(contentsOf: arr)
self.tableView.reloadData { self.reloadAndScrollToBottom()
self.scrollToBottom()
}
} }
} }
cell.updateBlock = { cell.updateBlock = {
......
...@@ -186,23 +186,25 @@ class YHAIRobotChatViewController: YHBaseViewController { ...@@ -186,23 +186,25 @@ class YHAIRobotChatViewController: YHBaseViewController {
self.navigationController?.popViewController(animated: true) self.navigationController?.popViewController(animated: true)
} }
func scrollToBottom() { func reloadAndScrollToBottom() {
self.tableView.reloadData()
if !canTriggerProgrammaticScroll() { if !canTriggerProgrammaticScroll() {
return return
} }
self.tableView.reloadData {
DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
self.tableView.setContentOffset(.zero, animated: true) self.tableView.setContentOffset(.zero, animated: true)
self.tableView.setContentOffset(CGPointMake(0, 90000), animated: true) self.tableView.setContentOffset(CGPointMake(0, 90000), animated: true)
// 使用 performBatchUpdates 来确保所有布局更新完成后再滚动 // 使用 performBatchUpdates 来确保所有布局更新完成后再滚动
self.tableView.performBatchUpdates(nil) { _ in self.tableView.performBatchUpdates(nil) { _ in
// 在布局更新完成后执行滚动 // 在布局更新完成后执行滚动
if self.messages.count > 0 { if self.messages.count > 0 {
let lastIndexPath = IndexPath(row: self.messages.count-1, section: 0) let lastIndexPath = IndexPath(row: self.messages.count-1, section: 0)
self.tableView.scrollToRow(at: lastIndexPath, at: .bottom, animated: true) self.tableView.scrollToRow(at: lastIndexPath, at: .bottom, animated: true)
} }
} }
} }
} }
...@@ -214,7 +216,7 @@ class YHAIRobotChatViewController: YHBaseViewController { ...@@ -214,7 +216,7 @@ class YHAIRobotChatViewController: YHBaseViewController {
messages.append(thinkingMessage) messages.append(thinkingMessage)
self.bottomInputView.status = .loading self.bottomInputView.status = .loading
self.chatConfig.disableHandleMessage = false self.chatConfig.disableHandleMessage = false
self.scrollToBottom() self.reloadAndScrollToBottom()
self.manager.requestAI(botId: self.robotId, conversationId: self.conversationId, question:text) { self.manager.requestAI(botId: self.robotId, conversationId: self.conversationId, question:text) {
[weak self] res, done in [weak self] res, done in
guard let self = self else { return } guard let self = self else { return }
...@@ -223,7 +225,7 @@ class YHAIRobotChatViewController: YHBaseViewController { ...@@ -223,7 +225,7 @@ class YHAIRobotChatViewController: YHBaseViewController {
print("RESPONSE-DONE") print("RESPONSE-DONE")
self.bottomInputView.status = self.bottomInputView.textView.text.isEmpty ? .disableSend : .enableSend self.bottomInputView.status = self.bottomInputView.textView.text.isEmpty ? .disableSend : .enableSend
} }
self.scrollToBottom() self.reloadAndScrollToBottom()
} }
} }
...@@ -262,9 +264,7 @@ class YHAIRobotChatViewController: YHBaseViewController { ...@@ -262,9 +264,7 @@ class YHAIRobotChatViewController: YHBaseViewController {
} }
} }
self.tableView.reloadData { self.reloadAndScrollToBottom()
self.scrollToBottom()
}
} }
func uploadEvaluationMessage(_ msg: YHAIChatMessage, callback:((Bool)->())? = nil) { func uploadEvaluationMessage(_ msg: YHAIChatMessage, callback:((Bool)->())? = nil) {
...@@ -279,7 +279,7 @@ class YHAIRobotChatViewController: YHBaseViewController { ...@@ -279,7 +279,7 @@ class YHAIRobotChatViewController: YHBaseViewController {
self.manager.stopChat(chatId: self.manager.chatId, conversationId: self.conversationId) { success, error in self.manager.stopChat(chatId: self.manager.chatId, conversationId: self.conversationId) { success, error in
self.bottomInputView.status = self.bottomInputView.textView.text.isEmpty ? .disableSend : .enableSend self.bottomInputView.status = self.bottomInputView.textView.text.isEmpty ? .disableSend : .enableSend
if success { if success {
self.scrollToBottom() self.reloadAndScrollToBottom()
self.manager.request?.cancel() self.manager.request?.cancel()
self.chatConfig.disableHandleMessage = true self.chatConfig.disableHandleMessage = true
DispatchQueue.main.asyncAfter(deadline: .now()+0.5) { DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
...@@ -452,9 +452,7 @@ extension YHAIRobotChatViewController: UITableViewDelegate, UITableViewDataSourc ...@@ -452,9 +452,7 @@ extension YHAIRobotChatViewController: UITableViewDelegate, UITableViewDataSourc
if arr.count > 0 { if arr.count > 0 {
messages.append(contentsOf: arr) messages.append(contentsOf: arr)
self.tableView.reloadData { self.reloadAndScrollToBottom()
self.scrollToBottom()
}
} }
} }
cell.updateBlock = { cell.updateBlock = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment