fix: duplicated comments (#4422)

This commit is contained in:
salahmak 2023-08-11 16:42:42 +01:00 committed by GitHub
parent d532344c77
commit 8230c7598a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -98,6 +98,11 @@ class CommentsMainFragment : Fragment() {
binding.errorTV.isVisible = true binding.errorTV.isVisible = true
return@observe 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( commentsAdapter.updateItems(
// only add the new comments to the recycler view // only add the new comments to the recycler view
it.comments.subList(commentsAdapter.itemCount, it.comments.size) it.comments.subList(commentsAdapter.itemCount, it.comments.size)

View File

@ -56,9 +56,13 @@ class CommentsViewModel : ViewModel() {
Log.e(TAG(), e.toString()) Log.e(TAG(), e.toString())
return@launch return@launch
} }
val updatedPage = commentsPage.value?.apply { val updatedPage = commentsPage.value?.apply {
comments += response.comments.filterNonEmptyComments() comments += response.comments
.filterNonEmptyComments()
.filter { comment -> comments.none { it.commentId == comment.commentId } }
} }
nextPage = response.nextpage nextPage = response.nextpage
commentsPage.postValue(updatedPage) commentsPage.postValue(updatedPage)
isLoading = false isLoading = false