Fixed displaying wrong score when rapidly clicking vote buttons in PostRecyclerViewAdapter.

This commit is contained in:
Alex Ning 2019-04-18 17:18:19 +08:00
parent fb525d62ed
commit 246d4f45ab
6 changed files with 93 additions and 103 deletions

Binary file not shown.

View File

@ -33,14 +33,14 @@ repositories {
dependencies { dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs') implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.1.0-alpha02' implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
implementation 'com.google.android.material:material:1.1.0-alpha03' implementation 'com.google.android.material:material:1.1.0-alpha05'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.legacy:legacy-support-v13:1.0.0' implementation 'androidx.legacy:legacy-support-v13:1.0.0'
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.2-alpha01' androidTestImplementation 'androidx.test:runner:1.2.0-alpha03'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.2-alpha01' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha03'
implementation 'com.google.android.exoplayer:exoplayer:2.7.0' implementation 'com.google.android.exoplayer:exoplayer:2.7.0'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.7.0' implementation 'com.google.android.exoplayer:exoplayer-dash:2.7.0'
implementation 'androidx.browser:browser:1.0.0' implementation 'androidx.browser:browser:1.0.0'
@ -48,7 +48,7 @@ dependencies {
implementation 'androidx.cardview:cardview:1.0.0' implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.github.bumptech.glide:glide:4.8.0' implementation 'com.github.bumptech.glide:glide:4.8.0'
implementation 'com.github.pwittchen:swipe-rx2:0.3.0' implementation 'com.github.pwittchen:swipe-rx2:0.3.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1' annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
// Room components // Room components
implementation "androidx.room:room-runtime:$rootProject.roomVersion" implementation "androidx.room:room-runtime:$rootProject.roomVersion"
annotationProcessor "androidx.room:room-compiler:$rootProject.roomVersion" annotationProcessor "androidx.room:room-compiler:$rootProject.roomVersion"

View File

@ -378,114 +378,103 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
} }
((DataViewHolder) holder).upvoteButton.setOnClickListener(view -> { ((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(); ((DataViewHolder) holder).downvoteButton.clearColorFilter();
if (((DataViewHolder) holder).upvoteButton.getColorFilter() == null) { if(upvoteButtonColorFilter == null) {
((DataViewHolder) holder).upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); //Not upvoted before
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); post.setVoteType(1);
if(isDownvotedBefore) { newVoteType = RedditUtils.DIR_UPVOTE;
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());
} else { } else {
//Upvoted before //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).upvoteButton.clearColorFilter();
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() - 1)); }
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)) {
post.setVoteType(1);
} else {
post.setVoteType(0); post.setVoteType(0);
post.setScore(post.getScore() - 1); }
((DataViewHolder) holder).downvoteButton.clearColorFilter();
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + post.getVoteType()));
} }
@Override @Override
public void onVoteThingFail(int position1) { public void onVoteThingFail(int position1) {
Toast.makeText(mContext, "Cannot unvote this post", Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, R.string.vote_failed, Toast.LENGTH_SHORT).show();
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + 1)); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + previousVoteType));
((DataViewHolder) holder).upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); ((DataViewHolder) holder).upvoteButton.setColorFilter(upvoteButtonColorFilter);
post.setScore(post.getScore() + 1); ((DataViewHolder) holder).downvoteButton.setColorFilter(downvoteButtonColorFilter);
}
}, id, RedditUtils.DIR_UNVOTE, holder.getAdapterPosition());
} }
}, id, newVoteType, holder.getAdapterPosition());
}); });
((DataViewHolder) holder).downvoteButton.setOnClickListener(view -> { ((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(); ((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() { if(downvoteButtonColorFilter == null) {
@Override //Not downvoted before
public void onVoteThingSuccess(int position12) {
post.setVoteType(-1); post.setVoteType(-1);
if(isUpvotedBefore) { newVoteType = RedditUtils.DIR_DOWNVOTE;
post.setScore(post.getScore() - 2);
} else { } else {
post.setScore(post.getScore() - 1); //downvoted before
} post.setVoteType(0);
newVoteType = RedditUtils.DIR_UNVOTE;
} }
@Override ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + post.getVoteType()));
public void onVoteThingFail(int position12) {
Toast.makeText(mContext, "Cannot downvote this post", Toast.LENGTH_SHORT).show(); if(((DataViewHolder) holder).downvoteButton.getColorFilter() == null) {
((DataViewHolder) holder).downvoteButton.clearColorFilter(); ((DataViewHolder) holder).downvoteButton
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore())); .setColorFilter(ContextCompat.getColor(mContext, R.color.colorAccent), android.graphics.PorterDuff.Mode.SRC_IN);
((DataViewHolder) holder).upvoteButton.setColorFilter(upvoteButtonColorFilter);
}
}, id, RedditUtils.DIR_DOWNVOTE, holder.getAdapterPosition());
} else { } else {
//Down voted before
((DataViewHolder) holder).downvoteButton.clearColorFilter(); ((DataViewHolder) holder).downvoteButton.clearColorFilter();
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + 1)); }
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() { VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() {
@Override @Override
public void onVoteThingSuccess(int position12) { public void onVoteThingSuccess(int position1) {
if(newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) {
post.setVoteType(-1);
} else {
post.setVoteType(0); post.setVoteType(0);
post.setScore(post.getScore()); }
((DataViewHolder) holder).upvoteButton.clearColorFilter();
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + post.getVoteType()));
} }
@Override @Override
public void onVoteThingFail(int position12) { public void onVoteThingFail(int position1) {
Toast.makeText(mContext, "Cannot unvote this post", Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, R.string.vote_failed, 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() + previousVoteType));
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore())); ((DataViewHolder) holder).upvoteButton.setColorFilter(upvoteButtonColorFilter);
post.setScore(post.getScore()); ((DataViewHolder) holder).downvoteButton.setColorFilter(downvoteButtonColorFilter);
}
}, id, RedditUtils.DIR_UNVOTE, holder.getAdapterPosition());
} }
}, id, newVoteType, holder.getAdapterPosition());
}); });
((DataViewHolder) holder).shareButton.setOnClickListener(view -> { ((DataViewHolder) holder).shareButton.setOnClickListener(view -> {

View File

@ -25,6 +25,7 @@
<string name="retry">Retry</string> <string name="retry">Retry</string>
<string name="comments">Comments</string> <string name="comments">Comments</string>
<string name="no_comments_yet">No comments yet. Write a comment?</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="nsfw">NSFW</string>
<string name="karma_info">Karma: %1$d</string> <string name="karma_info">Karma: %1$d</string>

View File

@ -7,7 +7,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.3.1' classpath 'com.android.tools.build:gradle:3.3.2'
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong
@ -31,6 +31,6 @@ task clean(type: Delete) {
} }
ext { ext {
roomVersion = '2.1.0-alpha04' roomVersion = '2.1.0-alpha06'
archLifecycleVersion = '2.1.0-alpha02' archLifecycleVersion = '2.1.0-alpha04'
} }