Set onClickListener in ViewHolder instead of onBindViewHolder in CommentRecyclerViewAdapter. Do nothing when failing to vote comments.

This commit is contained in:
Alex Ning 2019-06-23 17:53:30 +08:00
parent e9cec91a5b
commit 34d49d884c
5 changed files with 291 additions and 402 deletions

View File

@ -6,6 +6,10 @@ import android.os.Parcelable;
import java.util.ArrayList; import java.util.ArrayList;
class CommentData implements Parcelable { class CommentData implements Parcelable {
static final int VOTE_TYPE_NO_VOTE = 0;
static final int VOTE_TYPE_UPVOTE = 1;
static final int VOTE_TYPE_DOWNVOTE = -1;
private String id; private String id;
private String fullName; private String fullName;
private String author; private String author;

View File

@ -3,14 +3,12 @@ package ml.docilealligator.infinityforreddit;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.ColorFilter;
import android.net.Uri; import android.net.Uri;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.browser.customtabs.CustomTabsIntent; import androidx.browser.customtabs.CustomTabsIntent;
@ -29,8 +27,6 @@ import ru.noties.markwon.view.MarkwonView;
class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int VIEW_TYPE_COMMENT = 0; private static final int VIEW_TYPE_COMMENT = 0;
private static final int VIEW_TYPE_LOAD_MORE_COMMENT = 1; private static final int VIEW_TYPE_LOAD_MORE_COMMENT = 1;
private static final int VIEW_TYPE_IS_LOADING_MORE_COMMENT = 2;
private static final int VIEW_TYPE_LOAD_MORE_COMMENT_FAILED = 3;
private Activity mActivity; private Activity mActivity;
private Retrofit mRetrofit; private Retrofit mRetrofit;
@ -59,28 +55,17 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
if(!comment.isPlaceHolder()) { if(!comment.isPlaceHolder()) {
return VIEW_TYPE_COMMENT; return VIEW_TYPE_COMMENT;
} else { } else {
if(comment.isLoadMoreChildrenFailed()) { return VIEW_TYPE_LOAD_MORE_COMMENT;
return VIEW_TYPE_LOAD_MORE_COMMENT_FAILED;
} else if(comment.isLoadingMoreChildren()) {
return VIEW_TYPE_IS_LOADING_MORE_COMMENT;
} else {
return VIEW_TYPE_LOAD_MORE_COMMENT;
}
} }
} }
@NonNull @NonNull
@Override @Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
switch (viewType) { if(viewType == VIEW_TYPE_COMMENT) {
case VIEW_TYPE_COMMENT: return new CommentViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment, parent, false));
return new CommentViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment, parent, false)); } else {
case VIEW_TYPE_LOAD_MORE_COMMENT: return new LoadMoreCommentViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_load_more_comments_placeholder, parent, false));
return new LoadMoreCommentViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_load_more_comments, parent, false));
case VIEW_TYPE_IS_LOADING_MORE_COMMENT:
return new IsLoadingMoreCommentViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_is_loading_more_comments, parent, false));
default:
return new LoadMoreCommentFailedViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_load_more_comments_failed, parent, false));
} }
} }
@ -91,11 +76,6 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
String authorPrefixed = "u/" + commentItem.getAuthor(); String authorPrefixed = "u/" + commentItem.getAuthor();
((CommentViewHolder) holder).authorTextView.setText(authorPrefixed); ((CommentViewHolder) holder).authorTextView.setText(authorPrefixed);
((CommentViewHolder) holder).authorTextView.setOnClickListener(view -> {
Intent intent = new Intent(mActivity, ViewUserDetailActivity.class);
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, commentItem.getAuthor());
mActivity.startActivity(intent);
});
((CommentViewHolder) holder).commentTimeTextView.setText(commentItem.getCommentTime()); ((CommentViewHolder) holder).commentTimeTextView.setText(commentItem.getCommentTime());
@ -123,14 +103,6 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
((CommentViewHolder) holder).verticalBlock.getLayoutParams().width = commentItem.getDepth() * 16; ((CommentViewHolder) holder).verticalBlock.getLayoutParams().width = commentItem.getDepth() * 16;
((CommentViewHolder) holder).shareButton.setOnClickListener(view -> {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
String extraText = commentItem.getPermalink();
intent.putExtra(Intent.EXTRA_TEXT, extraText);
mActivity.startActivity(Intent.createChooser(intent, "Share"));
});
if (commentItem.hasReply()) { if (commentItem.hasReply()) {
if(commentItem.isExpanded()) { if(commentItem.isExpanded()) {
((CommentViewHolder) holder).expandButton.setImageResource(R.drawable.ic_expand_less_black_20dp); ((CommentViewHolder) holder).expandButton.setImageResource(R.drawable.ic_expand_less_black_20dp);
@ -140,29 +112,6 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
((CommentViewHolder) holder).expandButton.setVisibility(View.VISIBLE); ((CommentViewHolder) holder).expandButton.setVisibility(View.VISIBLE);
} }
((CommentViewHolder) holder).expandButton.setOnClickListener(view -> {
if(commentItem.isExpanded()) {
collapseChildren(holder.getAdapterPosition());
((CommentViewHolder) holder).expandButton
.setImageResource(R.drawable.ic_expand_more_black_20dp);
} else {
expandChildren(holder.getAdapterPosition());
commentItem.setExpanded(true);
((CommentViewHolder) holder).expandButton
.setImageResource(R.drawable.ic_expand_less_black_20dp);
}
});
((CommentViewHolder) holder).replyButton.setOnClickListener(view -> {
Intent intent = new Intent(mActivity, CommentActivity.class);
intent.putExtra(CommentActivity.EXTRA_PARENT_DEPTH_KEY, commentItem.getDepth() + 1);
intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_TEXT_KEY, commentItem.getCommentContent());
intent.putExtra(CommentActivity.EXTRA_PARENT_FULLNAME_KEY, commentItem.getFullName());
intent.putExtra(CommentActivity.EXTRA_IS_REPLYING_KEY, true);
intent.putExtra(CommentActivity.EXTRA_PARENT_POSITION_KEY, holder.getAdapterPosition());
mActivity.startActivityForResult(intent, CommentActivity.WRITE_COMMENT_REQUEST_CODE);
});
switch (commentItem.getVoteType()) { switch (commentItem.getVoteType()) {
case 1: case 1:
((CommentViewHolder) holder).upvoteButton ((CommentViewHolder) holder).upvoteButton
@ -173,282 +122,18 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
.setColorFilter(ContextCompat.getColor(mActivity, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN); .setColorFilter(ContextCompat.getColor(mActivity, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN);
break; break;
} }
} else {
((CommentViewHolder) holder).upvoteButton.setOnClickListener(view -> {
ColorFilter previousUpvoteButtonColorFilter = ((CommentViewHolder) holder).upvoteButton.getColorFilter();
ColorFilter previousDownvoteButtonColorFilter = ((CommentViewHolder) holder).downvoteButton.getColorFilter();
int previousVoteType = commentItem.getVoteType();
String newVoteType;
((CommentViewHolder) holder).downvoteButton.clearColorFilter();
if(previousUpvoteButtonColorFilter == null) {
//Not upvoted before
commentItem.setVoteType(1);
newVoteType = RedditUtils.DIR_UPVOTE;
((CommentViewHolder) holder).upvoteButton
.setColorFilter(ContextCompat.getColor(mActivity, R.color.backgroundColorPrimaryDark), android.graphics.PorterDuff.Mode.SRC_IN);
} else {
//Upvoted before
commentItem.setVoteType(0);
newVoteType = RedditUtils.DIR_UNVOTE;
((CommentViewHolder) holder).upvoteButton.clearColorFilter();
}
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + commentItem.getVoteType()));
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() {
@Override
public void onVoteThingSuccess(int position1) {
if(newVoteType.equals(RedditUtils.DIR_UPVOTE)) {
commentItem.setVoteType(1);
((CommentViewHolder) holder).upvoteButton
.setColorFilter(ContextCompat.getColor(mActivity, R.color.backgroundColorPrimaryDark), android.graphics.PorterDuff.Mode.SRC_IN);
} else {
commentItem.setVoteType(0);
((CommentViewHolder) holder).upvoteButton.clearColorFilter();
}
((CommentViewHolder) holder).downvoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + commentItem.getVoteType()));
}
@Override
public void onVoteThingFail(int position1) {
Toast.makeText(mActivity, R.string.vote_failed, Toast.LENGTH_SHORT).show();
commentItem.setVoteType(previousVoteType);
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + previousVoteType));
((CommentViewHolder) holder).upvoteButton.setColorFilter(previousUpvoteButtonColorFilter);
((CommentViewHolder) holder).downvoteButton.setColorFilter(previousDownvoteButtonColorFilter);
}
}, commentItem.getFullName(), newVoteType, holder.getAdapterPosition());
});
((CommentViewHolder) holder).downvoteButton.setOnClickListener(view -> {
ColorFilter previousUpvoteButtonColorFilter = ((CommentViewHolder) holder).upvoteButton.getColorFilter();
ColorFilter previousDownvoteButtonColorFilter = ((CommentViewHolder) holder).downvoteButton.getColorFilter();
int previousVoteType = commentItem.getVoteType();
String newVoteType;
((CommentViewHolder) holder).upvoteButton.clearColorFilter();
if(previousDownvoteButtonColorFilter == null) {
//Not downvoted before
commentItem.setVoteType(-1);
newVoteType = RedditUtils.DIR_DOWNVOTE;
((CommentViewHolder) holder).downvoteButton
.setColorFilter(ContextCompat.getColor(mActivity, R.color.colorAccent), android.graphics.PorterDuff.Mode.SRC_IN);
} else {
//Downvoted before
commentItem.setVoteType(0);
newVoteType = RedditUtils.DIR_UNVOTE;
((CommentViewHolder) holder).downvoteButton.clearColorFilter();
}
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + commentItem.getVoteType()));
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() {
@Override
public void onVoteThingSuccess(int position1) {
if(newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) {
commentItem.setVoteType(-1);
((CommentViewHolder) holder).downvoteButton
.setColorFilter(ContextCompat.getColor(mActivity, R.color.colorAccent), android.graphics.PorterDuff.Mode.SRC_IN);
} else {
commentItem.setVoteType(0);
((CommentViewHolder) holder).downvoteButton.clearColorFilter();
}
((CommentViewHolder) holder).upvoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + commentItem.getVoteType()));
}
@Override
public void onVoteThingFail(int position1) {
Toast.makeText(mActivity, R.string.vote_failed, Toast.LENGTH_SHORT).show();
commentItem.setVoteType(previousVoteType);
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + previousVoteType));
((CommentViewHolder) holder).upvoteButton.setColorFilter(previousUpvoteButtonColorFilter);
((CommentViewHolder) holder).downvoteButton.setColorFilter(previousDownvoteButtonColorFilter);
}
}, commentItem.getFullName(), newVoteType, holder.getAdapterPosition());
});
} else if(holder.getItemViewType() == VIEW_TYPE_LOAD_MORE_COMMENT) {
((LoadMoreCommentViewHolder) holder).verticalBlock.getLayoutParams().width = mVisibleComments.get(holder.getAdapterPosition()).getDepth() * 16; ((LoadMoreCommentViewHolder) holder).verticalBlock.getLayoutParams().width = mVisibleComments.get(holder.getAdapterPosition()).getDepth() * 16;
if(mVisibleComments.get(holder.getAdapterPosition()).isLoadingMoreChildren()) {
((LoadMoreCommentViewHolder) holder).textView.setOnClickListener(view -> { ((LoadMoreCommentViewHolder) holder).placeholderTextView.setText(R.string.loading);
loadMoreComments(holder.getAdapterPosition()); } else if(mVisibleComments.get(holder.getAdapterPosition()).isLoadMoreChildrenFailed()) {
}); ((LoadMoreCommentViewHolder) holder).placeholderTextView.setText(R.string.comment_load_more_comments_failed);
} else if(holder.getItemViewType() == VIEW_TYPE_IS_LOADING_MORE_COMMENT){ } else {
((IsLoadingMoreCommentViewHolder) holder).verticalBlock.getLayoutParams().width = mVisibleComments.get(holder.getAdapterPosition()).getDepth() * 16; ((LoadMoreCommentViewHolder) holder).placeholderTextView.setText(R.string.comment_load_more_comments);
} else if(holder.getItemViewType() == VIEW_TYPE_LOAD_MORE_COMMENT_FAILED) { }
((LoadMoreCommentFailedViewHolder) holder).verticalBlock.getLayoutParams().width = mVisibleComments.get(holder.getAdapterPosition()).getDepth() * 16;
((LoadMoreCommentFailedViewHolder) holder).retryTextView.setOnClickListener(view -> {
loadMoreComments(holder.getAdapterPosition());
});
} }
} }
private void loadMoreComments(int position) {
int parentPosition = getParentPosition(position);
CommentData parentComment = mVisibleComments.get(parentPosition);
mVisibleComments.get(position).setLoadingMoreChildren(true);
mVisibleComments.get(position).setLoadMoreChildrenFailed(false);
notifyItemChanged(position);
FetchComment.fetchMoreComment(mRetrofit, mSubredditNamePrefixed, parentComment.getMoreChildrenFullnames(),
parentComment.getMoreChildrenStartingIndex(), parentComment.getDepth() + 1, mLocale,
new FetchComment.FetchMoreCommentListener() {
@Override
public void onFetchMoreCommentSuccess(ArrayList<CommentData> expandedComments,
int childrenStartingIndex) {
if(mVisibleComments.size() > parentPosition
&& parentComment.getFullName().equals(mVisibleComments.get(parentPosition).getFullName())) {
if(mVisibleComments.get(parentPosition).isExpanded()) {
if(mVisibleComments.get(parentPosition).getChildren().size() > childrenStartingIndex) {
mVisibleComments.get(parentPosition).setMoreChildrenStartingIndex(childrenStartingIndex);
mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1)
.setLoadingMoreChildren(false);
mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1)
.setLoadMoreChildrenFailed(false);
int placeholderPosition = position;
if(mVisibleComments.get(position).getFullName().equals(parentComment.getFullName())) {
for(int i = parentPosition + 1; i < mVisibleComments.size(); i++) {
if(mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) {
placeholderPosition = i;
break;
}
}
}
mVisibleComments.get(placeholderPosition).setLoadingMoreChildren(false);
mVisibleComments.get(placeholderPosition).setLoadMoreChildrenFailed(false);
notifyItemChanged(placeholderPosition);
mVisibleComments.addAll(placeholderPosition, expandedComments);
notifyItemRangeInserted(placeholderPosition, expandedComments.size());
} else {
mVisibleComments.get(parentPosition).getChildren()
.remove(mVisibleComments.get(parentPosition).getChildren().size() - 1);
mVisibleComments.get(parentPosition).removeMoreChildrenFullnames();
int placeholderPosition = position;
if(mVisibleComments.get(position).getFullName().equals(parentComment.getFullName())) {
for(int i = parentPosition + 1; i < mVisibleComments.size(); i++) {
if(mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) {
placeholderPosition = i;
break;
}
}
}
mVisibleComments.remove(placeholderPosition);
notifyItemRemoved(placeholderPosition);
mVisibleComments.addAll(placeholderPosition, expandedComments);
notifyItemRangeInserted(placeholderPosition, expandedComments.size());
}
} else {
if(mVisibleComments.get(parentPosition).hasReply() && mVisibleComments.get(parentPosition).getChildren().size() <= childrenStartingIndex) {
mVisibleComments.get(parentPosition).getChildren()
.remove(mVisibleComments.get(parentPosition).getChildren().size() - 1);
mVisibleComments.get(parentPosition).removeMoreChildrenFullnames();
}
}
mVisibleComments.get(parentPosition).addChildren(expandedComments);
} else {
for(int i = 0; i < mVisibleComments.size(); i++) {
if(mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) {
if(mVisibleComments.get(i).isExpanded()) {
int placeholderPosition = i + mVisibleComments.get(i).getChildren().size();
if(!mVisibleComments.get(i).getFullName()
.equals(mVisibleComments.get(placeholderPosition).getFullName())) {
for(int j = i + 1; j < mVisibleComments.size(); j++) {
if(mVisibleComments.get(j).getFullName().equals(mVisibleComments.get(i).getFullName())) {
placeholderPosition = j;
}
}
}
mVisibleComments.get(placeholderPosition).setLoadingMoreChildren(false);
mVisibleComments.get(placeholderPosition).setLoadMoreChildrenFailed(false);
notifyItemChanged(placeholderPosition);
mVisibleComments.addAll(placeholderPosition, expandedComments);
notifyItemRangeInserted(placeholderPosition, expandedComments.size());
}
mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1)
.setLoadingMoreChildren(false);
mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1)
.setLoadMoreChildrenFailed(false);
mVisibleComments.get(i).addChildren(expandedComments);
break;
}
}
}
}
@Override
public void onFetchMoreCommentFailed() {
if(parentPosition < mVisibleComments.size()
&& parentComment.getFullName().equals(mVisibleComments.get(parentPosition).getFullName())) {
if(mVisibleComments.get(parentPosition).isExpanded()) {
int placeholderPosition = position;
if(!mVisibleComments.get(position).getFullName().equals(parentComment.getFullName())) {
for(int i = parentPosition + 1; i < mVisibleComments.size(); i++) {
if(mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) {
placeholderPosition = i;
break;
}
}
}
mVisibleComments.get(placeholderPosition).setLoadingMoreChildren(false);
mVisibleComments.get(placeholderPosition).setLoadMoreChildrenFailed(true);
notifyItemChanged(placeholderPosition);
}
mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1)
.setLoadingMoreChildren(false);
mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1)
.setLoadMoreChildrenFailed(true);
} else {
for(int i = 0; i < mVisibleComments.size(); i++) {
if(mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) {
if(mVisibleComments.get(i).isExpanded()) {
int placeholderPosition = i + mVisibleComments.get(i).getChildren().size();
if(!mVisibleComments.get(placeholderPosition).getFullName().equals(mVisibleComments.get(i).getFullName())) {
for(int j = i + 1; j < mVisibleComments.size(); j++) {
if(mVisibleComments.get(j).getFullName().equals(mVisibleComments.get(i).getFullName())) {
placeholderPosition = j;
break;
}
}
}
mVisibleComments.get(placeholderPosition).setLoadingMoreChildren(false);
mVisibleComments.get(placeholderPosition).setLoadMoreChildrenFailed(true);
notifyItemChanged(placeholderPosition);
}
mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1).setLoadingMoreChildren(false);
mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1).setLoadMoreChildrenFailed(true);
break;
}
}
}
}
});
}
private int getParentPosition(int position) { private int getParentPosition(int position) {
int childDepth = mVisibleComments.get(position).getDepth(); int childDepth = mVisibleComments.get(position).getDepth();
for(int i = position; i >= 0; i--) { for(int i = position; i >= 0; i--) {
@ -539,7 +224,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
return mVisibleComments.size(); return mVisibleComments.size();
} }
static class CommentViewHolder extends RecyclerView.ViewHolder { class CommentViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.author_text_view_item_post_comment) TextView authorTextView; @BindView(R.id.author_text_view_item_post_comment) TextView authorTextView;
@BindView(R.id.comment_time_text_view_item_post_comment) TextView commentTimeTextView; @BindView(R.id.comment_time_text_view_item_post_comment) TextView commentTimeTextView;
@BindView(R.id.comment_markdown_view_item_post_comment) MarkwonView commentMarkdownView; @BindView(R.id.comment_markdown_view_item_post_comment) MarkwonView commentMarkdownView;
@ -554,35 +239,290 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
CommentViewHolder(View itemView) { CommentViewHolder(View itemView) {
super(itemView); super(itemView);
ButterKnife.bind(this, itemView); ButterKnife.bind(this, itemView);
authorTextView.setOnClickListener(view -> {
Intent intent = new Intent(mActivity, ViewUserDetailActivity.class);
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, mVisibleComments.get(getAdapterPosition()).getAuthor());
mActivity.startActivity(intent);
});
shareButton.setOnClickListener(view -> {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
String extraText = mVisibleComments.get(getAdapterPosition()).getPermalink();
intent.putExtra(Intent.EXTRA_TEXT, extraText);
mActivity.startActivity(Intent.createChooser(intent, "Share"));
});
expandButton.setOnClickListener(view -> {
if(mVisibleComments.get(getAdapterPosition()).isExpanded()) {
collapseChildren(getAdapterPosition());
expandButton.setImageResource(R.drawable.ic_expand_more_black_20dp);
} else {
expandChildren(getAdapterPosition());
mVisibleComments.get(getAdapterPosition()).setExpanded(true);
expandButton.setImageResource(R.drawable.ic_expand_less_black_20dp);
}
});
replyButton.setOnClickListener(view -> {
Intent intent = new Intent(mActivity, CommentActivity.class);
intent.putExtra(CommentActivity.EXTRA_PARENT_DEPTH_KEY, mVisibleComments.get(getAdapterPosition()).getDepth() + 1);
intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_TEXT_KEY, mVisibleComments.get(getAdapterPosition()).getCommentContent());
intent.putExtra(CommentActivity.EXTRA_PARENT_FULLNAME_KEY, mVisibleComments.get(getAdapterPosition()).getFullName());
intent.putExtra(CommentActivity.EXTRA_IS_REPLYING_KEY, true);
intent.putExtra(CommentActivity.EXTRA_PARENT_POSITION_KEY, getAdapterPosition());
mActivity.startActivityForResult(intent, CommentActivity.WRITE_COMMENT_REQUEST_CODE);
});
upvoteButton.setOnClickListener(view -> {
int previousVoteType = mVisibleComments.get(getAdapterPosition()).getVoteType();
String newVoteType;
downvoteButton.clearColorFilter();
if(previousVoteType != CommentData.VOTE_TYPE_UPVOTE) {
//Not upvoted before
mVisibleComments.get(getAdapterPosition()).setVoteType(CommentData.VOTE_TYPE_UPVOTE);
newVoteType = RedditUtils.DIR_UPVOTE;
upvoteButton.setColorFilter(ContextCompat.getColor(mActivity, R.color.backgroundColorPrimaryDark), android.graphics.PorterDuff.Mode.SRC_IN);
} else {
//Upvoted before
mVisibleComments.get(getAdapterPosition()).setVoteType(CommentData.VOTE_TYPE_NO_VOTE);
newVoteType = RedditUtils.DIR_UNVOTE;
upvoteButton.clearColorFilter();
}
scoreTextView.setText(Integer.toString(mVisibleComments.get(getAdapterPosition()).getScore() + mVisibleComments.get(getAdapterPosition()).getVoteType()));
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() {
@Override
public void onVoteThingSuccess(int position) {
if(newVoteType.equals(RedditUtils.DIR_UPVOTE)) {
mVisibleComments.get(getAdapterPosition()).setVoteType(CommentData.VOTE_TYPE_UPVOTE);
upvoteButton.setColorFilter(ContextCompat.getColor(mActivity, R.color.backgroundColorPrimaryDark), android.graphics.PorterDuff.Mode.SRC_IN);
} else {
mVisibleComments.get(getAdapterPosition()).setVoteType(CommentData.VOTE_TYPE_NO_VOTE);
upvoteButton.clearColorFilter();
}
downvoteButton.clearColorFilter();
scoreTextView.setText(Integer.toString(mVisibleComments.get(getAdapterPosition()).getScore() + mVisibleComments.get(getAdapterPosition()).getVoteType()));
}
@Override
public void onVoteThingFail(int position) { }
}, mVisibleComments.get(getAdapterPosition()).getFullName(), newVoteType, getAdapterPosition());
});
downvoteButton.setOnClickListener(view -> {
int previousVoteType = mVisibleComments.get(getAdapterPosition()).getVoteType();
String newVoteType;
upvoteButton.clearColorFilter();
if(previousVoteType != CommentData.VOTE_TYPE_DOWNVOTE) {
//Not downvoted before
mVisibleComments.get(getAdapterPosition()).setVoteType(CommentData.VOTE_TYPE_DOWNVOTE);
newVoteType = RedditUtils.DIR_DOWNVOTE;
downvoteButton.setColorFilter(ContextCompat.getColor(mActivity, R.color.colorAccent), android.graphics.PorterDuff.Mode.SRC_IN);
} else {
//Downvoted before
mVisibleComments.get(getAdapterPosition()).setVoteType(CommentData.VOTE_TYPE_NO_VOTE);
newVoteType = RedditUtils.DIR_UNVOTE;
downvoteButton.clearColorFilter();
}
scoreTextView.setText(Integer.toString(mVisibleComments.get(getAdapterPosition()).getScore() + mVisibleComments.get(getAdapterPosition()).getVoteType()));
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() {
@Override
public void onVoteThingSuccess(int position1) {
if(newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) {
mVisibleComments.get(getAdapterPosition()).setVoteType(CommentData.VOTE_TYPE_DOWNVOTE);
downvoteButton.setColorFilter(ContextCompat.getColor(mActivity, R.color.colorAccent), android.graphics.PorterDuff.Mode.SRC_IN);
} else {
mVisibleComments.get(getAdapterPosition()).setVoteType(CommentData.VOTE_TYPE_NO_VOTE);
downvoteButton.clearColorFilter();
}
upvoteButton.clearColorFilter();
scoreTextView.setText(Integer.toString(mVisibleComments.get(getAdapterPosition()).getScore() + mVisibleComments.get(getAdapterPosition()).getVoteType()));
}
@Override
public void onVoteThingFail(int position1) { }
}, mVisibleComments.get(getAdapterPosition()).getFullName(), newVoteType, getAdapterPosition());
});
} }
} }
static class LoadMoreCommentViewHolder extends RecyclerView.ViewHolder { class LoadMoreCommentViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.vertical_block_item_load_more_comments) View verticalBlock; @BindView(R.id.vertical_block_item_load_more_comments) View verticalBlock;
@BindView(R.id.load_more_comments_text_view_item_load_more_comments) TextView textView; @BindView(R.id.placeholder_text_view_item_load_more_comments) TextView placeholderTextView;
LoadMoreCommentViewHolder(View itemView) { LoadMoreCommentViewHolder(View itemView) {
super(itemView); super(itemView);
ButterKnife.bind(this, itemView); ButterKnife.bind(this, itemView);
}
}
static class IsLoadingMoreCommentViewHolder extends RecyclerView.ViewHolder { placeholderTextView.setOnClickListener(view -> {
@BindView(R.id.vertical_block_item_is_loading_more_comments) View verticalBlock; int parentPosition = getParentPosition(getAdapterPosition());
CommentData parentComment = mVisibleComments.get(parentPosition);
IsLoadingMoreCommentViewHolder(View itemView) { mVisibleComments.get(getAdapterPosition()).setLoadingMoreChildren(true);
super(itemView); mVisibleComments.get(getAdapterPosition()).setLoadMoreChildrenFailed(false);
ButterKnife.bind(this, itemView); placeholderTextView.setText(R.string.loading);
}
}
static class LoadMoreCommentFailedViewHolder extends RecyclerView.ViewHolder { FetchComment.fetchMoreComment(mRetrofit, mSubredditNamePrefixed, parentComment.getMoreChildrenFullnames(),
@BindView(R.id.vertical_block_item_load_more_comments_failed) View verticalBlock; parentComment.getMoreChildrenStartingIndex(), parentComment.getDepth() + 1, mLocale,
@BindView(R.id.retry_text_view_item_load_more_comments_failed) TextView retryTextView; new FetchComment.FetchMoreCommentListener() {
@Override
public void onFetchMoreCommentSuccess(ArrayList<CommentData> expandedComments,
int childrenStartingIndex) {
if(mVisibleComments.size() > parentPosition
&& parentComment.getFullName().equals(mVisibleComments.get(parentPosition).getFullName())) {
if(mVisibleComments.get(parentPosition).isExpanded()) {
if(mVisibleComments.get(parentPosition).getChildren().size() > childrenStartingIndex) {
mVisibleComments.get(parentPosition).setMoreChildrenStartingIndex(childrenStartingIndex);
mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1)
.setLoadingMoreChildren(false);
mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1)
.setLoadMoreChildrenFailed(false);
LoadMoreCommentFailedViewHolder(View itemView) { int placeholderPosition = getAdapterPosition();
super(itemView); if(mVisibleComments.get(getAdapterPosition()).getFullName().equals(parentComment.getFullName())) {
ButterKnife.bind(this, itemView); for(int i = parentPosition + 1; i < mVisibleComments.size(); i++) {
if(mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) {
placeholderPosition = i;
break;
}
}
}
mVisibleComments.get(placeholderPosition).setLoadingMoreChildren(false);
mVisibleComments.get(placeholderPosition).setLoadMoreChildrenFailed(false);
placeholderTextView.setText(R.string.comment_load_more_comments);
mVisibleComments.addAll(placeholderPosition, expandedComments);
notifyItemRangeInserted(placeholderPosition, expandedComments.size());
} else {
mVisibleComments.get(parentPosition).getChildren()
.remove(mVisibleComments.get(parentPosition).getChildren().size() - 1);
mVisibleComments.get(parentPosition).removeMoreChildrenFullnames();
int placeholderPosition = getAdapterPosition();
if(mVisibleComments.get(getAdapterPosition()).getFullName().equals(parentComment.getFullName())) {
for(int i = parentPosition + 1; i < mVisibleComments.size(); i++) {
if(mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) {
placeholderPosition = i;
break;
}
}
}
mVisibleComments.remove(placeholderPosition);
notifyItemRemoved(placeholderPosition);
mVisibleComments.addAll(placeholderPosition, expandedComments);
notifyItemRangeInserted(placeholderPosition, expandedComments.size());
}
} else {
if(mVisibleComments.get(parentPosition).hasReply() && mVisibleComments.get(parentPosition).getChildren().size() <= childrenStartingIndex) {
mVisibleComments.get(parentPosition).getChildren()
.remove(mVisibleComments.get(parentPosition).getChildren().size() - 1);
mVisibleComments.get(parentPosition).removeMoreChildrenFullnames();
}
}
mVisibleComments.get(parentPosition).addChildren(expandedComments);
} else {
for(int i = 0; i < mVisibleComments.size(); i++) {
if(mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) {
if(mVisibleComments.get(i).isExpanded()) {
int placeholderPosition = i + mVisibleComments.get(i).getChildren().size();
if(!mVisibleComments.get(i).getFullName()
.equals(mVisibleComments.get(placeholderPosition).getFullName())) {
for(int j = i + 1; j < mVisibleComments.size(); j++) {
if(mVisibleComments.get(j).getFullName().equals(mVisibleComments.get(i).getFullName())) {
placeholderPosition = j;
}
}
}
mVisibleComments.get(placeholderPosition).setLoadingMoreChildren(false);
mVisibleComments.get(placeholderPosition).setLoadMoreChildrenFailed(false);
placeholderTextView.setText(R.string.comment_load_more_comments);
mVisibleComments.addAll(placeholderPosition, expandedComments);
notifyItemRangeInserted(placeholderPosition, expandedComments.size());
}
mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1)
.setLoadingMoreChildren(false);
mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1)
.setLoadMoreChildrenFailed(false);
mVisibleComments.get(i).addChildren(expandedComments);
break;
}
}
}
}
@Override
public void onFetchMoreCommentFailed() {
if(parentPosition < mVisibleComments.size()
&& parentComment.getFullName().equals(mVisibleComments.get(parentPosition).getFullName())) {
if(mVisibleComments.get(parentPosition).isExpanded()) {
int placeholderPosition = getAdapterPosition();
if(!mVisibleComments.get(getAdapterPosition()).getFullName().equals(parentComment.getFullName())) {
for(int i = parentPosition + 1; i < mVisibleComments.size(); i++) {
if(mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) {
placeholderPosition = i;
break;
}
}
}
mVisibleComments.get(placeholderPosition).setLoadingMoreChildren(false);
mVisibleComments.get(placeholderPosition).setLoadMoreChildrenFailed(true);
placeholderTextView.setText(R.string.comment_load_more_comments_failed);
}
mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1)
.setLoadingMoreChildren(false);
mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1)
.setLoadMoreChildrenFailed(true);
} else {
for(int i = 0; i < mVisibleComments.size(); i++) {
if(mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) {
if(mVisibleComments.get(i).isExpanded()) {
int placeholderPosition = i + mVisibleComments.get(i).getChildren().size();
if(!mVisibleComments.get(placeholderPosition).getFullName().equals(mVisibleComments.get(i).getFullName())) {
for(int j = i + 1; j < mVisibleComments.size(); j++) {
if(mVisibleComments.get(j).getFullName().equals(mVisibleComments.get(i).getFullName())) {
placeholderPosition = j;
break;
}
}
}
mVisibleComments.get(placeholderPosition).setLoadingMoreChildren(false);
mVisibleComments.get(placeholderPosition).setLoadMoreChildrenFailed(true);
placeholderTextView.setText(R.string.comment_load_more_comments_failed);
}
mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1).setLoadingMoreChildren(false);
mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1).setLoadMoreChildrenFailed(true);
break;
}
}
}
}
});
});
} }
} }
} }

View File

@ -1,26 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="@+id/vertical_block_item_is_loading_more_comments"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@color/textColorPrimaryDark" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="8dp"
android:text="@string/loading"
android:textColor="@color/primaryTextColor" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>

View File

@ -1,29 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="@+id/vertical_block_item_load_more_comments_failed"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@color/textColorPrimaryDark" />
<TextView
android:id="@+id/retry_text_view_item_load_more_comments_failed"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:gravity="center"
android:padding="8dp"
android:text="@string/comment_load_more_comments_failed"
android:textColor="@color/primaryTextColor" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>

View File

@ -14,7 +14,7 @@
android:background="@color/textColorPrimaryDark" /> android:background="@color/textColorPrimaryDark" />
<TextView <TextView
android:id="@+id/load_more_comments_text_view_item_load_more_comments" android:id="@+id/placeholder_text_view_item_load_more_comments"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center" android:gravity="center"