diff --git a/app/src/main/java/com/github/libretube/LibreTubeApp.kt b/app/src/main/java/com/github/libretube/LibreTubeApp.kt index 9a4620e064..4eedc56888 100644 --- a/app/src/main/java/com/github/libretube/LibreTubeApp.kt +++ b/app/src/main/java/com/github/libretube/LibreTubeApp.kt @@ -26,6 +26,7 @@ class LibreTubeApp : Application() { * Initialize the [PreferenceHelper] */ PreferenceHelper.initialize(applicationContext) + PreferenceHelper.migrate() /** * Set the api and the auth api url diff --git a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt index f676f46df8..cdc3667b4f 100644 --- a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt +++ b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt @@ -145,6 +145,7 @@ object PreferenceKeys { const val SELECTED_CHANNEL_GROUP = "selected_channel_group" const val SELECTED_DOWNLOAD_SORT_TYPE = "selected_download_sort_type" const val LAST_SHOWN_INFO_MESSAGE_VERSION_CODE = "last_shown_info_message_version" + const val PREFERENCE_VERSION = "PREFERENCE_VERSION" // use the helper methods at PreferenceHelper to access these const val LAST_USER_SEEN_FEED_TIME = "last_watched_feed_time" diff --git a/app/src/main/java/com/github/libretube/enums/SbSkipOptions.kt b/app/src/main/java/com/github/libretube/enums/SbSkipOptions.kt index e923cdf96c..071e726c78 100644 --- a/app/src/main/java/com/github/libretube/enums/SbSkipOptions.kt +++ b/app/src/main/java/com/github/libretube/enums/SbSkipOptions.kt @@ -2,7 +2,6 @@ package com.github.libretube.enums enum class SbSkipOptions { OFF, - VISIBLE, MANUAL, AUTOMATIC, AUTOMATIC_ONCE diff --git a/app/src/main/java/com/github/libretube/helpers/PreferenceHelper.kt b/app/src/main/java/com/github/libretube/helpers/PreferenceHelper.kt index 49a9a1cf89..fc7c021338 100644 --- a/app/src/main/java/com/github/libretube/helpers/PreferenceHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/PreferenceHelper.kt @@ -2,11 +2,24 @@ package com.github.libretube.helpers import android.content.Context import android.content.SharedPreferences +import android.util.Log import androidx.core.content.edit import androidx.preference.PreferenceManager +import com.github.libretube.LibreTubeApp +import com.github.libretube.R import com.github.libretube.constants.PreferenceKeys +import com.github.libretube.enums.SbSkipOptions object PreferenceHelper { + private val TAG = PreferenceHelper::class.simpleName + + /** + * Preference migration from [fromVersion] to [toVersion]. + */ + private class PreferenceMigration( + val fromVersion: Int, val toVersion: Int, val onMigration: () -> Unit + ) + /** * for normal preferences */ @@ -22,6 +35,29 @@ object PreferenceHelper { */ private const val USER_ID_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" + + /** + * Current version of the preferences. + */ + private const val PREFERENCE_VERSION = 1 + + /** + * Migrations required to migrate the application to a newer [PREFERENCE_VERSION]. + */ + private val MIGRATIONS = listOf( + PreferenceMigration(0, 1) { + LibreTubeApp.instance.resources + .getStringArray(R.array.sponsorBlockSegments) + .forEach { category -> + val key = "${category}_category" + val stored = getString(key, "visible") + if (stored == "visible") { + putString(key, SbSkipOptions.MANUAL.name.lowercase()) + } + } + }, + ) + /** * set the context that is being used to access the shared preferences */ @@ -30,6 +66,29 @@ object PreferenceHelper { authSettings = getAuthenticationPreferences(context) } + /** + * Migrate preference to a new version. + * + * Migrations are run up to [PREFERENCE_VERSION]. + */ + fun migrate() { + var currentPrefVersion = getInt(PreferenceKeys.PREFERENCE_VERSION, 0) + + while (currentPrefVersion < PREFERENCE_VERSION) { + val next = currentPrefVersion + 1 + + val migration = + MIGRATIONS.find { it.fromVersion == currentPrefVersion && it.toVersion == next } + Log.i(TAG, "Performing migration from $currentPrefVersion to $next") + migration?.onMigration?.invoke() + + currentPrefVersion++ + // mark as successfully migrated + putInt(PreferenceKeys.PREFERENCE_VERSION, currentPrefVersion) + } + + } + fun putString(key: String, value: String) { settings.edit(commit = true) { putString(key, value) } } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index 03c31fca63..62620ad892 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -1014,8 +1014,8 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions { if (segmentData != null && commonPlayerViewModel.isMiniPlayerVisible.value != true) { val (segment, sbSkipOption) = segmentData - val autoSkipTemporarilyDisabled = !binding.player.sponsorBlockAutoSkip && - sbSkipOption !in arrayOf(SbSkipOptions.OFF, SbSkipOptions.VISIBLE) + val autoSkipTemporarilyDisabled = + !binding.player.sponsorBlockAutoSkip && sbSkipOption != SbSkipOptions.OFF if (sbSkipOption in arrayOf( SbSkipOptions.AUTOMATIC_ONCE, diff --git a/app/src/main/res/values/array.xml b/app/src/main/res/values/array.xml index b94643a236..fec9a09b9d 100644 --- a/app/src/main/res/values/array.xml +++ b/app/src/main/res/values/array.xml @@ -422,7 +422,6 @@ @string/off - @string/visible @string/manual @string/automatic @string/automatic_once @@ -430,7 +429,6 @@ off - visible manual automatic automatic_once