Use List instead of Set to keep account order

This commit is contained in:
maxmoney21m
2023-03-12 22:26:38 +08:00
parent cc36dcffe0
commit 20431a87ea

View File

@@ -54,6 +54,8 @@ private object PrefKeys {
private val gson = GsonBuilder().create() private val gson = GsonBuilder().create()
object LocalPreferences { object LocalPreferences {
private const val comma = ","
private var currentAccount: String? private var currentAccount: String?
get() = encryptedPreferences().getString(PrefKeys.CURRENT_ACCOUNT, null) get() = encryptedPreferences().getString(PrefKeys.CURRENT_ACCOUNT, null)
set(npub) { set(npub) {
@@ -63,18 +65,21 @@ object LocalPreferences {
}.apply() }.apply()
} }
private val savedAccounts: Set<String> private val savedAccounts: List<String>
get() = encryptedPreferences().getStringSet(PrefKeys.SAVED_ACCOUNTS, null) ?: setOf() get() = encryptedPreferences()
.getString(PrefKeys.SAVED_ACCOUNTS, null)?.split(comma) ?: listOf()
private val prefsDirPath: String private val prefsDirPath: String
get() = "${Amethyst.instance.filesDir.parent}/shared_prefs/" get() = "${Amethyst.instance.filesDir.parent}/shared_prefs/"
private fun addAccount(npub: String) { private fun addAccount(npub: String) {
val accounts = savedAccounts.toMutableSet() val accounts = savedAccounts.toMutableList()
accounts.add(npub) if (npub !in accounts) {
accounts.add(npub)
}
val prefs = encryptedPreferences() val prefs = encryptedPreferences()
prefs.edit().apply { prefs.edit().apply {
putStringSet(PrefKeys.SAVED_ACCOUNTS, accounts) putString(PrefKeys.SAVED_ACCOUNTS, accounts.joinToString(comma))
}.apply() }.apply()
} }
@@ -92,12 +97,13 @@ object LocalPreferences {
* Removes the account from the app level shared preferences * Removes the account from the app level shared preferences
*/ */
private fun removeAccount(npub: String) { private fun removeAccount(npub: String) {
val accounts = savedAccounts.toMutableSet() val accounts = savedAccounts.toMutableList()
accounts.remove(npub) if (accounts.remove(npub)) {
val prefs = encryptedPreferences() val prefs = encryptedPreferences()
prefs.edit().apply { prefs.edit().apply {
putStringSet(PrefKeys.SAVED_ACCOUNTS, accounts) putString(PrefKeys.SAVED_ACCOUNTS, accounts.joinToString(comma))
}.apply() }.apply()
}
} }
/** /**