aboutsummaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorSergei Kozelko <KozelkoS@yandex.ru>2022-11-25 13:13:37 +0000
committerGitHub <noreply@github.com>2022-11-25 13:13:37 +0000
commitd3ccaea2cc16fc0efe8cee7142cfaea10bc17135 (patch)
tree52f2ad3df9608fa87268d29464b1ffc1a59db3b1 /app
parent1abda7c6e391b66ec87d12e4e577a889ca531c2f (diff)
downloadinfinity-for-reddit-d3ccaea2cc16fc0efe8cee7142cfaea10bc17135.tar
infinity-for-reddit-d3ccaea2cc16fc0efe8cee7142cfaea10bc17135.tar.gz
infinity-for-reddit-d3ccaea2cc16fc0efe8cee7142cfaea10bc17135.tar.bz2
infinity-for-reddit-d3ccaea2cc16fc0efe8cee7142cfaea10bc17135.tar.lz
infinity-for-reddit-d3ccaea2cc16fc0efe8cee7142cfaea10bc17135.tar.xz
infinity-for-reddit-d3ccaea2cc16fc0efe8cee7142cfaea10bc17135.tar.zst
infinity-for-reddit-d3ccaea2cc16fc0efe8cee7142cfaea10bc17135.zip
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>
Diffstat (limited to 'app')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java8
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/comment/Comment.java20
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/comment/FetchComment.java17
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java22
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java8
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);