feat: Refactor account management to use SharedPreferences for last logged public key for immediate access
This commit is contained in:
@@ -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<String?> = 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<String?> = 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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 = ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user