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) {