Skip to content

Commit a4ef318

Browse files
committed
「新增」
对话后自动将当前对话提到对话列表的最前面
1 parent 791a202 commit a4ef318

2 files changed

Lines changed: 11 additions & 11 deletions

File tree

app/src/main/java/com/xjyzs/aiapi/MainActivity.kt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import androidx.compose.foundation.layout.navigationBarsPadding
3737
import androidx.compose.foundation.layout.padding
3838
import androidx.compose.foundation.layout.size
3939
import androidx.compose.foundation.layout.width
40-
import androidx.compose.foundation.layout.wrapContentWidth
4140
import androidx.compose.foundation.lazy.LazyColumn
4241
import androidx.compose.foundation.lazy.LazyListState
4342
import androidx.compose.foundation.lazy.itemsIndexed
@@ -48,7 +47,6 @@ import androidx.compose.foundation.text.BasicTextField
4847
import androidx.compose.foundation.text.KeyboardOptions
4948
import androidx.compose.foundation.text.selection.SelectionContainer
5049
import androidx.compose.material.icons.Icons
51-
import androidx.compose.material.icons.filled.AccountCircle
5250
import androidx.compose.material.icons.filled.AddCircle
5351
import androidx.compose.material.icons.filled.Api
5452
import androidx.compose.material.icons.filled.ArrowUpward
@@ -322,11 +320,7 @@ fun MainUI(viewModel: ChatViewModel) {
322320
viewModel.sessions.addAll(Gson().fromJson(sessionsPref.getString("sessions", "[]")!!,
323321
object : TypeToken<List<String>>() {}.type))
324322
val assistantsPref = context.getSharedPreferences("assistants", Context.MODE_PRIVATE)
325-
val configsList = mutableListOf<String>()
326323
currentConfig = currentConfigPref.getString("currentConfig", "")!!
327-
for (i in assistantsPref.all) {
328-
configsList.add(i.key)
329-
}
330324
val assistants = JsonParser.parseString(
331325
assistantsPref.getString(
332326
currentConfig,
@@ -336,7 +330,7 @@ fun MainUI(viewModel: ChatViewModel) {
336330
api_url = assistants.get("apiUrl").asString;api_key = assistants.get("apiKey").asString;model =
337331
assistants.get("model").asString;systemPrompt = assistants.get("systemPrompt").asString
338332
if (viewModel.sessions.isEmpty()) {
339-
viewModel.sessions.add("新对话" + System.currentTimeMillis().toString())
333+
viewModel.sessions.add("新对话" + System.currentTimeMillis().toString()+"\u200B")
340334
}
341335
viewModel.currentSession = viewModel.sessions.last()
342336
if (history.getString(viewModel.currentSession, "")!!.isNotEmpty()) {
@@ -356,7 +350,7 @@ fun MainUI(viewModel: ChatViewModel) {
356350
}
357351

358352
// 自动滚动
359-
LaunchedEffect(if (viewModel.msgs.isNotEmpty()) viewModel.msgs.last().content.length else 0) {
353+
LaunchedEffect(if (viewModel.msgs.isNotEmpty()) viewModel.msgs.last().content.length else 0,viewModel.isLoading) {
360354
if (!lazyListState.isScrollInProgress) {
361355
if (lazyListState.layoutInfo.visibleItemsInfo.lastOrNull()?.index == viewModel.msgs.lastIndex) {
362356
if (distanceToBottomCurrentElement < 500) {
@@ -445,13 +439,15 @@ fun MainUI(viewModel: ChatViewModel) {
445439
msg = viewModel.inputMsg,
446440
onMsgChange = { viewModel.inputMsg = it },
447441
onSend = {
442+
viewModel.sessions.remove(viewModel.currentSession)
443+
viewModel.sessions.add(viewModel.currentSession)
448444
if (viewModel.isLoading) {
449445
cancel = true
450446
} else {
451447
try {
452448
if (it.isNotEmpty()) {
453449
if (currentConfig!!.isNotEmpty() && !api_url.startsWith("/")) {
454-
if ((viewModel.msgs.isEmpty() || viewModel.msgs.last().role == "system") && "新对话" in viewModel.currentSession) {
450+
if (((viewModel.msgs.isEmpty() || viewModel.msgs.last().role == "system")) && viewModel.currentSession.startsWith("新对话") && viewModel.currentSession.endsWith("\u200B")) {
455451
val withoutEnter = it.replace("\n", "")
456452
var newName = if (withoutEnter.length > 20) {
457453
withoutEnter.substring(0, 20)

app/src/main/java/com/xjyzs/aiapi/utils/MainUtil.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,12 @@ fun createNewSession(viewModel: ChatViewModel,context: Context,isInNewSessionChe
324324
} else {
325325
viewModel.msgs.clear()
326326
viewModel.addSystemMessage(systemPrompt)
327-
viewModel.currentSession = "新对话" + System.currentTimeMillis().toString()
328-
viewModel.sessions.add(viewModel.currentSession)
327+
if (viewModel.sessions.last().startsWith("新对话") && viewModel.sessions.last().endsWith("\u200B")){
328+
viewModel.currentSession = viewModel.sessions.last()
329+
}else {
330+
viewModel.currentSession = "新对话" + System.currentTimeMillis().toString() + "\u200B"
331+
viewModel.sessions.add(viewModel.currentSession)
332+
}
329333
}
330334
}
331335

0 commit comments

Comments
 (0)