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>
This commit is contained in:
Sergei Kozelko 2022-11-25 21:13:37 +08:00 committed by GitHub
parent 1abda7c6e3
commit d3ccaea2cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 27 deletions

View File

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

View File

@ -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() {

View File

@ -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();
}

View File

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

View File

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