fix: 补齐设置保存/登录的中间层在途守卫,并去重同文案提示

- 真机双击验证发现:设置页「保存」两次点击都会真的写一遍(DataStore 写太快,UI 标记在第二次点击前就已复位),会看到两条一样的提示
- AppContainer 增加 authInFlight / baseUrlSaving 在途标记,重复调用直接忽略
- snackbar 增加同文案 1.5 秒去重,连点不再堆两条相同提示
This commit is contained in:
2026-11-08 22:44:28 +08:00
parent 27d4bcc774
commit d71bfe2139
2 changed files with 46 additions and 18 deletions
+40 -18
View File
@@ -46,6 +46,14 @@ class AppContainer(private val app: Application) {
private val _baseUrl = MutableStateFlow(ApiClient.DEFAULT_BASE_URL)
val baseUrl: StateFlow<String> = _baseUrl.asStateFlow()
/** 登录在途标记:防重复提交 */
@Volatile
private var authInFlight = false
/** API 地址保存在途标记 */
@Volatile
private var baseUrlSaving = false
/** 登录页要展示的一次性提示(比如 token 失效),登录成功后清除 */
private val _authNotice = MutableStateFlow<String?>(null)
val authNotice: StateFlow<String?> = _authNotice.asStateFlow()
@@ -71,23 +79,30 @@ class AppContainer(private val app: Application) {
}
fun login(username: String, password: String, baseUrl: String, onResult: (String?) -> Unit) {
// 在途时直接忽略重复提交:登录是重操作,不能因为手抖发两次
if (authInFlight) return
authInFlight = true
scope.launch {
if (!apiClient.setBaseUrl(baseUrl)) {
onResult("API 地址不合法,需要形如 https://host/api")
return@launch
}
repository.login(username, password)
.onSuccess { token ->
apiClient.token = token
tokenStore.saveLogin(token, username, baseUrl)
_baseUrl.value = baseUrl
_username.value = username
_authNotice.value = null
_loggedIn.value = true
session.reconcile()
onResult(null)
try {
if (!apiClient.setBaseUrl(baseUrl)) {
onResult("API 地址不合法,需要形如 https://host/api")
return@launch
}
.onFailure { onResult(it.message ?: "登录失败") }
repository.login(username, password)
.onSuccess { token ->
apiClient.token = token
tokenStore.saveLogin(token, username, baseUrl)
_baseUrl.value = baseUrl
_username.value = username
_authNotice.value = null
_loggedIn.value = true
session.reconcile()
onResult(null)
}
.onFailure { onResult(it.message ?: "登录失败") }
} finally {
authInFlight = false
}
}
}
@@ -121,8 +136,10 @@ class AppContainer(private val app: Application) {
/**
* 保存 API 地址:合法才写入,不合法把错误文案回给界面(由界面弹提示)。
* 在途时忽略重复调用。
*/
fun updateBaseUrl(url: String, onResult: (String?) -> Unit) {
if (baseUrlSaving) return
val value = url.trim()
if (value.isBlank()) {
onResult("API 地址不能为空")
@@ -132,10 +149,15 @@ class AppContainer(private val app: Application) {
onResult("API 地址不合法,需要形如 https://host/api")
return
}
baseUrlSaving = true
scope.launch {
tokenStore.saveBaseUrl(value)
_baseUrl.value = value
onResult(null)
try {
tokenStore.saveBaseUrl(value)
_baseUrl.value = value
onResult(null)
} finally {
baseUrlSaving = false
}
}
}
}
@@ -103,7 +103,13 @@ private fun AppRoot(openEndRequest: androidx.compose.runtime.MutableState<Boolea
// 保存/开始/结束这类动作必须给出即时反馈,不能只靠页面底部的静态文案
val snackbarHostState = remember { SnackbarHostState() }
// 同一句话在 1.5 秒内重复出现就丢掉:连点不应该看到两条一模一样的提示
val lastToast = remember { java.util.concurrent.atomic.AtomicReference<Pair<String, Long>?>(null) }
fun toast(message: String, long: Boolean = false) {
val now = System.currentTimeMillis()
val previous = lastToast.get()
if (previous != null && previous.first == message && now - previous.second < 1_500) return
lastToast.set(message to now)
scope.launch {
snackbarHostState.showSnackbar(
message = message,