|
|
@@ -39,6 +39,9 @@ class ChatVM : BaseListViewModel<ChatItem>() {
|
|
|
private var dataSourceReady = false
|
|
|
private val mainHandler = Handler(Looper.getMainLooper())
|
|
|
|
|
|
+ /** 进入页面时由 prepareTopic 设置,后续所有 demoPostSse 复用 */
|
|
|
+ private var currentTopicId: Int = DEFAULT_TOPIC_ID
|
|
|
+
|
|
|
/** 最后一个真实 item 的索引(占位 item 之前) */
|
|
|
private val lastRealIndex get() = chatItems.size - 2
|
|
|
|
|
|
@@ -78,8 +81,8 @@ class ChatVM : BaseListViewModel<ChatItem>() {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 先请求 tbtopic 接口拿 topicId,成功则使用返回值,失败/code≠0 则用默认值,再发起 SSE。
|
|
|
- * 进入 Chat 界面时用此方法替代直接调 demoPostSse。
|
|
|
+ * 进入 Chat 页面时调用一次:请求 tbtopic 接口设置本次会话的 topicId,
|
|
|
+ * 成功(code==0)使用返回值,否则沿用默认值 14478,然后发起第一次 SSE。
|
|
|
*/
|
|
|
fun prepareTopic(question: String) {
|
|
|
HttpManager.getApi(Service::class.java)
|
|
|
@@ -87,14 +90,15 @@ class ChatVM : BaseListViewModel<ChatItem>() {
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
.subscribe({ model ->
|
|
|
- val topicId = if (model.code == 0) model.data else DEFAULT_TOPIC_ID
|
|
|
- demoPostSse(question, topicId)
|
|
|
+ currentTopicId = if (model.code == 0) model.data else DEFAULT_TOPIC_ID
|
|
|
+ demoPostSse(question)
|
|
|
}, { _ ->
|
|
|
- demoPostSse(question, DEFAULT_TOPIC_ID)
|
|
|
+ currentTopicId = DEFAULT_TOPIC_ID
|
|
|
+ demoPostSse(question)
|
|
|
}).also { add(it) }
|
|
|
}
|
|
|
|
|
|
- fun demoPostSse(question: String, topicId: Int = DEFAULT_TOPIC_ID) {
|
|
|
+ fun demoPostSse(question: String) {
|
|
|
currentTask?.dispose()
|
|
|
currentTask = null
|
|
|
stopThinkingAnimation()
|
|
|
@@ -106,7 +110,7 @@ class ChatVM : BaseListViewModel<ChatItem>() {
|
|
|
result.postValue(UUID.randomUUID().toString())
|
|
|
loading.postValue(true)
|
|
|
|
|
|
- currentTask = HttpManager.getApi(Service::class.java).chat(ChatData(question, topicId))
|
|
|
+ currentTask = HttpManager.getApi(Service::class.java).chat(ChatData(question, currentTopicId))
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
.subscribe({ body ->
|
|
|
var content = ""
|