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.content.SharedPreferences
import android.util.Log import android.util.Log
import androidx.compose.runtime.Immutable import androidx.compose.runtime.Immutable
import androidx.core.content.edit
import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.readValue
import com.vitorpamplona.amethyst.model.AccountLanguagePreferencesInternal import com.vitorpamplona.amethyst.model.AccountLanguagePreferencesInternal
import com.vitorpamplona.amethyst.model.AccountReactionPreferencesInternal import com.vitorpamplona.amethyst.model.AccountReactionPreferencesInternal
@@ -151,13 +152,13 @@ object LocalPreferences {
if (info == null) { if (info == null) {
currentAccount = null currentAccount = null
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
encryptedPreferences().edit().clear().apply() encryptedPreferences().edit { clear() }
} }
} else if (currentAccount != info.npub) { } else if (currentAccount != info.npub) {
currentAccount = info.npub currentAccount = info.npub
if (!info.isTransient) { if (!info.isTransient) {
withContext(Dispatchers.IO) { 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) 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) savedAccounts.emit(accounts)
encryptedPreferences() encryptedPreferences()
.edit() .edit {
.apply {
putString( putString(
PrefKeys.ALL_ACCOUNT_INFO, PrefKeys.ALL_ACCOUNT_INFO,
EventMapper.mapper.writeValueAsString(accounts.filter { !it.isTransient }), EventMapper.mapper.writeValueAsString(accounts.filter { !it.isTransient }),
) )
}.apply() }
} }
} }
@@ -280,7 +280,7 @@ object LocalPreferences {
suspend fun updatePrefsForLogout(accountInfo: AccountInfo) { suspend fun updatePrefsForLogout(accountInfo: AccountInfo) {
Log.d("LocalPreferences", "Saving to encrypted storage updatePrefsForLogout ${accountInfo.npub}") Log.d("LocalPreferences", "Saving to encrypted storage updatePrefsForLogout ${accountInfo.npub}")
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
encryptedPreferences(accountInfo.npub).edit().clear().commit() encryptedPreferences(accountInfo.npub).edit(commit = true) { clear() }
removeAccount(accountInfo) removeAccount(accountInfo)
deleteUserPreferenceFile(accountInfo.npub) deleteUserPreferenceFile(accountInfo.npub)
@@ -304,9 +304,7 @@ object LocalPreferences {
if (!settings.transientAccount) { if (!settings.transientAccount) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val prefs = encryptedPreferences(settings.keyPair.pubKey.toNpub()) val prefs = encryptedPreferences(settings.keyPair.pubKey.toNpub())
prefs prefs.edit {
.edit()
.apply {
putBoolean(PrefKeys.LOGIN_WITH_EXTERNAL_SIGNER, settings.externalSignerPackageName != null) putBoolean(PrefKeys.LOGIN_WITH_EXTERNAL_SIGNER, settings.externalSignerPackageName != null)
if (settings.externalSignerPackageName != null) { if (settings.externalSignerPackageName != null) {
remove(PrefKeys.NOSTR_PRIVKEY) remove(PrefKeys.NOSTR_PRIVKEY)
@@ -439,7 +437,7 @@ object LocalPreferences {
PrefKeys.PENDING_ATTESTATIONS, PrefKeys.PENDING_ATTESTATIONS,
EventMapper.mapper.writeValueAsString(settings.pendingAttestations.value), EventMapper.mapper.writeValueAsString(settings.pendingAttestations.value),
) )
}.apply() }
} }
} }
Log.d("LocalPreferences", "Saved to encrypted storage") Log.d("LocalPreferences", "Saved to encrypted storage")
@@ -501,8 +499,8 @@ object LocalPreferences {
private suspend fun innerLoadCurrentAccountFromEncryptedStorage(npub: String?): AccountSettings? { private suspend fun innerLoadCurrentAccountFromEncryptedStorage(npub: String?): AccountSettings? {
Log.d("LocalPreferences", "Load account from file $npub") Log.d("LocalPreferences", "Load account from file $npub")
val result =
return withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
checkNotInMainThread() checkNotInMainThread()
return@withContext with(encryptedPreferences(npub)) { 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? { private inline fun <reified T> SharedPreferences.parseOrNull(key: String): T? {