Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
59 changes: 59 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,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
*/
Expand All @@ -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
*/
Expand All @@ -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) }
}
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