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

@@ -40,12 +40,20 @@ class AccountManager(private val context: Context) {
// Synchronous read from SharedPreferences - always up to date
fun getLastLoggedPublicKey(): String? {
return sharedPrefs.getString(KEY_LAST_LOGGED, null)
val publicKey = sharedPrefs.getString(KEY_LAST_LOGGED, null)
android.util.Log.d("AccountManager", "📖 getLastLoggedPublicKey: ${publicKey?.take(16) ?: "null"}")
return publicKey
}
// Synchronous write to SharedPreferences
fun setLastLoggedPublicKey(publicKey: String) {
sharedPrefs.edit().putString(KEY_LAST_LOGGED, publicKey).commit() // commit() is synchronous
android.util.Log.d("AccountManager", "💾 Saving last logged account: ${publicKey.take(16)}...")
val success = sharedPrefs.edit().putString(KEY_LAST_LOGGED, publicKey).commit() // commit() is synchronous
android.util.Log.d("AccountManager", "💾 Save result: $success")
// Verify immediately
val saved = sharedPrefs.getString(KEY_LAST_LOGGED, null)
android.util.Log.d("AccountManager", "💾 Verification read: ${saved?.take(16) ?: "null"}")
}
suspend fun saveAccount(account: EncryptedAccount) {
@@ -78,13 +86,18 @@ class AccountManager(private val context: Context) {
}
suspend fun setCurrentAccount(publicKey: String) {
// Save to SharedPreferences synchronously first
android.util.Log.d("AccountManager", "🔐 setCurrentAccount called for: ${publicKey.take(16)}...")
// ⚡ ВАЖНО: Сначала сохраняем в SharedPreferences синхронно
setLastLoggedPublicKey(publicKey)
// Потом в DataStore асинхронно (для совместимости с потоками)
context.accountDataStore.edit { preferences ->
preferences[CURRENT_PUBLIC_KEY] = publicKey
preferences[IS_LOGGED_IN] = true
}
android.util.Log.d("AccountManager", "✅ setCurrentAccount completed for: ${publicKey.take(16)}...")
}
suspend fun logout() {