From 08e9cfefff97c71596c6d0fe01e70aff2b0bea67 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 26 Nov 2022 18:07:54 +0100 Subject: [PATCH] only fetch the API the first time opening the comments --- .../libretube/ui/fragments/PlayerFragment.kt | 14 +++- .../libretube/ui/sheets/CommentsSheet.kt | 65 +++++++++---------- 2 files changed, 44 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index a22e6d6c5..4b9983b9f 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -37,6 +37,7 @@ import com.github.libretube.R import com.github.libretube.api.CronetHelper import com.github.libretube.api.RetrofitInstance 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.Segment import com.github.libretube.api.obj.SegmentData @@ -143,6 +144,8 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { private lateinit var trackSelector: DefaultTrackSelector private lateinit var segmentData: SegmentData private lateinit var chapters: List + private val comments: MutableList = mutableListOf() + private var commentsNextPage: String? = null /** * for the player view @@ -323,7 +326,10 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { } 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 @@ -705,7 +711,11 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { if (nextVideoId != null) { videoId = nextVideoId - // TODO Reset comments + // reset the comments to be reloaded later + comments.clear() + commentsNextPage = null + + // play the next video playVideo() } } diff --git a/app/src/main/java/com/github/libretube/ui/sheets/CommentsSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/CommentsSheet.kt index 1beadc6f3..d25f5a2ed 100644 --- a/app/src/main/java/com/github/libretube/ui/sheets/CommentsSheet.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/CommentsSheet.kt @@ -5,24 +5,23 @@ import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.Toast import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager -import com.github.libretube.R import com.github.libretube.api.RetrofitInstance +import com.github.libretube.api.obj.Comment import com.github.libretube.databinding.BottomSheetBinding import com.github.libretube.extensions.TAG import com.github.libretube.ui.adapters.CommentsAdapter -import retrofit2.HttpException -import java.io.IOException class CommentsSheet( - private val videoId: String + private val videoId: String, + private val comments: MutableList, + private var nextPage: String?, + private val onMoreComments: (comments: List, nextPage: String?) -> Unit ) : ExpandedBottomSheet() { private lateinit var binding: BottomSheetBinding private var commentsAdapter: CommentsAdapter? = null - private var nextPage: String? = null private var isLoading = true override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { @@ -43,47 +42,47 @@ class CommentsSheet( } } - fetchComments() + if (comments.isNotEmpty()) { + commentsAdapter = CommentsAdapter(videoId, comments) + } else { + fetchComments() + } + } + + private fun setCommentsAdapter(comments: MutableList) { + commentsAdapter = CommentsAdapter(videoId, comments) + binding.optionsRecycler.adapter = commentsAdapter } private fun fetchComments() { lifecycleScope.launchWhenCreated { - val commentsResponse = try { + val response = 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") + } catch (e: Exception) { + Log.e(TAG(), e.toString()) return@launchWhenCreated } - commentsAdapter = CommentsAdapter(videoId, commentsResponse.comments) - binding.optionsRecycler.adapter = commentsAdapter - nextPage = commentsResponse.nextpage + setCommentsAdapter(response.comments) + nextPage = response.nextpage + onMoreComments.invoke(response.comments, response.nextpage) isLoading = false } } private fun fetchNextComments() { lifecycleScope.launchWhenCreated { - if (!isLoading) { - isLoading = true - val response = try { - RetrofitInstance.api.getCommentsNextPage(videoId, nextPage!!) - } catch (e: IOException) { - println(e) - 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 - } - nextPage = response.nextpage - commentsAdapter?.updateItems(response.comments) - isLoading = false + if (isLoading || nextPage == null) return@launchWhenCreated + isLoading = true + val response = try { + RetrofitInstance.api.getCommentsNextPage(videoId, nextPage!!) + } catch (e: Exception) { + Log.e(TAG(), e.toString()) + return@launchWhenCreated } + nextPage = response.nextpage + commentsAdapter?.updateItems(response.comments) + onMoreComments.invoke(response.comments, response.nextpage) + isLoading = false } } }