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 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
}
}