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 {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.1.0-alpha02'
implementation 'com.google.android.material:material:1.1.0-alpha03'
implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
implementation 'com.google.android.material:material:1.1.0-alpha05'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.legacy:legacy-support-v13:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.2-alpha01'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.2-alpha01'
androidTestImplementation 'androidx.test:runner:1.2.0-alpha03'
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-dash:2.7.0'
implementation 'androidx.browser:browser:1.0.0'
@ -48,7 +48,7 @@ dependencies {
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.github.bumptech.glide:glide:4.8.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
implementation "androidx.room:room-runtime:$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 -> {
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
((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) {
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());
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();
}
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);
}
((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));
if(downvoteButtonColorFilter == null) {
//Not downvoted before
post.setVoteType(-1);
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
.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);
} else {
post.setVoteType(0);
}
((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 position12) {
post.setVoteType(-1);
if(isUpvotedBefore) {
post.setScore(post.getScore() - 2);
} else {
post.setScore(post.getScore() - 1);
}
}
@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());
} 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) {
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());
}
@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 -> {

View File

@ -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>

View File

@ -7,7 +7,7 @@ buildscript {
jcenter()
}
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
@ -31,6 +31,6 @@ task clean(type: Delete) {
}
ext {
roomVersion = '2.1.0-alpha04'
archLifecycleVersion = '2.1.0-alpha02'
roomVersion = '2.1.0-alpha06'
archLifecycleVersion = '2.1.0-alpha04'
}