feat: Refactor account management to use SharedPreferences for last logged public key for immediate access

This commit is contained in:
k1ngsterr1
2026-01-09 04:10:49 +05:00
parent ecebaaf066
commit 68fec47790
2 changed files with 36 additions and 15 deletions

View File

@@ -19,9 +19,13 @@ class AccountManager(private val context: Context) {
private val CURRENT_PUBLIC_KEY = stringPreferencesKey("current_public_key") private val CURRENT_PUBLIC_KEY = stringPreferencesKey("current_public_key")
private val ACCOUNTS_JSON = stringPreferencesKey("accounts_json") private val ACCOUNTS_JSON = stringPreferencesKey("accounts_json")
private val IS_LOGGED_IN = booleanPreferencesKey("is_logged_in") 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<String?> = context.accountDataStore.data.map { preferences -> val currentPublicKey: Flow<String?> = context.accountDataStore.data.map { preferences ->
preferences[CURRENT_PUBLIC_KEY] preferences[CURRENT_PUBLIC_KEY]
} }
@@ -34,12 +38,14 @@ class AccountManager(private val context: Context) {
preferences[ACCOUNTS_JSON] preferences[ACCOUNTS_JSON]
} }
val lastLoggedPublicKey: Flow<String?> = context.accountDataStore.data.map { preferences -> // Synchronous read from SharedPreferences - always up to date
preferences[LAST_LOGGED_PUBLIC_KEY] fun getLastLoggedPublicKey(): String? {
return sharedPrefs.getString(KEY_LAST_LOGGED, null)
} }
suspend fun getLastLoggedPublicKey(): String? { // Synchronous write to SharedPreferences
return context.accountDataStore.data.first()[LAST_LOGGED_PUBLIC_KEY] fun setLastLoggedPublicKey(publicKey: String) {
sharedPrefs.edit().putString(KEY_LAST_LOGGED, publicKey).commit() // commit() is synchronous
} }
suspend fun saveAccount(account: EncryptedAccount) { suspend fun saveAccount(account: EncryptedAccount) {
@@ -72,10 +78,12 @@ class AccountManager(private val context: Context) {
} }
suspend fun setCurrentAccount(publicKey: String) { suspend fun setCurrentAccount(publicKey: String) {
// Save to SharedPreferences synchronously first
setLastLoggedPublicKey(publicKey)
context.accountDataStore.edit { preferences -> context.accountDataStore.edit { preferences ->
preferences[CURRENT_PUBLIC_KEY] = publicKey preferences[CURRENT_PUBLIC_KEY] = publicKey
preferences[IS_LOGGED_IN] = true preferences[IS_LOGGED_IN] = true
preferences[LAST_LOGGED_PUBLIC_KEY] = publicKey
} }
} }

View File

@@ -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() 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)}" }}") 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 { val targetAccount = when {
selectedAccountId != null -> accounts.find { it.publicKey == selectedAccountId } selectedAccountId != null -> {
lastLoggedKey != null -> accounts.find { it.publicKey == lastLoggedKey } Log.d("UnlockScreen", "Using selectedAccountId: $selectedAccountId")
else -> null 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() 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 // Filter accounts by search
@@ -136,8 +145,12 @@ fun UnlockScreen(
// Auto-focus search when dropdown opens // Auto-focus search when dropdown opens
LaunchedEffect(isDropdownExpanded) { LaunchedEffect(isDropdownExpanded) {
if (isDropdownExpanded) { if (isDropdownExpanded) {
kotlinx.coroutines.delay(200) kotlinx.coroutines.delay(300)
searchFocusRequester.requestFocus() try {
searchFocusRequester.requestFocus()
} catch (e: Exception) {
// FocusRequester may not be attached yet, ignore
}
} else { } else {
searchQuery = "" searchQuery = ""
} }