feat: Safely set last logged account public key using null safety

This commit is contained in:
k1ngsterr1
2026-01-12 17:37:42 +05:00
parent 8aa17383cf
commit fb339642fa
6 changed files with 134 additions and 26 deletions

View File

@@ -82,8 +82,12 @@ fun UnlockScreen(
var searchQuery by remember { mutableStateOf("") }
val searchFocusRequester = remember { FocusRequester() }
// Load accounts
// Load accounts and pre-select last used account
LaunchedEffect(Unit) {
// ⚡ СИНХРОННОЕ чтение последнего аккаунта ДО загрузки списка
val lastLoggedKey = accountManager.getLastLoggedPublicKey()
Log.d("UnlockScreen", "🔍 Last logged account from SharedPrefs: ${lastLoggedKey?.take(16) ?: "none"}")
val allAccounts = accountManager.getAllAccounts()
accounts = allAccounts.map { acc ->
AccountItem(
@@ -93,30 +97,26 @@ fun UnlockScreen(
)
}
// Get last logged account from SharedPreferences (synchronous, reliable)
val lastLoggedKey = accountManager.getLastLoggedPublicKey()
Log.d("UnlockScreen", "📋 Available accounts: ${accounts.map { "${it.name}:${it.publicKey.take(8)}" }}")
Log.d("UnlockScreen", "Loading accounts. lastLoggedKey=$lastLoggedKey")
Log.d("UnlockScreen", "Available accounts: ${accounts.map { "${it.name}:${it.publicKey.take(8)}" }}")
// Find the target account - prioritize selectedAccountId, then lastLoggedKey
// Find the target account - приоритет: selectedAccountId > lastLoggedKey > первый
val targetAccount = when {
selectedAccountId != null -> {
Log.d("UnlockScreen", "Using selectedAccountId: $selectedAccountId")
!selectedAccountId.isNullOrEmpty() -> {
Log.d("UnlockScreen", "Using selectedAccountId: ${selectedAccountId.take(16)}")
accounts.find { it.publicKey == selectedAccountId }
}
lastLoggedKey != null -> {
Log.d("UnlockScreen", "Using lastLoggedKey: ${lastLoggedKey.take(8)}")
!lastLoggedKey.isNullOrEmpty() -> {
Log.d("UnlockScreen", "Using lastLoggedKey: ${lastLoggedKey.take(16)}")
accounts.find { it.publicKey == lastLoggedKey }
}
else -> {
Log.d("UnlockScreen", "No lastLoggedKey, using first account")
null
Log.d("UnlockScreen", "⚠️ No preference, using first account")
accounts.firstOrNull()
}
}
selectedAccount = targetAccount ?: accounts.firstOrNull()
Log.d("UnlockScreen", "Final selected: ${selectedAccount?.name}:${selectedAccount?.publicKey?.take(8)}")
Log.d("UnlockScreen", "🎯 Final selected: ${selectedAccount?.name} (${selectedAccount?.publicKey?.take(16)})")
}
// Filter accounts by search