diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-04-18 09:18:19 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-04-18 09:18:19 +0000 |
commit | 246d4f45abdbc65976b808c462fb16708869a4dc (patch) | |
tree | 90148258d6ad8d7a14097b77f6649425fb023d31 /app/src | |
parent | fb525d62ed630c9731a4704ad3b9848b0fc10752 (diff) | |
download | infinity-for-reddit-246d4f45abdbc65976b808c462fb16708869a4dc.tar infinity-for-reddit-246d4f45abdbc65976b808c462fb16708869a4dc.tar.gz infinity-for-reddit-246d4f45abdbc65976b808c462fb16708869a4dc.tar.bz2 infinity-for-reddit-246d4f45abdbc65976b808c462fb16708869a4dc.tar.lz infinity-for-reddit-246d4f45abdbc65976b808c462fb16708869a4dc.tar.xz infinity-for-reddit-246d4f45abdbc65976b808c462fb16708869a4dc.tar.zst infinity-for-reddit-246d4f45abdbc65976b808c462fb16708869a4dc.zip |
Fixed displaying wrong score when rapidly clicking vote buttons in PostRecyclerViewAdapter.
Diffstat (limited to 'app/src')
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java | 159 | ||||
-rw-r--r-- | app/src/main/res/values/strings.xml | 1 |
2 files changed, 75 insertions, 85 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index 722969ed..9ab843f3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -378,114 +378,103 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo } ((DataViewHolder) holder).upvoteButton.setOnClickListener(view -> { - final boolean isDownvotedBefore = ((DataViewHolder) holder).downvoteButton.getColorFilter() != null; + ColorFilter upvoteButtonColorFilter = ((DataViewHolder) holder).upvoteButton.getColorFilter(); + ColorFilter downvoteButtonColorFilter = ((DataViewHolder) holder).downvoteButton.getColorFilter(); + int previousVoteType = post.getVoteType(); + String newVoteType; - final ColorFilter downvoteButtonColorFilter = ((DataViewHolder) holder).downvoteButton.getColorFilter(); ((DataViewHolder) holder).downvoteButton.clearColorFilter(); - if (((DataViewHolder) holder).upvoteButton.getColorFilter() == null) { - ((DataViewHolder) holder).upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); - if(isDownvotedBefore) { - ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + 2)); - } else { - ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + 1)); - } - - VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() { - @Override - public void onVoteThingSuccess(int position1) { - post.setVoteType(1); - if(isDownvotedBefore) { - post.setScore(post.getScore() + 2); - } else { - post.setScore(post.getScore() + 1); - } - } - - @Override - public void onVoteThingFail(int position1) { - Toast.makeText(mContext, "Cannot upvote this post", Toast.LENGTH_SHORT).show(); - ((DataViewHolder) holder).upvoteButton.clearColorFilter(); - ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore())); - ((DataViewHolder) holder).downvoteButton.setColorFilter(downvoteButtonColorFilter); - } - }, id, RedditUtils.DIR_UPVOTE, holder.getAdapterPosition()); + if(upvoteButtonColorFilter == null) { + //Not upvoted before + post.setVoteType(1); + newVoteType = RedditUtils.DIR_UPVOTE; } 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() - 1)); + } - 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)) { + post.setVoteType(1); + } else { post.setVoteType(0); - post.setScore(post.getScore() - 1); } - @Override - public void onVoteThingFail(int position1) { - Toast.makeText(mContext, "Cannot unvote this post", Toast.LENGTH_SHORT).show(); - ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + 1)); - ((DataViewHolder) holder).upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); - post.setScore(post.getScore() + 1); - } - }, id, RedditUtils.DIR_UNVOTE, holder.getAdapterPosition()); - } + ((DataViewHolder) holder).downvoteButton.clearColorFilter(); + ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + post.getVoteType())); + } + + @Override + 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); + } + }, id, newVoteType, holder.getAdapterPosition()); }); ((DataViewHolder) holder).downvoteButton.setOnClickListener(view -> { - final boolean isUpvotedBefore = ((DataViewHolder) holder).upvoteButton.getColorFilter() != null; + ColorFilter upvoteButtonColorFilter = ((DataViewHolder) holder).upvoteButton.getColorFilter(); + ColorFilter downvoteButtonColorFilter = ((DataViewHolder) holder).downvoteButton.getColorFilter(); + + int previousVoteType = post.getVoteType(); + String newVoteType; - final ColorFilter upvoteButtonColorFilter = ((DataViewHolder) holder).upvoteButton.getColorFilter(); ((DataViewHolder) holder).upvoteButton.clearColorFilter(); - if (((DataViewHolder) holder).downvoteButton.getColorFilter() == null) { - ((DataViewHolder) holder).downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN); - if (isUpvotedBefore) { - ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() - 2)); - } else { - ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() - 1)); - } - VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() { - @Override - public void onVoteThingSuccess(int position12) { - post.setVoteType(-1); - if(isUpvotedBefore) { - post.setScore(post.getScore() - 2); - } else { - post.setScore(post.getScore() - 1); - } - } + if(downvoteButtonColorFilter == null) { + //Not downvoted before + post.setVoteType(-1); + newVoteType = RedditUtils.DIR_DOWNVOTE; + } else { + //downvoted before + post.setVoteType(0); + newVoteType = RedditUtils.DIR_UNVOTE; + } - @Override - public void onVoteThingFail(int position12) { - Toast.makeText(mContext, "Cannot downvote this post", Toast.LENGTH_SHORT).show(); - ((DataViewHolder) holder).downvoteButton.clearColorFilter(); - ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore())); - ((DataViewHolder) holder).upvoteButton.setColorFilter(upvoteButtonColorFilter); - } - }, id, RedditUtils.DIR_DOWNVOTE, holder.getAdapterPosition()); + ((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 { - //Down voted before ((DataViewHolder) holder).downvoteButton.clearColorFilter(); - ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + 1)); + } - VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() { - @Override - public void onVoteThingSuccess(int position12) { + VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() { + @Override + public void onVoteThingSuccess(int position1) { + if(newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) { + post.setVoteType(-1); + } else { post.setVoteType(0); - post.setScore(post.getScore()); } - @Override - public void onVoteThingFail(int position12) { - Toast.makeText(mContext, "Cannot unvote this post", Toast.LENGTH_SHORT).show(); - ((DataViewHolder) holder).downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN); - ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore())); - post.setScore(post.getScore()); - } - }, id, RedditUtils.DIR_UNVOTE, holder.getAdapterPosition()); - } + ((DataViewHolder) holder).upvoteButton.clearColorFilter(); + ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + post.getVoteType())); + } + + @Override + 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); + } + }, id, newVoteType, holder.getAdapterPosition()); }); ((DataViewHolder) holder).shareButton.setOnClickListener(view -> { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 360767c5..85fc75f9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -25,6 +25,7 @@ <string name="retry">Retry</string> <string name="comments">Comments</string> <string name="no_comments_yet">No comments yet. Write a comment?</string> + <string name="vote_failed">Vote failed</string> <string name="nsfw">NSFW</string> <string name="karma_info">Karma: %1$d</string> |