Skip to content

Commit

Permalink
fix: duplicated comments (#4422)
Browse files Browse the repository at this point in the history
  • Loading branch information
salahmak authored Aug 11, 2023
1 parent d532344 commit 8230c75
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8230c75

Please sign in to comment.