Merge pull request #2533 from Bnyro/master

Add shorts filter to the feed
This commit is contained in:
Bnyro 2022-12-28 16:49:31 +01:00 committed by GitHub
commit 28adf33cd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 29 deletions

View File

@ -26,7 +26,8 @@ class SubscriptionsFragment : BaseFragment() {
private val viewModel: SubscriptionsViewModel by activityViewModels()
private var subscriptionAdapter: VideosAdapter? = null
private var sortOrder = 0
private var selectedSortOrder = 0
private var selectedFilter = 0
override fun onCreateView(
inflater: LayoutInflater,
@ -82,7 +83,27 @@ class SubscriptionsFragment : BaseFragment() {
}
binding.sortTV.setOnClickListener {
showSortDialog()
val sortOptions = resources.getStringArray(R.array.sortOptions)
BaseBottomSheet().apply {
setSimpleItems(sortOptions.toList()) { index ->
binding.sortTV.text = sortOptions[index]
selectedSortOrder = index
showFeed()
}
}.show(childFragmentManager)
}
binding.filterTV.setOnClickListener {
val filterOptions = resources.getStringArray(R.array.filterOptions)
BaseBottomSheet().apply {
setSimpleItems(filterOptions.toList()) { index ->
binding.filterTV.text = filterOptions[index]
selectedFilter = index
showFeed()
}
}.show(childFragmentManager)
}
binding.toggleSubs.visibility = View.VISIBLE
@ -117,27 +138,21 @@ class SubscriptionsFragment : BaseFragment() {
}
}
private fun showSortDialog() {
val sortOptions = resources.getStringArray(R.array.sortOptions)
val bottomSheet = BaseBottomSheet().apply {
setSimpleItems(sortOptions.toList()) { index ->
binding.sortTV.text = sortOptions[index]
sortOrder = index
showFeed()
}
}
bottomSheet.show(childFragmentManager, null)
}
private fun showFeed() {
if (viewModel.videoFeed.value == null) return
binding.subRefresh.isRefreshing = false
val feed = viewModel.videoFeed.value!!
val feed = viewModel.videoFeed.value!!.filter {
// apply the selected filter
when (selectedFilter) {
0 -> true
1 -> !it.isShort
2 -> it.isShort
else -> throw IllegalArgumentException()
}
}
// sort the feed
val sortedFeed = when (sortOrder) {
val sortedFeed = when (selectedSortOrder) {
0 -> feed
1 -> feed.reversed()
2 -> feed.sortedBy { it.views }.reversed()
@ -148,7 +163,7 @@ class SubscriptionsFragment : BaseFragment() {
}.toMutableList()
// add an "all caught up item"
if (sortOrder == 0) {
if (selectedSortOrder == 0) {
val lastCheckedFeedTime = PreferenceHelper.getLastCheckedFeedTime()
val caughtUpIndex = feed.indexOfFirst {
(it.uploaded ?: 0L) / 1000 < lastCheckedFeedTime

View File

@ -114,17 +114,35 @@
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/sortTV"
android:layout_width="wrap_content"
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginBottom="5dp"
android:drawablePadding="5dp"
android:paddingHorizontal="10dp"
android:text="@string/most_recent"
android:textSize="16sp"
app:drawableEndCompat="@drawable/ic_arrow_downward" />
android:layout_marginBottom="5dp">
<TextView
android:id="@+id/filterTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginStart="5dp"
android:drawablePadding="5dp"
android:paddingHorizontal="10dp"
android:text="@string/all"
android:textSize="16sp"
app:drawableEndCompat="@drawable/ic_filter" />
<TextView
android:id="@+id/sortTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:drawablePadding="5dp"
android:paddingHorizontal="10dp"
android:text="@string/most_recent"
android:textSize="16sp"
app:drawableEndCompat="@drawable/ic_arrow_downward" />
</FrameLayout>
<RelativeLayout
android:layout_width="match_parent"

View File

@ -331,6 +331,12 @@
<item>@string/channel_name_za</item>
</string-array>
<string-array name="filterOptions">
<item>@string/all</item>
<item>@string/videos</item>
<item>@string/yt_shorts</item>
</string-array>
<string-array name="requiredNetwork">
<item>@string/network_all</item>
<item>@string/network_wifi</item>