Skip to content

Commit b908dbd

Browse files
authored
Merge pull request libre-tube#7434 from FineFindus/feat/remove-sb-view-option
feat(Preferences/SponsorBlock): drop `VISIBLE` option
2 parents b586d53 + c746647 commit b908dbd

6 files changed

Lines changed: 63 additions & 5 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/enums/SbSkipOptions.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.github.libretube.enums
22

33
enum class SbSkipOptions {
44
OFF,
5-
VISIBLE,
65
MANUAL,
76
AUTOMATIC,
87
AUTOMATIC_ONCE

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@ 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.LibreTubeApp
9+
import com.github.libretube.R
710
import com.github.libretube.constants.PreferenceKeys
11+
import com.github.libretube.enums.SbSkipOptions
812

913
object PreferenceHelper {
14+
private val TAG = PreferenceHelper::class.simpleName
15+
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+
1023
/**
1124
* for normal preferences
1225
*/
@@ -22,6 +35,29 @@ object PreferenceHelper {
2235
*/
2336
private const val USER_ID_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
2437

38+
39+
/**
40+
* Current version of the preferences.
41+
*/
42+
private const val PREFERENCE_VERSION = 1
43+
44+
/**
45+
* Migrations required to migrate the application to a newer [PREFERENCE_VERSION].
46+
*/
47+
private val MIGRATIONS = listOf(
48+
PreferenceMigration(0, 1) {
49+
LibreTubeApp.instance.resources
50+
.getStringArray(R.array.sponsorBlockSegments)
51+
.forEach { category ->
52+
val key = "${category}_category"
53+
val stored = getString(key, "visible")
54+
if (stored == "visible") {
55+
putString(key, SbSkipOptions.MANUAL.name.lowercase())
56+
}
57+
}
58+
},
59+
)
60+
2561
/**
2662
* set the context that is being used to access the shared preferences
2763
*/
@@ -30,6 +66,29 @@ object PreferenceHelper {
3066
authSettings = getAuthenticationPreferences(context)
3167
}
3268

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)
88+
}
89+
90+
}
91+
3392
fun putString(key: String, value: String) {
3493
settings.edit(commit = true) { putString(key, value) }
3594
}

app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,8 +1013,8 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions {
10131013
if (segmentData != null && commonPlayerViewModel.isMiniPlayerVisible.value != true) {
10141014
val (segment, sbSkipOption) = segmentData
10151015

1016-
val autoSkipTemporarilyDisabled = !binding.player.sponsorBlockAutoSkip &&
1017-
sbSkipOption !in arrayOf(SbSkipOptions.OFF, SbSkipOptions.VISIBLE)
1016+
val autoSkipTemporarilyDisabled =
1017+
!binding.player.sponsorBlockAutoSkip && sbSkipOption != SbSkipOptions.OFF
10181018

10191019
if (sbSkipOption in arrayOf(
10201020
SbSkipOptions.AUTOMATIC_ONCE,

app/src/main/res/values/array.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,15 +422,13 @@
422422

423423
<string-array name="sb_skip_options">
424424
<item>@string/off</item>
425-
<item>@string/visible</item>
426425
<item>@string/manual</item>
427426
<item>@string/automatic</item>
428427
<item>@string/automatic_once</item>
429428
</string-array>
430429

431430
<string-array name="sb_skip_options_values">
432431
<item>off</item>
433-
<item>visible</item>
434432
<item>manual</item>
435433
<item>automatic</item>
436434
<item>automatic_once</item>

0 commit comments

Comments
 (0)