This commit is contained in:
parent
bd5bd63f42
commit
dbb430b088
|
|
@ -73,7 +73,9 @@ class ChatViewModel(application: Application) : AndroidViewModel(application) {
|
||||||
if (!messagesJson.isNullOrBlank()) {
|
if (!messagesJson.isNullOrBlank()) {
|
||||||
val messagesArray = JSONArray(messagesJson)
|
val messagesArray = JSONArray(messagesJson)
|
||||||
val restoredMessages = mutableListOf<ChatMessage>()
|
val restoredMessages = mutableListOf<ChatMessage>()
|
||||||
for (i in 0 until messagesArray.length()) {
|
val maxMessages = 200 // 👈 限制最多加载200条消息,避免大数据解析卡顿
|
||||||
|
val startIndex = (messagesArray.length() - maxMessages).coerceAtLeast(0)
|
||||||
|
for (i in startIndex until messagesArray.length()) {
|
||||||
val obj = messagesArray.getJSONObject(i)
|
val obj = messagesArray.getJSONObject(i)
|
||||||
restoredMessages.add(
|
restoredMessages.add(
|
||||||
ChatMessage(
|
ChatMessage(
|
||||||
|
|
@ -91,7 +93,6 @@ class ChatViewModel(application: Application) : AndroidViewModel(application) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果本地什么都没有,自动开启第一个默认会话
|
|
||||||
if (_sessions.value.isEmpty()) {
|
if (_sessions.value.isEmpty()) {
|
||||||
createNewSession()
|
createNewSession()
|
||||||
}
|
}
|
||||||
|
|
@ -201,7 +202,7 @@ class ChatViewModel(application: Application) : AndroidViewModel(application) {
|
||||||
// 👈 限制最多取最近10轮对话(20条消息),避免token超限
|
// 👈 限制最多取最近10轮对话(20条消息),避免token超限
|
||||||
val currentSessionMsgs = _messages.value
|
val currentSessionMsgs = _messages.value
|
||||||
.filter { it.sessionId == sessionId && it.id != userMsg.id && it.id != aiMsgId && !it.isStreaming }
|
.filter { it.sessionId == sessionId && it.id != userMsg.id && it.id != aiMsgId && !it.isStreaming }
|
||||||
.takeLast(20)
|
.takeLast(6)
|
||||||
|
|
||||||
// 👈 使用 JSONArray 构建历史记录,确保特殊字符被正确转义
|
// 👈 使用 JSONArray 构建历史记录,确保特殊字符被正确转义
|
||||||
val historyArray = JSONArray()
|
val historyArray = JSONArray()
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ import androidx.compose.animation.core.tween
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.gestures.detectDragGestures
|
import androidx.compose.foundation.gestures.detectDragGestures
|
||||||
import androidx.compose.foundation.layout.*
|
import androidx.compose.foundation.layout.*
|
||||||
import androidx.compose.foundation.shape.CircleShape
|
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
import androidx.compose.material3.*
|
import androidx.compose.material3.*
|
||||||
import androidx.compose.runtime.*
|
import androidx.compose.runtime.*
|
||||||
|
|
@ -27,18 +26,25 @@ import kotlin.math.roundToInt
|
||||||
fun FloatingChatWidget() {
|
fun FloatingChatWidget() {
|
||||||
var isExpanded by remember { mutableStateOf(false) }
|
var isExpanded by remember { mutableStateOf(false) }
|
||||||
val configuration = LocalConfiguration.current
|
val configuration = LocalConfiguration.current
|
||||||
|
val density = LocalDensity.current
|
||||||
|
|
||||||
// 1. 宽度缩小到屏幕的 45% (高品质窄窗设计)
|
val screenWidthPx = with(density) { configuration.screenWidthDp.dp.toPx() }
|
||||||
val maxWidth = (configuration.screenWidthDp * 0.45f).dp
|
val screenHeightPx = with(density) { configuration.screenHeightDp.dp.toPx() }
|
||||||
|
val maxWidth = (configuration.screenWidthDp * 0.45f).dp
|
||||||
val maxHeight = (configuration.screenHeightDp * 0.75f).dp
|
val maxHeight = (configuration.screenHeightDp * 0.75f).dp
|
||||||
|
val cardWidthPx = with(density) { maxWidth.toPx() }
|
||||||
|
val cardHeightPx = with(density) { maxHeight.toPx() }
|
||||||
|
val fabSizePx = with(density) { 56.dp.toPx() }
|
||||||
|
val paddingPx = with(density) { 16.dp.toPx() }
|
||||||
|
val navBarPx = with(density) { 32.dp.toPx() }
|
||||||
|
|
||||||
// 2. 悬浮圆圈的拖动坐标状态
|
// 👈 FAB偏移量,全屏幕可拖动
|
||||||
var fabOffsetX by remember { mutableStateOf(0f) }
|
var fabOffsetX by remember { mutableStateOf(0f) }
|
||||||
var fabOffsetY by remember { mutableStateOf(0f) }
|
var fabOffsetY by remember { mutableStateOf(0f) }
|
||||||
|
|
||||||
// 3. 聊天卡片相对于初始右下角位置的额外拖动位移状态
|
// 👈 对话框额外偏移(拖动对话框产生)
|
||||||
var cardOffsetX by remember { mutableStateOf(0f) }
|
var cardDragX by remember { mutableStateOf(0f) }
|
||||||
var cardOffsetY by remember { mutableStateOf(0f) }
|
var cardDragY by remember { mutableStateOf(0f) }
|
||||||
|
|
||||||
if (isExpanded) {
|
if (isExpanded) {
|
||||||
BackHandler {
|
BackHandler {
|
||||||
|
|
@ -46,31 +52,23 @@ fun FloatingChatWidget() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 👈 彻底终结“背部网页点不到”与“拖出空气墙被吃掉”的终极解决方案!
|
Box(modifier = Modifier.fillMaxSize()) {
|
||||||
// 根本原因分析:
|
// FAB - 全屏幕可拖动
|
||||||
// 之前使用 Popup(clippingEnabled = false) 时,虽然关闭了裁剪,但 Popup 的 Window 底层窗口尺寸仍被定死在右下角(340X500)。
|
|
||||||
// 在 Android/Undertow 容器中,一旦把子 View(卡片)使用 offset 偏移出 Popup 原本的 340X500 物理窗口区域:
|
|
||||||
// - 卡片移出部分由于脱离了 Popup 原生的 Window 视口,会被系统窗口裁剪(产生被空气墙强行吃掉的效果)。
|
|
||||||
// - 只要 Popup 处于显示状态,Undertow 底层会为它分配一个全屏幕捕获手势的不可见透明幕墙,封死了底下所有网页的点击。
|
|
||||||
//
|
|
||||||
// 终极破局:我们彻底抛弃 Android 系统内置的 Popup 视窗!
|
|
||||||
// 我们的 FloatingChatWidget 本身就是 MainActivity 动态 addView 进去的一个全屏 Compose 容器。
|
|
||||||
// 我们直接在 MainActivity 顶层的全屏 Box 画布中利用 Compose 的声明式叠加,像漂浮图层一样直接渲染卡片和 FAB。
|
|
||||||
// - 这样,卡片移动没有任何 Popup 物理窗口的边缘裁剪(彻底告别空气墙吃掉画面)!
|
|
||||||
// - 同时,我们在全屏的 Box 容器上应用 `Modifier.pointerInput` 拦截时,只在卡片和 FAB 本身尺寸上捕捉手势,
|
|
||||||
// 其他 100% 的空白区域通过 Compose 的无阻透传机制,根本不会有任何拦截,背后的 Web 网页可以 100% 自由交互、滚动和点击!
|
|
||||||
Box(
|
|
||||||
modifier = Modifier.fillMaxSize() // Canvas 覆盖全屏,不需要任何 Popup,彻底零空气墙!
|
|
||||||
) {
|
|
||||||
// 右下角悬浮按钮(支持手指任意位置拖拽)
|
|
||||||
if (!isExpanded) {
|
if (!isExpanded) {
|
||||||
FloatingActionButton(
|
FloatingActionButton(
|
||||||
onClick = { isExpanded = true },
|
onClick = { isExpanded = true },
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.align(Alignment.BottomEnd)
|
.align(Alignment.BottomEnd)
|
||||||
.padding(16.dp)
|
.padding(16.dp)
|
||||||
.padding(bottom = 32.dp) // 留出导航栏空间
|
.padding(bottom = 32.dp)
|
||||||
.offset { IntOffset(fabOffsetX.roundToInt(), fabOffsetY.roundToInt()) } // 应用拖拽偏移量
|
.offset {
|
||||||
|
IntOffset(
|
||||||
|
fabOffsetX.roundToInt()
|
||||||
|
.coerceIn(-(screenWidthPx - fabSizePx - paddingPx * 2).toInt(), 0),
|
||||||
|
fabOffsetY.roundToInt()
|
||||||
|
.coerceIn(-(screenHeightPx - fabSizePx - paddingPx * 2 - navBarPx).toInt(), 0)
|
||||||
|
)
|
||||||
|
}
|
||||||
.pointerInput(Unit) {
|
.pointerInput(Unit) {
|
||||||
detectDragGestures(
|
detectDragGestures(
|
||||||
onDragStart = { },
|
onDragStart = { },
|
||||||
|
|
@ -89,20 +87,25 @@ fun FloatingChatWidget() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 弹出的对话卡片(以纯浮动图层方式渲染,完全不用 Popup,完美响应键盘且 100% 不遮挡空白区域点击)
|
// 对话框 - 展开后必须在界面内
|
||||||
if (isExpanded) {
|
if (isExpanded) {
|
||||||
var visible by remember { mutableStateOf(false) }
|
var visible by remember { mutableStateOf(false) }
|
||||||
LaunchedEffect(Unit) {
|
LaunchedEffect(Unit) {
|
||||||
visible = true // 弹出时触发灵动入场动画
|
visible = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// 动态计算卡片初始弹窗位置(在屏幕右下角)
|
// 👈 用 FAB 位置计算对话框位置,但强制钳制在屏幕内
|
||||||
val initialX = with(LocalDensity.current) {
|
// FAB右下角为基准:对话框应该出现在 FAB 附近
|
||||||
(configuration.screenWidthDp.dp - maxWidth - 16.dp).toPx()
|
val fabRight = screenWidthPx - paddingPx * 2 + fabOffsetX // FAB右边缘
|
||||||
}
|
val fabBottom = screenHeightPx - paddingPx * 2 - navBarPx + fabOffsetY // FAB下边缘
|
||||||
val initialY = with(LocalDensity.current) {
|
|
||||||
(configuration.screenHeightDp.dp - maxHeight - 48.dp).toPx()
|
// 对话框优先放在 FAB 左上方,如果放不下就贴边
|
||||||
}
|
val rawX = fabRight - cardWidthPx - paddingPx // FAB左边
|
||||||
|
val rawY = fabBottom - cardHeightPx - paddingPx // FAB上边
|
||||||
|
|
||||||
|
// 👈 关键:钳制到屏幕内
|
||||||
|
val clampedX = rawX.coerceIn(0f, screenWidthPx - cardWidthPx)
|
||||||
|
val clampedY = rawY.coerceIn(0f, screenHeightPx - cardHeightPx)
|
||||||
|
|
||||||
AnimatedVisibility(
|
AnimatedVisibility(
|
||||||
visible = visible,
|
visible = visible,
|
||||||
|
|
@ -110,10 +113,11 @@ fun FloatingChatWidget() {
|
||||||
exit = fadeOut(animationSpec = tween(200)) + scaleOut(targetScale = 0.8f, animationSpec = tween(200)),
|
exit = fadeOut(animationSpec = tween(200)) + scaleOut(targetScale = 0.8f, animationSpec = tween(200)),
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.offset {
|
.offset {
|
||||||
// 初始右下角位置 + 用户手动拖拽位移
|
|
||||||
IntOffset(
|
IntOffset(
|
||||||
(initialX + cardOffsetX).roundToInt(),
|
(clampedX + cardDragX).roundToInt()
|
||||||
(initialY + cardOffsetY).roundToInt()
|
.coerceIn(0, (screenWidthPx - cardWidthPx).toInt()),
|
||||||
|
(clampedY + cardDragY).roundToInt()
|
||||||
|
.coerceIn(0, (screenHeightPx - cardHeightPx).toInt())
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
.width(maxWidth)
|
.width(maxWidth)
|
||||||
|
|
@ -124,15 +128,20 @@ fun FloatingChatWidget() {
|
||||||
) {
|
) {
|
||||||
ChatScaffold(
|
ChatScaffold(
|
||||||
onClose = {
|
onClose = {
|
||||||
visible = false // 触发退场动画
|
visible = false
|
||||||
isExpanded = false
|
isExpanded = false
|
||||||
|
// 👈 关闭时把FAB偏移同步(对话框拖动带动FAB)
|
||||||
|
fabOffsetX += cardDragX
|
||||||
|
fabOffsetY += cardDragY
|
||||||
|
cardDragX = 0f
|
||||||
|
cardDragY = 0f
|
||||||
},
|
},
|
||||||
onDrag = { dx, dy -> // 响应来自 ChatArea 标题栏的手势拖拽
|
onDrag = { dx, dy ->
|
||||||
cardOffsetX += dx
|
cardDragX += dx
|
||||||
cardOffsetY += dy
|
cardDragY += dy
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -93,10 +93,19 @@ fun ChatArea(
|
||||||
contentColor = if (isUser) Color.White else Color.Black
|
contentColor = if (isUser) Color.White else Color.Black
|
||||||
) {
|
) {
|
||||||
if (isUser) {
|
if (isUser) {
|
||||||
Text(
|
androidx.compose.ui.viewinterop.AndroidView(
|
||||||
text = msg.content,
|
factory = { ctx ->
|
||||||
modifier = Modifier.padding(12.dp),
|
SafeSelectableTextView(ctx).apply {
|
||||||
style = MaterialTheme.typography.bodyMedium
|
setTextColor(android.graphics.Color.WHITE)
|
||||||
|
textSize = 14f
|
||||||
|
setTextIsSelectable(true)
|
||||||
|
setPadding(0, 0, 0, 0)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
update = { view ->
|
||||||
|
view.text = msg.content
|
||||||
|
},
|
||||||
|
modifier = Modifier.padding(12.dp)
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
val contentWithCursor = msg.content + if (msg.isStreaming) " █" else ""
|
val contentWithCursor = msg.content + if (msg.isStreaming) " █" else ""
|
||||||
|
|
|
||||||
|
|
@ -58,19 +58,35 @@ fun ChatScaffold(
|
||||||
|
|
||||||
ModalNavigationDrawer(
|
ModalNavigationDrawer(
|
||||||
drawerState = drawerState,
|
drawerState = drawerState,
|
||||||
|
gesturesEnabled = false,
|
||||||
drawerContent = {
|
drawerContent = {
|
||||||
ModalDrawerSheet(modifier = Modifier.fillMaxWidth(0.7f)) {
|
ModalDrawerSheet(modifier = Modifier.width(240.dp)) {
|
||||||
Column(Modifier.fillMaxSize().padding(16.dp)) {
|
Column(Modifier.fillMaxSize().padding(12.dp)) {
|
||||||
|
// 👈 顶部关闭按钮
|
||||||
|
Row(
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
horizontalArrangement = Arrangement.SpaceBetween,
|
||||||
|
verticalAlignment = Alignment.CenterVertically
|
||||||
|
) {
|
||||||
|
Text("历史对话", style = MaterialTheme.typography.titleSmall)
|
||||||
|
TextButton(onClick = { scope.launch { drawerState.close() } }) {
|
||||||
|
Text("关闭")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Spacer(Modifier.height(8.dp))
|
||||||
|
|
||||||
Button(
|
Button(
|
||||||
onClick = {
|
onClick = {
|
||||||
viewModel.createNewSession()
|
viewModel.createNewSession()
|
||||||
scope.launch { drawerState.close() }
|
scope.launch { drawerState.close() }
|
||||||
},
|
},
|
||||||
modifier = Modifier.fillMaxWidth()
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
contentPadding = PaddingValues(vertical = 8.dp)
|
||||||
) {
|
) {
|
||||||
Icon(Icons.Default.Add, contentDescription = null)
|
Icon(Icons.Default.Add, contentDescription = null, modifier = Modifier.size(18.dp))
|
||||||
Spacer(Modifier.width(8.dp))
|
Spacer(Modifier.width(4.dp))
|
||||||
Text("新建话题")
|
Text("新建话题", style = MaterialTheme.typography.bodySmall)
|
||||||
}
|
}
|
||||||
|
|
||||||
Spacer(Modifier.height(16.dp))
|
Spacer(Modifier.height(16.dp))
|
||||||
|
|
|
||||||
|
|
@ -68,19 +68,19 @@ class AiChatRepository {
|
||||||
val curLine = line ?: continue
|
val curLine = line ?: continue
|
||||||
if (curLine.startsWith("data:")) {
|
if (curLine.startsWith("data:")) {
|
||||||
val content = curLine.substring(5)
|
val content = curLine.substring(5)
|
||||||
Timber.d("内容111111111:"+content)
|
// Timber.d("内容111111111:"+content)
|
||||||
if (content.contains("|") && lastcontent.contains("|")){
|
if (content.contains("|") && lastcontent.contains("|")){
|
||||||
trySend("\n")
|
trySend("\n")
|
||||||
Timber.d("1发送11111111111:换行")
|
// Timber.d("1发送11111111111:换行")
|
||||||
trySend(content)
|
trySend(content)
|
||||||
Timber.d("2发送11111111111:"+content)
|
// Timber.d("2发送11111111111:"+content)
|
||||||
}else if (content.length==0) {
|
}else if (content.length==0) {
|
||||||
// 空data行表示换行
|
// 空data行表示换行
|
||||||
trySend("\n")
|
trySend("\n")
|
||||||
Timber.d("3发送11111111111:换行")
|
// Timber.d("3发送11111111111:换行")
|
||||||
}else {
|
}else {
|
||||||
trySend(content)
|
trySend(content)
|
||||||
Timber.d("4发送11111111111:"+content)
|
// Timber.d("4发送11111111111:"+content)
|
||||||
}
|
}
|
||||||
lastcontent = content;
|
lastcontent = content;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue