From 8230c7598af91570480db0cf4afc220d86b9d3a6 Mon Sep 17 00:00:00 2001 From: salahmak Date: Fri, 11 Aug 2023 16:42:42 +0100 Subject: [PATCH] fix: duplicated comments (#4422) --- .../github/libretube/ui/fragments/CommentsMainFragment.kt | 5 +++++ .../com/github/libretube/ui/models/CommentsViewModel.kt | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/CommentsMainFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/CommentsMainFragment.kt index 5d2eaa5dc..6d8d9f394 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/CommentsMainFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/CommentsMainFragment.kt @@ -98,6 +98,11 @@ class CommentsMainFragment : Fragment() { binding.errorTV.isVisible = true return@observe } + + // sometimes the received comments have the same size as the existing ones + // which causes comments.subList to throw InvalidArgumentException + if (commentsAdapter.itemCount > it.comments.size) return@observe + commentsAdapter.updateItems( // only add the new comments to the recycler view it.comments.subList(commentsAdapter.itemCount, it.comments.size) diff --git a/app/src/main/java/com/github/libretube/ui/models/CommentsViewModel.kt b/app/src/main/java/com/github/libretube/ui/models/CommentsViewModel.kt index 8db9afdca..d084f75d6 100644 --- a/app/src/main/java/com/github/libretube/ui/models/CommentsViewModel.kt +++ b/app/src/main/java/com/github/libretube/ui/models/CommentsViewModel.kt @@ -56,9 +56,13 @@ class CommentsViewModel : ViewModel() { Log.e(TAG(), e.toString()) return@launch } + val updatedPage = commentsPage.value?.apply { - comments += response.comments.filterNonEmptyComments() + comments += response.comments + .filterNonEmptyComments() + .filter { comment -> comments.none { it.commentId == comment.commentId } } } + nextPage = response.nextpage commentsPage.postValue(updatedPage) isLoading = false