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 | |
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 '')
-rw-r--r-- | .idea/caches/build_file_checksums.ser | bin | 533 -> 533 bytes | |||
-rw-r--r-- | .idea/caches/gradle_models.ser | bin | 296620 -> 300258 bytes | |||
-rw-r--r-- | app/build.gradle | 10 | ||||
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java | 159 | ||||
-rw-r--r-- | app/src/main/res/values/strings.xml | 1 | ||||
-rw-r--r-- | build.gradle | 6 |
6 files changed, 83 insertions, 93 deletions
diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser Binary files differindex 75671f83..e2cbe3a1 100644 --- a/.idea/caches/build_file_checksums.ser +++ b/.idea/caches/build_file_checksums.ser diff --git a/.idea/caches/gradle_models.ser b/.idea/caches/gradle_models.ser Binary files differindex 34e30d6a..9a74a867 100644 --- a/.idea/caches/gradle_models.ser +++ b/.idea/caches/gradle_models.ser diff --git a/app/build.gradle b/app/build.gradle index 8e175db8..a8524124 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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" 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> diff --git a/build.gradle b/build.gradle index cdfe2895..dd60d87a 100644 --- a/build.gradle +++ b/build.gradle @@ -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' }
\ No newline at end of file |