lazyload comments

This commit is contained in:
Bnyro 2022-05-20 17:45:16 +02:00
parent 4ba65043b1
commit d762b9e018

View File

@ -83,6 +83,7 @@ class PlayerFragment : Fragment() {
private lateinit var relatedRecView: RecyclerView private lateinit var relatedRecView: RecyclerView
private lateinit var commentsRecView: RecyclerView private lateinit var commentsRecView: RecyclerView
private var commentsAdapter: CommentsAdapter? = null private var commentsAdapter: CommentsAdapter? = null
private var commentsLoaded: Boolean? = false
private var nextPage: String? = null private var nextPage: String? = null
private var isLoading = true private var isLoading = true
private lateinit var exoPlayerView: StyledPlayerView private lateinit var exoPlayerView: StyledPlayerView
@ -210,6 +211,7 @@ class PlayerFragment : Fragment() {
view.findViewById<com.google.android.material.card.MaterialCardView>(R.id.comments_toggle).setOnClickListener { view.findViewById<com.google.android.material.card.MaterialCardView>(R.id.comments_toggle).setOnClickListener {
commentsRecView.visibility = if (commentsRecView.isVisible) View.GONE else View.VISIBLE commentsRecView.visibility = if (commentsRecView.isVisible) View.GONE else View.VISIBLE
relatedRecView.visibility = if (relatedRecView.isVisible) View.GONE else View.VISIBLE relatedRecView.visibility = if (relatedRecView.isVisible) View.GONE else View.VISIBLE
if (!commentsLoaded!!) fetchComments()
} }
// FullScreen button trigger // FullScreen button trigger
@ -308,18 +310,6 @@ class PlayerFragment : Fragment() {
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated return@launchWhenCreated
} }
val commentsResponse = try {
RetrofitInstance.api.getComments(videoId!!)
} catch (e: IOException) {
println(e)
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")
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
}
if (SponsorBlockSettings.sponsorBlockEnabled) { if (SponsorBlockSettings.sponsorBlockEnabled) {
val categories: ArrayList<String> = arrayListOf() val categories: ArrayList<String> = arrayListOf()
if (SponsorBlockSettings.introEnabled) { if (SponsorBlockSettings.introEnabled) {
@ -603,9 +593,6 @@ class PlayerFragment : Fragment() {
} }
} }
}) })
commentsAdapter = CommentsAdapter(commentsResponse.comments)
commentsRecView.adapter = commentsAdapter
nextPage = commentsResponse.nextpage
relatedRecView.adapter = TrendingAdapter(response.relatedStreams!!) relatedRecView.adapter = TrendingAdapter(response.relatedStreams!!)
view.findViewById<TextView>(R.id.player_description).text = view.findViewById<TextView>(R.id.player_description).text =
@ -867,6 +854,27 @@ class PlayerFragment : Fragment() {
super.onResume() super.onResume()
} }
private fun fetchComments() {
lifecycleScope.launchWhenCreated {
val commentsResponse = try {
RetrofitInstance.api.getComments(videoId!!)
} catch (e: IOException) {
println(e)
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")
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
}
commentsAdapter = CommentsAdapter(commentsResponse.comments)
commentsRecView.adapter = commentsAdapter
nextPage = commentsResponse.nextpage
commentsLoaded = true
}
}
private fun fetchNextComments() { private fun fetchNextComments() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
if (!isLoading) { if (!isLoading) {