@@ -5,7 +5,6 @@ import android.content.SharedPreferences
55import android.util.Log
66import androidx.core.content.edit
77import androidx.preference.PreferenceManager
8- import com.github.libretube.BuildConfig
98import com.github.libretube.LibreTubeApp
109import com.github.libretube.R
1110import com.github.libretube.constants.PreferenceKeys
@@ -14,6 +13,13 @@ import com.github.libretube.enums.SbSkipOptions
1413object 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