Modernizing the edit access to LocalPreferences.

This commit is contained in:
Vitor Pamplona
2025-04-01 18:53:30 -04:00
parent 7e3f64fc61
commit 375931d4f9
@@ -25,6 +25,7 @@ import android.content.Context
import android.content.SharedPreferences
import android.util.Log
import androidx.compose.runtime.Immutable
import androidx.core.content.edit
import com.fasterxml.jackson.module.kotlin.readValue
import com.vitorpamplona.amethyst.model.AccountLanguagePreferencesInternal
import com.vitorpamplona.amethyst.model.AccountReactionPreferencesInternal
@@ -151,13 +152,13 @@ object LocalPreferences {
if (info == null) {
currentAccount = null
withContext(Dispatchers.IO) {
encryptedPreferences().edit().clear().apply()
encryptedPreferences().edit { clear() }
}
} else if (currentAccount != info.npub) {
currentAccount = info.npub
if (!info.isTransient) {
withContext(Dispatchers.IO) {
encryptedPreferences().edit().apply { putString(PrefKeys.CURRENT_ACCOUNT, info.npub) }.apply()
encryptedPreferences().edit { putString(PrefKeys.CURRENT_ACCOUNT, info.npub) }
}
}
}
@@ -189,7 +190,7 @@ object LocalPreferences {
savedAccounts.emit(migrated)
edit().apply { putString(PrefKeys.ALL_ACCOUNT_INFO, EventMapper.mapper.writeValueAsString(savedAccounts.value)) }.apply()
edit { putString(PrefKeys.ALL_ACCOUNT_INFO, EventMapper.mapper.writeValueAsString(savedAccounts.value)) }
}
}
}
@@ -206,13 +207,12 @@ object LocalPreferences {
savedAccounts.emit(accounts)
encryptedPreferences()
.edit()
.apply {
.edit {
putString(
PrefKeys.ALL_ACCOUNT_INFO,
EventMapper.mapper.writeValueAsString(accounts.filter { !it.isTransient }),
)
}.apply()
}
}
}
@@ -280,7 +280,7 @@ object LocalPreferences {
suspend fun updatePrefsForLogout(accountInfo: AccountInfo) {
Log.d("LocalPreferences", "Saving to encrypted storage updatePrefsForLogout ${accountInfo.npub}")
withContext(Dispatchers.IO) {
encryptedPreferences(accountInfo.npub).edit().clear().commit()
encryptedPreferences(accountInfo.npub).edit(commit = true) { clear() }
removeAccount(accountInfo)
deleteUserPreferenceFile(accountInfo.npub)
@@ -304,9 +304,7 @@ object LocalPreferences {
if (!settings.transientAccount) {
withContext(Dispatchers.IO) {
val prefs = encryptedPreferences(settings.keyPair.pubKey.toNpub())
prefs
.edit()
.apply {
prefs.edit {
putBoolean(PrefKeys.LOGIN_WITH_EXTERNAL_SIGNER, settings.externalSignerPackageName != null)
if (settings.externalSignerPackageName != null) {
remove(PrefKeys.NOSTR_PRIVKEY)
@@ -439,7 +437,7 @@ object LocalPreferences {
PrefKeys.PENDING_ATTESTATIONS,
EventMapper.mapper.writeValueAsString(settings.pendingAttestations.value),
)
}.apply()
}
}
}
Log.d("LocalPreferences", "Saved to encrypted storage")
@@ -501,8 +499,8 @@ object LocalPreferences {
private suspend fun innerLoadCurrentAccountFromEncryptedStorage(npub: String?): AccountSettings? {
Log.d("LocalPreferences", "Load account from file $npub")
return withContext(Dispatchers.IO) {
val result =
withContext(Dispatchers.IO) {
checkNotInMainThread()
return@withContext with(encryptedPreferences(npub)) {
@@ -656,6 +654,8 @@ object LocalPreferences {
)
}
}
Log.d("LocalPreferences", "Loaded account from file $npub")
return result
}
private inline fun <reified T> SharedPreferences.parseOrNull(key: String): T? {