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

// AI

parent fef975de
......@@ -111,19 +111,21 @@ class YHAIMainChatViewController: YHBaseViewController {
self.bottomInputView.showKeyBoard(false)
}
func scrollToBottom() {
func reloadAndScrollToBottom() {
self.tableView.reloadData()
if !canTriggerProgrammaticScroll() {
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(CGPointMake(0, 90000), animated: true)
// 使用 performBatchUpdates 来确保所有布局更新完成后再滚动
self.tableView.performBatchUpdates(nil) { _ in
// 在布局更新完成后执行滚动
if self.messages.count > 0 {
let lastIndexPath = IndexPath(row: self.messages.count-1, section: 0)
self.tableView.scrollToRow(at: lastIndexPath, at: .bottom, animated: true)
}
......@@ -139,7 +141,7 @@ class YHAIMainChatViewController: YHBaseViewController {
let question = YHAIChatMessage.createQuestionMessage(text)
self.messages.append(question)
self.messages.append(self.thinkingMessage)
self.scrollToBottom()
self.reloadAndScrollToBottom()
self.bottomInputView.status = .loading
self.chatConfig.disableHandleMessage = false
self.manager.requestAI(botId: self.robotId, conversationId: self.conversationId, question:text) {
......@@ -150,7 +152,7 @@ class YHAIMainChatViewController: YHBaseViewController {
print("RESPONSE-DONE")
self.bottomInputView.status = self.bottomInputView.textView.text.isEmpty ? .disableSend : .enableSend
}
self.scrollToBottom()
self.reloadAndScrollToBottom()
}
}
}
......@@ -178,9 +180,7 @@ class YHAIMainChatViewController: YHBaseViewController {
return !uuids.contains($0.messageId)
}
messages.insert(contentsOf: results, at: 0)
self.tableView.reloadData {
self.scrollToBottom()
}
self.reloadAndScrollToBottom()
}
}
......@@ -190,9 +190,7 @@ class YHAIMainChatViewController: YHBaseViewController {
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)
messages.append(msg)
self.tableView.reloadData {
self.scrollToBottom()
}
self.reloadAndScrollToBottom()
}
}
......@@ -201,7 +199,7 @@ class YHAIMainChatViewController: YHBaseViewController {
self.manager.stopChat(chatId: self.manager.chatId, conversationId: self.conversationId) { success, error in
self.bottomInputView.status = self.bottomInputView.textView.text.isEmpty ? .disableSend : .enableSend
if success {
self.scrollToBottom()
self.reloadAndScrollToBottom()
self.manager.request?.cancel()
self.chatConfig.disableHandleMessage = true
DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
......@@ -320,9 +318,7 @@ extension YHAIMainChatViewController: UITableViewDelegate, UITableViewDataSource
if arr.count > 0 {
messages.append(contentsOf: arr)
self.tableView.reloadData {
self.scrollToBottom()
}
self.reloadAndScrollToBottom()
}
}
cell.updateBlock = {
......
......@@ -186,15 +186,17 @@ class YHAIRobotChatViewController: YHBaseViewController {
self.navigationController?.popViewController(animated: true)
}
func scrollToBottom() {
func reloadAndScrollToBottom() {
self.tableView.reloadData()
if !canTriggerProgrammaticScroll() {
return
}
self.tableView.reloadData {
DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
self.tableView.setContentOffset(.zero, animated: true)
self.tableView.setContentOffset(CGPointMake(0, 90000), animated: true)
// 使用 performBatchUpdates 来确保所有布局更新完成后再滚动
self.tableView.performBatchUpdates(nil) { _ in
// 在布局更新完成后执行滚动
......@@ -214,7 +216,7 @@ class YHAIRobotChatViewController: YHBaseViewController {
messages.append(thinkingMessage)
self.bottomInputView.status = .loading
self.chatConfig.disableHandleMessage = false
self.scrollToBottom()
self.reloadAndScrollToBottom()
self.manager.requestAI(botId: self.robotId, conversationId: self.conversationId, question:text) {
[weak self] res, done in
guard let self = self else { return }
......@@ -223,7 +225,7 @@ class YHAIRobotChatViewController: YHBaseViewController {
print("RESPONSE-DONE")
self.bottomInputView.status = self.bottomInputView.textView.text.isEmpty ? .disableSend : .enableSend
}
self.scrollToBottom()
self.reloadAndScrollToBottom()
}
}
......@@ -262,9 +264,7 @@ class YHAIRobotChatViewController: YHBaseViewController {
}
}
self.tableView.reloadData {
self.scrollToBottom()
}
self.reloadAndScrollToBottom()
}
func uploadEvaluationMessage(_ msg: YHAIChatMessage, callback:((Bool)->())? = nil) {
......@@ -279,7 +279,7 @@ class YHAIRobotChatViewController: YHBaseViewController {
self.manager.stopChat(chatId: self.manager.chatId, conversationId: self.conversationId) { success, error in
self.bottomInputView.status = self.bottomInputView.textView.text.isEmpty ? .disableSend : .enableSend
if success {
self.scrollToBottom()
self.reloadAndScrollToBottom()
self.manager.request?.cancel()
self.chatConfig.disableHandleMessage = true
DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
......@@ -452,9 +452,7 @@ extension YHAIRobotChatViewController: UITableViewDelegate, UITableViewDataSourc
if arr.count > 0 {
messages.append(contentsOf: arr)
self.tableView.reloadData {
self.scrollToBottom()
}
self.reloadAndScrollToBottom()
}
}
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