Perform preference schema upgrades at startup (#3186)

* Perform preference schema upgrades at startup

Over time it can be desirable to change how preferences are interpreted.

Preferences might be removed, or renamed. Or the default value for a
preference might be changed.

When this happens it's important that users upgrading from one version to
the next (or jumping from one version to several versions ahead) get a
consistent experience. In particular:

- Preferences that no longer exist should be deleted
- Preferences that have been renamed should have the old preference values
  copied over
- If the user used the default value for the preference, and the default has
  changed, the previous default value should be explicitly set as their
  value for the preference

To support this, store a SCHEMA_VERSION as a preference. This is not exposed
to the user, and corresponds to the app's VERSION_CODE.

If the version code does not match the schema version then this is a newer
version of the app with older preferences that may need to be changed.

Those changes will be implemented in `upgradeSharedPreferences`.

* Translated using Weblate (Hungarian)

Currently translated at 100.0% (552 of 552 strings)

Co-authored-by: Gera, Zoltan <gerazo@manioka.hu>
Translate-URL: https://weblate.tusky.app/projects/tusky/tusky/hu/
Translation: Tusky/Tusky

* Translated using Weblate (Chinese (Simplified))

Currently translated at 100.0% (552 of 552 strings)

Co-authored-by: Eric <alchemillatruth@purelymail.com>
Translate-URL: https://weblate.tusky.app/projects/tusky/tusky/zh_Hans/
Translation: Tusky/Tusky

* Translated using Weblate (Ukrainian)

Currently translated at 100.0% (552 of 552 strings)

Co-authored-by: Ihor Hordiichuk <igor_ck@outlook.com>
Translate-URL: https://weblate.tusky.app/projects/tusky/tusky/uk/
Translation: Tusky/Tusky

* Translated using Weblate (Vietnamese)

Currently translated at 100.0% (552 of 552 strings)

Co-authored-by: Hồ Nhất Duy <mastoduy@gmail.com>
Translate-URL: https://weblate.tusky.app/projects/tusky/tusky/vi/
Translation: Tusky/Tusky

* Translated using Weblate (Belarusian)

Currently translated at 100.0% (552 of 552 strings)

Co-authored-by: Mikalai <mikalai.hryb@gmail.com>
Translate-URL: https://weblate.tusky.app/projects/tusky/tusky/be/
Translation: Tusky/Tusky

* Translated using Weblate (Belarusian)

Currently translated at 100.0% (552 of 552 strings)

Co-authored-by: Andrej Zabavin <andre.zabavin@gmail.com>
Translate-URL: https://weblate.tusky.app/projects/tusky/tusky/be/
Translation: Tusky/Tusky

* Translated using Weblate (Belarusian)

Currently translated at 100.0% (552 of 552 strings)

Co-authored-by: Mikalai <mikalai.hryb@gmail.com>
Translate-URL: https://weblate.tusky.app/projects/tusky/tusky/be/
Translation: Tusky/Tusky

* Translated using Weblate (Belarusian)

Currently translated at 100.0% (552 of 552 strings)

Translated using Weblate (Belarusian)

Currently translated at 100.0% (552 of 552 strings)

Co-authored-by: xzFantom <xzfantom@gmail.com>
Translate-URL: https://weblate.tusky.app/projects/tusky/tusky/be/
Translation: Tusky/Tusky

* Translated using Weblate (Japanese)

Currently translated at 91.3% (504 of 552 strings)

Co-authored-by: TAKAHASHI Shuuji <shuuji3@gmail.com>
Translate-URL: https://weblate.tusky.app/projects/tusky/tusky/ja/
Translation: Tusky/Tusky

* Translated using Weblate (Icelandic)

Currently translated at 100.0% (552 of 552 strings)

Co-authored-by: Sveinn í Felli <sv1@fellsnet.is>
Translate-URL: https://weblate.tusky.app/projects/tusky/tusky/is/
Translation: Tusky/Tusky

* Translated using Weblate (Belarusian)

Currently translated at 100.0% (552 of 552 strings)

Co-authored-by: Mikalai <mikalai.hryb@gmail.com>
Translate-URL: https://weblate.tusky.app/projects/tusky/tusky/be/
Translation: Tusky/Tusky

* Lint

---------

Co-authored-by: Gera, Zoltan <gerazo@manioka.hu>
Co-authored-by: Eric <alchemillatruth@purelymail.com>
Co-authored-by: Ihor Hordiichuk <igor_ck@outlook.com>
Co-authored-by: Hồ Nhất Duy <mastoduy@gmail.com>
Co-authored-by: Mikalai <mikalai.hryb@gmail.com>
Co-authored-by: Andrej Zabavin <andre.zabavin@gmail.com>
Co-authored-by: xzFantom <xzfantom@gmail.com>
Co-authored-by: TAKAHASHI Shuuji <shuuji3@gmail.com>
Co-authored-by: Sveinn í Felli <sv1@fellsnet.is>
This commit is contained in:
Nik Clayton 2023-02-04 20:19:01 +01:00 committed by GitHub
parent 3592318dc1
commit a1494ecc68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 4 deletions

View File

@ -16,12 +16,13 @@
package com.keylesspalace.tusky package com.keylesspalace.tusky
import android.app.Application import android.app.Application
import android.content.SharedPreferences
import android.util.Log import android.util.Log
import androidx.preference.PreferenceManager
import androidx.work.WorkManager import androidx.work.WorkManager
import autodispose2.AutoDisposePlugins import autodispose2.AutoDisposePlugins
import com.keylesspalace.tusky.components.notifications.NotificationWorkerFactory import com.keylesspalace.tusky.components.notifications.NotificationWorkerFactory
import com.keylesspalace.tusky.di.AppInjector import com.keylesspalace.tusky.di.AppInjector
import com.keylesspalace.tusky.settings.PrefKeys
import com.keylesspalace.tusky.util.APP_THEME_DEFAULT import com.keylesspalace.tusky.util.APP_THEME_DEFAULT
import com.keylesspalace.tusky.util.LocaleManager import com.keylesspalace.tusky.util.LocaleManager
import com.keylesspalace.tusky.util.setAppNightMode import com.keylesspalace.tusky.util.setAppNightMode
@ -36,7 +37,6 @@ import java.security.Security
import javax.inject.Inject import javax.inject.Inject
class TuskyApplication : Application(), HasAndroidInjector { class TuskyApplication : Application(), HasAndroidInjector {
@Inject @Inject
lateinit var androidInjector: DispatchingAndroidInjector<Any> lateinit var androidInjector: DispatchingAndroidInjector<Any>
@ -46,6 +46,9 @@ class TuskyApplication : Application(), HasAndroidInjector {
@Inject @Inject
lateinit var localeManager: LocaleManager lateinit var localeManager: LocaleManager
@Inject
lateinit var sharedPreferences: SharedPreferences
override fun onCreate() { override fun onCreate() {
// Uncomment me to get StrictMode violation logs // Uncomment me to get StrictMode violation logs
// if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { // if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
@ -65,7 +68,12 @@ class TuskyApplication : Application(), HasAndroidInjector {
AppInjector.init(this) AppInjector.init(this)
val preferences = PreferenceManager.getDefaultSharedPreferences(this) // Migrate shared preference keys and defaults from version to version. The last
// version that did not have a SCHEMA_VERSION was 97, so that's the default.
val oldVersion = sharedPreferences.getInt(PrefKeys.SCHEMA_VERSION, 97)
if (oldVersion != BuildConfig.VERSION_CODE) {
upgradeSharedPreferences(oldVersion, BuildConfig.VERSION_CODE)
}
// In this case, we want to have the emoji preferences merged with the other ones // In this case, we want to have the emoji preferences merged with the other ones
// Copied from PreferenceManager.getDefaultSharedPreferenceName // Copied from PreferenceManager.getDefaultSharedPreferenceName
@ -73,7 +81,7 @@ class TuskyApplication : Application(), HasAndroidInjector {
EmojiPackHelper.init(this, DefaultEmojiPackList.get(this), allowPackImports = false) EmojiPackHelper.init(this, DefaultEmojiPackList.get(this), allowPackImports = false)
// init night mode // init night mode
val theme = preferences.getString("appTheme", APP_THEME_DEFAULT) val theme = sharedPreferences.getString("appTheme", APP_THEME_DEFAULT)
setAppNightMode(theme) setAppNightMode(theme)
localeManager.setLocale() localeManager.setLocale()
@ -91,4 +99,18 @@ class TuskyApplication : Application(), HasAndroidInjector {
} }
override fun androidInjector() = androidInjector override fun androidInjector() = androidInjector
private fun upgradeSharedPreferences(oldVersion: Int, newVersion: Int) {
Log.d(TAG, "Upgrading shared preferences: $oldVersion -> $newVersion")
val editor = sharedPreferences.edit()
// Future upgrade code goes here
editor.putInt(PrefKeys.SCHEMA_VERSION, newVersion)
editor.apply()
}
companion object {
private const val TAG = "TuskyApplication"
}
} }

View File

@ -16,6 +16,7 @@ object PrefKeys {
// Note: not all of these keys are actually used as SharedPreferences keys but we must give // Note: not all of these keys are actually used as SharedPreferences keys but we must give
// each preference a key for it to work. // each preference a key for it to work.
const val SCHEMA_VERSION: String = "schema_version"
const val APP_THEME = "appTheme" const val APP_THEME = "appTheme"
const val EMOJI = "selected_emoji_font" const val EMOJI = "selected_emoji_font"
const val FAB_HIDE = "fabHide" const val FAB_HIDE = "fabHide"