From d3ccaea2cc16fc0efe8cee7142cfaea10bc17135 Mon Sep 17 00:00:00 2001 From: Sergei Kozelko Date: Fri, 25 Nov 2022 21:13:37 +0800 Subject: Store only first level replies as children (#1222) * Show the current image index in gallery in PostFragment. * Fixed issue where filters applied to same feed were behaving incorrectly (#1172) * Fix ItemTouchHelper and gallery swipe gesture fighting each other. * Show correct image in the gallery in ViewRedditGalleryActivity. * Swipe to view images in a gallery in Card Layout 2. * Fix gallery layout issues. * Remove `commentData` argument that is always an empty ArrayList * Return top level comments in addition to expanded comments after parsing Since 0f1c4d loading more comments loads not only first level replies, but also deeper comments. Because of this `expandedComments` can contain those deep replies if `expandChildren` is true. Adding `expandedComments` to parent causes a bug because parent's children are supposed to be only next level replies. Because of previously mentioned changes that is not true. Now expanding parent comment results in duplicate comments: one of them correctly comes from the parent of duplicated comment. The other one is shown because it is incorrectly stored in the parent of "load more comments" button. This comment separates top level comments (fist level replies) and expanded comments. `expandedComments` are still used for display, but only first level replies are added to the parent * Add debug assertion for children depth Co-authored-by: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Co-authored-by: Aidan223 <110802888+Aidan223@users.noreply.github.com> --- .../adapters/CommentsRecyclerViewAdapter.java | 8 +++++--- .../infinityforreddit/comment/Comment.java | 20 +++++++++++++++----- .../infinityforreddit/comment/FetchComment.java | 17 +++++++++++------ .../infinityforreddit/comment/ParseComment.java | 22 ++++++++++++---------- .../fragments/ViewPostDetailFragment.java | 8 +++++--- 5 files changed, 48 insertions(+), 27 deletions(-) (limited to 'app/src/main/java/ml/docilealligator') 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 55517b4e..4b2537e7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java @@ -631,7 +631,9 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter expandedComments, ArrayList moreChildrenIds) { + public void onFetchMoreCommentSuccess(ArrayList topLevelComments, + ArrayList expandedComments, + ArrayList moreChildrenIds) { if (mVisibleComments.size() > parentPosition && parentComment.getFullName().equals(mVisibleComments.get(parentPosition).getFullName())) { if (mVisibleComments.get(parentPosition).isExpanded()) { @@ -699,7 +701,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter children) { - this.children = children; - } - public void addChildren(ArrayList moreChildren) { if (children == null || children.size() == 0) { - setChildren(moreChildren); + children = moreChildren; } else { if (children.size() > 1 && children.get(children.size() - 1).placeholderType == PLACEHOLDER_LOAD_MORE_COMMENTS) { children.addAll(children.size() - 2, moreChildren); @@ -328,11 +325,13 @@ public class Comment implements Parcelable { } } childCount += moreChildren == null ? 0 : moreChildren.size(); + assertChildrenDepth(); } public void addChild(Comment comment) { addChild(comment, 0); childCount++; + assertChildrenDepth(); } public void addChild(Comment comment, int position) { @@ -340,6 +339,17 @@ public class Comment implements Parcelable { children = new ArrayList<>(); } children.add(position, comment); + assertChildrenDepth(); + } + + private void assertChildrenDepth() { + if (BuildConfig.DEBUG) { + for (Comment child: children) { + if (child.depth != depth + 1) { + throw new IllegalStateException("Child depth is not one more than parent depth"); + } + } + } } public ArrayList getMoreChildrenIds() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/FetchComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/FetchComment.java index a0d62086..85ecf185 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/FetchComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/FetchComment.java @@ -42,10 +42,11 @@ public class FetchComment { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { - ParseComment.parseComment(executor, handler, response.body(), new ArrayList<>(), + ParseComment.parseComment(executor, handler, response.body(), expandChildren, new ParseComment.ParseCommentListener() { @Override - public void onParseCommentSuccess(ArrayList expandedComments, + public void onParseCommentSuccess(ArrayList topLevelComments, + ArrayList expandedComments, String parentId, ArrayList moreChildrenIds) { fetchCommentListener.onFetchCommentSuccess(expandedComments, parentId, moreChildrenIds); @@ -97,12 +98,14 @@ public class FetchComment { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { - ParseComment.parseMoreComment(executor, handler, response.body(), new ArrayList<>(), + ParseComment.parseMoreComment(executor, handler, response.body(), expandChildren, new ParseComment.ParseCommentListener() { @Override - public void onParseCommentSuccess(ArrayList expandedComments, + public void onParseCommentSuccess(ArrayList topLevelComments, + ArrayList expandedComments, String parentId, ArrayList moreChildrenIds) { - fetchMoreCommentListener.onFetchMoreCommentSuccess(expandedComments, moreChildrenIds); + fetchMoreCommentListener.onFetchMoreCommentSuccess( + topLevelComments,expandedComments, moreChildrenIds); } @Override @@ -129,7 +132,9 @@ public class FetchComment { } public interface FetchMoreCommentListener { - void onFetchMoreCommentSuccess(ArrayList expandedComments, ArrayList moreChildrenIds); + void onFetchMoreCommentSuccess(ArrayList topLevelComments, + ArrayList expandedComments, + ArrayList moreChildrenIds); void onFetchMoreCommentFailed(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java index 62b87a1f..3dc73765 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java @@ -11,6 +11,7 @@ import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import org.checkerframework.checker.units.qual.A; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -24,7 +25,7 @@ import ml.docilealligator.infinityforreddit.utils.Utils; public class ParseComment { public static void parseComment(Executor executor, Handler handler, String response, - ArrayList commentData, boolean expandChildren, + boolean expandChildren, ParseCommentListener parseCommentListener) { executor.execute(() -> { try { @@ -40,13 +41,14 @@ public class ParseComment { parseCommentRecursion(childrenArray, newComments, moreChildrenIds, 0); expandChildren(newComments, expandedNewComments, expandChildren); + ArrayList commentData; if (expandChildren) { - commentData.addAll(expandedNewComments); + commentData = expandedNewComments; } else { - commentData.addAll(newComments); + commentData = newComments; } - handler.post(() -> parseCommentListener.onParseCommentSuccess(commentData, parentId, moreChildrenIds)); + handler.post(() -> parseCommentListener.onParseCommentSuccess(newComments, commentData, parentId, moreChildrenIds)); } catch (JSONException e) { e.printStackTrace(); handler.post(parseCommentListener::onParseCommentFailed); @@ -54,8 +56,7 @@ public class ParseComment { }); } - static void parseMoreComment(Executor executor, Handler handler, String response, - ArrayList commentData, boolean expandChildren, + static void parseMoreComment(Executor executor, Handler handler, String response, boolean expandChildren, ParseCommentListener parseCommentListener) { executor.execute(() -> { try { @@ -126,13 +127,14 @@ public class ParseComment { updateChildrenCount(newComments); expandChildren(newComments, expandedNewComments, expandChildren); + ArrayList commentData; if (expandChildren) { - commentData.addAll(expandedNewComments); + commentData = expandedNewComments; } else { - commentData.addAll(newComments); + commentData = newComments; } - handler.post(() -> parseCommentListener.onParseCommentSuccess(commentData, null, moreChildrenIds)); + handler.post(() -> parseCommentListener.onParseCommentSuccess(newComments, commentData, null, moreChildrenIds)); } catch (JSONException e) { e.printStackTrace(); handler.post(parseCommentListener::onParseCommentFailed); @@ -365,7 +367,7 @@ public class ParseComment { } public interface ParseCommentListener { - void onParseCommentSuccess(ArrayList expandedComments, String parentId, + void onParseCommentSuccess(ArrayList topLevelComments, ArrayList expandedComments, String parentId, ArrayList moreChildrenIds); void onParseCommentFailed(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java index fff5d62f..3196d705 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java @@ -1346,10 +1346,10 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic if (mRespectSubredditRecommendedSortType) { fetchCommentsRespectRecommendedSort(false); } else { - ParseComment.parseComment(mExecutor, new Handler(), response.body(), new ArrayList<>(), + ParseComment.parseComment(mExecutor, new Handler(), response.body(), mExpandChildren, new ParseComment.ParseCommentListener() { @Override - public void onParseCommentSuccess(ArrayList expandedComments, String parentId, ArrayList moreChildrenIds) { + public void onParseCommentSuccess(ArrayList topLevelComments, ArrayList expandedComments, String parentId, ArrayList moreChildrenIds) { ViewPostDetailFragment.this.children = moreChildrenIds; hasMoreChildren = children.size() != 0; @@ -1576,7 +1576,9 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic FetchComment.fetchMoreComment(mExecutor, new Handler(), retrofit, mAccessToken, children, mExpandChildren, mPost.getFullName(), sortType, new FetchComment.FetchMoreCommentListener() { @Override - public void onFetchMoreCommentSuccess(ArrayList expandedComments, ArrayList moreChildrenIds) { + public void onFetchMoreCommentSuccess(ArrayList topLevelComments, + ArrayList expandedComments, + ArrayList moreChildrenIds) { children = moreChildrenIds; hasMoreChildren = !children.isEmpty(); mCommentsAdapter.addComments(expandedComments, hasMoreChildren); -- cgit v1.2.3