From ec6770a9049718cde299d5c693c3ce75f01dd84c Mon Sep 17 00:00:00 2001 From: Balazs Toldi Date: Sun, 23 Jul 2023 21:46:43 +0200 Subject: [PATCH] Better Comment loading Signed-off-by: Balazs Toldi --- .../infinityforlemmy/comment/Comment.java | 2 +- .../comment/FetchComment.java | 2 +- .../comment/ParseComment.java | 84 +++++++------------ 3 files changed, 31 insertions(+), 57 deletions(-) diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/comment/Comment.java b/app/src/main/java/eu/toldi/infinityforlemmy/comment/Comment.java index 074a084c..e71bf13c 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/comment/Comment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/comment/Comment.java @@ -322,7 +322,7 @@ public class Comment implements Parcelable { public void addChild(Comment comment) { addChild(comment, 0); - childCount++; + //childCount++; assertChildrenDepth(); } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/comment/FetchComment.java b/app/src/main/java/eu/toldi/infinityforlemmy/comment/FetchComment.java index 9d0c87ae..d71f90a4 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/comment/FetchComment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/comment/FetchComment.java @@ -25,7 +25,7 @@ public class FetchComment { LemmyAPI api = retrofit.create(LemmyAPI.class); Call comments; - comments = api.getComments("All", sortType.value, 5, page, 25, null, null, article, commentId, false, accessToken); + comments = api.getComments("All", sortType.value, 8, page, 25, null, null, article, commentId, false, accessToken); comments.enqueue(new Callback() { diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/comment/ParseComment.java b/app/src/main/java/eu/toldi/infinityforlemmy/comment/ParseComment.java index 49ce9137..82fcd635 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/comment/ParseComment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/comment/ParseComment.java @@ -13,7 +13,9 @@ import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.concurrent.Executor; import java.util.regex.Pattern; @@ -34,19 +36,35 @@ public class ParseComment { ArrayList moreChildrenIds = new ArrayList<>(); ArrayList newComments = new ArrayList<>(); + + Map parsedComments = new HashMap<>(); + List orderedComments = new ArrayList<>(); + List topLevelComments = new ArrayList<>(); for (int i = 0; i < childrenArray.length(); i++) { Comment singleComment = parseSingleComment(childrenArray.getJSONObject(i)); - newComments.add(singleComment); - if (singleComment.getDepth() > 0) { - Comment parent = findDirectParent(newComments, singleComment); - moreChildrenIds.add(singleComment.getId()); - if (parent != null) - parent.addChild(singleComment); + orderedComments.add(singleComment); + parsedComments.put(singleComment.getId(), singleComment); + if (singleComment.getDepth() == 0) { + topLevelComments.add(singleComment); } } + for (int i = orderedComments.size() - 1; i >= 0; i--) { + Comment c = orderedComments.get(i); + //Add children to parent + if (c.getParentId() != null) { + Comment parent = parsedComments.get(c.getParentId()); + if (parent != null) { + parent.addChild(c); + } + } + } + + //Add all comments to newComments + for (int i = 0; i < topLevelComments.size(); i++) { + newComments.add(topLevelComments.get(i)); + } - //parseCommentRecursion(childrenArray, newComments, moreChildrenIds, 0); expandChildren(newComments, expandedNewComments, expandChildren); ArrayList commentData; @@ -64,10 +82,6 @@ public class ParseComment { }); } - private static void getChildrenCountRecursive(List commentList, Comment root) { - - } - static void parseMoreComment(Executor executor, Handler handler, String response, boolean expandChildren, ParseCommentListener parseCommentListener) { executor.execute(() -> { @@ -116,7 +130,7 @@ public class ParseComment { if (parentComment != null) { parentComment.setHasReply(true); parentComment.addChild(continueThreadPlaceholder, parentComment.getChildCount()); - parentComment.setChildCount(parentComment.getChildCount() + 1); + parentComment.setChildCount(parentComment.getChildCount()); } else { // assume that it is parent of this call newComments.add(continueThreadPlaceholder); @@ -130,7 +144,7 @@ public class ParseComment { if (parentComment != null) { parentComment.setHasReply(true); parentComment.addChild(comment, parentComment.getChildCount()); - parentComment.setChildCount(parentComment.getChildCount() + 1); + parentComment.setChildCount(parentComment.getChildCount()); } else { // assume that it is parent of this call newComments.add(comment); @@ -230,7 +244,7 @@ public class ParseComment { } else { c.setExpanded(true); } - if (c.hasMoreChildrenIds() && !c.getMoreChildrenIds().isEmpty()) { + if (c.getChildCount() > 0 && c.getChildCount() > getChildCount(c)) { //Add a load more placeholder Comment placeholder = new Comment(c.getFullName(), c.getDepth() + 1, Comment.PLACEHOLDER_LOAD_MORE_COMMENTS, c.getId()); visibleComments.add(placeholder); @@ -239,6 +253,7 @@ public class ParseComment { } } + public static Comment parseSingleComment(JSONObject jsonObject) throws JSONException { JSONObject commentObj = jsonObject.getJSONObject("comment"); JSONObject creatorObj = jsonObject.getJSONObject("creator"); @@ -316,47 +331,6 @@ public class ParseComment { return null; } - public static Comment findDirectParent(List commentList, Comment child) { - for (int i = 0; i < commentList.size(); i++) { - Comment result = findDirectParentRecursive(commentList.get(i), child); - if (result != null) - return result; - } - return null; - } - - public static Comment findDirectParentRecursive(Comment root, Comment child) { - // Base case: if root is null - if (root == null) { - return null; - } - - if (root.getId() == child.getParentId()) { - return root; - } - - // Check if any child of the root is the given child comment - List children = root.getChildren(); - if (children != null) { - for (Comment comment : children) { - if (comment.getId() == child.getId()) { - return root; - } - } - - // If the child comment is not an immediate child of the root, - // recursively call the function on the children of the root - for (Comment comment : children) { - Comment result = findDirectParentRecursive(comment, child); - if (result != null) { - return result; - } - } - } - - return null; - } - @Nullable private static Comment findCommentByFullName(@NonNull List comments, @NonNull Integer fullName) { for (Comment comment : comments) {