重复密钥添加时进行拦截提示
This commit is contained in:
@@ -293,6 +293,43 @@ fun ManualEntryForm(
|
||||
}
|
||||
}
|
||||
|
||||
if (state.isDuplicate) {
|
||||
Card(
|
||||
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.errorContainer),
|
||||
modifier = Modifier.fillMaxWidth().padding(bottom = 16.dp)
|
||||
) {
|
||||
Column(modifier = Modifier.padding(16.dp)) {
|
||||
Text(
|
||||
text = stringResource(R.string.duplicate_token_title),
|
||||
style = MaterialTheme.typography.titleSmall,
|
||||
color = MaterialTheme.colorScheme.onErrorContainer
|
||||
)
|
||||
Spacer(modifier = Modifier.height(4.dp))
|
||||
Text(
|
||||
text = stringResource(R.string.duplicate_token_message, state.duplicateServiceName),
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onErrorContainer
|
||||
)
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.End
|
||||
) {
|
||||
TextButton(onClick = { viewModel.dismissDuplicate() }) {
|
||||
Text(stringResource(R.string.skip))
|
||||
}
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Button(
|
||||
onClick = { viewModel.forceAddToken() },
|
||||
colors = ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.error)
|
||||
) {
|
||||
Text(stringResource(R.string.force_add))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth().padding(bottom = 32.dp),
|
||||
horizontalArrangement = Arrangement.End
|
||||
@@ -301,8 +338,10 @@ fun ManualEntryForm(
|
||||
Text(stringResource(R.string.cancel))
|
||||
}
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Button(onClick = viewModel::addTokenManually) {
|
||||
Text(stringResource(R.string.add))
|
||||
if (!state.isDuplicate) {
|
||||
Button(onClick = viewModel::addTokenManually) {
|
||||
Text(stringResource(R.string.add))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import cn.airnan.a2fair.data.repository.TokenRepository
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@@ -35,7 +36,9 @@ data class AddTokenUiState(
|
||||
val counter: Long = 0,
|
||||
val isError: Boolean = false,
|
||||
val errorMessage: String? = null,
|
||||
val isSuccess: Boolean = false
|
||||
val isSuccess: Boolean = false,
|
||||
val isDuplicate: Boolean = false,
|
||||
val duplicateServiceName: String = ""
|
||||
)
|
||||
|
||||
data class TokensUiState(
|
||||
@@ -117,6 +120,7 @@ class TokensViewModel(
|
||||
|
||||
private val _addTokenState = MutableStateFlow(AddTokenUiState())
|
||||
val addTokenState: StateFlow<AddTokenUiState> = _addTokenState.asStateFlow()
|
||||
private var pendingToken: Token? = null
|
||||
|
||||
fun updateIssuer(issuer: String) {
|
||||
_addTokenState.update { it.copy(issuer = issuer) }
|
||||
@@ -127,8 +131,8 @@ class TokensViewModel(
|
||||
}
|
||||
|
||||
fun updateSecret(secret: String) {
|
||||
// Strip spaces
|
||||
_addTokenState.update { it.copy(secret = secret.replace(" ", "")) }
|
||||
// Strip spaces and uppercase (Base32 is case-insensitive)
|
||||
_addTokenState.update { it.copy(secret = secret.replace(" ", "").uppercase()) }
|
||||
}
|
||||
|
||||
fun updateTokenType(type: TokenType) {
|
||||
@@ -158,7 +162,8 @@ class TokensViewModel(
|
||||
fun addTokenFromUri(uriString: String) {
|
||||
try {
|
||||
val token = OtpUriParser.parse(uriString)
|
||||
saveToken(token)
|
||||
// Normalize secret to uppercase (Base32 is case-insensitive)
|
||||
saveToken(token.copy(secret = SecretString(token.secret.value.uppercase())))
|
||||
} catch (e: Exception) {
|
||||
_addTokenState.update { it.copy(isError = true, errorMessage = getApplication<Application>().getString(R.string.failed_parse_qr, e.message ?: "")) }
|
||||
}
|
||||
@@ -188,17 +193,58 @@ class TokensViewModel(
|
||||
}
|
||||
}
|
||||
|
||||
private fun String.normalizeSecret(): String {
|
||||
return this.uppercase().replace(Regex("[^A-Z2-7]"), "")
|
||||
}
|
||||
|
||||
private fun saveToken(token: Token) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
// Check for duplicate
|
||||
val existing = tokenRepository.getAllTokens().first()
|
||||
val duplicate = existing.find {
|
||||
it.secret.value.normalizeSecret() == token.secret.value.normalizeSecret()
|
||||
}
|
||||
if (duplicate != null) {
|
||||
pendingToken = token
|
||||
_addTokenState.update {
|
||||
it.copy(
|
||||
isDuplicate = true,
|
||||
duplicateServiceName = duplicate.serviceName,
|
||||
isError = false,
|
||||
errorMessage = null
|
||||
)
|
||||
}
|
||||
return@launch
|
||||
}
|
||||
|
||||
tokenRepository.insertToken(token)
|
||||
_addTokenState.update { it.copy(isSuccess = true, isError = false, errorMessage = null) }
|
||||
pendingToken = null
|
||||
_addTokenState.update { it.copy(isSuccess = true, isError = false, errorMessage = null, isDuplicate = false) }
|
||||
} catch (e: Exception) {
|
||||
_addTokenState.update { it.copy(isError = true, errorMessage = getApplication<Application>().getString(R.string.failed_save_token, e.message ?: "")) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun dismissDuplicate() {
|
||||
pendingToken = null
|
||||
_addTokenState.update { it.copy(isDuplicate = false, duplicateServiceName = "") }
|
||||
}
|
||||
|
||||
fun forceAddToken() {
|
||||
val token = pendingToken ?: return
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
tokenRepository.insertToken(token)
|
||||
pendingToken = null
|
||||
_addTokenState.update { it.copy(isSuccess = true, isDuplicate = false, isError = false, errorMessage = null) }
|
||||
} catch (e: Exception) {
|
||||
_addTokenState.update { it.copy(isError = true, errorMessage = getApplication<Application>().getString(R.string.failed_save_token, e.message ?: ""), isDuplicate = false) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun clearError() {
|
||||
_addTokenState.update { it.copy(isError = false, errorMessage = null) }
|
||||
}
|
||||
|
||||
@@ -41,6 +41,10 @@
|
||||
<string name="failed_parse_qr">解析二维码失败:%s</string>
|
||||
<string name="secret_and_account_required">密钥和账户名称为必填项</string>
|
||||
<string name="failed_save_token">保存令牌失败:%s</string>
|
||||
<string name="duplicate_token_title">口令重复</string>
|
||||
<string name="duplicate_token_message">口令 \"%s\" 已存在,是否仍要添加?</string>
|
||||
<string name="skip">跳过</string>
|
||||
<string name="force_add">强制添加</string>
|
||||
|
||||
<!-- Settings Main -->
|
||||
<string name="security">安全</string>
|
||||
|
||||
@@ -41,6 +41,10 @@
|
||||
<string name="failed_parse_qr">解析 QR 碼失敗:%s</string>
|
||||
<string name="secret_and_account_required">金鑰和帳戶名稱為必填項</string>
|
||||
<string name="failed_save_token">儲存權杖失敗:%s</string>
|
||||
<string name="duplicate_token_title">權杖重複</string>
|
||||
<string name="duplicate_token_message">權杖 \"%s\" 已存在,是否仍要新增?</string>
|
||||
<string name="skip">跳過</string>
|
||||
<string name="force_add">強制新增</string>
|
||||
|
||||
<!-- Settings Main -->
|
||||
<string name="security">安全性</string>
|
||||
|
||||
@@ -41,6 +41,10 @@
|
||||
<string name="failed_parse_qr">Failed to parse QR code: %s</string>
|
||||
<string name="secret_and_account_required">Secret and Account Name are required</string>
|
||||
<string name="failed_save_token">Failed to save token: %s</string>
|
||||
<string name="duplicate_token_title">Duplicate Token</string>
|
||||
<string name="duplicate_token_message">Token \"%s\" already exists. Do you want to add it anyway?</string>
|
||||
<string name="skip">Skip</string>
|
||||
<string name="force_add">Force Add</string>
|
||||
|
||||
<!-- Settings Main -->
|
||||
<string name="security">Security</string>
|
||||
|
||||
Reference in New Issue
Block a user