Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/main/java/com/github/libretube/LibreTubeApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class LibreTubeApp : Application() {
* Initialize the [PreferenceHelper]
*/
PreferenceHelper.initialize(applicationContext)
PreferenceHelper.migrate()

/**
* Set the api and the auth api url
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.github.libretube.enums

enum class SbSkipOptions {
OFF,
VISIBLE,
MANUAL,
AUTOMATIC,
AUTOMATIC_ONCE
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/java/com/github/libretube/helpers/PreferenceHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ 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.BuildConfig
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

/**
* for normal preferences
*/
Expand All @@ -30,6 +37,32 @@ object PreferenceHelper {
authSettings = getAuthenticationPreferences(context)
}

/**
* Migrate preference to a new version
*/
fun migrate() {
val prefVersion = getInt(PreferenceKeys.PREFERENCE_VERSION, -1)
// check if there are any prefs to migrate
if (prefVersion == BuildConfig.VERSION_CODE)
return

if (prefVersion < 63) {
Log.i(TAG, "Migration to prefs v63")
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())
}
}
}

// mark as successfully migrated
putInt(PreferenceKeys.PREFERENCE_VERSION, BuildConfig.VERSION_CODE)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's better to use the version code here, or a separate constant, to avoid having the migrations run on every startup for builds where the version is not increased on every update, e.g. Debug/Nightly builds?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to manually increment the number, because not every version will have a preference migration, so it'd run the whole function body unnecessarily with each update even if there's no migration necessary.

Depending on how much we want to over-engineer it: In theory we could build a simple PreferenceMigration class similar to the room migrations and maintain a list of migrations numbered from 1 upwards, see https://github.com/libre-tube/LibreTube/blob/master/app/src/main/java/com/github/libretube/db/AppDatabase.kt. We could then have a PreferenceMigration(1, 2) { override fun onMigrate() {} }, that runs the migration.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented in c746647.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thank you!

}

fun putString(key: String, value: String) {
settings.edit(commit = true) { putString(key, value) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/res/values/array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -422,15 +422,13 @@

<string-array name="sb_skip_options">
<item>@string/off</item>
<item>@string/visible</item>
<item>@string/manual</item>
<item>@string/automatic</item>
<item>@string/automatic_once</item>
</string-array>

<string-array name="sb_skip_options_values">
<item>off</item>
<item>visible</item>
<item>manual</item>
<item>automatic</item>
<item>automatic_once</item>
Expand Down
Loading