From 68fec477901d5c6c43e9aa75e968fc3061fc0ae1 Mon Sep 17 00:00:00 2001 From: k1ngsterr1 Date: Fri, 9 Jan 2026 04:10:49 +0500 Subject: [PATCH] feat: Refactor account management to use SharedPreferences for last logged public key for immediate access --- .../rosetta/messenger/data/AccountManager.kt | 20 ++++++++---- .../rosetta/messenger/ui/auth/UnlockScreen.kt | 31 +++++++++++++------ 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/rosetta/messenger/data/AccountManager.kt b/app/src/main/java/com/rosetta/messenger/data/AccountManager.kt index b1138b6..57401c2 100644 --- a/app/src/main/java/com/rosetta/messenger/data/AccountManager.kt +++ b/app/src/main/java/com/rosetta/messenger/data/AccountManager.kt @@ -19,9 +19,13 @@ class AccountManager(private val context: Context) { private val CURRENT_PUBLIC_KEY = stringPreferencesKey("current_public_key") private val ACCOUNTS_JSON = stringPreferencesKey("accounts_json") private val IS_LOGGED_IN = booleanPreferencesKey("is_logged_in") - private val LAST_LOGGED_PUBLIC_KEY = stringPreferencesKey("last_logged_public_key") + private const val PREFS_NAME = "rosetta_account_prefs" + private const val KEY_LAST_LOGGED = "last_logged_public_key" } + // Use SharedPreferences for last logged account - more reliable for immediate reads + private val sharedPrefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) + val currentPublicKey: Flow = context.accountDataStore.data.map { preferences -> preferences[CURRENT_PUBLIC_KEY] } @@ -34,12 +38,14 @@ class AccountManager(private val context: Context) { preferences[ACCOUNTS_JSON] } - val lastLoggedPublicKey: Flow = context.accountDataStore.data.map { preferences -> - preferences[LAST_LOGGED_PUBLIC_KEY] + // Synchronous read from SharedPreferences - always up to date + fun getLastLoggedPublicKey(): String? { + return sharedPrefs.getString(KEY_LAST_LOGGED, null) } - suspend fun getLastLoggedPublicKey(): String? { - return context.accountDataStore.data.first()[LAST_LOGGED_PUBLIC_KEY] + // Synchronous write to SharedPreferences + fun setLastLoggedPublicKey(publicKey: String) { + sharedPrefs.edit().putString(KEY_LAST_LOGGED, publicKey).commit() // commit() is synchronous } suspend fun saveAccount(account: EncryptedAccount) { @@ -72,10 +78,12 @@ class AccountManager(private val context: Context) { } suspend fun setCurrentAccount(publicKey: String) { + // Save to SharedPreferences synchronously first + setLastLoggedPublicKey(publicKey) + context.accountDataStore.edit { preferences -> preferences[CURRENT_PUBLIC_KEY] = publicKey preferences[IS_LOGGED_IN] = true - preferences[LAST_LOGGED_PUBLIC_KEY] = publicKey } } 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 d05c33c..c62aea7 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 @@ -93,21 +93,30 @@ fun UnlockScreen( ) } - // Select account - prioritize: selectedAccountId > lastLoggedPublicKey > first account + // Get last logged account from SharedPreferences (synchronous, reliable) val lastLoggedKey = accountManager.getLastLoggedPublicKey() - Log.d("UnlockScreen", "Loading accounts. selectedAccountId=$selectedAccountId, lastLoggedKey=$lastLoggedKey") + Log.d("UnlockScreen", "Loading accounts. lastLoggedKey=$lastLoggedKey") Log.d("UnlockScreen", "Available accounts: ${accounts.map { "${it.name}:${it.publicKey.take(8)}" }}") - // Find the target account + // Find the target account - prioritize selectedAccountId, then lastLoggedKey val targetAccount = when { - selectedAccountId != null -> accounts.find { it.publicKey == selectedAccountId } - lastLoggedKey != null -> accounts.find { it.publicKey == lastLoggedKey } - else -> null + selectedAccountId != null -> { + Log.d("UnlockScreen", "Using selectedAccountId: $selectedAccountId") + accounts.find { it.publicKey == selectedAccountId } + } + lastLoggedKey != null -> { + Log.d("UnlockScreen", "Using lastLoggedKey: ${lastLoggedKey.take(8)}") + accounts.find { it.publicKey == lastLoggedKey } + } + else -> { + Log.d("UnlockScreen", "No lastLoggedKey, using first account") + null + } } selectedAccount = targetAccount ?: accounts.firstOrNull() - Log.d("UnlockScreen", "Selected account: ${selectedAccount?.name}:${selectedAccount?.publicKey?.take(8)}") + Log.d("UnlockScreen", "Final selected: ${selectedAccount?.name}:${selectedAccount?.publicKey?.take(8)}") } // Filter accounts by search @@ -136,8 +145,12 @@ fun UnlockScreen( // Auto-focus search when dropdown opens LaunchedEffect(isDropdownExpanded) { if (isDropdownExpanded) { - kotlinx.coroutines.delay(200) - searchFocusRequester.requestFocus() + kotlinx.coroutines.delay(300) + try { + searchFocusRequester.requestFocus() + } catch (e: Exception) { + // FocusRequester may not be attached yet, ignore + } } else { searchQuery = "" }