Makes sure only one npub is logged in at the same time

This commit is contained in:
Vitor Pamplona
2023-10-22 17:16:06 -04:00
parent eb6e81f81e
commit 191254d920

View File

@@ -156,13 +156,10 @@ object LocalPreferences {
private val prefsDirPath: String private val prefsDirPath: String
get() = "${Amethyst.instance.filesDir.parent}/shared_prefs/" get() = "${Amethyst.instance.filesDir.parent}/shared_prefs/"
private suspend fun addAccount(npub: AccountInfo) { private suspend fun addAccount(accInfo: AccountInfo) {
val accounts = savedAccounts().toMutableList() val accounts = savedAccounts().filter { it.npub != accInfo.npub }.plus(accInfo)
if (npub !in accounts) {
accounts.add(npub)
updateSavedAccounts(accounts) updateSavedAccounts(accounts)
} }
}
private suspend fun setCurrentAccount(account: Account) = withContext(Dispatchers.IO) { private suspend fun setCurrentAccount(account: Account) = withContext(Dispatchers.IO) {
val npub = account.userProfile().pubkeyNpub() val npub = account.userProfile().pubkeyNpub()
@@ -183,11 +180,9 @@ object LocalPreferences {
* Removes the account from the app level shared preferences * Removes the account from the app level shared preferences
*/ */
private suspend fun removeAccount(accountInfo: AccountInfo) { private suspend fun removeAccount(accountInfo: AccountInfo) {
val accounts = savedAccounts().toMutableList() val accounts = savedAccounts().filter { it.npub != accountInfo.npub }
if (accounts.remove(accountInfo)) {
updateSavedAccounts(accounts) updateSavedAccounts(accounts)
} }
}
/** /**
* Deletes the npub-specific shared preference file * Deletes the npub-specific shared preference file