Fixed displaying wrong score when rapidly clicking vote buttons in CommentMultiLevelRecyclerViewAdapter and ViewPostDetailActivity.

This commit is contained in:
Alex Ning 2019-04-18 18:11:11 +08:00
parent 246d4f45ab
commit a7e9695717
3 changed files with 186 additions and 240 deletions

View File

@ -5,10 +5,6 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.ColorFilter; import android.graphics.ColorFilter;
import android.net.Uri; import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -25,6 +21,10 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import androidx.annotation.NonNull;
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import retrofit2.Retrofit; import retrofit2.Retrofit;
@ -143,114 +143,101 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter {
} }
((CommentViewHolder) holder).upvoteButton.setOnClickListener(view -> { ((CommentViewHolder) holder).upvoteButton.setOnClickListener(view -> {
final boolean isDownvotedBefore = ((CommentViewHolder) holder).downvoteButton.getColorFilter() != null; ColorFilter previousUpvoteButtonColorFilter = ((CommentViewHolder) holder).upvoteButton.getColorFilter();
final ColorFilter minusButtonColorFilter = ((CommentViewHolder) holder).downvoteButton.getColorFilter(); ColorFilter previousDownvoteButtonColorFilter = ((CommentViewHolder) holder).downvoteButton.getColorFilter();
int previousVoteType = commentItem.getVoteType();
String newVoteType;
((CommentViewHolder) holder).downvoteButton.clearColorFilter(); ((CommentViewHolder) holder).downvoteButton.clearColorFilter();
if (((CommentViewHolder) holder).upvoteButton.getColorFilter() == null) { if(previousUpvoteButtonColorFilter == null) {
((CommentViewHolder) holder).upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); //Not upvoted before
if (isDownvotedBefore) { commentItem.setVoteType(1);
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 2)); newVoteType = RedditUtils.DIR_UPVOTE;
} else { ((CommentViewHolder) holder).upvoteButton
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 1)); .setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
}
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() {
@Override
public void onVoteThingSuccess(int position1) {
commentItem.setVoteType(1);
if (isDownvotedBefore) {
commentItem.setScore(commentItem.getScore() + 2);
} else {
commentItem.setScore(commentItem.getScore() + 1);
}
}
@Override
public void onVoteThingFail(int position1) {
Toast.makeText(mContext, "Cannot upvote this comment", Toast.LENGTH_SHORT).show();
((CommentViewHolder) holder).upvoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore()));
((CommentViewHolder) holder).downvoteButton.setColorFilter(minusButtonColorFilter);
}
}, commentItem.getFullName(), RedditUtils.DIR_UPVOTE, ((CommentViewHolder) holder).getAdapterPosition());
} else { } else {
//Upvoted before //Upvoted before
commentItem.setVoteType(0);
newVoteType = RedditUtils.DIR_UNVOTE;
((CommentViewHolder) holder).upvoteButton.clearColorFilter(); ((CommentViewHolder) holder).upvoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() - 1));
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() {
@Override
public void onVoteThingSuccess(int position1) {
commentItem.setVoteType(0);
commentItem.setScore(commentItem.getScore() - 1);
}
@Override
public void onVoteThingFail(int position1) {
Toast.makeText(mContext, "Cannot unvote this comment", Toast.LENGTH_SHORT).show();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 1));
((CommentViewHolder) holder).upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
commentItem.setScore(commentItem.getScore() + 1);
}
}, commentItem.getFullName(), RedditUtils.DIR_UNVOTE, ((CommentViewHolder) holder).getAdapterPosition());
} }
((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(mContext, R.color.colorPrimary), 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(mContext, R.string.vote_failed, Toast.LENGTH_SHORT).show();
((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 -> { ((CommentViewHolder) holder).downvoteButton.setOnClickListener(view -> {
final boolean isUpvotedBefore = ((CommentViewHolder) holder).upvoteButton.getColorFilter() != null; ColorFilter previousUpvoteButtonColorFilter = ((CommentViewHolder) holder).upvoteButton.getColorFilter();
ColorFilter previousDownvoteButtonColorFilter = ((CommentViewHolder) holder).downvoteButton.getColorFilter();
int previousVoteType = commentItem.getVoteType();
String newVoteType;
final ColorFilter upvoteButtonColorFilter = ((CommentViewHolder) holder).upvoteButton.getColorFilter();
((CommentViewHolder) holder).upvoteButton.clearColorFilter(); ((CommentViewHolder) holder).upvoteButton.clearColorFilter();
if (((CommentViewHolder) holder).downvoteButton.getColorFilter() == null) { if(previousDownvoteButtonColorFilter == null) {
((CommentViewHolder) holder).downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN); //Not downvoted before
if (isUpvotedBefore) { commentItem.setVoteType(-1);
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() - 2)); newVoteType = RedditUtils.DIR_DOWNVOTE;
} else { ((CommentViewHolder) holder).downvoteButton
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() - 1)); .setColorFilter(ContextCompat.getColor(mContext, 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(mContext, 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()));
} }
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() { @Override
@Override public void onVoteThingFail(int position1) {
public void onVoteThingSuccess(int position12) { Toast.makeText(mContext, R.string.vote_failed, Toast.LENGTH_SHORT).show();
commentItem.setVoteType(-1); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + previousVoteType));
if (isUpvotedBefore) { ((CommentViewHolder) holder).upvoteButton.setColorFilter(previousUpvoteButtonColorFilter);
commentItem.setScore(commentItem.getScore() - 2); ((CommentViewHolder) holder).downvoteButton.setColorFilter(previousDownvoteButtonColorFilter);
} else { }
commentItem.setScore(commentItem.getScore() - 1); }, commentItem.getFullName(), newVoteType, holder.getAdapterPosition());
}
}
@Override
public void onVoteThingFail(int position12) {
Toast.makeText(mContext, "Cannot downvote this comment", Toast.LENGTH_SHORT).show();
((CommentViewHolder) holder).downvoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore()));
((CommentViewHolder) holder).upvoteButton.setColorFilter(upvoteButtonColorFilter);
}
}, commentItem.getFullName(), RedditUtils.DIR_DOWNVOTE, holder.getAdapterPosition());
} else {
//Down voted before
((CommentViewHolder) holder).downvoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 1));
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() {
@Override
public void onVoteThingSuccess(int position12) {
commentItem.setVoteType(0);
commentItem.setScore(commentItem.getScore());
}
@Override
public void onVoteThingFail(int position12) {
Toast.makeText(mContext, "Cannot unvote this comment", Toast.LENGTH_SHORT).show();
((CommentViewHolder) holder).downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN);
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore()));
commentItem.setScore(commentItem.getScore());
}
}, commentItem.getFullName(), RedditUtils.DIR_UNVOTE, holder.getAdapterPosition());
}
}); });
} }

