From efba07a41b6d9e8ecd325595223de7aef2fce3d9 Mon Sep 17 00:00:00 2001 From: k1ngsterr1 Date: Sat, 24 Jan 2026 23:12:01 +0500 Subject: [PATCH] feat: Add password dialog for enabling biometric authentication in ProfileScreen --- .../messenger/ui/settings/ProfileScreen.kt | 117 ++++++++++++++++-- 1 file changed, 105 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/rosetta/messenger/ui/settings/ProfileScreen.kt b/app/src/main/java/com/rosetta/messenger/ui/settings/ProfileScreen.kt index a28865b..5355622 100644 --- a/app/src/main/java/com/rosetta/messenger/ui/settings/ProfileScreen.kt +++ b/app/src/main/java/com/rosetta/messenger/ui/settings/ProfileScreen.kt @@ -161,6 +161,9 @@ fun ProfileScreen( // Biometric state var biometricAvailable by remember { mutableStateOf(BiometricAvailability.NotAvailable("Checking...")) } var isBiometricEnabled by remember { mutableStateOf(false) } + var showPasswordDialog by remember { mutableStateOf(false) } + var passwordInput by remember { mutableStateOf("") } + var passwordError by remember { mutableStateOf(null) } // Check biometric availability LaunchedEffect(Unit) { @@ -430,9 +433,9 @@ fun ProfileScreen( TelegramBiometricItem( isEnabled = isBiometricEnabled, onToggle = { - scope.launch { - if (isBiometricEnabled) { - // Disable biometric + if (isBiometricEnabled) { + // Disable biometric + scope.launch { biometricPrefs.disableBiometric() biometricPrefs.removeEncryptedPassword(accountPublicKey) isBiometricEnabled = false @@ -441,16 +444,12 @@ fun ProfileScreen( "Biometric authentication disabled", android.widget.Toast.LENGTH_SHORT ).show() - } else { - // Enable biometric - biometricPrefs.enableBiometric() - isBiometricEnabled = true - android.widget.Toast.makeText( - context, - "Biometric authentication enabled. Your password will be securely saved on next unlock.", - android.widget.Toast.LENGTH_LONG - ).show() } + } else { + // Enable biometric - show password dialog first + passwordInput = "" + passwordError = null + showPasswordDialog = true } }, isDarkTheme = isDarkTheme @@ -504,6 +503,100 @@ fun ProfileScreen( avatarRepository = avatarRepository ) } + + // Password dialog for biometric setup + if (showPasswordDialog && activity != null) { + AlertDialog( + onDismissRequest = { + showPasswordDialog = false + passwordInput = "" + passwordError = null + }, + title = { Text("Enable Biometric Authentication") }, + text = { + Column { + Text("Enter your password to securely save it for biometric unlock:") + Spacer(modifier = Modifier.height(16.dp)) + OutlinedTextField( + value = passwordInput, + onValueChange = { + passwordInput = it + passwordError = null + }, + label = { Text("Password") }, + singleLine = true, + visualTransformation = androidx.compose.ui.text.input.PasswordVisualTransformation(), + isError = passwordError != null, + modifier = Modifier.fillMaxWidth() + ) + if (passwordError != null) { + Spacer(modifier = Modifier.height(4.dp)) + Text( + text = passwordError!!, + color = Color.Red, + fontSize = 12.sp + ) + } + } + }, + confirmButton = { + TextButton( + onClick = { + if (passwordInput.isEmpty()) { + passwordError = "Password cannot be empty" + return@TextButton + } + + // Try to encrypt the password with biometric + biometricManager.encryptPassword( + activity = activity, + password = passwordInput, + onSuccess = { encryptedPassword -> + scope.launch { + // Save encrypted password + biometricPrefs.saveEncryptedPassword(accountPublicKey, encryptedPassword) + // Enable biometric + biometricPrefs.enableBiometric() + isBiometricEnabled = true + + showPasswordDialog = false + passwordInput = "" + passwordError = null + + android.widget.Toast.makeText( + context, + "Biometric authentication enabled successfully", + android.widget.Toast.LENGTH_SHORT + ).show() + } + }, + onError = { error -> + passwordError = error + }, + onCancel = { + showPasswordDialog = false + passwordInput = "" + passwordError = null + } + ) + } + ) { + Text("Enable") + } + }, + dismissButton = { + TextButton( + onClick = { + showPasswordDialog = false + passwordInput = "" + passwordError = null + } + ) { + Text("Cancel") + } + } + ) + } } // ═════════════════════════════════════════════════════════════