Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Simplify ContentFilter toggling #5544

Merged
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
44 changes: 25 additions & 19 deletions app/src/main/java/com/github/libretube/enums/ContentFilter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,32 @@
SHORTS,
LIVESTREAMS;

fun isEnabled() = enabledFiltersSet.contains(ordinal.toString())
var isEnabled
get() = name in enabledFiltersSet
set(enabled) {
val newFilters = enabledFiltersSet

Check failure on line 14 in app/src/main/java/com/github/libretube/enums/ContentFilter.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 A multiline expression should start on a new line Raw Output: app/src/main/java/com/github/libretube/enums/ContentFilter.kt:14:30: error: A multiline expression should start on a new line (standard:multiline-expression-wrapping)
.apply { if (enabled) add(name) else remove(name) }

fun setState(enabled: Boolean) {
val newFilters = enabledFiltersSet
.apply {if (enabled) add(ordinal.toString()) else remove(ordinal.toString()) }
.joinToString(",")

PreferenceHelper.putString(SELECTED_FEED_FILTERS, newFilters)
}
PreferenceHelper.putStringSet(SELECTED_FEED_FILTERS, newFilters)
}

companion object {

private val enabledFiltersSet get() = PreferenceHelper
.getString(
key = SELECTED_FEED_FILTERS,
defValue = entries.joinToString(",") { it.ordinal.toString() }
)
.split(',')
.toMutableSet()

private val enabledFiltersSet: MutableSet<String> get() {
val entryNames = try {

Check failure on line 22 in app/src/main/java/com/github/libretube/enums/ContentFilter.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 A multiline expression should start on a new line Raw Output: app/src/main/java/com/github/libretube/enums/ContentFilter.kt:22:30: error: A multiline expression should start on a new line (standard:multiline-expression-wrapping)
PreferenceHelper
.getStringSet(SELECTED_FEED_FILTERS, entries.mapTo(mutableSetOf()) { it.name })
} catch (e: ClassCastException) {
// TODO: Remove the conversion code below.
// Assume the old preference is present and convert it.
val string = PreferenceHelper.getString(SELECTED_FEED_FILTERS, "")
PreferenceHelper.remove(SELECTED_FEED_FILTERS)
val set = string.split(',')

Check failure on line 30 in app/src/main/java/com/github/libretube/enums/ContentFilter.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 A multiline expression should start on a new line Raw Output: app/src/main/java/com/github/libretube/enums/ContentFilter.kt:30:27: error: A multiline expression should start on a new line (standard:multiline-expression-wrapping)
.mapTo(mutableSetOf()) { entries[it.toInt()].name }
PreferenceHelper.putStringSet(SELECTED_FEED_FILTERS, set)
set
}

return entryNames.toMutableSet()
}
}

}
}
17 changes: 9 additions & 8 deletions app/src/main/java/com/github/libretube/helpers/BackupHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,19 @@ object BackupHelper {
is Float -> putFloat(key, value)
is Long -> putLong(key, value)
is Int -> {
when {
// we only use integers for SponsorBlock colors and the start fragment
key == PreferenceKeys.START_FRAGMENT || key.orEmpty().contains("_color") -> putInt(
key,
value
)
else -> putLong(key, value.toLong())
// we only use integers for SponsorBlock colors and the start fragment
if (key == PreferenceKeys.START_FRAGMENT || "_color" in key.orEmpty()) {
putInt(key, value)
} else {
putLong(key, value.toLong())
}
}

is String -> {
if (key == PreferenceKeys.HOME_TAB_CONTENT) {
if (
key == PreferenceKeys.HOME_TAB_CONTENT ||
key == PreferenceKeys.SELECTED_FEED_FILTERS
) {
putStringSet(key, value.split(",").toSet())
} else {
putString(key, value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@
settings.edit(commit = true) { putLong(key, value) }
}

fun putStringSet(key: String, value: Set<String>) {

Check failure on line 50 in app/src/main/java/com/github/libretube/helpers/PreferenceHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Newline expected after opening parenthesis Raw Output: app/src/main/java/com/github/libretube/helpers/PreferenceHelper.kt:50:22: error: Newline expected after opening parenthesis (standard:function-signature)

Check failure on line 50 in app/src/main/java/com/github/libretube/helpers/PreferenceHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Parameter should start on a newline Raw Output: app/src/main/java/com/github/libretube/helpers/PreferenceHelper.kt:50:35: error: Parameter should start on a newline (standard:function-signature)

Check failure on line 50 in app/src/main/java/com/github/libretube/helpers/PreferenceHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Newline expected before closing parenthesis Raw Output: app/src/main/java/com/github/libretube/helpers/PreferenceHelper.kt:50:53: error: Newline expected before closing parenthesis (standard:function-signature)
settings.edit(commit = true) { putStringSet(key, value) }
}

fun remove(key: String) {
settings.edit(commit = true) { remove(key) }
}

fun getString(key: String?, defValue: String): String {
return settings.getString(key, defValue) ?: defValue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment() {
val isVideo = !it.isShort && !it.isLive

return@filter when {
!ContentFilter.SHORTS.isEnabled() && it.isShort -> false
!ContentFilter.VIDEOS.isEnabled() && isVideo -> false
!ContentFilter.LIVESTREAMS.isEnabled() && it.isLive -> false
!ContentFilter.SHORTS.isEnabled && it.isShort -> false
!ContentFilter.VIDEOS.isEnabled && isVideo -> false
!ContentFilter.LIVESTREAMS.isEnabled && it.isLive -> false
else -> true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ class HomeViewModel: ViewModel() {
}

private suspend fun List<StreamItem>.filterWatched(): List<StreamItem> {
val allowShorts = ContentFilter.SHORTS.isEnabled()
val allowVideos = ContentFilter.VIDEOS.isEnabled()
val allowShorts = ContentFilter.SHORTS.isEnabled
val allowVideos = ContentFilter.VIDEOS.isEnabled
val allowAll = (!allowShorts && !allowVideos)

val filteredFeed = this.filter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ class FilterSortBottomSheet: ExpandedBottomSheet() {
}

private fun setInitialFiltersState() {
binding.filterVideos.isChecked = ContentFilter.VIDEOS.isEnabled()
binding.filterShorts.isChecked = ContentFilter.SHORTS.isEnabled()
binding.filterLivestreams.isChecked = ContentFilter.LIVESTREAMS.isEnabled()
binding.filterVideos.isChecked = ContentFilter.VIDEOS.isEnabled
binding.filterShorts.isChecked = ContentFilter.SHORTS.isEnabled
binding.filterLivestreams.isChecked = ContentFilter.LIVESTREAMS.isEnabled
binding.hideWatchedCheckbox.isChecked = hideWatched
}

private fun observeFiltersChanges() {
binding.filters.setOnCheckedStateChangeListener { _, _ ->
ContentFilter.VIDEOS.setState(binding.filterVideos.isChecked)
ContentFilter.SHORTS.setState(binding.filterShorts.isChecked)
ContentFilter.LIVESTREAMS.setState(binding.filterLivestreams.isChecked)
ContentFilter.VIDEOS.isEnabled = binding.filterVideos.isChecked
ContentFilter.SHORTS.isEnabled = binding.filterShorts.isChecked
ContentFilter.LIVESTREAMS.isEnabled = binding.filterLivestreams.isChecked
notifyChange()
}
}
Expand Down
Loading