only fetch the API the first time opening the comments

This commit is contained in:
Bnyro 2022-11-26 18:07:54 +01:00
parent 35602aaaca
commit 08e9cfefff
2 changed files with 44 additions and 35 deletions

View File

@ -37,6 +37,7 @@ import com.github.libretube.R
import com.github.libretube.api.CronetHelper import com.github.libretube.api.CronetHelper
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.ChapterSegment import com.github.libretube.api.obj.ChapterSegment
import com.github.libretube.api.obj.Comment
import com.github.libretube.api.obj.PipedStream import com.github.libretube.api.obj.PipedStream
import com.github.libretube.api.obj.Segment import com.github.libretube.api.obj.Segment
import com.github.libretube.api.obj.SegmentData import com.github.libretube.api.obj.SegmentData
@ -143,6 +144,8 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
private lateinit var trackSelector: DefaultTrackSelector private lateinit var trackSelector: DefaultTrackSelector
private lateinit var segmentData: SegmentData private lateinit var segmentData: SegmentData
private lateinit var chapters: List<ChapterSegment> private lateinit var chapters: List<ChapterSegment>
private val comments: MutableList<Comment> = mutableListOf()
private var commentsNextPage: String? = null
/** /**
* for the player view * for the player view
@ -323,7 +326,10 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
} }
binding.commentsToggle.setOnClickListener { binding.commentsToggle.setOnClickListener {
CommentsSheet(videoId!!).show(childFragmentManager) CommentsSheet(videoId!!, comments, commentsNextPage) { comments, nextPage ->
this.comments.addAll(comments)
this.commentsNextPage = nextPage
}.show(childFragmentManager)
} }
playerBinding.queueToggle.visibility = View.VISIBLE playerBinding.queueToggle.visibility = View.VISIBLE
@ -705,7 +711,11 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
if (nextVideoId != null) { if (nextVideoId != null) {
videoId = nextVideoId videoId = nextVideoId
// TODO Reset comments // reset the comments to be reloaded later
comments.clear()
commentsNextPage = null
// play the next video
playVideo() playVideo()
} }
} }

View File

@ -5,24 +5,23 @@ import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.Comment
import com.github.libretube.databinding.BottomSheetBinding import com.github.libretube.databinding.BottomSheetBinding
import com.github.libretube.extensions.TAG import com.github.libretube.extensions.TAG
import com.github.libretube.ui.adapters.CommentsAdapter import com.github.libretube.ui.adapters.CommentsAdapter
import retrofit2.HttpException
import java.io.IOException
class CommentsSheet( class CommentsSheet(
private val videoId: String private val videoId: String,
private val comments: MutableList<Comment>,
private var nextPage: String?,
private val onMoreComments: (comments: List<Comment>, nextPage: String?) -> Unit
) : ExpandedBottomSheet() { ) : ExpandedBottomSheet() {
private lateinit var binding: BottomSheetBinding private lateinit var binding: BottomSheetBinding
private var commentsAdapter: CommentsAdapter? = null private var commentsAdapter: CommentsAdapter? = null
private var nextPage: String? = null
private var isLoading = true private var isLoading = true
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
@ -43,47 +42,47 @@ class CommentsSheet(
} }
} }
if (comments.isNotEmpty()) {
commentsAdapter = CommentsAdapter(videoId, comments)
} else {
fetchComments() fetchComments()
} }
}
private fun setCommentsAdapter(comments: MutableList<Comment>) {
commentsAdapter = CommentsAdapter(videoId, comments)
binding.optionsRecycler.adapter = commentsAdapter
}
private fun fetchComments() { private fun fetchComments() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val commentsResponse = try { val response = try {
RetrofitInstance.api.getComments(videoId) RetrofitInstance.api.getComments(videoId)
} catch (e: IOException) { } catch (e: Exception) {
println(e) Log.e(TAG(), e.toString())
Log.e(TAG(), "IOException, you might not have internet connection")
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG(), "HttpException, unexpected response")
return@launchWhenCreated return@launchWhenCreated
} }
commentsAdapter = CommentsAdapter(videoId, commentsResponse.comments) setCommentsAdapter(response.comments)
binding.optionsRecycler.adapter = commentsAdapter nextPage = response.nextpage
nextPage = commentsResponse.nextpage onMoreComments.invoke(response.comments, response.nextpage)
isLoading = false isLoading = false
} }
} }
private fun fetchNextComments() { private fun fetchNextComments() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
if (!isLoading) { if (isLoading || nextPage == null) return@launchWhenCreated
isLoading = true isLoading = true
val response = try { val response = try {
RetrofitInstance.api.getCommentsNextPage(videoId, nextPage!!) RetrofitInstance.api.getCommentsNextPage(videoId, nextPage!!)
} catch (e: IOException) { } catch (e: Exception) {
println(e) Log.e(TAG(), e.toString())
Log.e(TAG(), "IOException, you might not have internet connection")
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG(), "HttpException, unexpected response," + e.response())
return@launchWhenCreated return@launchWhenCreated
} }
nextPage = response.nextpage nextPage = response.nextpage
commentsAdapter?.updateItems(response.comments) commentsAdapter?.updateItems(response.comments)
onMoreComments.invoke(response.comments, response.nextpage)
isLoading = false isLoading = false
} }
} }
}
} }