Merge pull request #1547 from Bnyro/master

Add a button to fetch more comment replies
This commit is contained in:
Bnyro 2022-10-13 14:11:22 +02:00 committed by GitHub
commit fa83e203f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 28 deletions

View File

@ -22,8 +22,6 @@ import com.github.libretube.util.NavigationHelper
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import retrofit2.HttpException
import java.io.IOException
class CommentsAdapter( class CommentsAdapter(
private val videoId: String, private val videoId: String,
@ -32,8 +30,7 @@ class CommentsAdapter(
) : RecyclerView.Adapter<CommentsViewHolder>() { ) : RecyclerView.Adapter<CommentsViewHolder>() {
private var isLoading = false private var isLoading = false
private var nextpage = "" private lateinit var repliesPage: CommentsPage
private var repliesPage = CommentsPage()
fun clear() { fun clear() {
val size: Int = comments.size val size: Int = comments.size
@ -68,33 +65,46 @@ class CommentsAdapter(
ImageHelper.loadImage(comment.thumbnail, commentorImage) ImageHelper.loadImage(comment.thumbnail, commentorImage)
likesTextView.text = comment.likeCount?.toLong().formatShort() likesTextView.text = comment.likeCount?.toLong().formatShort()
if (comment.verified == true) { if (comment.verified == true) verifiedImageView.visibility = View.VISIBLE
verifiedImageView.visibility = View.VISIBLE if (comment.pinned == true) pinnedImageView.visibility = View.VISIBLE
} if (comment.hearted == true) heartedImageView.visibility = View.VISIBLE
if (comment.pinned == true) { if (comment.repliesPage != null) commentsAvailable.visibility = View.VISIBLE
pinnedImageView.visibility = View.VISIBLE
}
if (comment.hearted == true) {
heartedImageView.visibility = View.VISIBLE
}
if (comment.repliesPage != null) {
commentsAvailable.visibility = View.VISIBLE
}
commentorImage.setOnClickListener { commentorImage.setOnClickListener {
NavigationHelper.navigateChannel(root.context, comment.commentorUrl) NavigationHelper.navigateChannel(root.context, comment.commentorUrl)
} }
repliesRecView.layoutManager = LinearLayoutManager(root.context) repliesRecView.layoutManager = LinearLayoutManager(root.context)
val repliesAdapter = CommentsAdapter(videoId, CommentsPage().comments, true) lateinit var repliesAdapter: CommentsAdapter
repliesAdapter = CommentsAdapter(
videoId,
mutableListOf(),
true
)
repliesRecView.adapter = repliesAdapter repliesRecView.adapter = repliesAdapter
if (!isRepliesAdapter && comment.repliesPage != null) { if (!isRepliesAdapter && comment.repliesPage != null) {
root.setOnClickListener { root.setOnClickListener {
when { when {
repliesAdapter.itemCount.equals(0) -> { repliesAdapter.itemCount.equals(0) -> {
nextpage = comment.repliesPage fetchReplies(comment.repliesPage) {
fetchReplies(nextpage, repliesAdapter) repliesAdapter.updateItems(it.comments)
if (repliesPage.nextpage == null) {
showMore.visibility = View.GONE
return@fetchReplies
}
showMore.visibility = View.VISIBLE
showMore.setOnClickListener {
if (repliesPage.nextpage == null) {
it.visibility = View.GONE
return@setOnClickListener
}
fetchReplies(
repliesPage.nextpage!!
) {
repliesAdapter.updateItems(repliesPage.comments)
}
}
}
} }
else -> repliesAdapter.clear() else -> repliesAdapter.clear()
} }
@ -106,6 +116,8 @@ class CommentsAdapter(
Toast.makeText(root.context, R.string.copied, Toast.LENGTH_SHORT).show() Toast.makeText(root.context, R.string.copied, Toast.LENGTH_SHORT).show()
true true
} }
// if (isRepliesAdapter && comments)
} }
} }
@ -113,19 +125,17 @@ class CommentsAdapter(
return comments.size return comments.size
} }
private fun fetchReplies(nextPage: String, repliesAdapter: CommentsAdapter) { private fun fetchReplies(nextPage: String, onFinished: (CommentsPage) -> Unit) {
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.Main).launch {
if (isLoading) return@launch if (isLoading) return@launch
isLoading = true isLoading = true
try { repliesPage = try {
repliesPage = RetrofitInstance.api.getCommentsNextPage(videoId, nextPage) RetrofitInstance.api.getCommentsNextPage(videoId, nextPage)
} catch (e: IOException) { } catch (e: Exception) {
println(e)
Log.e(TAG(), "IOException, you might not have internet connection") Log.e(TAG(), "IOException, you might not have internet connection")
} catch (e: HttpException) { return@launch
Log.e(TAG(), "HttpException, unexpected response," + e.response())
} }
repliesAdapter.updateItems(repliesPage.comments) onFinished.invoke(repliesPage)
isLoading = false isLoading = false
} }
} }

View File

@ -127,5 +127,19 @@
android:background="@null" android:background="@null"
android:nestedScrollingEnabled="false" /> android:nestedScrollingEnabled="false" />
<com.google.android.material.button.MaterialButton
android:visibility="gone"
android:id="@+id/show_more"
style="@style/Widget.Material3.Button.ElevatedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginVertical="8dp"
android:stateListAnimator="@null"
android:text="@string/show_more"
android:textColor="?android:attr/textColorPrimary"
android:textSize="12sp"
app:cornerRadius="20dp" />
</LinearLayout> </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -345,6 +345,7 @@
<string name="playlistNameReversed">Playlist name (reversed)</string> <string name="playlistNameReversed">Playlist name (reversed)</string>
<string name="recentlyUpdated">Recently updated</string> <string name="recentlyUpdated">Recently updated</string>
<string name="recentlyUpdatedReversed">Recently updated (reversed)</string> <string name="recentlyUpdatedReversed">Recently updated (reversed)</string>
<string name="show_more">Show more</string>
<!-- Notification channel strings --> <!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string> <string name="download_channel_name">Download Service</string>