Skip to content

Commit 62a8f27

Browse files
committed
feat(Preference): add migration support
Introduces a new function, that checks for preferences, that need to be migrated, and migrates them if necessary.
1 parent e8673f4 commit 62a8f27

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

app/src/main/java/com/github/libretube/LibreTubeApp.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class LibreTubeApp : Application() {
2626
* Initialize the [PreferenceHelper]
2727
*/
2828
PreferenceHelper.initialize(applicationContext)
29+
PreferenceHelper.migrate()
2930

3031
/**
3132
* Set the api and the auth api url

app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ object PreferenceKeys {
145145
const val SELECTED_CHANNEL_GROUP = "selected_channel_group"
146146
const val SELECTED_DOWNLOAD_SORT_TYPE = "selected_download_sort_type"
147147
const val LAST_SHOWN_INFO_MESSAGE_VERSION_CODE = "last_shown_info_message_version"
148+
const val PREFERENCE_VERSION = "PREFERENCE_VERSION"
148149

149150
// use the helper methods at PreferenceHelper to access these
150151
const val LAST_USER_SEEN_FEED_TIME = "last_watched_feed_time"

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ package com.github.libretube.helpers
22

33
import android.content.Context
44
import android.content.SharedPreferences
5+
import android.util.Log
56
import androidx.core.content.edit
67
import androidx.preference.PreferenceManager
8+
import com.github.libretube.BuildConfig
79
import com.github.libretube.constants.PreferenceKeys
810

911
object PreferenceHelper {
12+
private val TAG = PreferenceHelper::class.simpleName
13+
1014
/**
1115
* for normal preferences
1216
*/
@@ -30,6 +34,19 @@ object PreferenceHelper {
3034
authSettings = getAuthenticationPreferences(context)
3135
}
3236

37+
/**
38+
* Migrate preference to a new version
39+
*/
40+
fun migrate() {
41+
val prefVersion = getInt(PreferenceKeys.PREFERENCE_VERSION, -1)
42+
// check if there are any prefs to migrate
43+
if (prefVersion == BuildConfig.VERSION_CODE)
44+
return
45+
46+
// mark as successfully migrated
47+
putInt(PreferenceKeys.PREFERENCE_VERSION, BuildConfig.VERSION_CODE)
48+
}
49+
3350
fun putString(key: String, value: String) {
3451
settings.edit(commit = true) { putString(key, value) }
3552
}

0 commit comments

Comments
 (0)