Commit 2f573fa6 authored by Steven杜宇's avatar Steven杜宇

// AI

parent 072319e5
...@@ -18,6 +18,10 @@ class YHAIMainChatViewController: YHBaseViewController { ...@@ -18,6 +18,10 @@ class YHAIMainChatViewController: YHBaseViewController {
var isNeedAutoResponseImage = false var isNeedAutoResponseImage = false
var messages:[YHAIChatMessage] = [] var messages:[YHAIChatMessage] = []
var isUserScrolling: Bool = false
var lastUserScrollTime: CFTimeInterval = 0.0
let minimumScrollInterval: CFTimeInterval = 3.0
let manager = YHAIRequestManager() let manager = YHAIRequestManager()
let viewModel = YHAIViewModel() let viewModel = YHAIViewModel()
...@@ -108,10 +112,21 @@ class YHAIMainChatViewController: YHBaseViewController { ...@@ -108,10 +112,21 @@ class YHAIMainChatViewController: YHBaseViewController {
} }
func scrollToBottom() { func scrollToBottom() {
if !canTriggerProgrammaticScroll() {
return
}
self.tableView.reloadData { self.tableView.reloadData {
if self.messages.count > 0 { if self.messages.count > 0 {
let indexPath = IndexPath(row: self.messages.count-1, section: 0) self.tableView.setContentOffset(.zero, animated: true)
self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: true) self.tableView.setContentOffset(CGPointMake(0, 90000), animated: true)
// 使用 performBatchUpdates 来确保所有布局更新完成后再滚动
self.tableView.performBatchUpdates(nil) { _ in
// 在布局更新完成后执行滚动
let lastIndexPath = IndexPath(row: self.messages.count-1, section: 0)
self.tableView.scrollToRow(at: lastIndexPath, at: .bottom, animated: true)
}
} }
} }
} }
...@@ -182,11 +197,15 @@ class YHAIMainChatViewController: YHBaseViewController { ...@@ -182,11 +197,15 @@ class YHAIMainChatViewController: YHBaseViewController {
} }
func stopAutoResponse(completion:((Bool)->())? = nil) { func stopAutoResponse(completion:((Bool)->())? = nil) {
YHHUD.flash(message: "加载中...")
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.manager.request?.cancel() self.manager.request?.cancel()
self.chatConfig.disableHandleMessage = true self.chatConfig.disableHandleMessage = true
DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
YHHUD.hide()
self.chatConfig.removeThinkingMessageFromChatList(&self.messages) self.chatConfig.removeThinkingMessageFromChatList(&self.messages)
self.bottomInputView.status = self.bottomInputView.textView.text.isEmpty ? .disableSend : .enableSend self.bottomInputView.status = self.bottomInputView.textView.text.isEmpty ? .disableSend : .enableSend
self.tableView.reloadData() self.tableView.reloadData()
...@@ -194,6 +213,7 @@ class YHAIMainChatViewController: YHBaseViewController { ...@@ -194,6 +213,7 @@ class YHAIMainChatViewController: YHBaseViewController {
} }
} }
} }
}
func uploadEvaluationMessage(_ msg: YHAIChatMessage, callback:((Bool)->())? = nil) { func uploadEvaluationMessage(_ msg: YHAIChatMessage, callback:((Bool)->())? = nil) {
viewModel.createMessage(conversationId: conversationId, role: "assistant", msg: msg) { viewModel.createMessage(conversationId: conversationId, role: "assistant", msg: msg) {
...@@ -355,6 +375,26 @@ extension YHAIMainChatViewController: UITableViewDelegate, UITableViewDataSource ...@@ -355,6 +375,26 @@ extension YHAIMainChatViewController: UITableViewDelegate, UITableViewDataSource
return 1.0 return 1.0
} }
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
isUserScrolling = true
lastUserScrollTime = CACurrentMediaTime()
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if !decelerate {
isUserScrolling = false
}
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
isUserScrolling = false
}
// 检查是否可以执行代码触发的滚动
private func canTriggerProgrammaticScroll() -> Bool {
let currentTime = CACurrentMediaTime()
return !self.tableView.isDragging && (currentTime - lastUserScrollTime) >= minimumScrollInterval
}
} }
extension YHAIMainChatViewController: JXSegmentedListContainerViewListDelegate { extension YHAIMainChatViewController: JXSegmentedListContainerViewListDelegate {
......
...@@ -15,6 +15,9 @@ class YHAIRobotChatViewController: YHBaseViewController { ...@@ -15,6 +15,9 @@ class YHAIRobotChatViewController: YHBaseViewController {
var myTitle: String = "" var myTitle: String = ""
var robotId: String = "" var robotId: String = ""
var listItemId: Int = 0 var listItemId: Int = 0
var isUserScrolling: Bool = false
var lastUserScrollTime: CFTimeInterval = 0.0
let minimumScrollInterval: CFTimeInterval = 3.0
var conversationId: String = "" var conversationId: String = ""
var messages:[YHAIChatMessage] = [] var messages:[YHAIChatMessage] = []
var isNeedShowBannerHeader: Bool = false var isNeedShowBannerHeader: Bool = false
...@@ -29,7 +32,7 @@ class YHAIRobotChatViewController: YHBaseViewController { ...@@ -29,7 +32,7 @@ class YHAIRobotChatViewController: YHBaseViewController {
lazy var tableView: UITableView = { lazy var tableView: UITableView = {
let tableView = UITableView(frame:.zero, style:.grouped) let tableView = UITableView(frame: CGRectMake(0, 0, k_Height_NavigationtBarAndStatuBar, KScreenHeight-k_Height_NavigationtBarAndStatuBar-64-k_Height_safeAreaInsetsBottom()), style:.grouped)
if #available(iOS 11.0, *) { if #available(iOS 11.0, *) {
tableView.contentInsetAdjustmentBehavior = .never tableView.contentInsetAdjustmentBehavior = .never
...@@ -164,7 +167,7 @@ class YHAIRobotChatViewController: YHBaseViewController { ...@@ -164,7 +167,7 @@ class YHAIRobotChatViewController: YHBaseViewController {
make.left.right.bottom.equalToSuperview() make.left.right.bottom.equalToSuperview()
} }
getHistoryMessages(false) getHistoryMessages()
} }
override func viewWillDisappear(_ animated: Bool) { override func viewWillDisappear(_ animated: Bool) {
...@@ -183,12 +186,21 @@ class YHAIRobotChatViewController: YHBaseViewController { ...@@ -183,12 +186,21 @@ class YHAIRobotChatViewController: YHBaseViewController {
} }
func scrollToBottom() { func scrollToBottom() {
if !canTriggerProgrammaticScroll() {
return
}
self.tableView.reloadData { self.tableView.reloadData {
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 { if self.messages.count > 0 {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: { let lastIndexPath = IndexPath(row: self.messages.count-1, section: 0)
let indexPath = IndexPath(row: self.messages.count-1, section: 0) self.tableView.scrollToRow(at: lastIndexPath, at: .bottom, animated: true)
self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: true) }
})
} }
} }
} }
...@@ -214,20 +226,13 @@ class YHAIRobotChatViewController: YHBaseViewController { ...@@ -214,20 +226,13 @@ class YHAIRobotChatViewController: YHBaseViewController {
} }
} }
// isPull 是否是下拉组件触发 func getHistoryMessages() {
func getHistoryMessages(_ isPull: Bool) {
if !isPull {
YHHUD.show(.progress(message: "加载中..."))
} else {
printLog("PULL MESSAGE HISTORY")
}
// YHHUD.show(.progress(message: "加载中..."))
viewModel.getHistoryChatMessages(botId: robotId, conversationId: conversationId, messageId: "") { viewModel.getHistoryChatMessages(botId: robotId, conversationId: conversationId, messageId: "") {
[weak self] success, error in [weak self] success, error in
YHHUD.hide() // YHHUD.hide()
guard let self = self else { return } guard let self = self else { return }
self.tableView.es.stopPullToRefresh()
var results = self.viewModel.historyMessages.map { var results = self.viewModel.historyMessages.map {
return $0.convertToChatMessage() return $0.convertToChatMessage()
...@@ -250,7 +255,7 @@ class YHAIRobotChatViewController: YHBaseViewController { ...@@ -250,7 +255,7 @@ class YHAIRobotChatViewController: YHBaseViewController {
func autoResponseLocalTextMessage() { func autoResponseLocalTextMessage() {
if messages.count <= 0 { if messages.count <= 0 {
if robotType == YHAIRobotType.sale.rawValue { if robotType == YHAIRobotType.sale.rawValue {
let text = "银河集团能够为您提供全方位、一站式的香港身份规划及本地生活服务。\n在身份规划方面,我们专注于香港优才、专才、留学等身份产品服务,根据您的个人情况和需求,量身定制最合适的身份规划方案,助您轻松获取香港身份,享受香港的各项福利和优势。\n同时,我们还提供丰富的香港本地生活服务,包括教育咨询、商务支持、个人账户开设、港宝(即香港宝宝)相关服务、保险规划以及房产购置等。我们的专业团队对香港市场有着深入的了解,能够为您提供精准、高效的服务,让您在香港的生活更加便捷、舒适。\n银河集团凭借多年的行业经验和丰富的成功案例,以及专业、敬业的团队,致力于为客户提供最优质、最贴心的服务。选择银河集团,您将享受到专业、高效、便捷的服务体验,让我们携手共创美好未来。" let text = "你好,我是新港生活规划师,如果您有关于香港身份政策或者办理细节方面的问题,可以随时问我哦。"
let msg = self.chatConfig.createRobotResponseTextMessage(text) let msg = self.chatConfig.createRobotResponseTextMessage(text)
messages.append(msg) messages.append(msg)
...@@ -270,11 +275,15 @@ class YHAIRobotChatViewController: YHBaseViewController { ...@@ -270,11 +275,15 @@ class YHAIRobotChatViewController: YHBaseViewController {
} }
func stopAutoResponse(completion:((Bool)->())? = nil) { func stopAutoResponse(completion:((Bool)->())? = nil) {
YHHUD.flash(message: "加载中...")
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.manager.request?.cancel() self.manager.request?.cancel()
self.chatConfig.disableHandleMessage = true self.chatConfig.disableHandleMessage = true
DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
YHHUD.hide()
self.chatConfig.removeThinkingMessageFromChatList(&self.messages) self.chatConfig.removeThinkingMessageFromChatList(&self.messages)
self.bottomInputView.status = self.bottomInputView.textView.text.isEmpty ? .disableSend : .enableSend self.bottomInputView.status = self.bottomInputView.textView.text.isEmpty ? .disableSend : .enableSend
self.tableView.reloadData() self.tableView.reloadData()
...@@ -282,6 +291,7 @@ class YHAIRobotChatViewController: YHBaseViewController { ...@@ -282,6 +291,7 @@ class YHAIRobotChatViewController: YHBaseViewController {
} }
} }
} }
}
@objc func didCleanButtonClicked() { @objc func didCleanButtonClicked() {
...@@ -491,6 +501,27 @@ extension YHAIRobotChatViewController: UITableViewDelegate, UITableViewDataSourc ...@@ -491,6 +501,27 @@ extension YHAIRobotChatViewController: UITableViewDelegate, UITableViewDataSourc
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 1.0 return 1.0
} }
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
isUserScrolling = true
lastUserScrollTime = CACurrentMediaTime()
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if !decelerate {
isUserScrolling = false
}
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
isUserScrolling = false
}
// 检查是否可以执行代码触发的滚动
private func canTriggerProgrammaticScroll() -> Bool {
let currentTime = CACurrentMediaTime()
return !self.tableView.isDragging && (currentTime - lastUserScrollTime) >= minimumScrollInterval
}
} }
extension YHAIRobotChatViewController: JXSegmentedListContainerViewListDelegate { extension YHAIRobotChatViewController: JXSegmentedListContainerViewListDelegate {
......
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