View File

@ -378,38 +378,38 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
} }
((DataViewHolder) holder).upvoteButton.setOnClickListener(view -> { ((DataViewHolder) holder).upvoteButton.setOnClickListener(view -> {
ColorFilter upvoteButtonColorFilter = ((DataViewHolder) holder).upvoteButton.getColorFilter(); ColorFilter previousUpvoteButtonColorFilter = ((DataViewHolder) holder).upvoteButton.getColorFilter();
ColorFilter downvoteButtonColorFilter = ((DataViewHolder) holder).downvoteButton.getColorFilter(); ColorFilter previousDownvoteButtonColorFilter = ((DataViewHolder) holder).downvoteButton.getColorFilter();
int previousVoteType = post.getVoteType(); int previousVoteType = post.getVoteType();
String newVoteType; String newVoteType;
((DataViewHolder) holder).downvoteButton.clearColorFilter(); ((DataViewHolder) holder).downvoteButton.clearColorFilter();
if(upvoteButtonColorFilter == null) { if(previousUpvoteButtonColorFilter == null) {
//Not upvoted before //Not upvoted before
post.setVoteType(1); post.setVoteType(1);
newVoteType = RedditUtils.DIR_UPVOTE; newVoteType = RedditUtils.DIR_UPVOTE;
((DataViewHolder) holder).upvoteButton
.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
} else { } else {
//Upvoted before //Upvoted before
post.setVoteType(0); post.setVoteType(0);
newVoteType = RedditUtils.DIR_UNVOTE; newVoteType = RedditUtils.DIR_UNVOTE;
}
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + post.getVoteType()));
if(((DataViewHolder) holder).upvoteButton.getColorFilter() == null) {
((DataViewHolder) holder).upvoteButton
.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
} else {
((DataViewHolder) holder).upvoteButton.clearColorFilter(); ((DataViewHolder) holder).upvoteButton.clearColorFilter();
} }
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + post.getVoteType()));
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() { VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() {
@Override @Override
public void onVoteThingSuccess(int position1) { public void onVoteThingSuccess(int position1) {
if(newVoteType.equals(RedditUtils.DIR_UPVOTE)) { if(newVoteType.equals(RedditUtils.DIR_UPVOTE)) {
post.setVoteType(1); post.setVoteType(1);
((DataViewHolder) holder).upvoteButton
.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
} else { } else {
post.setVoteType(0); post.setVoteType(0);
((DataViewHolder) holder).upvoteButton.clearColorFilter();
} }
((DataViewHolder) holder).downvoteButton.clearColorFilter(); ((DataViewHolder) holder).downvoteButton.clearColorFilter();
@ -420,47 +420,46 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
public void onVoteThingFail(int position1) { public void onVoteThingFail(int position1) {
Toast.makeText(mContext, R.string.vote_failed, Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, R.string.vote_failed, Toast.LENGTH_SHORT).show();
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + previousVoteType)); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + previousVoteType));
((DataViewHolder) holder).upvoteButton.setColorFilter(upvoteButtonColorFilter); ((DataViewHolder) holder).upvoteButton.setColorFilter(previousUpvoteButtonColorFilter);
((DataViewHolder) holder).downvoteButton.setColorFilter(downvoteButtonColorFilter); ((DataViewHolder) holder).downvoteButton.setColorFilter(previousDownvoteButtonColorFilter);
} }
}, id, newVoteType, holder.getAdapterPosition()); }, id, newVoteType, holder.getAdapterPosition());
}); });
((DataViewHolder) holder).downvoteButton.setOnClickListener(view -> { ((DataViewHolder) holder).downvoteButton.setOnClickListener(view -> {
ColorFilter upvoteButtonColorFilter = ((DataViewHolder) holder).upvoteButton.getColorFilter(); ColorFilter previousUpvoteButtonColorFilter = ((DataViewHolder) holder).upvoteButton.getColorFilter();
ColorFilter downvoteButtonColorFilter = ((DataViewHolder) holder).downvoteButton.getColorFilter(); ColorFilter previousDownvoteButtonColorFilter = ((DataViewHolder) holder).downvoteButton.getColorFilter();
int previousVoteType = post.getVoteType(); int previousVoteType = post.getVoteType();
String newVoteType; String newVoteType;
((DataViewHolder) holder).upvoteButton.clearColorFilter(); ((DataViewHolder) holder).upvoteButton.clearColorFilter();
if(downvoteButtonColorFilter == null) { if(previousDownvoteButtonColorFilter == null) {
//Not downvoted before //Not downvoted before
post.setVoteType(-1); post.setVoteType(-1);
newVoteType = RedditUtils.DIR_DOWNVOTE; newVoteType = RedditUtils.DIR_DOWNVOTE;
} else {
//downvoted before
post.setVoteType(0);
newVoteType = RedditUtils.DIR_UNVOTE;
}
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + post.getVoteType()));
if(((DataViewHolder) holder).downvoteButton.getColorFilter() == null) {
((DataViewHolder) holder).downvoteButton ((DataViewHolder) holder).downvoteButton
.setColorFilter(ContextCompat.getColor(mContext, R.color.colorAccent), android.graphics.PorterDuff.Mode.SRC_IN); .setColorFilter(ContextCompat.getColor(mContext, R.color.colorAccent), android.graphics.PorterDuff.Mode.SRC_IN);
} else { } else {
//Downvoted before
post.setVoteType(0);
newVoteType = RedditUtils.DIR_UNVOTE;
((DataViewHolder) holder).downvoteButton.clearColorFilter(); ((DataViewHolder) holder).downvoteButton.clearColorFilter();
} }
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + post.getVoteType()));
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() { VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() {
@Override @Override
public void onVoteThingSuccess(int position1) { public void onVoteThingSuccess(int position1) {
if(newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) { if(newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) {
post.setVoteType(-1); post.setVoteType(-1);
((DataViewHolder) holder).downvoteButton
.setColorFilter(ContextCompat.getColor(mContext, R.color.colorAccent), android.graphics.PorterDuff.Mode.SRC_IN);
} else { } else {
post.setVoteType(0); post.setVoteType(0);
((DataViewHolder) holder).downvoteButton.clearColorFilter();
} }
((DataViewHolder) holder).upvoteButton.clearColorFilter(); ((DataViewHolder) holder).upvoteButton.clearColorFilter();
@ -471,8 +470,8 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
public void onVoteThingFail(int position1) { public void onVoteThingFail(int position1) {
Toast.makeText(mContext, R.string.vote_failed, Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, R.string.vote_failed, Toast.LENGTH_SHORT).show();
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + previousVoteType)); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + previousVoteType));
((DataViewHolder) holder).upvoteButton.setColorFilter(upvoteButtonColorFilter); ((DataViewHolder) holder).upvoteButton.setColorFilter(previousUpvoteButtonColorFilter);
((DataViewHolder) holder).downvoteButton.setColorFilter(downvoteButtonColorFilter); ((DataViewHolder) holder).downvoteButton.setColorFilter(previousDownvoteButtonColorFilter);
} }
}, id, newVoteType, holder.getAdapterPosition()); }, id, newVoteType, holder.getAdapterPosition());
}); });

