diff options
Diffstat (limited to 'app/src/main/java/ml/docilealligator/infinityforreddit')
5 files changed, 48 insertions, 27 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 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<RecyclerVi parentComment.getMoreChildrenIds(), mExpandChildren, mPost.getFullName(), sortType, new FetchComment.FetchMoreCommentListener() { @Override - public void onFetchMoreCommentSuccess(ArrayList<Comment> expandedComments, ArrayList<String> moreChildrenIds) { + public void onFetchMoreCommentSuccess(ArrayList<Comment> topLevelComments, + ArrayList<Comment> expandedComments, + ArrayList<String> 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<RecyclerVi } } - mVisibleComments.get(parentPosition).addChildren(expandedComments); + mVisibleComments.get(parentPosition).addChildren(topLevelComments); if (mIsSingleCommentThreadMode) { notifyItemChanged(parentPosition + 1); } else { @@ -736,7 +738,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi .setLoadingMoreChildren(false); mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1) .setLoadMoreChildrenFailed(false); - mVisibleComments.get(i).addChildren(expandedComments); + mVisibleComments.get(i).addChildren(topLevelComments); if (mIsSingleCommentThreadMode) { notifyItemChanged(i + 1); } else { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/Comment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/Comment.java index d6a6f173..fd06e0e7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/Comment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/Comment.java @@ -5,6 +5,7 @@ import android.os.Parcelable; import java.util.ArrayList; +import ml.docilealligator.infinityforreddit.BuildConfig; import ml.docilealligator.infinityforreddit.utils.APIUtils; public class Comment implements Parcelable { @@ -313,13 +314,9 @@ public class Comment implements Parcelable { return children; } - public void setChildren(ArrayList<Comment> children) { - this.children = children; - } - public void addChildren(ArrayList<Comment> 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<String> 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<String> call, @NonNull Response<String> 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<Comment> expandedComments, + public void onParseCommentSuccess(ArrayList<Comment> topLevelComments, + ArrayList<Comment> expandedComments, String parentId, ArrayList<String> moreChildrenIds) { fetchCommentListener.onFetchCommentSuccess(expandedComments, parentId, moreChildrenIds); @@ -97,12 +98,14 @@ public class FetchComment { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> 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<Comment> expandedComments, + public void onParseCommentSuccess(ArrayList<Comment> topLevelComments, + ArrayList<Comment> expandedComments, String parentId, ArrayList<String> moreChildrenIds) { - fetchMoreCommentListener.onFetchMoreCommentSuccess(expandedComments, moreChildrenIds); + fetchMoreCommentListener.onFetchMoreCommentSuccess( + topLevelComments,expandedComments, moreChildrenIds); } @Override @@ -129,7 +132,9 @@ public class FetchComment { } public interface FetchMoreCommentListener { - void onFetchMoreCommentSuccess(ArrayList<Comment> expandedComments, ArrayList<String> moreChildrenIds); + void onFetchMoreCommentSuccess(ArrayList<Comment> topLevelComments, + ArrayList<Comment> expandedComments, + ArrayList<String> 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<Comment> 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<Comment> 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<Comment> 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<Comment> 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<Comment> expandedComments, String parentId, + void onParseCommentSuccess(ArrayList<Comment> topLevelComments, ArrayList<Comment> expandedComments, String parentId, ArrayList<String> 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<Comment> expandedComments, String parentId, ArrayList<String> moreChildrenIds) { + public void onParseCommentSuccess(ArrayList<Comment> topLevelComments, ArrayList<Comment> expandedComments, String parentId, ArrayList<String> 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<Comment> expandedComments, ArrayList<String> moreChildrenIds) { + public void onFetchMoreCommentSuccess(ArrayList<Comment> topLevelComments, + ArrayList<Comment> expandedComments, + ArrayList<String> moreChildrenIds) { children = moreChildrenIds; hasMoreChildren = !children.isEmpty(); mCommentsAdapter.addComments(expandedComments, hasMoreChildren); |