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