Introduce explicit app routes

This commit is contained in:
OpenClaw committed 2026-03-07 15:38:55 +08:00
1 parent 6543f83b3a
commit dc7083507d
4 files changed
+207 -197

No files matched your search

@@ -3,10 +3,13 @@ package cn.xiaobai.openclawapp
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
@@ -16,7 +19,7 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import cn.xiaobai.openclawapp.app.MainTab
import cn.xiaobai.openclawapp.app.AppRoute
import cn.xiaobai.openclawapp.app.OpenClawAppState
import cn.xiaobai.openclawapp.data.ServerConfig
import cn.xiaobai.openclawapp.feature.chat.ChatScreen
@@ -24,6 +27,7 @@ import cn.xiaobai.openclawapp.feature.config.ServerConfigScreen
import cn.xiaobai.openclawapp.feature.sessions.SessionListScreen
import cn.xiaobai.openclawapp.feature.settings.SettingsScreen
import cn.xiaobai.openclawapp.feature.status.StatusScreen
import cn.xiaobai.openclawapp.platform.PlatformBackHandler
import cn.xiaobai.openclawapp.platform.rememberAttachmentOpener
import cn.xiaobai.openclawapp.platform.rememberAttachmentPicker
import cn.xiaobai.openclawapp.platform.rememberAttachmentSaver
@@ -77,9 +81,10 @@ fun App() {
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun OpenClawRoot(appState: OpenClawAppState) {
val activeSession = appState.activeSessionId?.let(appState.repository::findSession)
val activeSession = (appState.currentRoute as? AppRoute.Chat)?.sessionId?.let(appState.repository::findSession)
val coroutineScope = rememberCoroutineScope()
val pickAttachment = rememberAttachmentPicker(
onPicked = { picked ->
@@ -107,6 +112,10 @@ private fun OpenClawRoot(appState: OpenClawAppState) {
appState.feedbackMessage = message
}
PlatformBackHandler(enabled = appState.currentRoute is AppRoute.Chat) {
appState.navigateBack()
}
LaunchedEffect(appState.feedbackMessage) {
if (appState.feedbackMessage != null) {
delay(2200)
@@ -129,24 +138,67 @@ private fun OpenClawRoot(appState: OpenClawAppState) {
}
Scaffold(
topBar = {
when (val route = appState.currentRoute) {
is AppRoute.Chat -> {
CenterAlignedTopAppBar(
title = {
Text(activeSession?.title ?: "会话")
},
navigationIcon = {
TextButton(onClick = appState::closeChat) {
Text("返回")
}
},
actions = {
TextButton(onClick = { pickAttachment() }) {
Text("附件")
}
TextButton(onClick = {
activeSession?.id?.let { sessionId ->
coroutineScope.launch {
appState.clearSyncError()
appState.openSession(sessionId)
}
}
}) {
Text(if (appState.repository.streamStatusFor(route.sessionId) == "failed") "重连" else "刷新")
}
},
)
}
AppRoute.Sessions -> {
CenterAlignedTopAppBar(title = { Text("会话") })
}
AppRoute.Status -> {
CenterAlignedTopAppBar(title = { Text("状态") })
}
AppRoute.Settings -> {
CenterAlignedTopAppBar(title = { Text("设置") })
}
}
},
bottomBar = {
if (activeSession == null) {
if (appState.currentRoute !is AppRoute.Chat) {
NavigationBar {
NavigationBarItem(
selected = appState.selectedTab == MainTab.Sessions,
onClick = { appState.selectedTab = MainTab.Sessions },
selected = appState.currentRoute == AppRoute.Sessions,
onClick = { appState.navigateTo(AppRoute.Sessions) },
icon = { Text("💬") },
label = { Text("会话") },
)
NavigationBarItem(
selected = appState.selectedTab == MainTab.Status,
onClick = { appState.selectedTab = MainTab.Status },
selected = appState.currentRoute == AppRoute.Status,
onClick = { appState.navigateTo(AppRoute.Status) },
icon = { Text("📡") },
label = { Text("状态") },
)
NavigationBarItem(
selected = appState.selectedTab == MainTab.Settings,
onClick = { appState.selectedTab = MainTab.Settings },
selected = appState.currentRoute == AppRoute.Settings,
onClick = { appState.navigateTo(AppRoute.Settings) },
icon = { Text("⚙️") },
label = { Text("设置") },
)
@@ -167,88 +219,91 @@ private fun OpenClawRoot(appState: OpenClawAppState) {
)
}
when {
activeSession != null -> ChatScreen(
modifier = Modifier.fillMaxSize(),
sessionTitle = activeSession.title,
messages = appState.repository.messagesFor(activeSession.id),
isSending = appState.sendingSessionId == activeSession.id,
isLoading = appState.isSyncing,
errorMessage = appState.syncError,
streamStatus = appState.repository.streamStatusFor(activeSession.id),
pendingQueueStatus = appState.repository.pendingQueueStatus(activeSession.id).let { queue ->
when {
queue.recoveryInFlight && queue.sessionCount > 0 -> "当前会话还有 ${queue.sessionCount} 条待发项,系统正在恢复并继续处理本地队列。"
queue.recoveryRecentlySkipped && queue.sessionCount > 0 -> "当前会话的待发队列刚刚已处理过一轮,本次先跳过重复恢复。"
queue.isDraining && queue.sessionCount > 0 -> "当前会话还有 ${queue.sessionCount} 条待发项,系统正在按本地队列继续处理。"
queue.sessionCount > 0 -> "当前会话还有 ${queue.sessionCount} 条待发项未完成,可继续重试或等待下一次自动处理。"
else -> null
}
},
cacheNotice = if (appState.isShowingCachedData) {
"当前展示的是本地缓存${appState.cacheSavedAt?.let { "(保存于 $it)" } ?: ""},正在等待最新数据。"
} else null,
onBack = appState::closeChat,
onRetry = {
coroutineScope.launch {
appState.clearSyncError()
appState.openSession(activeSession.id)
}
},
onSend = { text ->
coroutineScope.launch {
appState.sendMessage(activeSession.id, text)
}
},
onSendAttachmentStub = {
pickAttachment()
},
onRetryAttachment = { messageId ->
coroutineScope.launch {
appState.retryAttachment(activeSession.id, messageId)
}
},
onRetryMessage = { messageId ->
coroutineScope.launch {
appState.retryMessage(activeSession.id, messageId)
}
},
onCopyAttachmentName = { fileName ->
copyToClipboard(fileName)
},
onOpenAttachment = { messageId ->
coroutineScope.launch {
runCatching {
appState.repository.ensureAttachmentOpenPayload(activeSession.id, messageId)
}.onSuccess { payload ->
if (payload != null) {
openAttachment(payload)
} else {
appState.feedbackMessage = "当前附件暂无可打开内容"
}
}.onFailure {
appState.feedbackMessage = it.message ?: "打开附件失败"
when (val route = appState.currentRoute) {
is AppRoute.Chat -> {
val session = activeSession ?: return@Column
ChatScreen(
modifier = Modifier.fillMaxSize(),
sessionTitle = session.title,
messages = appState.repository.messagesFor(session.id),
isSending = appState.sendingSessionId == session.id,
isLoading = appState.isSyncing,
errorMessage = appState.syncError,
streamStatus = appState.repository.streamStatusFor(session.id),
pendingQueueStatus = appState.repository.pendingQueueStatus(session.id).let { queue ->
when {
queue.recoveryInFlight && queue.sessionCount > 0 -> "当前会话还有 ${queue.sessionCount} 条待发项,系统正在恢复并继续处理本地队列。"
queue.recoveryRecentlySkipped && queue.sessionCount > 0 -> "当前会话的待发队列刚刚已处理过一轮,本次先跳过重复恢复。"
queue.isDraining && queue.sessionCount > 0 -> "当前会话还有 ${queue.sessionCount} 条待发项,系统正在按本地队列继续处理。"
queue.sessionCount > 0 -> "当前会话还有 ${queue.sessionCount} 条待发项未完成,可继续重试或等待下一次自动处理。"
else -> null
}
}
},
onDownloadAttachment = { messageId ->
coroutineScope.launch {
runCatching {
appState.repository.ensureAttachmentOpenPayload(activeSession.id, messageId)
}.onSuccess { payload ->
if (payload != null) {
saveAttachment(payload)
} else {
appState.feedbackMessage = "当前附件暂无可下载内容"
}
}.onFailure {
appState.feedbackMessage = it.message ?: "下载附件失败"
},
cacheNotice = if (appState.isShowingCachedData) {
"当前展示的是本地缓存${appState.cacheSavedAt?.let { "(保存于 $it)" } ?: ""},正在等待最新数据。"
} else null,
onBack = appState::closeChat,
onRetry = {
coroutineScope.launch {
appState.clearSyncError()
appState.openSession(session.id)
}
}
},
)
},
onSend = { text ->
coroutineScope.launch {
appState.sendMessage(session.id, text)
}
},
onSendAttachmentStub = {
pickAttachment()
},
onRetryAttachment = { messageId ->
coroutineScope.launch {
appState.retryAttachment(session.id, messageId)
}
},
onRetryMessage = { messageId ->
coroutineScope.launch {
appState.retryMessage(session.id, messageId)
}
},
onCopyAttachmentName = { fileName ->
copyToClipboard(fileName)
},
onOpenAttachment = { messageId ->
coroutineScope.launch {
runCatching {
appState.repository.ensureAttachmentOpenPayload(session.id, messageId)
}.onSuccess { payload ->
if (payload != null) {
openAttachment(payload)
} else {
appState.feedbackMessage = "当前附件暂无可打开内容"
}
}.onFailure {
appState.feedbackMessage = it.message ?: "打开附件失败"
}
}
},
onDownloadAttachment = { messageId ->
coroutineScope.launch {
runCatching {
appState.repository.ensureAttachmentOpenPayload(session.id, messageId)
}.onSuccess { payload ->
if (payload != null) {
saveAttachment(payload)
} else {
appState.feedbackMessage = "当前附件暂无可下载内容"
}
}.onFailure {
appState.feedbackMessage = it.message ?: "下载附件失败"
}
}
},
)
}
appState.selectedTab == MainTab.Sessions -> SessionListScreen(
AppRoute.Sessions -> SessionListScreen(
modifier = Modifier.fillMaxSize(),
sessions = appState.repository.sessions,
recentSession = appState.repository.recentSession(),
@@ -269,12 +324,12 @@ private fun OpenClawRoot(appState: OpenClawAppState) {
},
)
appState.selectedTab == MainTab.Status -> StatusScreen(
AppRoute.Status -> StatusScreen(
modifier = Modifier.fillMaxSize(),
status = appState.repository.status,
)
appState.selectedTab == MainTab.Settings -> SettingsScreen(
AppRoute.Settings -> SettingsScreen(
modifier = Modifier.fillMaxSize(),
serverConfig = appState.serverConfig,
currentModel = appState.repository.status.currentModel,
@@ -13,8 +13,9 @@ import cn.xiaobai.openclawapp.network.MobileGatewayApi
class OpenClawAppState(
private val preferences: AppPreferences = AppPreferences(),
) {
var selectedTab by mutableStateOf(MainTab.Sessions)
var activeSessionId by mutableStateOf<String?>(null)
var currentRoute by mutableStateOf<AppRoute>(AppRoute.Sessions)
val activeSessionId: String?
get() = (currentRoute as? AppRoute.Chat)?.sessionId
var serverConfig by mutableStateOf(preferences.loadServerConfig())
var feedbackMessage by mutableStateOf<String?>(null)
var themeMode by mutableStateOf(preferences.loadThemeMode())
@@ -78,8 +79,7 @@ class OpenClawAppState(
fun disconnect() {
isConfigured = false
activeSessionId = null
selectedTab = MainTab.Sessions
currentRoute = AppRoute.Sessions
syncError = null
configError = null
serverConfig = serverConfig.copy(token = "")
@@ -88,6 +88,19 @@ class OpenClawAppState(
feedbackMessage = "连接已断开"
}
fun navigateTo(route: AppRoute) {
currentRoute = route
}
fun navigateBack(): Boolean {
return if (currentRoute is AppRoute.Chat) {
currentRoute = AppRoute.Sessions
true
} else {
false
}
}
suspend fun syncRemoteIfNeeded() {
val remote = repository as? RemoteOpenClawRepository ?: return
isSyncing = true
@@ -130,7 +143,7 @@ class OpenClawAppState(
suspend fun openSession(sessionId: String) {
repository.markSessionOpened(sessionId)
activeSessionId = sessionId
currentRoute = AppRoute.Chat(sessionId)
val remote = repository as? RemoteOpenClawRepository ?: return
isSyncing = true
runCatching {
@@ -153,7 +166,7 @@ class OpenClawAppState(
}
fun closeChat() {
activeSessionId = null
navigateBack()
}
suspend fun sendMessage(sessionId: String, text: String) {
@@ -289,10 +302,11 @@ class OpenClawAppState(
}
}
enum class MainTab {
Sessions,
Status,
Settings,
sealed interface AppRoute {
data object Sessions : AppRoute
data object Status : AppRoute
data object Settings : AppRoute
data class Chat(val sessionId: String) : AppRoute
}
enum class AppThemeMode(val label: String) {
@@ -38,7 +38,6 @@ import cn.xiaobai.openclawapp.data.LocalMessagePhase
import cn.xiaobai.openclawapp.data.MessageKind
import cn.xiaobai.openclawapp.data.MessageRole
import cn.xiaobai.openclawapp.data.localMessagePhase
import cn.xiaobai.openclawapp.platform.PlatformBackHandler
import cn.xiaobai.openclawapp.ui.EmptyStateCard
import cn.xiaobai.openclawapp.ui.StatusPill
import cn.xiaobai.openclawapp.ui.StatusPillTone
@@ -55,18 +54,14 @@ fun ChatScreen(
streamStatus: String?,
pendingQueueStatus: String?,
cacheNotice: String?,
onBack: () -> Unit,
onRetry: () -> Unit,
onSend: (String) -> Unit,
onSendAttachmentStub: () -> Unit,
onRetryAttachment: (String) -> Unit,
onRetryMessage: (String) -> Unit,
onCopyAttachmentName: (String) -> Unit,
onOpenAttachment: (String) -> Unit,
onDownloadAttachment: (String) -> Unit,
) {
PlatformBackHandler(onBack = onBack)
var input by remember { mutableStateOf("") }
val listState = rememberLazyListState()
@@ -91,13 +86,7 @@ fun ChatScreen(
modifier = modifier.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(12.dp),
) {
ChatHeader(
sessionTitle = sessionTitle,
topStatus = topStatus,
onBack = onBack,
onRetry = onRetry,
onSendAttachmentStub = onSendAttachmentStub,
)
ChatStatusBanner(topStatus = topStatus)
if (messages.isEmpty() && !isLoading) {
EmptyStateCard(
@@ -170,7 +159,7 @@ fun ChatScreen(
onValueChange = { input = it },
modifier = Modifier.weight(1f),
label = { Text("输入消息") },
placeholder = { Text("给 OpenClaw 发一句话") },
placeholder = { Text("给 $sessionTitle 发一句话") },
maxLines = 4,
enabled = !isSending,
)
@@ -194,7 +183,6 @@ fun ChatScreen(
private data class ChatTopStatus(
val text: String,
val tone: StatusPillTone,
val actionLabel: String,
)
private fun buildChatTopStatus(
@@ -209,149 +197,100 @@ private fun buildChatTopStatus(
errorMessage != null -> ChatTopStatus(
text = errorMessage,
tone = StatusPillTone.Error,
actionLabel = "重试",
)
pendingQueueStatus != null -> ChatTopStatus(
text = pendingQueueStatus,
tone = StatusPillTone.Warning,
actionLabel = if (streamStatus == "failed") "重连" else "刷新",
)
cacheNotice != null -> ChatTopStatus(
text = cacheNotice,
tone = StatusPillTone.Warning,
actionLabel = if (streamStatus == "failed") "重连" else "刷新",
)
isSending -> ChatTopStatus(
text = "正在等待实时回复…",
tone = StatusPillTone.Info,
actionLabel = "刷新",
)
isLoading -> ChatTopStatus(
text = "正在同步消息…",
tone = StatusPillTone.Info,
actionLabel = "刷新",
)
streamStatus == "recovering" -> ChatTopStatus(
text = "上游恢复中,消息会继续同步。",
tone = StatusPillTone.Warning,
actionLabel = "刷新",
)
streamStatus == "reconnecting" -> ChatTopStatus(
text = "正在重连实时流。",
tone = StatusPillTone.Warning,
actionLabel = "重连",
)
streamStatus == "failed" -> ChatTopStatus(
text = "实时流已断开,请重试连接。",
tone = StatusPillTone.Error,
actionLabel = "重连",
)
streamStatus == "connecting" -> ChatTopStatus(
text = "正在连接实时流。",
tone = StatusPillTone.Info,
actionLabel = "刷新",
)
else -> ChatTopStatus(
text = "在线,可继续浏览和发送消息。",
tone = StatusPillTone.Success,
actionLabel = "刷新",
)
}
}
@Composable
private fun ChatHeader(
sessionTitle: String,
private fun ChatStatusBanner(
topStatus: ChatTopStatus,
onBack: () -> Unit,
onRetry: () -> Unit,
onSendAttachmentStub: () -> Unit,
) {
Card(
modifier = Modifier.fillMaxWidth(),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.surfaceContainerLow,
containerColor = when (topStatus.tone) {
StatusPillTone.Error -> MaterialTheme.colorScheme.errorContainer
StatusPillTone.Warning -> MaterialTheme.colorScheme.tertiaryContainer.copy(alpha = 0.55f)
StatusPillTone.Success -> MaterialTheme.colorScheme.secondaryContainer.copy(alpha = 0.55f)
StatusPillTone.Info -> MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.5f)
StatusPillTone.Neutral -> MaterialTheme.colorScheme.surfaceContainerHigh
},
),
) {
Column(
Row(
modifier = Modifier
.fillMaxWidth()
.padding(12.dp),
verticalArrangement = Arrangement.spacedBy(10.dp),
.padding(horizontal = 12.dp, vertical = 10.dp),
horizontalArrangement = Arrangement.spacedBy(10.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(12.dp),
verticalAlignment = Alignment.CenterVertically,
) {
TextButton(onClick = onBack) { Text("返回") }
Text(
text = sessionTitle,
modifier = Modifier.weight(1f),
style = MaterialTheme.typography.titleMedium,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
TextButton(onClick = onSendAttachmentStub) {
Text("附件")
}
TextButton(onClick = onRetry) {
Text(topStatus.actionLabel)
}
}
Card(
modifier = Modifier.fillMaxWidth(),
colors = CardDefaults.cardColors(
containerColor = when (topStatus.tone) {
StatusPillTone.Error -> MaterialTheme.colorScheme.errorContainer
StatusPillTone.Warning -> MaterialTheme.colorScheme.tertiaryContainer.copy(alpha = 0.55f)
StatusPillTone.Success -> MaterialTheme.colorScheme.secondaryContainer.copy(alpha = 0.55f)
StatusPillTone.Info -> MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.5f)
StatusPillTone.Neutral -> MaterialTheme.colorScheme.surfaceContainerHigh
},
),
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 12.dp, vertical = 10.dp),
horizontalArrangement = Arrangement.spacedBy(10.dp),
verticalAlignment = Alignment.CenterVertically,
) {
StatusPill(
text = when (topStatus.tone) {
StatusPillTone.Error -> "需处理"
StatusPillTone.Warning -> "处理中"
StatusPillTone.Success -> "在线"
StatusPillTone.Info -> "同步中"
StatusPillTone.Neutral -> "提示"
},
tone = topStatus.tone,
)
Text(
text = topStatus.text,
style = MaterialTheme.typography.bodySmall,
color = when (topStatus.tone) {
StatusPillTone.Error -> MaterialTheme.colorScheme.onErrorContainer
StatusPillTone.Warning -> MaterialTheme.colorScheme.onTertiaryContainer
StatusPillTone.Success -> MaterialTheme.colorScheme.onSecondaryContainer
StatusPillTone.Info -> MaterialTheme.colorScheme.onPrimaryContainer
StatusPillTone.Neutral -> MaterialTheme.colorScheme.onSurfaceVariant
},
modifier = Modifier.weight(1f),
)
}
}
StatusPill(
text = when (topStatus.tone) {
StatusPillTone.Error -> "需处理"
StatusPillTone.Warning -> "处理中"
StatusPillTone.Success -> "在线"
StatusPillTone.Info -> "同步中"
StatusPillTone.Neutral -> "提示"
},
tone = topStatus.tone,
)
Text(
text = topStatus.text,
style = MaterialTheme.typography.bodySmall,
color = when (topStatus.tone) {
StatusPillTone.Error -> MaterialTheme.colorScheme.onErrorContainer
StatusPillTone.Warning -> MaterialTheme.colorScheme.onTertiaryContainer
StatusPillTone.Success -> MaterialTheme.colorScheme.onSecondaryContainer
StatusPillTone.Info -> MaterialTheme.colorScheme.onPrimaryContainer
StatusPillTone.Neutral -> MaterialTheme.colorScheme.onSurfaceVariant
},
modifier = Modifier.weight(1f),
)
}
}
}
+2
View File
@@ -439,3 +439,5 @@ Android/iOS App
- 2026-03-07:设置页重做为分组式连接中心:移除 HeroCard 式说明块,改成“连接与账号 / 默认 Agent / 外观 / 诊断信息”四组,减少说明书感,重点回到用户真正能查看和切换的配置项。
- 2026-03-07:顺手把状态页头部也收轻:去掉大 HeroCard,改为直接标题 + 指标卡 + 详情卡,避免多个页面同时维持两套厚重的页头语言。
- 2026-03-07:补了一轮真正的详情页导航语义:聊天详情页重新加回明确的顶部导航头(返回 / 标题 / 附件 / 刷新),不再只是内容区里塞一个“返回”按钮;同时抽了一个轻量 `PlatformBackHandler`,先在 Android 接上系统返回,让详情页能正常退回首页/列表。
- 2026-03-07:进一步把页面切换从“状态变量拼页面”收成显式 route:`OpenClawAppState` 现在用 `AppRoute`(Sessions / Status / Settings / Chat(sessionId))管理当前页面,详情页与首页切换不再依赖 `selectedTab + activeSessionId` 的隐式组合,后续继续加真正导航能力会顺很多。
- 2026-03-07:把详情页顶部导航头正式迁回 `Scaffold.topBar`:聊天页内部不再自己伪造导航栏,而是由 app 壳层根据 route 决定顶栏标题、返回、附件、刷新动作;这样详情页终于有了正经的二级页面导航结构。