Better Comment loading

Signed-off-by: Balazs Toldi <balazs@toldi.eu>
This commit is contained in:
Balazs Toldi 2023-07-23 21:46:43 +02:00
parent cbd6f0616b
commit ec6770a904
No known key found for this signature in database
GPG Key ID: 6C7D440036F99D58
3 changed files with 31 additions and 57 deletions

View File

@ -322,7 +322,7 @@ public class Comment implements Parcelable {
public void addChild(Comment comment) {
addChild(comment, 0);
childCount++;
//childCount++;
assertChildrenDepth();
}

View File

@ -25,7 +25,7 @@ public class FetchComment {
LemmyAPI api = retrofit.create(LemmyAPI.class);
Call<String> 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<String>() {

View File

@ -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<Integer> moreChildrenIds = new ArrayList<>();
ArrayList<Comment> newComments = new ArrayList<>();
Map<Integer, Comment> parsedComments = new HashMap<>();
List<Comment> orderedComments = new ArrayList<>();
List<Comment> 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<Comment> commentData;
@ -64,10 +82,6 @@ public class ParseComment {
});
}
private static void getChildrenCountRecursive(List<Comment> 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<Comment> 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<Comment> 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<Comment> comments, @NonNull Integer fullName) {
for (Comment comment : comments) {