Skip to content

Commit 39484ab

Browse files
committed
feat(AccountSwitcher): add automatic migration to shared prefs
1 parent e7072ac commit 39484ab

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

plugin/AccountSwitcher/build.gradle.kts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
version = "1.3.0"
1+
version = "1.3.1"
22
description = "Adds the ability to quickly switch accounts"
33

44
aliucord.changelog.set(
55
"""
6+
# 1.3.1
7+
* Added automatic migration for accounts from the old storage format to the new one.
8+
69
# 1.3.0
710
* Redesigned how accounts are stored, now using a SharedPreferences backed map, which allows for better security and less error prone code.
811
* Added helper text to explain how to access the switcher and add accounts.

plugin/AccountSwitcher/src/main/kotlin/AccountSwitcher.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import accountswitcher.SharedPreferencesBackedMap
44
import accountswitcher.SwitcherPage
5+
import accountswitcher.migrate
56
import accountswitcher.settings.PluginSettings
67
import android.content.Context
78
import com.aliucord.Utils
@@ -21,6 +22,8 @@ class AccountSwitcher : Plugin() {
2122
}
2223

2324
override fun start(context: Context) {
25+
migrate(settings)
26+
2427
patcher.instead<WidgetSettings>("showLogoutDialog", Context::class.java) {
2528
Utils.openPageWithProxy(Utils.appActivity, SwitcherPage())
2629
}

plugin/AccountSwitcher/src/main/kotlin/accountswitcher/Util.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
package accountswitcher
44

5+
import AccountSwitcher.Companion.accounts
56
import android.content.Context
67
import com.aliucord.Http
8+
import com.aliucord.api.SettingsAPI
79
import com.aliucord.utils.GsonUtils
810
import com.aliucord.utils.GsonUtils.fromJson
911
import com.aliucord.utils.GsonUtils.toJson
@@ -12,9 +14,10 @@ import com.discord.utilities.rest.RestAPI
1214
import com.google.gson.reflect.TypeToken
1315
import java.lang.reflect.Type
1416

17+
private val type: Type = object : TypeToken<ArrayList<Account>>() {}.type
18+
1519
class SharedPreferencesBackedMap(context: Context) : AbstractMutableMap<Long, Account>() {
1620
private val prefs = context.getSharedPreferences("AccountSwitcher", Context.MODE_PRIVATE)
17-
private val type: Type = object : TypeToken<ArrayList<Account>>() {}.type
1821

1922
private fun getMap(): MutableMap<Long, Account> {
2023
val json = prefs.getString("accounts", "[]")
@@ -55,6 +58,16 @@ class SharedPreferencesBackedMap(context: Context) : AbstractMutableMap<Long, Ac
5558
}
5659
}
5760

61+
fun migrate(oldSettings: SettingsAPI) {
62+
if (!oldSettings.exists("accounts")) return
63+
64+
oldSettings
65+
.getObject("accounts", ArrayList<Account>(), type)
66+
.forEach { accounts[it.id] = it }
67+
68+
oldSettings.resetSettings()
69+
}
70+
5871
fun fetchUser(token: String): MeUser? = try {
5972
Http
6073
.Request("https://discord.com/api/v9/users/@me")

0 commit comments

Comments
 (0)