Skip to content

Commit

Permalink
Merge pull request #5772 from Bnyro/master
Browse files Browse the repository at this point in the history
fix: respect feed filter on home tab
  • Loading branch information
Bnyro authored Mar 19, 2024
2 parents 4274162 + 2332714 commit 30a8166
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
23 changes: 23 additions & 0 deletions app/src/main/java/com/github/libretube/db/DatabaseHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.db.DatabaseHolder.Database
import com.github.libretube.db.obj.SearchHistoryItem
import com.github.libretube.db.obj.WatchHistoryItem
import com.github.libretube.enums.ContentFilter
import com.github.libretube.extensions.toID
import com.github.libretube.helpers.PreferenceHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlinx.datetime.Instant
import kotlinx.datetime.TimeZone
Expand Down Expand Up @@ -92,4 +94,25 @@ object DatabaseHelper {
}
}
}

fun filterByStatusAndWatchPosition(streams: List<StreamItem>, hideWatched: Boolean): List<StreamItem> {
val streamItems = streams.filter {
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
else -> true
}
}

return if (hideWatched) {
runBlocking {
filterUnwatched(streamItems)
}
} else {
streamItems
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import com.github.libretube.R
import com.github.libretube.api.PlaylistsHelper
import com.github.libretube.api.obj.Playlists
import com.github.libretube.api.obj.StreamItem
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.constants.PreferenceKeys.HOME_TAB_CONTENT
import com.github.libretube.databinding.FragmentHomeBinding
import com.github.libretube.db.DatabaseHelper
import com.github.libretube.db.obj.PlaylistBookmark
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.ui.activities.SettingsActivity
Expand Down Expand Up @@ -154,7 +156,12 @@ class HomeFragment : Fragment() {
if (streamItems == null) return

makeVisible(binding.featuredRV, binding.featuredTV)
val feedVideos = streamItems.take(20).toMutableList()
val hideWatched = PreferenceHelper.getBoolean(PreferenceKeys.HIDE_WATCHED_FROM_FEED, false)
val feedVideos = streamItems
.let { DatabaseHelper.filterByStatusAndWatchPosition(it, hideWatched) }
.take(20)
.toMutableList()

with(binding.featuredRV) {
layoutManager = LinearLayoutManager(context, HORIZONTAL, false)
adapter = VideosAdapter(feedVideos, forceMode = LayoutMode.RELATED_COLUMN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.databinding.FragmentSubscriptionsBinding
import com.github.libretube.db.DatabaseHelper
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.enums.ContentFilter
import com.github.libretube.extensions.dpToPx
import com.github.libretube.extensions.formatShort
import com.github.libretube.extensions.toID
Expand All @@ -46,7 +45,6 @@ import com.github.libretube.util.PlayingQueue
import com.google.android.material.chip.Chip
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

class SubscriptionsFragment : DynamicLayoutManagerFragment() {
private var _binding: FragmentSubscriptionsBinding? = null
Expand Down Expand Up @@ -226,7 +224,7 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment() {
private fun playByGroup(groupIndex: Int) {
val streams = viewModel.videoFeed.value.orEmpty()
.filterByGroup(groupIndex)
.filterByStatusAndWatchPosition()
.let { DatabaseHelper.filterByStatusAndWatchPosition(it, hideWatched) }
.sortedBySelectedOrder()

if (streams.isEmpty()) return
Expand Down Expand Up @@ -291,27 +289,6 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment() {
return filter { group?.channels?.contains(it.url.toID()) != false }
}

private fun List<StreamItem>.filterByStatusAndWatchPosition(): List<StreamItem> {
val streamItems = this.filter {
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
else -> true
}
}

return if (hideWatched) {
runBlocking {
DatabaseHelper.filterUnwatched(streamItems)
}
} else {
streamItems
}
}

private fun List<StreamItem>.sortedBySelectedOrder() = when (selectedSortOrder) {
0 -> this
1 -> this.reversed()
Expand All @@ -328,7 +305,9 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment() {
binding.subRefresh.isRefreshing = false
val feed = videoFeed
.filterByGroup(selectedFilterGroup)
.filterByStatusAndWatchPosition()
.let {
DatabaseHelper.filterByStatusAndWatchPosition(it, hideWatched)
}

val sortedFeed = feed
.sortedBySelectedOrder()
Expand Down

0 comments on commit 30a8166

Please sign in to comment.