Merge pull request #2914 from Isira-Seneviratne/take

Use take() extension.
This commit is contained in:
Bnyro 2023-01-30 16:20:12 +01:00 committed by GitHub
commit a293480bac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 11 deletions

View File

@ -1,5 +0,0 @@
package com.github.libretube.ui.extensions
fun <T> List<T>.withMaxSize(maxSize: Int): List<T> {
return this.filterIndexed { index, _ -> index < maxSize }
}

View File

@ -22,7 +22,6 @@ import com.github.libretube.ui.adapters.PlaylistBookmarkAdapter
import com.github.libretube.ui.adapters.PlaylistsAdapter import com.github.libretube.ui.adapters.PlaylistsAdapter
import com.github.libretube.ui.adapters.VideosAdapter import com.github.libretube.ui.adapters.VideosAdapter
import com.github.libretube.ui.base.BaseFragment import com.github.libretube.ui.base.BaseFragment
import com.github.libretube.ui.extensions.withMaxSize
import com.github.libretube.util.LocaleHelper import com.github.libretube.util.LocaleHelper
import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -73,7 +72,7 @@ class HomeFragment : BaseFragment() {
private suspend fun fetchHome() { private suspend fun fetchHome() {
runOrError { runOrError {
val feed = SubscriptionHelper.getFeed().withMaxSize(20) val feed = SubscriptionHelper.getFeed().take(20)
if (feed.isEmpty()) return@runOrError if (feed.isEmpty()) return@runOrError
runOnUiThread { runOnUiThread {
makeVisible(binding.featuredRV, binding.featuredTV) makeVisible(binding.featuredRV, binding.featuredTV)
@ -90,9 +89,8 @@ class HomeFragment : BaseFragment() {
} }
runOrError { runOrError {
val trending = RetrofitInstance.api.getTrending( val region = LocaleHelper.getTrendingRegion(requireContext())
LocaleHelper.getTrendingRegion(requireContext()) val trending = RetrofitInstance.api.getTrending(region).take(10)
).withMaxSize(10)
if (trending.isEmpty()) return@runOrError if (trending.isEmpty()) return@runOrError
runOnUiThread { runOnUiThread {
makeVisible(binding.trendingRV, binding.trendingTV) makeVisible(binding.trendingRV, binding.trendingTV)
@ -105,7 +103,7 @@ class HomeFragment : BaseFragment() {
} }
runOrError { runOrError {
val playlists = PlaylistsHelper.getPlaylists().withMaxSize(20) val playlists = PlaylistsHelper.getPlaylists().take(20)
if (playlists.isEmpty()) return@runOrError if (playlists.isEmpty()) return@runOrError
runOnUiThread { runOnUiThread {
makeVisible(binding.playlistsRV, binding.playlistsTV) makeVisible(binding.playlistsRV, binding.playlistsTV)