diff options
Diffstat (limited to '')
3 files changed, 171 insertions, 225 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentMultiLevelRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentMultiLevelRecyclerViewAdapter.java index 08b465b3..a649ed15 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentMultiLevelRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentMultiLevelRecyclerViewAdapter.java @@ -5,10 +5,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.graphics.ColorFilter; 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.View; import android.view.ViewGroup; @@ -25,6 +21,10 @@ import java.util.ArrayList; import java.util.List; 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.ButterKnife; import retrofit2.Retrofit; @@ -143,114 +143,101 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { } ((CommentViewHolder) holder).upvoteButton.setOnClickListener(view -> { - final boolean isDownvotedBefore = ((CommentViewHolder) holder).downvoteButton.getColorFilter() != null; - final ColorFilter minusButtonColorFilter = ((CommentViewHolder) holder).downvoteButton.getColorFilter(); - ((CommentViewHolder) holder).downvoteButton.clearColorFilter(); - - if (((CommentViewHolder) holder).upvoteButton.getColorFilter() == null) { - ((CommentViewHolder) holder).upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); - if (isDownvotedBefore) { - ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 2)); - } else { - ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 1)); - } + ColorFilter previousUpvoteButtonColorFilter = ((CommentViewHolder) holder).upvoteButton.getColorFilter(); + ColorFilter previousDownvoteButtonColorFilter = ((CommentViewHolder) holder).downvoteButton.getColorFilter(); + int previousVoteType = commentItem.getVoteType(); + String newVoteType; - 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); - } - } + ((CommentViewHolder) holder).downvoteButton.clearColorFilter(); - @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()); + if(previousUpvoteButtonColorFilter == null) { + //Not upvoted before + commentItem.setVoteType(1); + newVoteType = RedditUtils.DIR_UPVOTE; + ((CommentViewHolder) holder).upvoteButton + .setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), 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() - 1)); + } + + ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + commentItem.getVoteType())); - VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() { - @Override - public void onVoteThingSuccess(int position1) { + 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); - commentItem.setScore(commentItem.getScore() - 1); + ((CommentViewHolder) holder).upvoteButton.clearColorFilter(); } - @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).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 -> { - 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(); - if (((CommentViewHolder) holder).downvoteButton.getColorFilter() == null) { - ((CommentViewHolder) holder).downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN); - if (isUpvotedBefore) { - ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() - 2)); - } else { - ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() - 1)); - } - - VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() { - @Override - public void onVoteThingSuccess(int position12) { - commentItem.setVoteType(-1); - if (isUpvotedBefore) { - commentItem.setScore(commentItem.getScore() - 2); - } else { - commentItem.setScore(commentItem.getScore() - 1); - } - } - - @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()); + if(previousDownvoteButtonColorFilter == null) { + //Not downvoted before + commentItem.setVoteType(-1); + newVoteType = RedditUtils.DIR_DOWNVOTE; + ((CommentViewHolder) holder).downvoteButton + .setColorFilter(ContextCompat.getColor(mContext, R.color.colorAccent), android.graphics.PorterDuff.Mode.SRC_IN); } else { - //Down voted before + //Downvoted before + commentItem.setVoteType(0); + newVoteType = RedditUtils.DIR_UNVOTE; ((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) { + ((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); - commentItem.setScore(commentItem.getScore()); + ((CommentViewHolder) holder).downvoteButton.clearColorFilter(); } - @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()); - } + ((CommentViewHolder) holder).upvoteButton.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()); }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index 9ab843f3..9aec077f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -378,38 +378,38 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo } ((DataViewHolder) holder).upvoteButton.setOnClickListener(view -> { - ColorFilter upvoteButtonColorFilter = ((DataViewHolder) holder).upvoteButton.getColorFilter(); - ColorFilter downvoteButtonColorFilter = ((DataViewHolder) holder).downvoteButton.getColorFilter(); + ColorFilter previousUpvoteButtonColorFilter = ((DataViewHolder) holder).upvoteButton.getColorFilter(); + ColorFilter previousDownvoteButtonColorFilter = ((DataViewHolder) holder).downvoteButton.getColorFilter(); int previousVoteType = post.getVoteType(); String newVoteType; ((DataViewHolder) holder).downvoteButton.clearColorFilter(); - if(upvoteButtonColorFilter == null) { + if(previousUpvoteButtonColorFilter == null) { //Not upvoted before post.setVoteType(1); newVoteType = RedditUtils.DIR_UPVOTE; + ((DataViewHolder) holder).upvoteButton + .setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); } else { //Upvoted before post.setVoteType(0); 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).scoreTextView.setText(Integer.toString(post.getScore() + post.getVoteType())); + VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position1) { if(newVoteType.equals(RedditUtils.DIR_UPVOTE)) { post.setVoteType(1); + ((DataViewHolder) holder).upvoteButton + .setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); } else { post.setVoteType(0); + ((DataViewHolder) holder).upvoteButton.clearColorFilter(); } ((DataViewHolder) holder).downvoteButton.clearColorFilter(); @@ -420,47 +420,46 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo public void onVoteThingFail(int position1) { Toast.makeText(mContext, R.string.vote_failed, Toast.LENGTH_SHORT).show(); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + previousVoteType)); - ((DataViewHolder) holder).upvoteButton.setColorFilter(upvoteButtonColorFilter); - ((DataViewHolder) holder).downvoteButton.setColorFilter(downvoteButtonColorFilter); + ((DataViewHolder) holder).upvoteButton.setColorFilter(previousUpvoteButtonColorFilter); + ((DataViewHolder) holder).downvoteButton.setColorFilter(previousDownvoteButtonColorFilter); } }, id, newVoteType, holder.getAdapterPosition()); }); ((DataViewHolder) holder).downvoteButton.setOnClickListener(view -> { - ColorFilter upvoteButtonColorFilter = ((DataViewHolder) holder).upvoteButton.getColorFilter(); - ColorFilter downvoteButtonColorFilter = ((DataViewHolder) holder).downvoteButton.getColorFilter(); + ColorFilter previousUpvoteButtonColorFilter = ((DataViewHolder) holder).upvoteButton.getColorFilter(); + ColorFilter previousDownvoteButtonColorFilter = ((DataViewHolder) holder).downvoteButton.getColorFilter(); int previousVoteType = post.getVoteType(); String newVoteType; ((DataViewHolder) holder).upvoteButton.clearColorFilter(); - if(downvoteButtonColorFilter == null) { + if(previousDownvoteButtonColorFilter == null) { //Not downvoted before post.setVoteType(-1); newVoteType = RedditUtils.DIR_DOWNVOTE; + ((DataViewHolder) holder).downvoteButton + .setColorFilter(ContextCompat.getColor(mContext, R.color.colorAccent), android.graphics.PorterDuff.Mode.SRC_IN); } else { - //downvoted before + //Downvoted before post.setVoteType(0); newVoteType = RedditUtils.DIR_UNVOTE; + ((DataViewHolder) holder).downvoteButton.clearColorFilter(); } ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + post.getVoteType())); - if(((DataViewHolder) holder).downvoteButton.getColorFilter() == null) { - ((DataViewHolder) holder).downvoteButton - .setColorFilter(ContextCompat.getColor(mContext, R.color.colorAccent), android.graphics.PorterDuff.Mode.SRC_IN); - } else { - ((DataViewHolder) holder).downvoteButton.clearColorFilter(); - } - VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position1) { if(newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) { post.setVoteType(-1); + ((DataViewHolder) holder).downvoteButton + .setColorFilter(ContextCompat.getColor(mContext, R.color.colorAccent), android.graphics.PorterDuff.Mode.SRC_IN); } else { post.setVoteType(0); + ((DataViewHolder) holder).downvoteButton.clearColorFilter(); } ((DataViewHolder) holder).upvoteButton.clearColorFilter(); @@ -471,8 +470,8 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo public void onVoteThingFail(int position1) { Toast.makeText(mContext, R.string.vote_failed, Toast.LENGTH_SHORT).show(); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + previousVoteType)); - ((DataViewHolder) holder).upvoteButton.setColorFilter(upvoteButtonColorFilter); - ((DataViewHolder) holder).downvoteButton.setColorFilter(downvoteButtonColorFilter); + ((DataViewHolder) holder).upvoteButton.setColorFilter(previousUpvoteButtonColorFilter); + ((DataViewHolder) holder).downvoteButton.setColorFilter(previousDownvoteButtonColorFilter); } }, id, newVoteType, holder.getAdapterPosition()); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 770c55a0..505d2fb1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -311,138 +311,98 @@ public class ViewPostDetailActivity extends AppCompatActivity { 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 -> { - 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(); - if (mUpvoteButton.getColorFilter() == null) { - mUpvoteButton.setColorFilter(ContextCompat.getColor(ViewPostDetailActivity.this, R.color.colorPrimary), PorterDuff.Mode.SRC_IN); - if(isDownvotedBefore) { - mScoreTextView.setText(Integer.toString(mPost.getScore() + 2)); - } else { - 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); + if(previousUpvoteButtonColorFilter == null) { + //Not upvoted before + mPost.setVoteType(1); + newVoteType = RedditUtils.DIR_UPVOTE; + mUpvoteButton.setColorFilter(ContextCompat.getColor(this, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); } else { //Upvoted before + mPost.setVoteType(0); + newVoteType = RedditUtils.DIR_UNVOTE; mUpvoteButton.clearColorFilter(); - mScoreTextView.setText(Integer.toString(mPost.getScore() - 1)); + } - VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingWithoutPositionListener() { - @Override - public void onVoteThingSuccess() { + 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); - mPost.setScore(mPost.getScore() - 1); + mUpvoteButton.clearColorFilter(); } - @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); - } + 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 -> { - 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(); - if (mDownvoteButton.getColorFilter() == null) { - mDownvoteButton.setColorFilter(ContextCompat.getColor(ViewPostDetailActivity.this, R.color.minusButtonColor), PorterDuff.Mode.SRC_IN); - if (isUpvotedBefore) { - mScoreTextView.setText(Integer.toString(mPost.getScore() - 2)); - } else { - mScoreTextView.setText(Integer.toString(mPost.getScore() - 1)); - } - - VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingWithoutPositionListener() { - @Override - public void onVoteThingSuccess() { - mPost.setVoteType(-1); - if(isUpvotedBefore) { - mPost.setScore(mPost.getScore() - 2); - } else { - mPost.setScore(mPost.getScore() - 1); - } - } - - @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); + if(previousDownvoteButtonColorFilter == null) { + //Not upvoted before + mPost.setVoteType(-1); + newVoteType = RedditUtils.DIR_DOWNVOTE; + mDownvoteButton.setColorFilter(ContextCompat.getColor(this, R.color.colorAccent), android.graphics.PorterDuff.Mode.SRC_IN); } else { - //Down voted before + //Upvoted before + mPost.setVoteType(0); + newVoteType = RedditUtils.DIR_UNVOTE; mDownvoteButton.clearColorFilter(); - mScoreTextView.setText(Integer.toString(mPost.getScore() + 1)); + } - VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingWithoutPositionListener() { - @Override - public void onVoteThingSuccess() { + 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); - mPost.setScore(mPost.getScore()); + mDownvoteButton.clearColorFilter(); } - @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); - } + mUpvoteButton.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); }); } |