Skip to content

Commit c746647

Browse files
committed
refactor(Preferences): explicitly define preference migrations
This system allows to run migrations even without changing version codes, allowing for mid-version preferences changes to are applied to debug/nightly builds.
1 parent f0307e7 commit c746647

1 file changed

Lines changed: 43 additions & 17 deletions

File tree

app/src/main/java/com/github/libretube/helpers/PreferenceHelper.kt

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import android.content.SharedPreferences
55
import android.util.Log
66
import androidx.core.content.edit
77
import androidx.preference.PreferenceManager
8-
import com.github.libretube.BuildConfig
98
import com.github.libretube.LibreTubeApp
109
import com.github.libretube.R
1110
import com.github.libretube.constants.PreferenceKeys
@@ -14,6 +13,13 @@ import com.github.libretube.enums.SbSkipOptions
1413
object PreferenceHelper {
1514
private val TAG = PreferenceHelper::class.simpleName
1615

16+
/**
17+
* Preference migration from [fromVersion] to [toVersion].
18+
*/
19+
private class PreferenceMigration(
20+
val fromVersion: Int, val toVersion: Int, val onMigration: () -> Unit
21+
)
22+
1723
/**
1824
* for normal preferences
1925
*/
@@ -29,25 +35,17 @@ object PreferenceHelper {
2935
*/
3036
private const val USER_ID_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
3137

38+
3239
/**
33-
* set the context that is being used to access the shared preferences
40+
* Current version of the preferences.
3441
*/
35-
fun initialize(context: Context) {
36-
settings = getDefaultSharedPreferences(context)
37-
authSettings = getAuthenticationPreferences(context)
38-
}
42+
private const val PREFERENCE_VERSION = 1
3943

4044
/**
41-
* Migrate preference to a new version
45+
* Migrations required to migrate the application to a newer [PREFERENCE_VERSION].
4246
*/
43-
fun migrate() {
44-
val prefVersion = getInt(PreferenceKeys.PREFERENCE_VERSION, -1)
45-
// check if there are any prefs to migrate
46-
if (prefVersion == BuildConfig.VERSION_CODE)
47-
return
48-
49-
if (prefVersion < 63) {
50-
Log.i(TAG, "Migration to prefs v63")
47+
private val MIGRATIONS = listOf(
48+
PreferenceMigration(0, 1) {
5149
LibreTubeApp.instance.resources
5250
.getStringArray(R.array.sponsorBlockSegments)
5351
.forEach { category ->
@@ -57,10 +55,38 @@ object PreferenceHelper {
5755
putString(key, SbSkipOptions.MANUAL.name.lowercase())
5856
}
5957
}
58+
},
59+
)
60+
61+
/**
62+
* set the context that is being used to access the shared preferences
63+
*/
64+
fun initialize(context: Context) {
65+
settings = getDefaultSharedPreferences(context)
66+
authSettings = getAuthenticationPreferences(context)
67+
}
68+
69+
/**
70+
* Migrate preference to a new version.
71+
*
72+
* Migrations are run up to [PREFERENCE_VERSION].
73+
*/
74+
fun migrate() {
75+
var currentPrefVersion = getInt(PreferenceKeys.PREFERENCE_VERSION, 0)
76+
77+
while (currentPrefVersion < PREFERENCE_VERSION) {
78+
val next = currentPrefVersion + 1
79+
80+
val migration =
81+
MIGRATIONS.find { it.fromVersion == currentPrefVersion && it.toVersion == next }
82+
Log.i(TAG, "Performing migration from $currentPrefVersion to $next")
83+
migration?.onMigration?.invoke()
84+
85+
currentPrefVersion++
86+
// mark as successfully migrated
87+
putInt(PreferenceKeys.PREFERENCE_VERSION, currentPrefVersion)
6088
}
6189

62-
// mark as successfully migrated
63-
putInt(PreferenceKeys.PREFERENCE_VERSION, BuildConfig.VERSION_CODE)
6490
}
6591

6692
fun putString(key: String, value: String) {

0 commit comments

Comments
 (0)