From 70f4a7fc4c82e97b5b9a9b7682d6e8c3fade631e Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 18 Jun 2019 17:05:49 +0800 Subject: [PATCH] Fixed refreshing comments and sending comments. --- .../infinityforreddit/CommentData.java | 179 +----------------- .../CommentRecyclerViewAdapter.java | 11 +- .../infinityforreddit/FetchComment.java | 1 - .../ViewPostDetailActivity.java | 25 +-- 4 files changed, 22 insertions(+), 194 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentData.java index 043c0071..6ade3dd8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentData.java @@ -59,7 +59,8 @@ class CommentData implements Parcelable { hasReply = in.readByte() != 0; scoreHidden = in.readByte() != 0; isExpanded = in.readByte() != 0; - in.readStringList(moreChildrenIds); + children = in.readArrayList(null); + moreChildrenIds = in.readArrayList(null); } public static final Creator CREATOR = new Creator() { @@ -200,179 +201,7 @@ class CommentData implements Parcelable { parcel.writeByte((byte) (hasReply ? 1 : 0)); parcel.writeByte((byte) (scoreHidden ? 1 : 0)); parcel.writeByte((byte) (isExpanded ? 1 : 0)); - parcel.writeStringList(moreChildrenIds); + parcel.writeList(children); + parcel.writeList(moreChildrenIds); } } - -/*class CommentData extends RecyclerViewItem implements Parcelable { - private String id; - private String fullName; - private String author; - private String commentTime; - private String commentContent; - private String parentId; - private int score; - private int voteType; - private boolean isSubmitter; - private String permalink; - private int depth; - private boolean collapsed; - private boolean hasReply; - private boolean scoreHidden; - private ArrayList moreChildrenIds; - - CommentData(String id, String fullName, String author, String commentTime, String commentContent, - String parentId, int score, boolean isSubmitter, String permalink, int depth, - boolean collapsed, boolean hasReply, boolean scoreHidden) { - super(depth); - this.id = id; - this.fullName = fullName; - this.author = author; - this.commentTime = commentTime; - this.commentContent = commentContent; - this.parentId = parentId; - this.score = score; - this.isSubmitter = isSubmitter; - this.permalink = RedditUtils.API_BASE_URI + permalink; - this.depth = depth; - this.collapsed = collapsed; - this.hasReply = hasReply; - this.scoreHidden = scoreHidden; - } - - protected CommentData(Parcel in) { - super(0); - id = in.readString(); - fullName = in.readString(); - author = in.readString(); - commentTime = in.readString(); - commentContent = in.readString(); - parentId = in.readString(); - score = in.readInt(); - voteType = in.readInt(); - isSubmitter = in.readByte() != 0; - permalink = in.readString(); - depth = in.readInt(); - collapsed = in.readByte() != 0; - hasReply = in.readByte() != 0; - scoreHidden = in.readByte() != 0; - in.readStringList(moreChildrenIds); - super.setLevel(depth); - } - - public static final Creator CREATOR = new Creator() { - @Override - public CommentData createFromParcel(Parcel in) { - return new CommentData(in); - } - - @Override - public CommentData[] newArray(int size) { - return new CommentData[size]; - } - }; - - public String getId() { - return id; - } - - public String getFullName() { - return fullName; - } - - public String getAuthor() { - return author; - } - - public String getCommentTime() { - return commentTime; - } - - public String getCommentContent() { - return commentContent; - } - - public String getParentId() { - return parentId; - } - - public void setParentId(String parentId) { - this.parentId = parentId; - } - - public int getScore() { - return score; - } - - public void setScore(int score) { - this.score = score; - } - - public boolean isSubmitter() { - return isSubmitter; - } - - public String getPermalink() { - return permalink; - } - - public int getDepth() { - return depth; - } - - public boolean isCollapsed() { - return collapsed; - } - - public boolean hasReply() { - return hasReply; - } - - public void setHasReply(boolean hasReply) { - this.hasReply = hasReply; - } - - public boolean isScoreHidden() { - return scoreHidden; - } - - public int getVoteType() { - return voteType; - } - - public void setVoteType(int voteType) { - this.voteType = voteType; - } - - public ArrayList getMoreChildrenIds() { - return moreChildrenIds; - } - - public void setMoreChildrenIds(ArrayList moreChildrenIds) { - this.moreChildrenIds = moreChildrenIds; - } - - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(Parcel parcel, int i) { - parcel.writeString(id); - parcel.writeString(fullName); - parcel.writeString(author); - parcel.writeString(commentTime); - parcel.writeString(commentContent); - parcel.writeString(parentId); - parcel.writeInt(score); - parcel.writeInt(voteType); - parcel.writeByte((byte) (isSubmitter ? 1 : 0)); - parcel.writeString(permalink); - parcel.writeInt(depth); - parcel.writeByte((byte) (collapsed ? 1 : 0)); - parcel.writeByte((byte) (hasReply ? 1 : 0)); - parcel.writeByte((byte) (scoreHidden ? 1 : 0)); - parcel.writeStringList(moreChildrenIds); - } -}*/ diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java index e9da8bde..260bb5bf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java @@ -56,6 +56,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter(); + new Handler().post(() -> { makeChildrenVisible(commentData, mVisibleComments); notifyDataSetChanged(); @@ -80,7 +81,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter moreChildrenBasicInfo = api.getMoreChildren(mParentId, stringBuilder.toString()); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 3c3db7b5..95a9ba0e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -85,7 +85,6 @@ public class ViewPostDetailActivity extends AppCompatActivity { private boolean isLoadingMoreChildren = false; private boolean isRefreshing = false; - private ArrayList mCommentsData; private ArrayList children; private int mChildrenStartingIndex = 0; @@ -167,8 +166,6 @@ public class ViewPostDetailActivity extends AppCompatActivity { postListPosition = getIntent().getExtras().getInt(EXTRA_POST_LIST_POSITION); } - mCommentsData = new ArrayList<>(); - mRecyclerView.setNestedScrollingEnabled(false); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); @@ -507,15 +504,9 @@ public class ViewPostDetailActivity extends AppCompatActivity { String parentId, ArrayList children) { ViewPostDetailActivity.this.children = children; mCommentProgressbar.setVisibility(View.GONE); - mCommentsData.addAll(commentsData); - if (mCommentsData.size() > 0) { + if (commentsData.size() > 0) { if(mAdapter == null) { - mAdapter = new CommentRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, - mOauthRetrofit, mSharedPreferences, commentsData, mRecyclerView, - mPost.getSubredditNamePrefixed(), mPost.getId(), mLocale); - mRecyclerView.setAdapter(mAdapter); - mNestedScrollView.getViewTreeObserver().addOnScrollChangedListener(() -> { if(!isLoadingMoreChildren) { View view = mNestedScrollView.getChildAt(mNestedScrollView.getChildCount() - 1); @@ -526,10 +517,13 @@ public class ViewPostDetailActivity extends AppCompatActivity { } } }); - } else { - mAdapter.notifyDataSetChanged(); } + mAdapter = new CommentRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, + mOauthRetrofit, mSharedPreferences, commentsData, mRecyclerView, + mPost.getSubredditNamePrefixed(), mPost.getId(), mLocale); + mRecyclerView.setAdapter(mAdapter); + mCommentCardView.setVisibility(View.VISIBLE); } else { mNoCommentWrapperLinearLayout.setVisibility(View.VISIBLE); @@ -627,11 +621,10 @@ public class ViewPostDetailActivity extends AppCompatActivity { private void refresh() { if(!isRefreshing) { isRefreshing = true; + mChildrenStartingIndex = 0; - if(mAdapter != null && mCommentsData != null) { - int size = mCommentsData.size(); - mCommentsData.clear(); - mAdapter.notifyItemRangeRemoved(0, size); + if(mAdapter != null) { + mAdapter.clearData(); } fetchComment();