diff --git a/app/src/main/java/com/rosetta/messenger/ui/auth/UnlockScreen.kt b/app/src/main/java/com/rosetta/messenger/ui/auth/UnlockScreen.kt index f6ed0dc..4c34f34 100644 --- a/app/src/main/java/com/rosetta/messenger/ui/auth/UnlockScreen.kt +++ b/app/src/main/java/com/rosetta/messenger/ui/auth/UnlockScreen.kt @@ -180,6 +180,8 @@ fun UnlockScreen( val biometricManager = remember { BiometricAuthManager(context) } val biometricPrefs = remember { BiometricPreferences(context) } val scope = rememberCoroutineScope() + + android.util.Log.d("UnlockScreen", "🎬 SCREEN INITIALIZED - Activity: $activity") var password by remember { mutableStateOf("") } var passwordVisible by remember { mutableStateOf(false) } @@ -191,6 +193,7 @@ fun UnlockScreen( var isBiometricEnabled by remember { mutableStateOf(false) } var savedPasswordsMap by remember { mutableStateOf>(emptyMap()) } var dataLoaded by remember { mutableStateOf(false) } + var biometricAttempted by remember { mutableStateOf(false) } // ΠžΡ‚ΡΠ»Π΅ΠΆΠΈΠ²Π°Π΅ΠΌ ΠΏΠΎΠΏΡ‹Ρ‚ΠΊΡƒ Π±ΠΈΠΎΠΌΠ΅Ρ‚Ρ€ΠΈΠΈ // Account selection state var accounts by remember { mutableStateOf>(emptyList()) } @@ -201,6 +204,7 @@ fun UnlockScreen( // Load accounts and pre-select last used account LaunchedEffect(Unit) { + android.util.Log.d("UnlockScreen", "πŸ“¦ LOADING DATA...") // ⚑ БИНΠ₯Π ΠžΠΠΠžΠ• Ρ‡Ρ‚Π΅Π½ΠΈΠ΅ послСднСго Π°ΠΊΠΊΠ°ΡƒΠ½Ρ‚Π° Π”Πž Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ списка val lastLoggedKey = accountManager.getLastLoggedPublicKey() @@ -248,60 +252,86 @@ fun UnlockScreen( android.util.Log.d("UnlockScreen", "πŸ” Has password for selected: ${selectedAccount?.let { passwords.containsKey(it.publicKey) }}") dataLoaded = true + + android.util.Log.d("UnlockScreen", "βœ… DATA LOADED") } - // АвтоматичСски пытаСмся Ρ€Π°Π·Π±Π»ΠΎΠΊΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ Ρ‡Π΅Ρ€Π΅Π· Π±ΠΈΠΎΠΌΠ΅Ρ‚Ρ€ΠΈΡŽ ΠΏΡ€ΠΈ Π²Ρ‹Π±ΠΎΡ€Π΅ Π°ΠΊΠΊΠ°ΡƒΠ½Ρ‚Π° - LaunchedEffect(selectedAccount, isBiometricEnabled, savedPasswordsMap, dataLoaded) { - android.util.Log.d("UnlockScreen", "πŸš€ LaunchedEffect triggered") - android.util.Log.d("UnlockScreen", "πŸš€ dataLoaded: $dataLoaded") - android.util.Log.d("UnlockScreen", "πŸš€ selectedAccount: ${selectedAccount?.publicKey}") - android.util.Log.d("UnlockScreen", "πŸš€ isBiometricEnabled: $isBiometricEnabled") - android.util.Log.d("UnlockScreen", "πŸš€ biometricAvailable: $biometricAvailable") - android.util.Log.d("UnlockScreen", "πŸš€ activity: $activity") + // Ѐункция для запуска биомСтричСской Ρ€Π°Π·Π±Π»ΠΎΠΊΠΈΡ€ΠΎΠ²ΠΊΠΈ + fun tryBiometricUnlock() { + if (selectedAccount == null || activity == null) { + android.util.Log.d("UnlockScreen", "❌ Cannot start biometric: no account or activity") + return + } + + val encryptedPassword = savedPasswordsMap[selectedAccount!!.publicKey] + if (encryptedPassword == null) { + android.util.Log.d("UnlockScreen", "❌ No saved password for account") + return + } + + android.util.Log.d("UnlockScreen", "πŸ” Starting biometric unlock...") + biometricAttempted = true + + biometricManager.decryptPassword( + activity = activity, + encryptedData = encryptedPassword, + onSuccess = { decryptedPassword -> + android.util.Log.d("UnlockScreen", "βœ… Biometric success, unlocking...") + scope.launch { + performUnlock( + selectedAccount = selectedAccount, + password = decryptedPassword, + accountManager = accountManager, + onUnlocking = { isUnlocking = it }, + onError = { error = it }, + onSuccess = { decryptedAccount -> + onUnlocked(decryptedAccount) + } + ) + } + }, + onError = { errorMessage -> + android.util.Log.e("UnlockScreen", "❌ Biometric error: $errorMessage") + error = errorMessage + }, + onCancel = { + android.util.Log.d("UnlockScreen", "🚫 Biometric cancelled") + } + ) + } + + // АвтоматичСски запускаСм Π±ΠΈΠΎΠΌΠ΅Ρ‚Ρ€ΠΈΡŽ ΠΏΡ€ΠΈ Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠ΅ Π΄Π°Π½Π½Ρ‹Ρ… + LaunchedEffect(dataLoaded, selectedAccount, isBiometricEnabled, savedPasswordsMap) { + android.util.Log.d("UnlockScreen", "πŸš€ Auto-biometric LaunchedEffect") + android.util.Log.d("UnlockScreen", " dataLoaded: $dataLoaded") + android.util.Log.d("UnlockScreen", " selectedAccount: ${selectedAccount?.publicKey}") + android.util.Log.d("UnlockScreen", " isBiometricEnabled: $isBiometricEnabled") + android.util.Log.d("UnlockScreen", " biometricAvailable: $biometricAvailable") + android.util.Log.d("UnlockScreen", " activity: $activity") + android.util.Log.d("UnlockScreen", " biometricAttempted: $biometricAttempted") if (!dataLoaded) { - android.util.Log.d("UnlockScreen", "⏸️ Data not loaded yet, waiting...") + android.util.Log.d("UnlockScreen", "⏸️ Data not loaded yet") return@LaunchedEffect } - if (selectedAccount != null && isBiometricEnabled && activity != null && + if (biometricAttempted) { + android.util.Log.d("UnlockScreen", "⏸️ Biometric already attempted") + return@LaunchedEffect + } + + if (selectedAccount != null && + isBiometricEnabled && + activity != null && biometricAvailable is BiometricAvailability.Available) { val encryptedPassword = savedPasswordsMap[selectedAccount!!.publicKey] - android.util.Log.d("UnlockScreen", "πŸ”‘ Encrypted password found: ${encryptedPassword != null}") + android.util.Log.d("UnlockScreen", "πŸ”‘ Has password: ${encryptedPassword != null}") if (encryptedPassword != null) { // НСбольшая Π·Π°Π΄Π΅Ρ€ΠΆΠΊΠ° для Π°Π½ΠΈΠΌΠ°Ρ†ΠΈΠΈ UI - delay(500) - - // ЗапускаСм Π±ΠΈΠΎΠΌΠ΅Ρ‚Ρ€ΠΈΡ‡Π΅ΡΠΊΡƒΡŽ Π°ΡƒΡ‚Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ†ΠΈΡŽ - biometricManager.decryptPassword( - activity = activity, - encryptedData = encryptedPassword, - onSuccess = { decryptedPassword -> - password = decryptedPassword - // АвтоматичСски Ρ€Π°Π·Π±Π»ΠΎΠΊΠΈΡ€ΡƒΠ΅ΠΌ - scope.launch { - performUnlock( - selectedAccount = selectedAccount, - password = decryptedPassword, - accountManager = accountManager, - onUnlocking = { isUnlocking = it }, - onError = { error = it }, - onSuccess = { decryptedAccount -> - onUnlocked(decryptedAccount) - } - ) - } - }, - onError = { errorMessage -> - // Если биомСтрия Π½Π΅ сработала, ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒ ΠΌΠΎΠΆΠ΅Ρ‚ ввСсти ΠΏΠ°Ρ€ΠΎΠ»ΡŒ Π²Ρ€ΡƒΡ‡Π½ΡƒΡŽ - android.util.Log.e("UnlockScreen", "Biometric error: $errorMessage") - }, - onCancel = { - // ΠŸΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒ ΠΎΡ‚ΠΌΠ΅Π½ΠΈΠ» Π±ΠΈΠΎΠΌΠ΅Ρ‚Ρ€ΠΈΡŽ, ΠΏΠΎΠΊΠ°ΠΆΠ΅ΠΌ ΠΏΠΎΠ»Π΅ для Π²Π²ΠΎΠ΄Π° пароля - } - ) + delay(300) + tryBiometricUnlock() } } } @@ -813,35 +843,7 @@ fun UnlockScreen( if (showBiometricButton) { Button( onClick = { - if (selectedAccount == null || activity == null) return@Button - - val encryptedPassword = savedPasswordsMap[selectedAccount!!.publicKey] - if (encryptedPassword != null) { - biometricManager.decryptPassword( - activity = activity, - encryptedData = encryptedPassword, - onSuccess = { decryptedPassword -> - scope.launch { - performUnlock( - selectedAccount = selectedAccount, - password = decryptedPassword, - accountManager = accountManager, - onUnlocking = { isUnlocking = it }, - onError = { error = it }, - onSuccess = { decryptedAccount -> - onUnlocked(decryptedAccount) - } - ) - } - }, - onError = { errorMessage -> - error = errorMessage - }, - onCancel = { - // User cancelled - } - ) - } + tryBiometricUnlock() }, enabled = !isUnlocking, modifier = Modifier