From d11fb884c26c739e9e919ec98c1a0990f0953c93 Mon Sep 17 00:00:00 2001 From: Sergei Kozelko Date: Sun, 16 Oct 2022 16:06:10 +0700 Subject: [PATCH] Fix comment expanding logic (#1155) Child comment expanding was broken because it did not take into account children of children of children and deeper levels of comments when calculating new comment's position. Replaced with a simple tree to list conversion in pre-order. --- .../adapters/CommentsRecyclerViewAdapter.java | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java index aee4be21..feaedc98 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java @@ -802,16 +802,12 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter comments, ArrayList newList, int position) { + private void expandChildren(ArrayList comments, ArrayList newList) { if (comments != null && comments.size() > 0) { - newList.addAll(position, comments); - for (int i = 0; i < comments.size(); i++) { - position++; - if (comments.get(i).getChildren() != null && comments.get(i).getChildren().size() > 0) { - expandChildren(comments.get(i).getChildren(), newList, position); - position = position + comments.get(i).getChildren().size(); - } - comments.get(i).setExpanded(true); + for (Comment comment : comments) { + newList.add(comment); + expandChildren(comment.getChildren(), newList); + comment.setExpanded(true); } } } @@ -909,7 +905,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter newList = new ArrayList<>(); - expandChildren(mVisibleComments.get(parentPosition).getChildren(), newList, 0); + expandChildren(mVisibleComments.get(parentPosition).getChildren(), newList); mVisibleComments.get(parentPosition).setExpanded(true); mVisibleComments.addAll(parentPosition + 1, newList); if (mIsSingleCommentThreadMode) { @@ -1572,7 +1568,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter newList = new ArrayList<>(); - expandChildren(mVisibleComments.get(commentPosition).getChildren(), newList, 0); + expandChildren(mVisibleComments.get(commentPosition).getChildren(), newList); mVisibleComments.get(commentPosition).setExpanded(true); mVisibleComments.addAll(commentPosition + 1, newList); @@ -1740,7 +1736,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter newList = new ArrayList<>(); - expandChildren(mVisibleComments.get(commentPosition).getChildren(), newList, 0); + expandChildren(mVisibleComments.get(commentPosition).getChildren(), newList); mVisibleComments.get(commentPosition).setExpanded(true); mVisibleComments.addAll(commentPosition + 1, newList);