Fix issues in expanding comments when the expand button is hidden in CommentsRecyclerViewAdapterNew.

This commit is contained in:
Docile-Alligator
2026-07-27 17:39:38 -04:00
parent fc3bdd63b8
commit 0ead5fff09
3 changed files with 40 additions and 33 deletions

View File

@ -802,7 +802,7 @@ public class CommentsRecyclerViewAdapterNew extends ListAdapter<Comment, Recycle
}
public interface CommentRecyclerViewAdapterCallback {
void expandComment(int position);
boolean toggleExpandComment(int position);
void collapseComment(int position);
void fetchMoreChildComments(int position);
}
@ -1275,9 +1275,8 @@ public class CommentsRecyclerViewAdapterNew extends ListAdapter<Comment, Recycle
});
expandButton.setOnClickListener(view -> {
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<Comment, Recycle
}
}
private boolean expandComments() {
private void expandComments() {
expandButton.performClick();
return true;
}
private boolean hideToolbar() {
@ -1418,7 +1416,7 @@ public class CommentsRecyclerViewAdapterNew extends ListAdapter<Comment, Recycle
}
itemView.setOnClickListener(view -> {
mCommentRecyclerViewAdapterCallback.expandComment(getBindingAdapterPosition());
mCommentRecyclerViewAdapterCallback.toggleExpandComment(getBindingAdapterPosition());
});
itemView.setOnLongClickListener(view -> {

View File

@ -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

View File

@ -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<Comment>()
expandComment(it.children, newList)
val newList = ArrayList<Comment>()
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..<comments.size) {
@ -1129,18 +1132,24 @@ class ViewPostDetailFragmentViewModelNew(
}
}
val updatedComments = ArrayList(comments)
updatedComments[position] = updatedComment
if (allChildrenSize > 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) {