From 0ead5fff0913f6c96a1b7ee526f5b3ff3480c381 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Mon, 27 Jul 2026 17:39:38 -0400 Subject: [PATCH] Fix issues in expanding comments when the expand button is hidden in CommentsRecyclerViewAdapterNew. --- .../CommentsRecyclerViewAdapterNew.java | 12 ++-- .../fragments/ViewPostDetailFragmentNew.java | 4 +- .../ViewPostDetailFragmentViewModelNew.kt | 57 +++++++++++-------- 3 files changed, 40 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapterNew.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapterNew.java index 4e3cb74c..dac8bb78 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapterNew.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapterNew.java @@ -802,7 +802,7 @@ public class CommentsRecyclerViewAdapterNew extends ListAdapter { - if (expandButton.getVisibility() == View.VISIBLE) { - mCommentRecyclerViewAdapterCallback.expandComment(getBindingAdapterPosition()); - } else if (mFullyCollapseComment) { + if (!mCommentRecyclerViewAdapterCallback.toggleExpandComment(getBindingAdapterPosition()) + && mFullyCollapseComment) { mCommentRecyclerViewAdapterCallback.collapseComment(getBindingAdapterPosition()); } }); @@ -1336,9 +1335,8 @@ public class CommentsRecyclerViewAdapterNew extends ListAdapter { - mCommentRecyclerViewAdapterCallback.expandComment(getBindingAdapterPosition()); + mCommentRecyclerViewAdapterCallback.toggleExpandComment(getBindingAdapterPosition()); }); itemView.setOnLongClickListener(view -> { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragmentNew.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragmentNew.java index 676d1f95..8449e187 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragmentNew.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragmentNew.java @@ -503,8 +503,8 @@ public class ViewPostDetailFragmentNew extends Fragment implements FragmentCommu mSharedPreferences, mNsfwAndSpoilerSharedPreferences, new CommentsRecyclerViewAdapterNew.CommentRecyclerViewAdapterCallback() { @Override - public void expandComment(int position) { - viewPostDetailFragmentViewModel.expandComment(position); + public boolean toggleExpandComment(int position) { + return viewPostDetailFragmentViewModel.toggleExpandComment(position); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/viewmodels/ViewPostDetailFragmentViewModelNew.kt b/app/src/main/java/ml/docilealligator/infinityforreddit/viewmodels/ViewPostDetailFragmentViewModelNew.kt index 90bacf0d..1675112c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/viewmodels/ViewPostDetailFragmentViewModelNew.kt +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/viewmodels/ViewPostDetailFragmentViewModelNew.kt @@ -1074,29 +1074,35 @@ class ViewPostDetailFragmentViewModelNew( refresh(fetchPost = false, fetchComments = true) } - fun expandComment(position: Int) { + fun toggleExpandComment(position: Int): Boolean { _dataState.value.comments?.let { comments -> val comment = comments.getOrNull(position) comment?.let { if (it.isExpanded) { - collapseComment(position) + return collapseComment(position) } else { - val updatedComment = Comment(it) - updatedComment.setExpanded(true) + if (!it.children.isNullOrEmpty()) { + val newList = ArrayList() + expandComment(it.children, newList) - val newList = ArrayList() - expandComment(it.children, newList) + val updatedComment = Comment(it) + updatedComment.setExpanded(true) - val updatedComments = ArrayList(comments) - updatedComments[position] = updatedComment - updatedComments.addAll(position + 1, newList) + val updatedComments = ArrayList(comments) + updatedComments[position] = updatedComment + updatedComments.addAll(position + 1, newList) - _dataState.value = _dataState.value.copy( - comments = updatedComments - ) + _dataState.value = _dataState.value.copy( + comments = updatedComments + ) + + return true + } } } } + + return false } private fun expandComment( @@ -1112,13 +1118,10 @@ class ViewPostDetailFragmentViewModelNew( } } - fun collapseComment(position: Int) { + fun collapseComment(position: Int): Boolean { _dataState.value.comments?.let { comments -> val comment = comments.getOrNull(position) comment?.let { - val updatedComment = Comment(it) - updatedComment.setExpanded(false) - val depth: Int = it.depth var allChildrenSize = 0 for (i in position + 1.. 0) { - updatedComments.subList(position + 1, position + 1 + allChildrenSize).clear() - } + val updatedComment = Comment(it) + updatedComment.setExpanded(false) - _dataState.value = _dataState.value.copy( - comments = updatedComments - ) + val updatedComments = ArrayList(comments) + updatedComments[position] = updatedComment + updatedComments.subList(position + 1, position + 1 + allChildrenSize).clear() + + _dataState.value = _dataState.value.copy( + comments = updatedComments + ) + + return true + } } } + + return false } fun addComment(comment: Comment) {