diff --git a/app/src/main/java/com/github/libretube/ui/adapters/PlaylistAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/PlaylistAdapter.kt index 67a7c92e7..c5510315c 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/PlaylistAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/PlaylistAdapter.kt @@ -31,8 +31,13 @@ class PlaylistAdapter( private val playlistType: PlaylistType ) : RecyclerView.Adapter() { + 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) { @@ -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) diff --git a/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt index 00decf927..0e44dfd25 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt @@ -36,13 +36,12 @@ class VideosAdapter( private val hideWatched: Boolean = false ) : RecyclerView.Adapter() { - 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) { diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt index b6783c949..20eb662d8 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt @@ -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() private var playlistAdapter: PlaylistAdapter? = null + private var nextPage: String? = null private var isLoading = true private var isBookmarked = false - private val playlistFeed = mutableListOf() + // 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