From 258ff290bbe1c86b9ea262974bfcf3ca7d95f7bc Mon Sep 17 00:00:00 2001 From: Balazs Toldi Date: Fri, 21 Jul 2023 22:04:04 +0200 Subject: [PATCH] Bug fixes + Buggy comment loading Signed-off-by: Balazs Toldi --- .../eu/toldi/infinityforlemmy/SortType.java | 2 +- .../eu/toldi/infinityforlemmy/VoteThing.java | 83 ++++-- .../activities/ViewPostDetailActivity.java | 16 -- .../activities/ViewUserDetailActivity.java | 35 +-- .../CommentsListingRecyclerViewAdapter.java | 83 ++---- .../adapters/CommentsRecyclerViewAdapter.java | 87 +++--- .../HistoryPostRecyclerViewAdapter.java | 12 +- .../PostDetailRecyclerViewAdapter.java | 42 +-- .../adapters/PostRecyclerViewAdapter.java | 12 +- .../toldi/infinityforlemmy/apis/LemmyAPI.java | 24 +- .../CommentMoreBottomSheetFragment.java | 2 +- .../infinityforlemmy/comment/Comment.java | 122 ++++----- .../comment/CommentDataSource.java | 2 +- .../comment/FetchComment.java | 43 ++- .../comment/FetchRemovedCommentReveddit.java | 2 +- .../comment/ParseComment.java | 248 ++++++++++-------- .../infinityforlemmy/dto/CommentVoteDTO.java | 25 ++ .../dto/{VoteDTO.java => PostVoteDTO.java} | 4 +- .../fragments/CommentsListingFragment.java | 5 - .../fragments/ViewPostDetailFragment.java | 79 ++---- .../eu/toldi/infinityforlemmy/post/Post.java | 6 +- app/src/main/res/values/strings.xml | 2 +- 22 files changed, 458 insertions(+), 478 deletions(-) create mode 100644 app/src/main/java/eu/toldi/infinityforlemmy/dto/CommentVoteDTO.java rename app/src/main/java/eu/toldi/infinityforlemmy/dto/{VoteDTO.java => PostVoteDTO.java} (82%) diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/SortType.java b/app/src/main/java/eu/toldi/infinityforlemmy/SortType.java index 47eaf1c2..9b7f4acf 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/SortType.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/SortType.java @@ -35,7 +35,7 @@ public class SortType { NEW("New", "New"), RANDOM("random", "Random"), RISING("rising", "Rising"), - TOP("top", "Top"), + TOP("Top", "Top"), CONTROVERSIAL("controversial", "Controversial"), RELEVANCE("relevance", "Relevance"), COMMENTS("comments", "Comments"), diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/VoteThing.java b/app/src/main/java/eu/toldi/infinityforlemmy/VoteThing.java index fb4c25aa..831cdf1f 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/VoteThing.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/VoteThing.java @@ -5,13 +5,9 @@ import android.widget.Toast; import androidx.annotation.NonNull; -import java.util.HashMap; -import java.util.Map; - import eu.toldi.infinityforlemmy.apis.LemmyAPI; -import eu.toldi.infinityforlemmy.apis.RedditAPI; -import eu.toldi.infinityforlemmy.dto.VoteDTO; -import eu.toldi.infinityforlemmy.utils.APIUtils; +import eu.toldi.infinityforlemmy.dto.CommentVoteDTO; +import eu.toldi.infinityforlemmy.dto.PostVoteDTO; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Retrofit; @@ -22,14 +18,13 @@ import retrofit2.Retrofit; public class VoteThing { - public static void voteThing(Context context, final Retrofit retrofit, String accessToken, - final VoteThingListener voteThingListener, final int postID, - final int point, final int position) { + public static void votePost(Context context, final Retrofit retrofit, String accessToken, + final VoteThingListener voteThingListener, final int postID, + final int point, final int position) { LemmyAPI api = retrofit.create(LemmyAPI.class); - - Call voteThingCall = api.postLike(new VoteDTO(postID,point,accessToken)); + Call voteThingCall = api.postLike(new PostVoteDTO(postID, point, accessToken)); voteThingCall.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { @@ -49,17 +44,65 @@ public class VoteThing { }); } - public static void voteThing(Context context, final Retrofit retrofit, String accessToken, - final VoteThingWithoutPositionListener voteThingWithoutPositionListener, - final String fullName, final String point) { - RedditAPI api = retrofit.create(RedditAPI.class); + public static void votePost(Context context, final Retrofit retrofit, String accessToken, + final VoteThingWithoutPositionListener voteThingWithoutPositionListener, + final int postID, final int point) { + LemmyAPI api = retrofit.create(LemmyAPI.class); - Map params = new HashMap<>(); - params.put(APIUtils.DIR_KEY, point); - params.put(APIUtils.ID_KEY, fullName); - params.put(APIUtils.RANK_KEY, APIUtils.RANK); - Call voteThingCall = api.voteThing(APIUtils.getOAuthHeader(accessToken), params); + Call voteThingCall = api.postLike(new PostVoteDTO(postID, point, accessToken)); + voteThingCall.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + if (response.isSuccessful()) { + voteThingWithoutPositionListener.onVoteThingSuccess(); + } else { + voteThingWithoutPositionListener.onVoteThingFail(); + Toast.makeText(context, "Code " + response.code() + " Body: " + response.body(), Toast.LENGTH_LONG).show(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + voteThingWithoutPositionListener.onVoteThingFail(); + Toast.makeText(context, "Network error " + "Body: " + t.getMessage(), Toast.LENGTH_LONG).show(); + } + }); + } + + public static void voteComment(Context context, final Retrofit retrofit, String accessToken, + final VoteThingListener voteThingListener, final int commentId, + final int point, final int position) { + LemmyAPI api = retrofit.create(LemmyAPI.class); + + + Call voteThingCall = api.commentLike(new CommentVoteDTO(commentId, point, accessToken)); + voteThingCall.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + if (response.isSuccessful()) { + voteThingListener.onVoteThingSuccess(position); + } else { + voteThingListener.onVoteThingFail(position); + Toast.makeText(context, "Code " + response.code() + " Body: " + response.body(), Toast.LENGTH_LONG).show(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + voteThingListener.onVoteThingFail(position); + Toast.makeText(context, "Network error " + "Body: " + t.getMessage(), Toast.LENGTH_LONG).show(); + } + }); + } + + public static void voteComment(Context context, final Retrofit retrofit, String accessToken, + final VoteThingWithoutPositionListener voteThingWithoutPositionListener, + final int commentId, final int point) { + LemmyAPI api = retrofit.create(LemmyAPI.class); + + + Call voteThingCall = api.commentLike(new CommentVoteDTO(commentId, point, accessToken)); voteThingCall.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewPostDetailActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewPostDetailActivity.java index 822999f6..b16e131f 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewPostDetailActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewPostDetailActivity.java @@ -423,14 +423,6 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele } } - private void awardGiven(String awardsHTML, int awardCount, int position) { - if (sectionsPagerAdapter != null) { - ViewPostDetailFragment fragment = sectionsPagerAdapter.getCurrentFragment(); - if (fragment != null) { - fragment.awardGiven(awardsHTML, awardCount, position); - } - } - } public void deleteComment(String fullName, int position) { if (sectionsPagerAdapter != null) { @@ -779,14 +771,6 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele data.getStringExtra(EditCommentActivity.EXTRA_EDITED_COMMENT_CONTENT), data.getExtras().getInt(EditCommentActivity.EXTRA_EDITED_COMMENT_POSITION)); } - } else if (requestCode == GIVE_AWARD_REQUEST_CODE) { - if (data != null && resultCode == Activity.RESULT_OK) { - Toast.makeText(this, R.string.give_award_success, Toast.LENGTH_SHORT).show(); - int position = data.getIntExtra(GiveAwardActivity.EXTRA_RETURN_ITEM_POSITION, 0); - String newAwardsHTML = data.getStringExtra(GiveAwardActivity.EXTRA_RETURN_NEW_AWARDS); - int newAwardsCount = data.getIntExtra(GiveAwardActivity.EXTRA_RETURN_NEW_AWARDS_COUNT, 0); - awardGiven(newAwardsHTML, newAwardsCount, position); - } } else if (requestCode == CommentActivity.WRITE_COMMENT_REQUEST_CODE) { if (data != null && resultCode == Activity.RESULT_OK) { if (data.hasExtra(RETURN_EXTRA_COMMENT_DATA_KEY)) { diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewUserDetailActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewUserDetailActivity.java index 6cfed395..874e2510 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewUserDetailActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewUserDetailActivity.java @@ -51,7 +51,6 @@ import com.google.android.material.textfield.TextInputEditText; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; -import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Locale; import java.util.concurrent.Executor; @@ -61,14 +60,6 @@ import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; -import eu.toldi.infinityforlemmy.RetrofitHolder; -import io.noties.markwon.AbstractMarkwonPlugin; -import io.noties.markwon.Markwon; -import io.noties.markwon.MarkwonConfiguration; -import io.noties.markwon.MarkwonPlugin; -import io.noties.markwon.core.MarkwonTheme; -import jp.wasabeef.glide.transformations.RoundedCornersTransformation; -import me.saket.bettermovementmethod.BetterLinkMovementMethod; import eu.toldi.infinityforlemmy.ActivityToolbarInterface; import eu.toldi.infinityforlemmy.AppBarStateChangeListener; import eu.toldi.infinityforlemmy.DeleteThing; @@ -77,6 +68,7 @@ import eu.toldi.infinityforlemmy.MarkPostAsReadInterface; import eu.toldi.infinityforlemmy.R; import eu.toldi.infinityforlemmy.RecyclerViewContentScrollingInterface; import eu.toldi.infinityforlemmy.RedditDataRoomDatabase; +import eu.toldi.infinityforlemmy.RetrofitHolder; import eu.toldi.infinityforlemmy.SortType; import eu.toldi.infinityforlemmy.SortTypeSelectionCallback; import eu.toldi.infinityforlemmy.adapters.SubredditAutocompleteRecyclerViewAdapter; @@ -86,7 +78,6 @@ import eu.toldi.infinityforlemmy.asynctasks.CheckIsFollowingUser; import eu.toldi.infinityforlemmy.asynctasks.SwitchAccount; import eu.toldi.infinityforlemmy.bottomsheetfragments.CopyTextBottomSheetFragment; import eu.toldi.infinityforlemmy.bottomsheetfragments.FABMoreOptionsBottomSheetFragment; -import eu.toldi.infinityforlemmy.bottomsheetfragments.KarmaInfoBottomSheetFragment; import eu.toldi.infinityforlemmy.bottomsheetfragments.PostLayoutBottomSheetFragment; import eu.toldi.infinityforlemmy.bottomsheetfragments.PostTypeBottomSheetFragment; import eu.toldi.infinityforlemmy.bottomsheetfragments.RandomBottomSheetFragment; @@ -118,6 +109,13 @@ import eu.toldi.infinityforlemmy.user.UserViewModel; import eu.toldi.infinityforlemmy.utils.APIUtils; import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils; import eu.toldi.infinityforlemmy.utils.Utils; +import io.noties.markwon.AbstractMarkwonPlugin; +import io.noties.markwon.Markwon; +import io.noties.markwon.MarkwonConfiguration; +import io.noties.markwon.MarkwonPlugin; +import io.noties.markwon.core.MarkwonTheme; +import jp.wasabeef.glide.transformations.RoundedCornersTransformation; +import me.saket.bettermovementmethod.BetterLinkMovementMethod; import pl.droidsonroids.gif.GifImageView; import retrofit2.Call; import retrofit2.Callback; @@ -1216,14 +1214,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { - if (requestCode == GIVE_AWARD_REQUEST_CODE) { - Toast.makeText(this, R.string.give_award_success, Toast.LENGTH_SHORT).show(); - int position = data.getIntExtra(GiveAwardActivity.EXTRA_RETURN_ITEM_POSITION, 0); - String newAwardsHTML = data.getStringExtra(GiveAwardActivity.EXTRA_RETURN_NEW_AWARDS); - if (sectionsPagerAdapter != null) { - sectionsPagerAdapter.giveAward(newAwardsHTML, position); - } - } else if (requestCode == EDIT_COMMENT_REQUEST_CODE) { + if (requestCode == EDIT_COMMENT_REQUEST_CODE) { if (data != null) { if (sectionsPagerAdapter != null) { sectionsPagerAdapter.editComment( @@ -1723,14 +1714,6 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele } } - void giveAward(String awardsHTML, int position) { - if (fragmentManager != null) { - Fragment fragment = fragmentManager.findFragmentByTag("f1"); - if (fragment instanceof CommentsListingFragment) { - ((CommentsListingFragment) fragment).giveAward(awardsHTML, position); - } - } - } void editComment(String commentMarkdown, int position) { if (fragmentManager != null) { diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/adapters/CommentsListingRecyclerViewAdapter.java b/app/src/main/java/eu/toldi/infinityforlemmy/adapters/CommentsListingRecyclerViewAdapter.java index 94be7ba7..8c75ee40 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/adapters/CommentsListingRecyclerViewAdapter.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/adapters/CommentsListingRecyclerViewAdapter.java @@ -30,12 +30,6 @@ import java.util.Locale; import butterknife.BindView; import butterknife.ButterKnife; -import io.noties.markwon.AbstractMarkwonPlugin; -import io.noties.markwon.Markwon; -import io.noties.markwon.MarkwonConfiguration; -import io.noties.markwon.MarkwonPlugin; -import io.noties.markwon.core.MarkwonTheme; -import me.saket.bettermovementmethod.BetterLinkMovementMethod; import eu.toldi.infinityforlemmy.NetworkState; import eu.toldi.infinityforlemmy.R; import eu.toldi.infinityforlemmy.SaveThing; @@ -58,6 +52,12 @@ import eu.toldi.infinityforlemmy.markdown.MarkdownUtils; import eu.toldi.infinityforlemmy.utils.APIUtils; import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils; import eu.toldi.infinityforlemmy.utils.Utils; +import io.noties.markwon.AbstractMarkwonPlugin; +import io.noties.markwon.Markwon; +import io.noties.markwon.MarkwonConfiguration; +import io.noties.markwon.MarkwonPlugin; +import io.noties.markwon.core.MarkwonTheme; +import me.saket.bettermovementmethod.BetterLinkMovementMethod; import retrofit2.Retrofit; public class CommentsListingRecyclerViewAdapter extends PagedListAdapter { @@ -189,17 +189,10 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter= 0 && position < getItemCount()) { - Comment comment = getItem(position); - if (comment != null) { - comment.addAwards(awardsHTML); - notifyItemChanged(position); - } - } - } - public void editComment(String commentContentMarkdown, int position) { Comment comment = getItem(position); if (comment != null) { @@ -461,7 +435,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter { @@ -100,7 +100,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter topLevelComments, ArrayList expandedComments, - ArrayList moreChildrenIds) { + ArrayList moreChildrenIds) { if (mVisibleComments.size() > parentPosition && parentComment.getFullName().equals(mVisibleComments.get(parentPosition).getFullName())) { if (mVisibleComments.get(parentPosition).isExpanded()) { @@ -791,6 +773,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter { @@ -1249,20 +1249,20 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter postLike(@Body VoteDTO params); + Call postLike(@Body PostVoteDTO params); + + @Headers("Content-Type: application/json") + @POST("api/v3/comment/like") + Call commentLike(@Body CommentVoteDTO params); + + @GET("api/v3/comment/list") + Call getComments( + @Query("type_") String type, + @Query("sort") String sort, + @Query("max_depth") Integer maxDepth, + @Query("page") Integer page, + @Query("limit") Integer limit, + @Query("community_id") Integer communityId, + @Query("community_name") String communityName, + @Query("post_id") Integer postId, + @Query("parent_id") Integer parentId, + @Query("saved_only") Boolean savedOnly, + @Query("auth") String auth + ); } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/bottomsheetfragments/CommentMoreBottomSheetFragment.java b/app/src/main/java/eu/toldi/infinityforlemmy/bottomsheetfragments/CommentMoreBottomSheetFragment.java index e5cfeedb..bd55e945 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/bottomsheetfragments/CommentMoreBottomSheetFragment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/bottomsheetfragments/CommentMoreBottomSheetFragment.java @@ -193,7 +193,7 @@ public class CommentMoreBottomSheetFragment extends LandscapeExpandedRoundedBott reportTextView.setOnClickListener(view -> { Intent intent = new Intent(activity, ReportActivity.class); - intent.putExtra(ReportActivity.EXTRA_SUBREDDIT_NAME, comment.getSubredditName()); + intent.putExtra(ReportActivity.EXTRA_SUBREDDIT_NAME, comment.getCommunityName()); intent.putExtra(ReportActivity.EXTRA_THING_FULLNAME, comment.getFullName()); activity.startActivity(intent); 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 46513344..074a084c 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/comment/Comment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/comment/Comment.java @@ -4,9 +4,9 @@ import android.os.Parcel; import android.os.Parcelable; import java.util.ArrayList; +import java.util.List; import eu.toldi.infinityforlemmy.BuildConfig; -import eu.toldi.infinityforlemmy.utils.APIUtils; public class Comment implements Parcelable { public static final int VOTE_TYPE_NO_VOTE = 0; @@ -29,79 +29,77 @@ public class Comment implements Parcelable { private int id; private String fullName; private String author; - private String authorFlair; - private String authorFlairHTML; + private String authorQualifiedName; private String authorIconUrl; private String linkAuthor; private long commentTimeMillis; private String commentMarkdown; private String commentRawText; private String linkId; - private String subredditName; - private String parentId; + private String communityName; + + private String communityQualifiedName; + private Integer parentId; private int score; private int voteType; private boolean isSubmitter; private String distinguished; private String permalink; - private String awards; private int depth; private int childCount; private boolean collapsed; private boolean hasReply; - private boolean scoreHidden; private boolean saved; private boolean isExpanded; private boolean hasExpandedBefore; - private ArrayList children; - private ArrayList moreChildrenIds; + private ArrayList children = new ArrayList<>(); + private ArrayList moreChildrenIds; private int placeholderType; private boolean isLoadingMoreChildren; private boolean loadMoreChildrenFailed; private long editedTimeMillis; - public Comment(int id, String fullName, String author, String authorFlair, - String authorFlairHTML, String linkAuthor, + private String[] path; + + public Comment(int id, String fullName, String author, String authorQualifiedName, String linkAuthor, long commentTimeMillis, String commentMarkdown, String commentRawText, - String linkId, String subredditName, String parentId, int score, + String linkId, String communityName, String communityQualifiedName, Integer parentId, int score, int voteType, boolean isSubmitter, String distinguished, String permalink, - String awards, int depth, boolean collapsed, boolean hasReply, - boolean scoreHidden, boolean saved, long edited) { + int depth, boolean collapsed, boolean hasReply, boolean saved, long edited, String[] path) { this.id = id; this.fullName = fullName; this.author = author; - this.authorFlair = authorFlair; - this.authorFlairHTML = authorFlairHTML; + this.authorQualifiedName = authorQualifiedName; this.linkAuthor = linkAuthor; this.commentTimeMillis = commentTimeMillis; this.commentMarkdown = commentMarkdown; this.commentRawText = commentRawText; this.linkId = linkId; - this.subredditName = subredditName; + this.communityName = communityName; + this.communityQualifiedName = communityQualifiedName; this.parentId = parentId; this.score = score; this.voteType = voteType; this.isSubmitter = isSubmitter; this.distinguished = distinguished; - this.permalink = APIUtils.API_BASE_URI + permalink; - this.awards = awards; + this.permalink = permalink; this.depth = depth; this.collapsed = collapsed; this.hasReply = hasReply; - this.scoreHidden = scoreHidden; this.saved = saved; this.isExpanded = false; this.hasExpandedBefore = false; this.editedTimeMillis = edited; + this.path = path; placeholderType = NOT_PLACEHOLDER; } - public Comment(String parentFullName, int depth, int placeholderType) { + public Comment(String parentFullName, int depth, int placeholderType, Integer parentId) { if (placeholderType == PLACEHOLDER_LOAD_MORE_COMMENTS) { this.fullName = parentFullName; } else { this.fullName = parentFullName; - this.parentId = parentFullName.substring(3); + this.parentId = parentId; } this.depth = depth; this.placeholderType = placeholderType; @@ -117,36 +115,39 @@ public class Comment implements Parcelable { id = in.readInt(); fullName = in.readString(); author = in.readString(); - authorFlair = in.readString(); - authorFlairHTML = in.readString(); + authorQualifiedName = in.readString(); authorIconUrl = in.readString(); linkAuthor = in.readString(); commentTimeMillis = in.readLong(); commentMarkdown = in.readString(); commentRawText = in.readString(); linkId = in.readString(); - subredditName = in.readString(); - parentId = in.readString(); + communityName = in.readString(); + communityQualifiedName = in.readString(); + parentId = in.readInt(); score = in.readInt(); voteType = in.readInt(); isSubmitter = in.readByte() != 0; distinguished = in.readString(); permalink = in.readString(); - awards = in.readString(); depth = in.readInt(); childCount = in.readInt(); collapsed = in.readByte() != 0; hasReply = in.readByte() != 0; - scoreHidden = in.readByte() != 0; isExpanded = in.readByte() != 0; hasExpandedBefore = in.readByte() != 0; children = new ArrayList<>(); in.readTypedList(children, Comment.CREATOR); moreChildrenIds = new ArrayList<>(); - in.readStringList(moreChildrenIds); + List childrenIDs = new ArrayList<>(); + in.readStringList(childrenIDs); + for (int i = 0; i < childrenIDs.size(); i++) { + moreChildrenIds.add(Integer.valueOf(childrenIDs.get(i))); + } placeholderType = in.readInt(); isLoadingMoreChildren = in.readByte() != 0; loadMoreChildrenFailed = in.readByte() != 0; + in.readStringArray(path); } public int getId() { @@ -169,13 +170,6 @@ public class Comment implements Parcelable { this.author = author; } - public String getAuthorFlair() { - return authorFlair; - } - - public String getAuthorFlairHTML() { - return authorFlairHTML; - } public String getAuthorIconUrl() { return authorIconUrl; @@ -213,15 +207,15 @@ public class Comment implements Parcelable { return linkId; } - public String getSubredditName() { - return subredditName; + public String getCommunityName() { + return communityName; } - public String getParentId() { + public Integer getParentId() { return parentId; } - public void setParentId(String parentId) { + public void setParentId(Integer parentId) { this.parentId = parentId; } @@ -253,14 +247,6 @@ public class Comment implements Parcelable { return permalink; } - public String getAwards() { - return awards; - } - - public void addAwards(String newAwardsHTML) { - awards += newAwardsHTML; - } - public int getDepth() { return depth; } @@ -285,10 +271,6 @@ public class Comment implements Parcelable { this.hasReply = hasReply; } - public boolean isScoreHidden() { - return scoreHidden; - } - public boolean isSaved() { return saved; } @@ -362,11 +344,11 @@ public class Comment implements Parcelable { } } - public ArrayList getMoreChildrenIds() { + public ArrayList getMoreChildrenIds() { return moreChildrenIds; } - public void setMoreChildrenIds(ArrayList moreChildrenIds) { + public void setMoreChildrenIds(ArrayList moreChildrenIds) { this.moreChildrenIds = moreChildrenIds; } @@ -408,40 +390,58 @@ public class Comment implements Parcelable { parcel.writeInt(id); parcel.writeString(fullName); parcel.writeString(author); - parcel.writeString(authorFlair); - parcel.writeString(authorFlairHTML); + parcel.writeString(authorQualifiedName); parcel.writeString(authorIconUrl); parcel.writeString(linkAuthor); parcel.writeLong(commentTimeMillis); parcel.writeString(commentMarkdown); parcel.writeString(commentRawText); parcel.writeString(linkId); - parcel.writeString(subredditName); - parcel.writeString(parentId); + parcel.writeString(communityName); + parcel.writeString(communityQualifiedName); + parcel.writeInt(parentId == null ? 0 : parentId); parcel.writeInt(score); parcel.writeInt(voteType); parcel.writeByte((byte) (isSubmitter ? 1 : 0)); parcel.writeString(distinguished); parcel.writeString(permalink); - parcel.writeString(awards); parcel.writeInt(depth); parcel.writeInt(childCount); parcel.writeByte((byte) (collapsed ? 1 : 0)); parcel.writeByte((byte) (hasReply ? 1 : 0)); - parcel.writeByte((byte) (scoreHidden ? 1 : 0)); parcel.writeByte((byte) (isExpanded ? 1 : 0)); parcel.writeByte((byte) (hasExpandedBefore ? 1 : 0)); parcel.writeTypedList(children); - parcel.writeStringList(moreChildrenIds); + List childrenIds = new ArrayList<>(); + if (moreChildrenIds != null) { + for (int j = 0; j < moreChildrenIds.size(); j++) { + childrenIds.add(String.valueOf(moreChildrenIds.get(i))); + } + } + parcel.writeStringList(childrenIds); parcel.writeInt(placeholderType); parcel.writeByte((byte) (isLoadingMoreChildren ? 1 : 0)); parcel.writeByte((byte) (loadMoreChildrenFailed ? 1 : 0)); + parcel.writeStringArray(path); + } + + public String[] getPath() { + return path; } public boolean isEdited() { return editedTimeMillis != 0; } + public long getEditedTimeMillis() { return editedTimeMillis; } + + public String getAuthorQualifiedName() { + return authorQualifiedName; + } + + public String getCommunityQualifiedName() { + return communityQualifiedName; + } } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/comment/CommentDataSource.java b/app/src/main/java/eu/toldi/infinityforlemmy/comment/CommentDataSource.java index b196fb09..54f917a7 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/comment/CommentDataSource.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/comment/CommentDataSource.java @@ -217,7 +217,7 @@ public class CommentDataSource extends PageKeyedDataSource { for (int i = 0; i < commentsJSONArray.length(); i++) { try { JSONObject commentJSON = commentsJSONArray.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY); - comments.add(ParseComment.parseSingleComment(commentJSON, 0)); + comments.add(ParseComment.parseSingleComment(commentJSON)); } catch (JSONException ignored) { } } 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 68ac278a..9d0c87ae 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/comment/FetchComment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/comment/FetchComment.java @@ -6,10 +6,10 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import java.util.ArrayList; -import java.util.Locale; import java.util.concurrent.Executor; import eu.toldi.infinityforlemmy.SortType; +import eu.toldi.infinityforlemmy.apis.LemmyAPI; import eu.toldi.infinityforlemmy.apis.RedditAPI; import eu.toldi.infinityforlemmy.utils.APIUtils; import retrofit2.Call; @@ -19,36 +19,25 @@ import retrofit2.Retrofit; public class FetchComment { public static void fetchComments(Executor executor, Handler handler, Retrofit retrofit, - @Nullable String accessToken, String article, - String commentId, SortType.Type sortType, String contextNumber, boolean expandChildren, - Locale locale, FetchCommentListener fetchCommentListener) { - RedditAPI api = retrofit.create(RedditAPI.class); + @Nullable String accessToken, Integer article, + Integer commentId, SortType.Type sortType, boolean expandChildren, + Integer page, FetchCommentListener fetchCommentListener) { + LemmyAPI api = retrofit.create(LemmyAPI.class); Call comments; - if (accessToken == null) { - if (commentId == null) { - comments = api.getPostAndCommentsById(article, sortType); - } else { - comments = api.getPostAndCommentsSingleThreadById(article, commentId, sortType, contextNumber); - } - } else { - if (commentId == null) { - comments = api.getPostAndCommentsByIdOauth(article, sortType, APIUtils.getOAuthHeader(accessToken)); - } else { - comments = api.getPostAndCommentsSingleThreadByIdOauth(article, commentId, sortType, contextNumber, - APIUtils.getOAuthHeader(accessToken)); - } - } + + comments = api.getComments("All", sortType.value, 5, page, 25, null, null, article, commentId, false, accessToken); + comments.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { - ParseComment.parseComment(executor, handler, response.body(), + ParseComment.parseComments(executor, handler, response.body(), expandChildren, new ParseComment.ParseCommentListener() { @Override public void onParseCommentSuccess(ArrayList topLevelComments, ArrayList expandedComments, - String parentId, ArrayList moreChildrenIds) { + Integer parentId, ArrayList moreChildrenIds) { fetchCommentListener.onFetchCommentSuccess(expandedComments, parentId, moreChildrenIds); } @@ -72,7 +61,7 @@ public class FetchComment { public static void fetchMoreComment(Executor executor, Handler handler, Retrofit retrofit, @Nullable String accessToken, - ArrayList allChildren, + ArrayList allChildren, boolean expandChildren, String postFullName, SortType.Type sortType, FetchMoreCommentListener fetchMoreCommentListener) { @@ -80,7 +69,7 @@ public class FetchComment { return; } - String childrenIds = String.join(",", allChildren); + String childrenIds = ""; if (childrenIds.isEmpty()) { return; @@ -104,9 +93,9 @@ public class FetchComment { @Override public void onParseCommentSuccess(ArrayList topLevelComments, ArrayList expandedComments, - String parentId, ArrayList moreChildrenIds) { + Integer parentId, ArrayList moreChildrenIds) { fetchMoreCommentListener.onFetchMoreCommentSuccess( - topLevelComments,expandedComments, moreChildrenIds); + topLevelComments, expandedComments, moreChildrenIds); } @Override @@ -127,7 +116,7 @@ public class FetchComment { } public interface FetchCommentListener { - void onFetchCommentSuccess(ArrayList expandedComments, String parentId, ArrayList children); + void onFetchCommentSuccess(ArrayList expandedComments, Integer parentId, ArrayList children); void onFetchCommentFailed(); } @@ -135,7 +124,7 @@ public class FetchComment { public interface FetchMoreCommentListener { void onFetchMoreCommentSuccess(ArrayList topLevelComments, ArrayList expandedComments, - ArrayList moreChildrenIds); + ArrayList moreChildrenIds); void onFetchMoreCommentFailed(); } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/comment/FetchRemovedCommentReveddit.java b/app/src/main/java/eu/toldi/infinityforlemmy/comment/FetchRemovedCommentReveddit.java index f340daea..7d1c6ba6 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/comment/FetchRemovedCommentReveddit.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/comment/FetchRemovedCommentReveddit.java @@ -20,7 +20,7 @@ public class FetchRemovedCommentReveddit { public static void fetchRemovedComment(Executor executor, Handler handler, Retrofit retrofit, Comment comment, long postCreatedUtc, int nComments, FetchRemovedCommentListener listener) { executor.execute(() -> { - String parentIdWithoutPrefix = comment.getParentId().substring(3); + String parentIdWithoutPrefix = " comment.getParentId().substring(3)"; String rootCommentId = parentIdWithoutPrefix.equals(comment.getLinkId()) ? String.valueOf(comment.getId()) : parentIdWithoutPrefix; try { Response response = retrofit.create(RevedditAPI.class).getRemovedComments( 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 d9d628bd..49ce9137 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/comment/ParseComment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/comment/ParseComment.java @@ -1,11 +1,6 @@ package eu.toldi.infinityforlemmy.comment; -import static eu.toldi.infinityforlemmy.comment.Comment.VOTE_TYPE_DOWNVOTE; -import static eu.toldi.infinityforlemmy.comment.Comment.VOTE_TYPE_NO_VOTE; -import static eu.toldi.infinityforlemmy.comment.Comment.VOTE_TYPE_UPVOTE; - import android.os.Handler; -import android.text.Html; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -14,29 +9,44 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executor; +import java.util.regex.Pattern; import eu.toldi.infinityforlemmy.utils.JSONUtils; -import eu.toldi.infinityforlemmy.utils.Utils; +import eu.toldi.infinityforlemmy.utils.LemmyUtils; + public class ParseComment { - public static void parseComment(Executor executor, Handler handler, String response, - boolean expandChildren, - ParseCommentListener parseCommentListener) { + public static void parseComments(Executor executor, Handler handler, String response, + boolean expandChildren, + ParseCommentListener parseCommentListener) { executor.execute(() -> { try { - JSONArray childrenArray = new JSONArray(response); - String parentId = childrenArray.getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY) - .getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.NAME_KEY); - childrenArray = childrenArray.getJSONObject(1).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); + JSONArray childrenArray = new JSONObject(response).getJSONArray("comments"); + ArrayList expandedNewComments = new ArrayList<>(); - ArrayList moreChildrenIds = new ArrayList<>(); + ArrayList moreChildrenIds = new ArrayList<>(); ArrayList newComments = new ArrayList<>(); - parseCommentRecursion(childrenArray, newComments, moreChildrenIds, 0); + 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); + } + } + + + //parseCommentRecursion(childrenArray, newComments, moreChildrenIds, 0); expandChildren(newComments, expandedNewComments, expandChildren); ArrayList commentData; @@ -46,7 +56,7 @@ public class ParseComment { commentData = newComments; } - handler.post(() -> parseCommentListener.onParseCommentSuccess(newComments, commentData, parentId, moreChildrenIds)); + handler.post(() -> parseCommentListener.onParseCommentSuccess(newComments, commentData, (newComments.size() == 0) ? null : newComments.get(0).getId(), moreChildrenIds)); } catch (JSONException e) { e.printStackTrace(); handler.post(parseCommentListener::onParseCommentFailed); @@ -54,6 +64,10 @@ 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(() -> { @@ -63,7 +77,7 @@ public class ParseComment { ArrayList newComments = new ArrayList<>(); ArrayList expandedNewComments = new ArrayList<>(); - ArrayList moreChildrenIds = new ArrayList<>(); + ArrayList moreChildrenIds = new ArrayList<>(); // api response is a flat list of comments tree // process it in order and rebuild the tree @@ -71,13 +85,13 @@ public class ParseComment { JSONObject child = childrenArray.getJSONObject(i); JSONObject childData = child.getJSONObject(JSONUtils.DATA_KEY); if (child.getString(JSONUtils.KIND_KEY).equals(JSONUtils.KIND_VALUE_MORE)) { - String parentFullName = childData.getString(JSONUtils.PARENT_ID_KEY); + Integer parentFullName = Integer.valueOf(childData.getString(JSONUtils.PARENT_ID_KEY)); JSONArray childrenIds = childData.getJSONArray(JSONUtils.CHILDREN_KEY); if (childrenIds.length() != 0) { - ArrayList localMoreChildrenIds = new ArrayList<>(childrenIds.length()); + ArrayList localMoreChildrenIds = new ArrayList<>(childrenIds.length()); for (int j = 0; j < childrenIds.length(); j++) { - localMoreChildrenIds.add(childrenIds.getString(j)); + localMoreChildrenIds.add(childrenIds.getInt(j)); } Comment parentComment = findCommentByFullName(newComments, parentFullName); @@ -90,13 +104,15 @@ public class ParseComment { moreChildrenIds.addAll(localMoreChildrenIds); } } else { + Comment parentComment = findCommentByFullName(newComments, parentFullName); Comment continueThreadPlaceholder = new Comment( - parentFullName, + parentComment.getFullName(), childData.getInt(JSONUtils.DEPTH_KEY), - Comment.PLACEHOLDER_CONTINUE_THREAD + Comment.PLACEHOLDER_CONTINUE_THREAD, + parentComment.getId() ); - Comment parentComment = findCommentByFullName(newComments, parentFullName); + if (parentComment != null) { parentComment.setHasReply(true); parentComment.addChild(continueThreadPlaceholder, parentComment.getChildCount()); @@ -107,8 +123,8 @@ public class ParseComment { } } } else { - Comment comment = parseSingleComment(childData, 0); - String parentFullName = comment.getParentId(); + Comment comment = parseSingleComment(childData); + Integer parentFullName = comment.getParentId(); Comment parentComment = findCommentByFullName(newComments, parentFullName); if (parentComment != null) { @@ -145,7 +161,7 @@ public class ParseComment { executor.execute(() -> { try { JSONObject sentCommentData = new JSONObject(response); - Comment comment = parseSingleComment(sentCommentData, depth); + Comment comment = parseSingleComment(sentCommentData); handler.post(() -> parseSentCommentListener.onParseSentCommentSuccess(comment)); } catch (JSONException e) { @@ -177,7 +193,7 @@ public class ParseComment { actualCommentLength = comments.length() - 1; if (moreChildrenIds.isEmpty() && comments.getJSONObject(comments.length() - 1).getString(JSONUtils.KIND_KEY).equals(JSONUtils.KIND_VALUE_MORE)) { - newCommentData.add(new Comment(more.getString(JSONUtils.PARENT_ID_KEY), more.getInt(JSONUtils.DEPTH_KEY), Comment.PLACEHOLDER_CONTINUE_THREAD)); + //newCommentData.add(new Comment(more.getString(JSONUtils.PARENT_ID_KEY), more.getInt(JSONUtils.DEPTH_KEY), Comment.PLACEHOLDER_CONTINUE_THREAD)); return; } } else { @@ -186,19 +202,7 @@ public class ParseComment { for (int i = 0; i < actualCommentLength; i++) { JSONObject data = comments.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY); - Comment singleComment = parseSingleComment(data, depth); - - if (data.get(JSONUtils.REPLIES_KEY) instanceof JSONObject) { - JSONArray childrenArray = data.getJSONObject(JSONUtils.REPLIES_KEY) - .getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); - ArrayList children = new ArrayList<>(); - ArrayList nextMoreChildrenIds = new ArrayList<>(); - parseCommentRecursion(childrenArray, children, nextMoreChildrenIds, singleComment.getDepth()); - singleComment.addChildren(children); - singleComment.setMoreChildrenIds(nextMoreChildrenIds); - singleComment.setChildCount(getChildCount(singleComment)); - } - + Comment singleComment = parseSingleComment(data); newCommentData.add(singleComment); } } @@ -228,90 +232,61 @@ public class ParseComment { } if (c.hasMoreChildrenIds() && !c.getMoreChildrenIds().isEmpty()) { //Add a load more placeholder - Comment placeholder = new Comment(c.getFullName(), c.getDepth() + 1, Comment.PLACEHOLDER_LOAD_MORE_COMMENTS); + Comment placeholder = new Comment(c.getFullName(), c.getDepth() + 1, Comment.PLACEHOLDER_LOAD_MORE_COMMENTS, c.getId()); visibleComments.add(placeholder); c.addChild(placeholder, c.getChildren().size()); } } } - static Comment parseSingleComment(JSONObject singleCommentData, int depth) throws JSONException { - String id = singleCommentData.getString(JSONUtils.ID_KEY); - String fullName = singleCommentData.getString(JSONUtils.NAME_KEY); - String author = singleCommentData.getString(JSONUtils.AUTHOR_KEY); - StringBuilder authorFlairHTMLBuilder = new StringBuilder(); - if (singleCommentData.has(JSONUtils.AUTHOR_FLAIR_RICHTEXT_KEY)) { - JSONArray flairArray = singleCommentData.getJSONArray(JSONUtils.AUTHOR_FLAIR_RICHTEXT_KEY); - for (int i = 0; i < flairArray.length(); i++) { - JSONObject flairObject = flairArray.getJSONObject(i); - String e = flairObject.getString(JSONUtils.E_KEY); - if (e.equals("text")) { - authorFlairHTMLBuilder.append(Html.escapeHtml(flairObject.getString(JSONUtils.T_KEY))); - } else if (e.equals("emoji")) { - authorFlairHTMLBuilder.append(""); - } - } - } - String authorFlair = singleCommentData.isNull(JSONUtils.AUTHOR_FLAIR_TEXT_KEY) ? "" : singleCommentData.getString(JSONUtils.AUTHOR_FLAIR_TEXT_KEY); - String linkAuthor = singleCommentData.has(JSONUtils.LINK_AUTHOR_KEY) ? singleCommentData.getString(JSONUtils.LINK_AUTHOR_KEY) : null; - String linkId = singleCommentData.getString(JSONUtils.LINK_ID_KEY).substring(3); - String subredditName = singleCommentData.getString(JSONUtils.SUBREDDIT_KEY); - String parentId = singleCommentData.getString(JSONUtils.PARENT_ID_KEY); - boolean isSubmitter = singleCommentData.getBoolean(JSONUtils.IS_SUBMITTER_KEY); - String distinguished = singleCommentData.getString(JSONUtils.DISTINGUISHED_KEY); - String commentMarkdown = ""; - if (!singleCommentData.isNull(JSONUtils.BODY_KEY)) { - commentMarkdown = Utils.parseInlineGifInComments(Utils.modifyMarkdown(Utils.trimTrailingWhitespace(singleCommentData.getString(JSONUtils.BODY_KEY)))); - if (!singleCommentData.isNull(JSONUtils.MEDIA_METADATA_KEY)) { - JSONObject mediaMetadataObject = singleCommentData.getJSONObject(JSONUtils.MEDIA_METADATA_KEY); - commentMarkdown = Utils.parseInlineEmotes(commentMarkdown, mediaMetadataObject); - } - } - String commentRawText = Utils.trimTrailingWhitespace( - Html.fromHtml(singleCommentData.getString(JSONUtils.BODY_HTML_KEY))).toString(); - String permalink = Html.fromHtml(singleCommentData.getString(JSONUtils.PERMALINK_KEY)).toString(); - StringBuilder awardingsBuilder = new StringBuilder(); - JSONArray awardingsArray = singleCommentData.getJSONArray(JSONUtils.ALL_AWARDINGS_KEY); - for (int i = 0; i < awardingsArray.length(); i++) { - JSONObject award = awardingsArray.getJSONObject(i); - int count = award.getInt(JSONUtils.COUNT_KEY); - JSONArray icons = award.getJSONArray(JSONUtils.RESIZED_ICONS_KEY); - if (icons.length() > 4) { - String iconUrl = icons.getJSONObject(3).getString(JSONUtils.URL_KEY); - awardingsBuilder.append(" ").append("x").append(count).append(" "); - } else if (icons.length() > 0) { - String iconUrl = icons.getJSONObject(icons.length() - 1).getString(JSONUtils.URL_KEY); - awardingsBuilder.append(" ").append("x").append(count).append(" "); - } - } - int score = singleCommentData.getInt(JSONUtils.SCORE_KEY); - int voteType; - if (singleCommentData.isNull(JSONUtils.LIKES_KEY)) { - voteType = VOTE_TYPE_NO_VOTE; - } else { - voteType = singleCommentData.getBoolean(JSONUtils.LIKES_KEY) ? VOTE_TYPE_UPVOTE : VOTE_TYPE_DOWNVOTE; - score -= voteType; - } - long submitTime = singleCommentData.getLong(JSONUtils.PUBLISHED) * 1000; - boolean scoreHidden = singleCommentData.getBoolean(JSONUtils.SCORE_HIDDEN_KEY); - boolean saved = singleCommentData.getBoolean(JSONUtils.SAVED_KEY); + public static Comment parseSingleComment(JSONObject jsonObject) throws JSONException { + JSONObject commentObj = jsonObject.getJSONObject("comment"); + JSONObject creatorObj = jsonObject.getJSONObject("creator"); + JSONObject postObj = jsonObject.getJSONObject("post"); + JSONObject communityObj = jsonObject.getJSONObject("community"); + JSONObject countsObj = jsonObject.getJSONObject("counts"); - if (singleCommentData.has(JSONUtils.DEPTH_KEY)) { - depth = singleCommentData.getInt(JSONUtils.DEPTH_KEY); + int id = commentObj.getInt("id"); + String fullName = creatorObj.getString("name"); + String author = creatorObj.getString("name"); + String authorQualifiedName = LemmyUtils.actorID2FullName(creatorObj.getString("actor_id")); + String linkAuthor = creatorObj.getString("actor_id"); + long commentTimeMillis = 0; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + commentTimeMillis = ZonedDateTime.parse(commentObj.getString("published"), + DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.of("Z"))).toInstant().toEpochMilli(); } + String commentMarkdown = commentObj.getString("content"); + String commentRawText = commentObj.getString("content"); + String linkId = postObj.getString("id"); + String communityName = communityObj.getString("name"); + String communityQualifiedName = communityObj.getString("actor_id"); - boolean collapsed = singleCommentData.getBoolean(JSONUtils.COLLAPSED_KEY); - boolean hasReply = !(singleCommentData.get(JSONUtils.REPLIES_KEY) instanceof String); + int score = countsObj.getInt("score"); + int voteType = (jsonObject.isNull("my_vote")) ? 0 : jsonObject.getInt("my_vote"); + boolean isSubmitter = creatorObj.getInt("id") == postObj.getInt("creator_id"); + String distinguished = commentObj.getString("distinguished"); + String permalink = commentObj.getString("ap_id"); + String[] path = commentObj.getString("path").split(Pattern.quote(".")); + ; - // this key can either be a bool (false) or a long (edited timestamp) - long edited = singleCommentData.optLong(JSONUtils.EDITED_KEY) * 1000; + int depth = path.length - 2; - return new Comment(Integer.parseInt(id), fullName, author, authorFlair, authorFlairHTMLBuilder.toString(), - linkAuthor, submitTime, commentMarkdown, commentRawText, - linkId, subredditName, parentId, score, voteType, isSubmitter, distinguished, - permalink, awardingsBuilder.toString(), depth, collapsed, hasReply, scoreHidden, saved, edited); + Integer parentId = (depth > 0) ? Integer.valueOf(path[path.length - 2]) : null; + boolean collapsed = false; + boolean hasReply = countsObj.getInt("child_count") > 0; + boolean saved = jsonObject.getBoolean("saved"); + long edited = 0; + + Comment comment = new Comment(id, fullName, author, authorQualifiedName, linkAuthor, commentTimeMillis, + commentMarkdown, commentRawText, linkId, communityName, communityQualifiedName, parentId, + score, voteType, isSubmitter, distinguished, permalink, depth, collapsed, hasReply, saved, edited, path); + int child_count = countsObj.getInt("child_count"); + comment.setChildCount(child_count); + return comment; } + @Nullable private static String parseSentCommentErrorMessage(String response) { try { @@ -341,10 +316,51 @@ 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 String fullName) { - for (Comment comment: comments) { - if (comment.getFullName().equals(fullName) && + private static Comment findCommentByFullName(@NonNull List comments, @NonNull Integer fullName) { + for (Comment comment : comments) { + if (comment.getId() == fullName && comment.getPlaceholderType() == Comment.NOT_PLACEHOLDER) { return comment; } @@ -368,8 +384,8 @@ public class ParseComment { } public interface ParseCommentListener { - void onParseCommentSuccess(ArrayList topLevelComments, ArrayList expandedComments, String parentId, - ArrayList moreChildrenIds); + void onParseCommentSuccess(ArrayList topLevelComments, ArrayList expandedComments, Integer parentId, + ArrayList moreChildrenIds); void onParseCommentFailed(); } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/dto/CommentVoteDTO.java b/app/src/main/java/eu/toldi/infinityforlemmy/dto/CommentVoteDTO.java new file mode 100644 index 00000000..7d0057b2 --- /dev/null +++ b/app/src/main/java/eu/toldi/infinityforlemmy/dto/CommentVoteDTO.java @@ -0,0 +1,25 @@ +package eu.toldi.infinityforlemmy.dto; + +public class CommentVoteDTO { + int comment_id; + int score; + String auth; + + public CommentVoteDTO(int comment_id, int score, String auth) { + this.comment_id = comment_id; + this.score = score; + this.auth = auth; + } + + public int getComment_id() { + return comment_id; + } + + public int getScore() { + return score; + } + + public String getAuth() { + return auth; + } +} diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/dto/VoteDTO.java b/app/src/main/java/eu/toldi/infinityforlemmy/dto/PostVoteDTO.java similarity index 82% rename from app/src/main/java/eu/toldi/infinityforlemmy/dto/VoteDTO.java rename to app/src/main/java/eu/toldi/infinityforlemmy/dto/PostVoteDTO.java index 01fbc26a..b13c01f5 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/dto/VoteDTO.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/dto/PostVoteDTO.java @@ -1,12 +1,12 @@ package eu.toldi.infinityforlemmy.dto; -public class VoteDTO { +public class PostVoteDTO { private final int post_id; private final int score; private final String auth; - public VoteDTO(int post_id, int vote, String auth) { + public PostVoteDTO(int post_id, int vote, String auth) { this.post_id = post_id; this.score = vote; this.auth = auth; diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/fragments/CommentsListingFragment.java b/app/src/main/java/eu/toldi/infinityforlemmy/fragments/CommentsListingFragment.java index 86120381..258b93dd 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/fragments/CommentsListingFragment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/fragments/CommentsListingFragment.java @@ -400,11 +400,6 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni return sortType; } - public void giveAward(String awardsHTML, int position) { - if (mAdapter != null) { - mAdapter.giveAward(awardsHTML, position); - } - } public void editComment(String commentMarkdown, int position) { if (mAdapter != null) { diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewPostDetailFragment.java b/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewPostDetailFragment.java index 10314e92..cea93a1f 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewPostDetailFragment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewPostDetailFragment.java @@ -84,6 +84,7 @@ import eu.toldi.infinityforlemmy.activities.SubmitCrosspostActivity; import eu.toldi.infinityforlemmy.activities.ViewPostDetailActivity; import eu.toldi.infinityforlemmy.adapters.CommentsRecyclerViewAdapter; import eu.toldi.infinityforlemmy.adapters.PostDetailRecyclerViewAdapter; +import eu.toldi.infinityforlemmy.apis.LemmyAPI; import eu.toldi.infinityforlemmy.apis.RedditAPI; import eu.toldi.infinityforlemmy.apis.StreamableAPI; import eu.toldi.infinityforlemmy.asynctasks.LoadUserData; @@ -202,7 +203,10 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic @State ArrayList comments; @State - ArrayList children; + ArrayList children; + + @State + int pages_loaded = 0; @State boolean loadMoreChildrenSuccess = true; @State @@ -224,7 +228,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic private String mAccessToken; private String mAccountName; private int postListPosition = -1; - private String mSingleCommentId; + private Integer mSingleCommentId; private String mContextNumber; private boolean showToast = false; private boolean mIsSmoothScrolling = false; @@ -541,7 +545,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic } }; - mSingleCommentId = getArguments().getString(EXTRA_SINGLE_COMMENT_ID); + mSingleCommentId = (getArguments().getString(EXTRA_SINGLE_COMMENT_ID) == null) ? null : Integer.valueOf(getArguments().getString(EXTRA_SINGLE_COMMENT_ID)); mContextNumber = getArguments().getString(EXTRA_CONTEXT_NUMBER, "8"); if (savedInstanceState == null) { @@ -770,12 +774,6 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic } } - public void awardGiven(String awardsHTML, int awardCount, int position) { - if (mCommentsAdapter != null) { - mCommentsAdapter.giveAward(awardsHTML, awardCount, position); - } - } - public void changeFlair(Flair flair) { Map params = new HashMap<>(); params.put(APIUtils.API_TYPE_KEY, APIUtils.API_TYPE_JSON); @@ -1253,24 +1251,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic mSwipeRefreshLayout.setRefreshing(true); mGlide.clear(mFetchPostInfoImageView); - Call postAndComments; - if (mAccessToken == null) { - if (isSingleCommentThreadMode && mSingleCommentId != null) { - postAndComments = mRetrofit.getRetrofit().create(RedditAPI.class).getPostAndCommentsSingleThreadById( - subredditId, mSingleCommentId, sortType, mContextNumber); - } else { - postAndComments = mRetrofit.getRetrofit().create(RedditAPI.class).getPostAndCommentsById(subredditId, - sortType); - } - } else { - if (isSingleCommentThreadMode && mSingleCommentId != null) { - postAndComments = mOauthRetrofit.create(RedditAPI.class).getPostAndCommentsSingleThreadByIdOauth(subredditId, - mSingleCommentId, sortType, mContextNumber, APIUtils.getOAuthHeader(mAccessToken)); - } else { - postAndComments = mOauthRetrofit.create(RedditAPI.class).getPostAndCommentsByIdOauth(subredditId, - sortType, APIUtils.getOAuthHeader(mAccessToken)); - } - } + Call postAndComments = mRetrofit.getRetrofit().create(LemmyAPI.class).getComments("All", sortType.value, 5, 1, 25, null, null, Integer.valueOf(subredditId), mSingleCommentId, false, mAccessToken); postAndComments.enqueue(new Callback<>() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { @@ -1331,10 +1312,10 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic if (mRespectSubredditRecommendedSortType) { fetchCommentsRespectRecommendedSort(false); } else { - ParseComment.parseComment(mExecutor, new Handler(), response.body(), + ParseComment.parseComments(mExecutor, new Handler(), response.body(), mExpandChildren, new ParseComment.ParseCommentListener() { @Override - public void onParseCommentSuccess(ArrayList topLevelComments, ArrayList expandedComments, String parentId, ArrayList moreChildrenIds) { + public void onParseCommentSuccess(ArrayList topLevelComments, ArrayList expandedComments, Integer parentId, ArrayList moreChildrenIds) { ViewPostDetailFragment.this.children = moreChildrenIds; hasMoreChildren = children.size() != 0; @@ -1466,23 +1447,27 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic isFetchingComments = true; mCommentsAdapter.setSingleComment(mSingleCommentId, isSingleCommentThreadMode); mCommentsAdapter.initiallyLoading(); - String commentId = null; + Integer commentId = null; if (isSingleCommentThreadMode) { commentId = mSingleCommentId; } - Retrofit retrofit = mAccessToken == null ? mRetrofit.getRetrofit() : mOauthRetrofit; - FetchComment.fetchComments(mExecutor, new Handler(), retrofit, mAccessToken, String.valueOf(mPost.getId()), commentId, sortType, - mContextNumber, mExpandChildren, mLocale, new FetchComment.FetchCommentListener() { + + FetchComment.fetchComments(mExecutor, new Handler(), mRetrofit.getRetrofit(), mAccessToken, mPost.getId(), commentId, sortType, mExpandChildren, pages_loaded + 1, + new FetchComment.FetchCommentListener() { @Override public void onFetchCommentSuccess(ArrayList expandedComments, - String parentId, ArrayList children) { + Integer parentId, ArrayList children) { ViewPostDetailFragment.this.children = children; - + pages_loaded++; comments = expandedComments; - hasMoreChildren = children.size() != 0; + hasMoreChildren = expandedComments.size() != 0; mCommentsAdapter.addComments(expandedComments, hasMoreChildren); + if (hasMoreChildren) { + fetchMoreComments(); + } +/* if (children.size() > 0) { (mCommentsRecyclerView == null ? mRecyclerView : mCommentsRecyclerView).clearOnScrollListeners(); (mCommentsRecyclerView == null ? mRecyclerView : mCommentsRecyclerView).addOnScrollListener(new RecyclerView.OnScrollListener() { @@ -1527,7 +1512,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic } } }); - } + }*/ if (changeRefreshState) { isRefreshing = false; } @@ -1559,13 +1544,11 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic isLoadingMoreChildren = true; Retrofit retrofit = mAccessToken == null ? mRetrofit.getRetrofit() : mOauthRetrofit; - FetchComment.fetchMoreComment(mExecutor, new Handler(), retrofit, mAccessToken, children, - mExpandChildren, mPost.getFullName(), sortType, new FetchComment.FetchMoreCommentListener() { + FetchComment.fetchComments(mExecutor, new Handler(), retrofit, mAccessToken, + mPost.getId(), null, sortType, mExpandChildren, pages_loaded + 1, new FetchComment.FetchCommentListener() { @Override - public void onFetchMoreCommentSuccess(ArrayList topLevelComments, - ArrayList expandedComments, - ArrayList moreChildrenIds) { - children = moreChildrenIds; + public void onFetchCommentSuccess(ArrayList expandedComments, Integer parentId, ArrayList children) { + pages_loaded++; hasMoreChildren = !children.isEmpty(); mCommentsAdapter.addComments(expandedComments, hasMoreChildren); isLoadingMoreChildren = false; @@ -1573,7 +1556,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic } @Override - public void onFetchMoreCommentFailed() { + public void onFetchCommentFailed() { isLoadingMoreChildren = false; loadMoreChildrenSuccess = false; mCommentsAdapter.loadMoreCommentsFailed(); @@ -1593,12 +1576,8 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic } if (fetchPost) { - Retrofit retrofit; - if (mAccessToken == null) { - retrofit = mRetrofit.getRetrofit(); - } else { - retrofit = mOauthRetrofit; - } + Retrofit retrofit = mRetrofit.getRetrofit(); + FetchPost.fetchPost(mExecutor, new Handler(), retrofit, String.valueOf(mPost.getId()), mAccessToken, new FetchPost.FetchPostListener() { @Override diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/post/Post.java b/app/src/main/java/eu/toldi/infinityforlemmy/post/Post.java index 107d3a85..185cc6b6 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/post/Post.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/post/Post.java @@ -7,8 +7,6 @@ import androidx.annotation.Nullable; import java.util.ArrayList; -import eu.toldi.infinityforlemmy.utils.APIUtils; - /** * Created by alex on 3/1/18. */ @@ -90,7 +88,7 @@ public class Post implements Parcelable { this.authorNamePrefixed = authorNamePrefixed; this.postTimeMillis = postTimeMillis; this.title = title; - this.permalink = APIUtils.API_BASE_URI + permalink; + this.permalink = permalink; this.score = score; this.postType = postType; this.voteType = voteType; @@ -122,7 +120,7 @@ public class Post implements Parcelable { this.postTimeMillis = postTimeMillis; this.title = title; this.url = url; - this.permalink = APIUtils.API_BASE_URI + permalink; + this.permalink = permalink; this.score = score; this.postType = postType; this.voteType = voteType; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1e856b4a..be8a9eae 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -261,7 +261,7 @@ Search in All subreddits - Best + Active Hot New Random