Merge pull request #2596 from Bnyro/master

Only load 10 videos at once in private playlists
This commit is contained in:
Bnyro 2023-01-05 18:03:53 +01:00 committed by GitHub
commit ee4a872661
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 15 deletions

View File

@ -31,8 +31,13 @@ class PlaylistAdapter(
private val playlistType: PlaylistType private val playlistType: PlaylistType
) : RecyclerView.Adapter<PlaylistViewHolder>() { ) : RecyclerView.Adapter<PlaylistViewHolder>() {
var visibleCount = minOf(20, videoFeed.size)
override fun getItemCount(): Int { override fun getItemCount(): Int {
return videoFeed.size return when (playlistType) {
PlaylistType.PUBLIC -> videoFeed.size
else -> visibleCount
}
} }
fun updateItems(newItems: List<StreamItem>) { fun updateItems(newItems: List<StreamItem>) {
@ -41,6 +46,13 @@ class PlaylistAdapter(
notifyItemRangeInserted(oldSize, videoFeed.size) 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 { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlaylistViewHolder {
val layoutInflater = LayoutInflater.from(parent.context) val layoutInflater = LayoutInflater.from(parent.context)
val binding = VideoRowBinding.inflate(layoutInflater, parent, false) val binding = VideoRowBinding.inflate(layoutInflater, parent, false)

View File

@ -36,13 +36,12 @@ class VideosAdapter(
private val hideWatched: Boolean = false private val hideWatched: Boolean = false
) : RecyclerView.Adapter<VideosViewHolder>() { ) : RecyclerView.Adapter<VideosViewHolder>() {
var index = 10 private var visibleCount = minOf(10, streamItems.size)
override fun getItemCount(): Int { override fun getItemCount(): Int {
return when { return when {
showAllAtOnce -> streamItems.size showAllAtOnce -> streamItems.size
index >= streamItems.size -> streamItems.size - 1 else -> visibleCount
else -> index
} }
} }
@ -51,9 +50,10 @@ class VideosAdapter(
} }
fun updateItems() { fun updateItems() {
val oldSize = index val oldSize = visibleCount
index += 10 visibleCount += minOf(10, streamItems.size - oldSize)
notifyItemRangeInserted(oldSize, index) if (visibleCount == oldSize) return
notifyItemRangeInserted(oldSize, visibleCount)
} }
fun insertItems(newItems: List<StreamItem>) { fun insertItems(newItems: List<StreamItem>) {

View File

@ -41,15 +41,19 @@ import retrofit2.HttpException
class PlaylistFragment : BaseFragment() { class PlaylistFragment : BaseFragment() {
private lateinit var binding: FragmentPlaylistBinding private lateinit var binding: FragmentPlaylistBinding
// general playlist information
private var playlistId: String? = null private var playlistId: String? = null
private var playlistName: String? = null private var playlistName: String? = null
private var playlistType: PlaylistType = PlaylistType.PUBLIC 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 playlistAdapter: PlaylistAdapter? = null
private var nextPage: String? = null
private var isLoading = true private var isLoading = true
private var isBookmarked = false private var isBookmarked = false
private val playlistFeed = mutableListOf<StreamItem>()
// view models
private val playerViewModel: PlayerViewModel by activityViewModels() private val playerViewModel: PlayerViewModel by activityViewModels()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -104,7 +108,6 @@ class PlaylistFragment : BaseFragment() {
val response = try { val response = try {
PlaylistsHelper.getPlaylist(playlistId!!) PlaylistsHelper.getPlaylist(playlistId!!)
} catch (e: IOException) { } catch (e: IOException) {
println(e)
Log.e(TAG(), "IOException, you might not have internet connection") Log.e(TAG(), "IOException, you might not have internet connection")
return@launchWhenCreated return@launchWhenCreated
} catch (e: HttpException) { } catch (e: HttpException) {
@ -217,17 +220,20 @@ class PlaylistFragment : BaseFragment() {
if (binding.playlistScrollview.getChildAt(0).bottom if (binding.playlistScrollview.getChildAt(0).bottom
== (binding.playlistScrollview.height + binding.playlistScrollview.scrollY) == (binding.playlistScrollview.height + binding.playlistScrollview.scrollY)
) { ) {
// scroll view is at bottom if (isLoading) return@addOnScrollChangedListener
if (nextPage != null && !isLoading) {
// append more playlists to the recycler view
if (playlistType != PlaylistType.PUBLIC) {
isLoading = true isLoading = true
playlistAdapter?.showMoreItems()
isLoading = false
} else {
fetchNextPage() fetchNextPage()
} }
} }
} }
/** // listener for swiping to the left or right
* listener for swiping to the left or right
*/
if (playlistType != PlaylistType.PUBLIC) { if (playlistType != PlaylistType.PUBLIC) {
val itemTouchCallback = object : ItemTouchHelper.SimpleCallback( val itemTouchCallback = object : ItemTouchHelper.SimpleCallback(
0, 0,
@ -271,6 +277,9 @@ class PlaylistFragment : BaseFragment() {
} }
private fun fetchNextPage() { private fun fetchNextPage() {
if (nextPage == null || isLoading) return
isLoading = true
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
// load locally stored playlists with the auth api // load locally stored playlists with the auth api
@ -289,6 +298,7 @@ class PlaylistFragment : BaseFragment() {
Log.e(TAG(), e.toString()) Log.e(TAG(), e.toString())
return@launchWhenCreated return@launchWhenCreated
} }
nextPage = response.nextpage nextPage = response.nextpage
playlistAdapter?.updateItems(response.relatedStreams!!) playlistAdapter?.updateItems(response.relatedStreams!!)
isLoading = false isLoading = false