fix: 补齐设置保存/登录的中间层在途守卫,并去重同文案提示
- 真机双击验证发现:设置页「保存」两次点击都会真的写一遍(DataStore 写太快,UI 标记在第二次点击前就已复位),会看到两条一样的提示 - AppContainer 增加 authInFlight / baseUrlSaving 在途标记,重复调用直接忽略 - snackbar 增加同文案 1.5 秒去重,连点不再堆两条相同提示
This commit is contained in:
@@ -46,6 +46,14 @@ class AppContainer(private val app: Application) {
|
|||||||
private val _baseUrl = MutableStateFlow(ApiClient.DEFAULT_BASE_URL)
|
private val _baseUrl = MutableStateFlow(ApiClient.DEFAULT_BASE_URL)
|
||||||
val baseUrl: StateFlow<String> = _baseUrl.asStateFlow()
|
val baseUrl: StateFlow<String> = _baseUrl.asStateFlow()
|
||||||
|
|
||||||
|
/** 登录在途标记:防重复提交 */
|
||||||
|
@Volatile
|
||||||
|
private var authInFlight = false
|
||||||
|
|
||||||
|
/** API 地址保存在途标记 */
|
||||||
|
@Volatile
|
||||||
|
private var baseUrlSaving = false
|
||||||
|
|
||||||
/** 登录页要展示的一次性提示(比如 token 失效),登录成功后清除 */
|
/** 登录页要展示的一次性提示(比如 token 失效),登录成功后清除 */
|
||||||
private val _authNotice = MutableStateFlow<String?>(null)
|
private val _authNotice = MutableStateFlow<String?>(null)
|
||||||
val authNotice: StateFlow<String?> = _authNotice.asStateFlow()
|
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) {
|
fun login(username: String, password: String, baseUrl: String, onResult: (String?) -> Unit) {
|
||||||
|
// 在途时直接忽略重复提交:登录是重操作,不能因为手抖发两次
|
||||||
|
if (authInFlight) return
|
||||||
|
authInFlight = true
|
||||||
scope.launch {
|
scope.launch {
|
||||||
if (!apiClient.setBaseUrl(baseUrl)) {
|
try {
|
||||||
onResult("API 地址不合法,需要形如 https://host/api")
|
if (!apiClient.setBaseUrl(baseUrl)) {
|
||||||
return@launch
|
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)
|
|
||||||
}
|
}
|
||||||
.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 地址:合法才写入,不合法把错误文案回给界面(由界面弹提示)。
|
* 保存 API 地址:合法才写入,不合法把错误文案回给界面(由界面弹提示)。
|
||||||
|
* 在途时忽略重复调用。
|
||||||
*/
|
*/
|
||||||
fun updateBaseUrl(url: String, onResult: (String?) -> Unit) {
|
fun updateBaseUrl(url: String, onResult: (String?) -> Unit) {
|
||||||
|
if (baseUrlSaving) return
|
||||||
val value = url.trim()
|
val value = url.trim()
|
||||||
if (value.isBlank()) {
|
if (value.isBlank()) {
|
||||||
onResult("API 地址不能为空")
|
onResult("API 地址不能为空")
|
||||||
@@ -132,10 +149,15 @@ class AppContainer(private val app: Application) {
|
|||||||
onResult("API 地址不合法,需要形如 https://host/api")
|
onResult("API 地址不合法,需要形如 https://host/api")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
baseUrlSaving = true
|
||||||
scope.launch {
|
scope.launch {
|
||||||
tokenStore.saveBaseUrl(value)
|
try {
|
||||||
_baseUrl.value = value
|
tokenStore.saveBaseUrl(value)
|
||||||
onResult(null)
|
_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() }
|
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) {
|
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 {
|
scope.launch {
|
||||||
snackbarHostState.showSnackbar(
|
snackbarHostState.showSnackbar(
|
||||||
message = message,
|
message = message,
|
||||||
|
|||||||
Reference in New Issue
Block a user