View File

@ -311,138 +311,98 @@ public class ViewPostDetailActivity extends AppCompatActivity {
fetchComment(); fetchComment();
/*CommentAdapter mAdapter = new CommentAdapter(this, mRetrofit, mOauthRetrofit, mSharedPreferences,
mRecyclerView, mPost.getSubredditNamePrefixed(), mPost.getId(), getResources().getConfiguration().locale);
CommentViewModel.Factory factory = new CommentViewModel.Factory(mRetrofit, getResources().getConfiguration().locale,
mPost.getSubredditNamePrefixed(), mPost.getId(), null, true, new CommentDataSource.OnCommentFetchedCallback() {
@Override
public void hasComment() {
mCommentProgressbar.setVisibility(View.GONE);
mRecyclerView.setAdapter(mAdapter);
mCommentCardView.setVisibility(View.VISIBLE);
}
@Override
public void noComment() {
}
});
CommentViewModel commentViewModel = ViewModelProviders.of(this, factory).get(CommentViewModel.class);
commentViewModel.getComments().observe(this, posts -> mAdapter.submitList(posts));*/
mUpvoteButton.setOnClickListener(view -> { mUpvoteButton.setOnClickListener(view -> {
final boolean isDownvotedBefore = mDownvoteButton.getColorFilter() != null; ColorFilter previousUpvoteButtonColorFilter = mUpvoteButton.getColorFilter();
ColorFilter previousDownvoteButtonColorFilter = mDownvoteButton.getColorFilter();
int previousVoteType = mPost.getVoteType();
String newVoteType;
final ColorFilter downVoteButtonColorFilter = mDownvoteButton.getColorFilter();
mDownvoteButton.clearColorFilter(); mDownvoteButton.clearColorFilter();
if (mUpvoteButton.getColorFilter() == null) { if(previousUpvoteButtonColorFilter == null) {
mUpvoteButton.setColorFilter(ContextCompat.getColor(ViewPostDetailActivity.this, R.color.colorPrimary), PorterDuff.Mode.SRC_IN); //Not upvoted before
if(isDownvotedBefore) { mPost.setVoteType(1);
mScoreTextView.setText(Integer.toString(mPost.getScore() + 2)); newVoteType = RedditUtils.DIR_UPVOTE;
} else { mUpvoteButton.setColorFilter(ContextCompat.getColor(this, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
mScoreTextView.setText(Integer.toString(mPost.getScore() + 1));
}
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingWithoutPositionListener() {
@Override
public void onVoteThingSuccess() {
mPost.setVoteType(1);
if(isDownvotedBefore) {
mPost.setScore(mPost.getScore() + 2);
} else {
mPost.setScore(mPost.getScore() + 1);
}
}
@Override
public void onVoteThingFail() {
Toast.makeText(ViewPostDetailActivity.this, "Cannot upvote this post", Toast.LENGTH_SHORT).show();
mUpvoteButton.clearColorFilter();
mScoreTextView.setText(Integer.toString(mPost.getScore()));
mDownvoteButton.setColorFilter(downVoteButtonColorFilter);
}
}, mPost.getFullName(), RedditUtils.DIR_UPVOTE);
} else { } else {
//Upvoted before //Upvoted before
mPost.setVoteType(0);
newVoteType = RedditUtils.DIR_UNVOTE;
mUpvoteButton.clearColorFilter(); mUpvoteButton.clearColorFilter();
mScoreTextView.setText(Integer.toString(mPost.getScore() - 1));
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingWithoutPositionListener() {
@Override
public void onVoteThingSuccess() {
mPost.setVoteType(0);
mPost.setScore(mPost.getScore() - 1);
}
@Override
public void onVoteThingFail() {
Toast.makeText(ViewPostDetailActivity.this, "Cannot unvote this post", Toast.LENGTH_SHORT).show();
mScoreTextView.setText(Integer.toString(mPost.getScore() + 1));
mUpvoteButton.setColorFilter(ContextCompat.getColor(ViewPostDetailActivity.this, R.color.colorPrimary), PorterDuff.Mode.SRC_IN);
mPost.setScore(mPost.getScore() + 1);
}
}, mPost.getFullName(), RedditUtils.DIR_UNVOTE);
} }
mScoreTextView.setText(Integer.toString(mPost.getScore() + mPost.getVoteType()));
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingWithoutPositionListener() {
@Override
public void onVoteThingSuccess() {
if(newVoteType.equals(RedditUtils.DIR_UPVOTE)) {
mPost.setVoteType(1);
mUpvoteButton.setColorFilter(ContextCompat.getColor(ViewPostDetailActivity.this, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
} else {
mPost.setVoteType(0);
mUpvoteButton.clearColorFilter();
}
mDownvoteButton.clearColorFilter();
mScoreTextView.setText(Integer.toString(mPost.getScore() + mPost.getVoteType()));
}
@Override
public void onVoteThingFail() {
Toast.makeText(ViewPostDetailActivity.this, R.string.vote_failed, Toast.LENGTH_SHORT).show();
mScoreTextView.setText(Integer.toString(mPost.getScore() + previousVoteType));
mUpvoteButton.setColorFilter(previousUpvoteButtonColorFilter);
mDownvoteButton.setColorFilter(previousDownvoteButtonColorFilter);
}
}, mPost.getFullName(), newVoteType);
}); });
mDownvoteButton.setOnClickListener(view -> { mDownvoteButton.setOnClickListener(view -> {
final boolean isUpvotedBefore = mUpvoteButton.getColorFilter() != null; ColorFilter previousUpvoteButtonColorFilter = mUpvoteButton.getColorFilter();
ColorFilter previousDownvoteButtonColorFilter = mDownvoteButton.getColorFilter();
int previousVoteType = mPost.getVoteType();
String newVoteType;
final ColorFilter upvoteButtonColorFilter = mUpvoteButton.getColorFilter();
mUpvoteButton.clearColorFilter(); mUpvoteButton.clearColorFilter();
if (mDownvoteButton.getColorFilter() == null) { if(previousDownvoteButtonColorFilter == null) {
mDownvoteButton.setColorFilter(ContextCompat.getColor(ViewPostDetailActivity.this, R.color.minusButtonColor), PorterDuff.Mode.SRC_IN); //Not upvoted before
if (isUpvotedBefore) { mPost.setVoteType(-1);
mScoreTextView.setText(Integer.toString(mPost.getScore() - 2)); newVoteType = RedditUtils.DIR_DOWNVOTE;
} else { mDownvoteButton.setColorFilter(ContextCompat.getColor(this, R.color.colorAccent), android.graphics.PorterDuff.Mode.SRC_IN);
mScoreTextView.setText(Integer.toString(mPost.getScore() - 1)); } else {
//Upvoted before
mPost.setVoteType(0);
newVoteType = RedditUtils.DIR_UNVOTE;
mDownvoteButton.clearColorFilter();
}
mScoreTextView.setText(Integer.toString(mPost.getScore() + mPost.getVoteType()));
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingWithoutPositionListener() {
@Override
public void onVoteThingSuccess() {
if(newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) {
mPost.setVoteType(-1);
mDownvoteButton.setColorFilter(ContextCompat.getColor(ViewPostDetailActivity.this, R.color.colorAccent), android.graphics.PorterDuff.Mode.SRC_IN);
} else {
mPost.setVoteType(0);
mDownvoteButton.clearColorFilter();
}
mUpvoteButton.clearColorFilter();
mScoreTextView.setText(Integer.toString(mPost.getScore() + mPost.getVoteType()));
} }
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingWithoutPositionListener() { @Override
@Override public void onVoteThingFail() {
public void onVoteThingSuccess() { Toast.makeText(ViewPostDetailActivity.this, R.string.vote_failed, Toast.LENGTH_SHORT).show();
mPost.setVoteType(-1); mScoreTextView.setText(Integer.toString(mPost.getScore() + previousVoteType));
if(isUpvotedBefore) { mUpvoteButton.setColorFilter(previousUpvoteButtonColorFilter);
mPost.setScore(mPost.getScore() - 2); mDownvoteButton.setColorFilter(previousDownvoteButtonColorFilter);
} else { }
mPost.setScore(mPost.getScore() - 1); }, mPost.getFullName(), newVoteType);
}
}
@Override
public void onVoteThingFail() {
Toast.makeText(ViewPostDetailActivity.this, "Cannot downvote this post", Toast.LENGTH_SHORT).show();
mDownvoteButton.clearColorFilter();
mScoreTextView.setText(Integer.toString(mPost.getScore()));
mUpvoteButton.setColorFilter(upvoteButtonColorFilter);
}
}, mPost.getFullName(), RedditUtils.DIR_DOWNVOTE);
} else {
//Down voted before
mDownvoteButton.clearColorFilter();
mScoreTextView.setText(Integer.toString(mPost.getScore() + 1));
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingWithoutPositionListener() {
@Override
public void onVoteThingSuccess() {
mPost.setVoteType(0);
mPost.setScore(mPost.getScore());
}
@Override
public void onVoteThingFail() {
Toast.makeText(ViewPostDetailActivity.this, "Cannot unvote this post", Toast.LENGTH_SHORT).show();
mDownvoteButton.setColorFilter(ContextCompat.getColor(ViewPostDetailActivity.this, R.color.minusButtonColor), PorterDuff.Mode.SRC_IN);
mScoreTextView.setText(Integer.toString(mPost.getScore()));
mPost.setScore(mPost.getScore());
}
}, mPost.getFullName(), RedditUtils.DIR_UNVOTE);
}
}); });
} }