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

// AI

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