feat: persist login state and remove mock entry flow
This commit is contained in:
1 parent
6ec824f078
commit
eeff16a4ca
7 files changed
+76
-90
No files matched your search
@@ -62,6 +62,7 @@ kotlin {
|
||||
implementation(libs.ktor.client.contentNegotiation)
|
||||
implementation(libs.ktor.serialization.kotlinx.json)
|
||||
implementation(libs.kotlinx.serialization.json)
|
||||
implementation(libs.multiplatform.settings.no.arg)
|
||||
}
|
||||
commonTest.dependencies {
|
||||
implementation(libs.kotlin.test)
|
||||
|
||||
@@ -18,7 +18,6 @@ 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.DataSourceMode
|
||||
import cn.xiaobai.openclawapp.app.MainTab
|
||||
import cn.xiaobai.openclawapp.app.OpenClawAppState
|
||||
import cn.xiaobai.openclawapp.data.ServerConfig
|
||||
@@ -49,14 +48,12 @@ fun App() {
|
||||
token = token,
|
||||
password = password,
|
||||
defaultAgent = agent,
|
||||
dataSource = appState.dataSource,
|
||||
isConnecting = appState.isConnecting,
|
||||
errorMessage = appState.configError,
|
||||
onBaseUrlChange = { baseUrl = it },
|
||||
onTokenChange = { token = it },
|
||||
onPasswordChange = { password = it },
|
||||
onDefaultAgentChange = { agent = it },
|
||||
onDataSourceChange = appState::updateDataSource,
|
||||
onConnect = {
|
||||
coroutineScope.launch {
|
||||
appState.clearConfigError()
|
||||
@@ -90,8 +87,8 @@ private fun OpenClawRoot(appState: OpenClawAppState) {
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(appState.isConfigured, appState.dataSource) {
|
||||
if (appState.isConfigured && appState.dataSource == DataSourceMode.Remote) {
|
||||
LaunchedEffect(appState.isConfigured) {
|
||||
if (appState.isConfigured) {
|
||||
appState.syncRemoteIfNeeded()
|
||||
}
|
||||
}
|
||||
@@ -204,10 +201,8 @@ private fun OpenClawRoot(appState: OpenClawAppState) {
|
||||
currentModel = appState.repository.status.currentModel,
|
||||
availableAgents = appState.repository.availableAgents,
|
||||
currentTheme = appState.themeMode,
|
||||
currentDataSource = appState.dataSource,
|
||||
onSelectAgent = appState::updateDefaultAgent,
|
||||
onSelectTheme = appState::updateTheme,
|
||||
onSelectDataSource = appState::updateDataSource,
|
||||
onDisconnect = appState::disconnect,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -3,34 +3,36 @@ package cn.xiaobai.openclawapp.app
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import cn.xiaobai.openclawapp.data.AppPreferences
|
||||
import cn.xiaobai.openclawapp.data.OpenClawRepository
|
||||
import cn.xiaobai.openclawapp.data.RemoteOpenClawRepository
|
||||
import cn.xiaobai.openclawapp.data.RepositoryFactory
|
||||
import cn.xiaobai.openclawapp.data.ServerConfig
|
||||
import cn.xiaobai.openclawapp.network.MobileGatewayApi
|
||||
|
||||
class OpenClawAppState {
|
||||
var isConfigured by mutableStateOf(false)
|
||||
class OpenClawAppState(
|
||||
private val preferences: AppPreferences = AppPreferences(),
|
||||
) {
|
||||
var selectedTab by mutableStateOf(MainTab.Sessions)
|
||||
var activeSessionId by mutableStateOf<String?>(null)
|
||||
var serverConfig by mutableStateOf(ServerConfig())
|
||||
var serverConfig by mutableStateOf(preferences.loadServerConfig())
|
||||
var feedbackMessage by mutableStateOf<String?>(null)
|
||||
var themeMode by mutableStateOf(AppThemeMode.Dark)
|
||||
var dataSource by mutableStateOf(DataSourceMode.Mock)
|
||||
var themeMode by mutableStateOf(preferences.loadThemeMode())
|
||||
var isSyncing by mutableStateOf(false)
|
||||
var isSending by mutableStateOf(false)
|
||||
var isConnecting by mutableStateOf(false)
|
||||
var syncError by mutableStateOf<String?>(null)
|
||||
var configError by mutableStateOf<String?>(null)
|
||||
var repository: OpenClawRepository by mutableStateOf(RepositoryFactory.createMock())
|
||||
var repository: OpenClawRepository by mutableStateOf(RepositoryFactory.createRemote(serverConfig))
|
||||
var isConfigured by mutableStateOf(serverConfig.baseUrl.isNotBlank() && serverConfig.token.isNotBlank())
|
||||
|
||||
suspend fun connect(config: ServerConfig, password: String) {
|
||||
isConnecting = true
|
||||
configError = null
|
||||
|
||||
runCatching {
|
||||
val resolvedConfig = if (dataSource == DataSourceMode.Remote && config.token.isBlank()) {
|
||||
require(password.isNotBlank()) { "Remote 模式下请输入登录密码或直接填写 token" }
|
||||
val resolvedConfig = if (config.token.isBlank()) {
|
||||
require(password.isNotBlank()) { "请输入 BFF 登录密码或直接填写 token" }
|
||||
val loginApi = MobileGatewayApi(baseUrl = config.baseUrl, tokenProvider = { "" })
|
||||
val login = loginApi.login(password)
|
||||
config.copy(token = login.token)
|
||||
@@ -39,16 +41,11 @@ class OpenClawAppState {
|
||||
}
|
||||
|
||||
serverConfig = resolvedConfig
|
||||
repository = when (dataSource) {
|
||||
DataSourceMode.Mock -> RepositoryFactory.createMock()
|
||||
DataSourceMode.Remote -> RepositoryFactory.createRemote(resolvedConfig)
|
||||
}
|
||||
preferences.saveServerConfig(resolvedConfig)
|
||||
repository = RepositoryFactory.createRemote(resolvedConfig)
|
||||
isConfigured = true
|
||||
syncError = null
|
||||
feedbackMessage = when (dataSource) {
|
||||
DataSourceMode.Mock -> "已进入 Mock 模式"
|
||||
DataSourceMode.Remote -> "已连接到 ${resolvedConfig.baseUrl}"
|
||||
}
|
||||
feedbackMessage = "已连接到 ${resolvedConfig.baseUrl}"
|
||||
}.onFailure {
|
||||
configError = it.message ?: "连接失败"
|
||||
feedbackMessage = "连接失败"
|
||||
@@ -63,6 +60,9 @@ class OpenClawAppState {
|
||||
selectedTab = MainTab.Sessions
|
||||
syncError = null
|
||||
configError = null
|
||||
serverConfig = serverConfig.copy(token = "")
|
||||
preferences.clearLoginState()
|
||||
repository = RepositoryFactory.createRemote(serverConfig)
|
||||
feedbackMessage = "连接已断开"
|
||||
}
|
||||
|
||||
@@ -100,12 +100,7 @@ class OpenClawAppState {
|
||||
}
|
||||
|
||||
suspend fun sendMessage(sessionId: String, text: String) {
|
||||
val remote = repository as? RemoteOpenClawRepository
|
||||
if (remote == null) {
|
||||
repository.sendMessage(sessionId, text)
|
||||
return
|
||||
}
|
||||
|
||||
val remote = repository as? RemoteOpenClawRepository ?: return
|
||||
isSending = true
|
||||
runCatching {
|
||||
remote.sendMessageRemote(sessionId, text)
|
||||
@@ -118,12 +113,14 @@ class OpenClawAppState {
|
||||
|
||||
fun updateDefaultAgent(agent: String) {
|
||||
serverConfig = serverConfig.copy(defaultAgent = agent)
|
||||
preferences.saveServerConfig(serverConfig)
|
||||
repository.status = repository.status.copy(defaultAgent = agent)
|
||||
feedbackMessage = "默认 Agent 已切换到 $agent"
|
||||
}
|
||||
|
||||
fun updateTheme(theme: AppThemeMode) {
|
||||
themeMode = theme
|
||||
preferences.saveThemeMode(theme)
|
||||
feedbackMessage = when (theme) {
|
||||
AppThemeMode.Dark -> "已切到深色主题"
|
||||
AppThemeMode.Light -> "已切到浅色主题"
|
||||
@@ -135,14 +132,6 @@ class OpenClawAppState {
|
||||
feedbackMessage = if (pinned) "已置顶会话" else "已取消置顶"
|
||||
}
|
||||
|
||||
fun updateDataSource(mode: DataSourceMode) {
|
||||
dataSource = mode
|
||||
feedbackMessage = when (mode) {
|
||||
DataSourceMode.Mock -> "数据源已切到 Mock"
|
||||
DataSourceMode.Remote -> "数据源已切到 Remote"
|
||||
}
|
||||
}
|
||||
|
||||
fun clearFeedback() {
|
||||
feedbackMessage = null
|
||||
}
|
||||
@@ -166,8 +155,3 @@ enum class AppThemeMode(val label: String) {
|
||||
Dark("深色"),
|
||||
Light("浅色"),
|
||||
}
|
||||
|
||||
enum class DataSourceMode(val label: String) {
|
||||
Mock("Mock"),
|
||||
Remote("Remote"),
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package cn.xiaobai.openclawapp.data
|
||||
|
||||
import cn.xiaobai.openclawapp.app.AppThemeMode
|
||||
import com.russhwolf.settings.Settings
|
||||
|
||||
class AppPreferences(
|
||||
private val settings: Settings = Settings(),
|
||||
) {
|
||||
fun loadServerConfig(): ServerConfig {
|
||||
return ServerConfig(
|
||||
baseUrl = settings.getStringOrNull(KEY_BASE_URL) ?: "http://192.168.31.116:8788",
|
||||
token = settings.getStringOrNull(KEY_TOKEN) ?: "",
|
||||
defaultAgent = settings.getStringOrNull(KEY_DEFAULT_AGENT) ?: "main",
|
||||
)
|
||||
}
|
||||
|
||||
fun saveServerConfig(config: ServerConfig) {
|
||||
settings.putString(KEY_BASE_URL, config.baseUrl)
|
||||
settings.putString(KEY_TOKEN, config.token)
|
||||
settings.putString(KEY_DEFAULT_AGENT, config.defaultAgent)
|
||||
}
|
||||
|
||||
fun clearLoginState() {
|
||||
settings.remove(KEY_TOKEN)
|
||||
}
|
||||
|
||||
fun loadThemeMode(): AppThemeMode {
|
||||
return when (settings.getStringOrNull(KEY_THEME_MODE)) {
|
||||
AppThemeMode.Light.name -> AppThemeMode.Light
|
||||
else -> AppThemeMode.Dark
|
||||
}
|
||||
}
|
||||
|
||||
fun saveThemeMode(themeMode: AppThemeMode) {
|
||||
settings.putString(KEY_THEME_MODE, themeMode.name)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val KEY_BASE_URL = "server.base_url"
|
||||
private const val KEY_TOKEN = "server.token"
|
||||
private const val KEY_DEFAULT_AGENT = "server.default_agent"
|
||||
private const val KEY_THEME_MODE = "ui.theme_mode"
|
||||
}
|
||||
}
|
||||
+5
-29
@@ -1,6 +1,5 @@
|
||||
package cn.xiaobai.openclawapp.feature.config
|
||||
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
@@ -17,9 +16,7 @@ import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import cn.xiaobai.openclawapp.app.DataSourceMode
|
||||
import cn.xiaobai.openclawapp.ui.HeroCard
|
||||
import cn.xiaobai.openclawapp.ui.SelectableRowCard
|
||||
|
||||
@Composable
|
||||
fun ServerConfigScreen(
|
||||
@@ -27,14 +24,12 @@ fun ServerConfigScreen(
|
||||
token: String,
|
||||
password: String,
|
||||
defaultAgent: String,
|
||||
dataSource: DataSourceMode,
|
||||
isConnecting: Boolean,
|
||||
errorMessage: String?,
|
||||
onBaseUrlChange: (String) -> Unit,
|
||||
onTokenChange: (String) -> Unit,
|
||||
onPasswordChange: (String) -> Unit,
|
||||
onDefaultAgentChange: (String) -> Unit,
|
||||
onDataSourceChange: (DataSourceMode) -> Unit,
|
||||
onConnect: () -> Unit,
|
||||
) {
|
||||
Column(
|
||||
@@ -46,29 +41,10 @@ fun ServerConfigScreen(
|
||||
) {
|
||||
HeroCard(
|
||||
title = "连接 OpenClaw",
|
||||
subtitle = "第一版先支持 Mock 和 Remote 两种数据源。Remote 模式下可以直接用 BFF 登录密码换 token。",
|
||||
badge = "Setup",
|
||||
subtitle = "现在默认直接连接真实 BFF。地址和 token 会自动持久化,下次打开 app 可以直接恢复。",
|
||||
badge = "Remote Only",
|
||||
)
|
||||
|
||||
Card(modifier = Modifier.fillMaxWidth()) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(18.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(14.dp),
|
||||
) {
|
||||
Text("数据源", style = MaterialTheme.typography.titleMedium)
|
||||
DataSourceMode.entries.forEach { mode ->
|
||||
SelectableRowCard(
|
||||
title = mode.label,
|
||||
trailing = if (mode == dataSource) "当前" else null,
|
||||
selected = mode == dataSource,
|
||||
modifier = Modifier.clickable { onDataSourceChange(mode) },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Card(modifier = Modifier.fillMaxWidth()) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
@@ -89,7 +65,7 @@ fun ServerConfigScreen(
|
||||
onValueChange = onPasswordChange,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
label = { Text("登录密码") },
|
||||
placeholder = { Text("Remote 模式可直接填 BFF 登录密码") },
|
||||
placeholder = { Text("优先推荐填 BFF 登录密码") },
|
||||
singleLine = true,
|
||||
)
|
||||
OutlinedTextField(
|
||||
@@ -97,7 +73,7 @@ fun ServerConfigScreen(
|
||||
onValueChange = onTokenChange,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
label = { Text("访问令牌(可选)") },
|
||||
placeholder = { Text("如果已知 token,也可以直接填写") },
|
||||
placeholder = { Text("如果已经登录过,也可以直接填 token") },
|
||||
singleLine = true,
|
||||
)
|
||||
OutlinedTextField(
|
||||
@@ -129,7 +105,7 @@ fun ServerConfigScreen(
|
||||
if (isConnecting) {
|
||||
CircularProgressIndicator()
|
||||
} else {
|
||||
Text(if (dataSource == DataSourceMode.Mock) "进入应用(Mock)" else "登录并进入")
|
||||
Text("登录并进入")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-19
@@ -13,7 +13,6 @@ import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import cn.xiaobai.openclawapp.app.AppThemeMode
|
||||
import cn.xiaobai.openclawapp.app.DataSourceMode
|
||||
import cn.xiaobai.openclawapp.data.ServerConfig
|
||||
import cn.xiaobai.openclawapp.ui.HeroCard
|
||||
import cn.xiaobai.openclawapp.ui.LabelValueCard
|
||||
@@ -26,10 +25,8 @@ fun SettingsScreen(
|
||||
currentModel: String,
|
||||
availableAgents: List<String>,
|
||||
currentTheme: AppThemeMode,
|
||||
currentDataSource: DataSourceMode,
|
||||
onSelectAgent: (String) -> Unit,
|
||||
onSelectTheme: (AppThemeMode) -> Unit,
|
||||
onSelectDataSource: (DataSourceMode) -> Unit,
|
||||
onDisconnect: () -> Unit,
|
||||
) {
|
||||
LazyColumn(
|
||||
@@ -39,7 +36,7 @@ fun SettingsScreen(
|
||||
item {
|
||||
HeroCard(
|
||||
title = "连接配置",
|
||||
subtitle = "这里先承载服务器地址、token 状态、默认 Agent、主题和数据源。等网络层接通后,再继续拆细。",
|
||||
subtitle = "现在 app 默认只走真实 BFF。这里主要管理地址、token 状态、默认 Agent 和主题。",
|
||||
badge = "Settings",
|
||||
)
|
||||
}
|
||||
@@ -51,20 +48,7 @@ fun SettingsScreen(
|
||||
LabelValueCard(label = "当前模型", value = currentModel)
|
||||
}
|
||||
item {
|
||||
LabelValueCard(label = "访问令牌", value = if (serverConfig.token.isBlank()) "未设置" else "已配置")
|
||||
}
|
||||
|
||||
item {
|
||||
Text("数据源", style = MaterialTheme.typography.titleMedium)
|
||||
}
|
||||
|
||||
items(DataSourceMode.entries) { mode ->
|
||||
SelectableRowCard(
|
||||
title = mode.label,
|
||||
trailing = if (mode == currentDataSource) "当前" else null,
|
||||
selected = mode == currentDataSource,
|
||||
modifier = Modifier.clickable { onSelectDataSource(mode) },
|
||||
)
|
||||
LabelValueCard(label = "访问令牌", value = if (serverConfig.token.isBlank()) "未设置" else "已持久化")
|
||||
}
|
||||
|
||||
item {
|
||||
@@ -95,7 +79,7 @@ fun SettingsScreen(
|
||||
|
||||
item {
|
||||
Button(onClick = onDisconnect, modifier = Modifier.fillMaxWidth()) {
|
||||
Text("断开连接")
|
||||
Text("退出登录")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user