mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-15 14:50:30 +05:30
Merge pull request #2596 from Bnyro/master
Only load 10 videos at once in private playlists
This commit is contained in:
commit
ee4a872661
@ -31,8 +31,13 @@ class PlaylistAdapter(
|
||||
private val playlistType: PlaylistType
|
||||
) : RecyclerView.Adapter<PlaylistViewHolder>() {
|
||||
|
||||
var visibleCount = minOf(20, videoFeed.size)
|
||||
|
||||
override fun getItemCount(): Int {
|
||||
return videoFeed.size
|
||||
return when (playlistType) {
|
||||
PlaylistType.PUBLIC -> videoFeed.size
|
||||
else -> visibleCount
|
||||
}
|
||||
}
|
||||
|
||||
fun updateItems(newItems: List<StreamItem>) {
|
||||
@ -41,6 +46,13 @@ class PlaylistAdapter(
|
||||
notifyItemRangeInserted(oldSize, videoFeed.size)
|
||||
}
|
||||
|
||||
fun showMoreItems() {
|
||||
val oldSize = visibleCount
|
||||
visibleCount += minOf(10, videoFeed.size - oldSize)
|
||||
if (visibleCount == oldSize) return
|
||||
notifyItemRangeInserted(oldSize, visibleCount)
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlaylistViewHolder {
|
||||
val layoutInflater = LayoutInflater.from(parent.context)
|
||||
val binding = VideoRowBinding.inflate(layoutInflater, parent, false)
|
||||
|
@ -36,13 +36,12 @@ class VideosAdapter(
|
||||
private val hideWatched: Boolean = false
|
||||
) : RecyclerView.Adapter<VideosViewHolder>() {
|
||||
|
||||
var index = 10
|
||||
private var visibleCount = minOf(10, streamItems.size)
|
||||
|
||||
override fun getItemCount(): Int {
|
||||
return when {
|
||||
showAllAtOnce -> streamItems.size
|
||||
index >= streamItems.size -> streamItems.size - 1
|
||||
else -> index
|
||||
else -> visibleCount
|
||||
}
|
||||
}
|
||||
|
||||
@ -51,9 +50,10 @@ class VideosAdapter(
|
||||
}
|
||||
|
||||
fun updateItems() {
|
||||
val oldSize = index
|
||||
index += 10
|
||||
notifyItemRangeInserted(oldSize, index)
|
||||
val oldSize = visibleCount
|
||||
visibleCount += minOf(10, streamItems.size - oldSize)
|
||||
if (visibleCount == oldSize) return
|
||||
notifyItemRangeInserted(oldSize, visibleCount)
|
||||
}
|
||||
|
||||
fun insertItems(newItems: List<StreamItem>) {
|
||||
|
@ -41,15 +41,19 @@ import retrofit2.HttpException
|
||||
class PlaylistFragment : BaseFragment() {
|
||||
private lateinit var binding: FragmentPlaylistBinding
|
||||
|
||||
// general playlist information
|
||||
private var playlistId: String? = null
|
||||
private var playlistName: String? = null
|
||||
private var playlistType: PlaylistType = PlaylistType.PUBLIC
|
||||
private var nextPage: String? = null
|
||||
|
||||
// runtime variables
|
||||
private val playlistFeed = mutableListOf<StreamItem>()
|
||||
private var playlistAdapter: PlaylistAdapter? = null
|
||||
private var nextPage: String? = null
|
||||
private var isLoading = true
|
||||
private var isBookmarked = false
|
||||
private val playlistFeed = mutableListOf<StreamItem>()
|
||||
|
||||
// view models
|
||||
private val playerViewModel: PlayerViewModel by activityViewModels()
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
@ -104,7 +108,6 @@ class PlaylistFragment : BaseFragment() {
|
||||
val response = try {
|
||||
PlaylistsHelper.getPlaylist(playlistId!!)
|
||||
} catch (e: IOException) {
|
||||
println(e)
|
||||
Log.e(TAG(), "IOException, you might not have internet connection")
|
||||
return@launchWhenCreated
|
||||
} catch (e: HttpException) {
|
||||
@ -217,17 +220,20 @@ class PlaylistFragment : BaseFragment() {
|
||||
if (binding.playlistScrollview.getChildAt(0).bottom
|
||||
== (binding.playlistScrollview.height + binding.playlistScrollview.scrollY)
|
||||
) {
|
||||
// scroll view is at bottom
|
||||
if (nextPage != null && !isLoading) {
|
||||
if (isLoading) return@addOnScrollChangedListener
|
||||
|
||||
// append more playlists to the recycler view
|
||||
if (playlistType != PlaylistType.PUBLIC) {
|
||||
isLoading = true
|
||||
playlistAdapter?.showMoreItems()
|
||||
isLoading = false
|
||||
} else {
|
||||
fetchNextPage()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* listener for swiping to the left or right
|
||||
*/
|
||||
// listener for swiping to the left or right
|
||||
if (playlistType != PlaylistType.PUBLIC) {
|
||||
val itemTouchCallback = object : ItemTouchHelper.SimpleCallback(
|
||||
0,
|
||||
@ -271,6 +277,9 @@ class PlaylistFragment : BaseFragment() {
|
||||
}
|
||||
|
||||
private fun fetchNextPage() {
|
||||
if (nextPage == null || isLoading) return
|
||||
isLoading = true
|
||||
|
||||
lifecycleScope.launchWhenCreated {
|
||||
val response = try {
|
||||
// load locally stored playlists with the auth api
|
||||
@ -289,6 +298,7 @@ class PlaylistFragment : BaseFragment() {
|
||||
Log.e(TAG(), e.toString())
|
||||
return@launchWhenCreated
|
||||
}
|
||||
|
||||
nextPage = response.nextpage
|
||||
playlistAdapter?.updateItems(response.relatedStreams!!)
|
||||
isLoading = false
|
||||
|
Loading…
Reference in New Issue
Block a user