|
|
@@ -35,13 +35,17 @@ class ChatActivity : BaseListFormLayoutNormalActivity<ChatItem, ChatVM, Activity
|
|
|
override fun initData() {
|
|
|
super.initData()
|
|
|
|
|
|
+ // SSE 流式内容更新时持续滚底
|
|
|
viewModel.result.observe(this) {
|
|
|
- val lastIndex = (recyclerView.adapter?.itemCount ?: 1) - 1
|
|
|
- if (lastIndex >= 0) recyclerView.scrollToPosition(lastIndex)
|
|
|
+ scrollToBottom()
|
|
|
}
|
|
|
|
|
|
+ // loading=true:pb 显示旋转 + 延一帧滚底(等 PagedList 提交到 adapter)
|
|
|
+ // loading=false:pb 停转并隐藏
|
|
|
viewModel.loading.observe(this) { loading ->
|
|
|
- binding.pb.isIndeterminate = loading
|
|
|
+ binding.pb.visibility = if (loading) View.VISIBLE else View.INVISIBLE
|
|
|
+ binding.pb1.visibility = if (!loading) View.VISIBLE else View.INVISIBLE
|
|
|
+ recyclerView.post { scrollToBottom() }
|
|
|
}
|
|
|
|
|
|
val question = intent.getStringExtra("question") ?: ""
|
|
|
@@ -50,18 +54,47 @@ class ChatActivity : BaseListFormLayoutNormalActivity<ChatItem, ChatVM, Activity
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 调用意图识别,识别成功后发起 SSE 请求。
|
|
|
+ * 立即启动 pb 旋转,不等 recognize 接口响应。
|
|
|
+ */
|
|
|
private fun recognizeAndChat() {
|
|
|
+ binding.pb.visibility = View.VISIBLE
|
|
|
+ binding.pb1.visibility = View.INVISIBLE
|
|
|
IntentRecognizeHelper.recognize(
|
|
|
context = this,
|
|
|
scence = "decision",
|
|
|
onSuccess = { action ->
|
|
|
if (action.name == "goToDecisionCenter") {
|
|
|
viewModel.demoPostSse(action.params.question)
|
|
|
+ } else {
|
|
|
+ // 识别成功但没有匹配动作,停止旋转
|
|
|
+ binding.pb.isIndeterminate = false
|
|
|
+ binding.pb.visibility = View.INVISIBLE
|
|
|
}
|
|
|
+ },
|
|
|
+ onComplete = {
|
|
|
+ // recognize 失败时兜底停止旋转(成功路径由 loading LiveData 接管)
|
|
|
+ binding.pb.isIndeterminate = false
|
|
|
+ binding.pb.visibility = View.INVISIBLE
|
|
|
}
|
|
|
)
|
|
|
}
|
|
|
|
|
|
+ private fun scrollToBottom() {
|
|
|
+ val lastIndex = (recyclerView.adapter?.itemCount ?: 1) - 1
|
|
|
+ if (lastIndex < 0) return
|
|
|
+ val lm = recyclerView.layoutManager as? androidx.recyclerview.widget.LinearLayoutManager ?: return
|
|
|
+ // 第一帧:确保 lastIndex 进入可视区域
|
|
|
+ lm.scrollToPosition(lastIndex)
|
|
|
+ // 第二帧:layout 完成后,把 lastView 的底部对齐到 RecyclerView 底部
|
|
|
+ recyclerView.post {
|
|
|
+ val lastView = lm.findViewByPosition(lastIndex) ?: return@post
|
|
|
+ val gap = lastView.bottom - (recyclerView.height - recyclerView.paddingBottom)
|
|
|
+ if (gap > 0) recyclerView.scrollBy(0, gap)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
override fun adapter() = object : CommonPagedAdapter<ChatItem>(R.layout.item_chat) {
|
|
|
override fun convert(holder: ViewHolder, item: ChatItem, position: Int) {
|
|
|
holder.setVisibility(R.id.line, position != 0)
|