Show a error message in toast for voting things.

This commit is contained in:
Alex Ning 2020-02-22 14:32:47 +08:00
parent 2dfc2a86b0
commit 30ce4285c9
5 changed files with 25 additions and 16 deletions

View File

@ -900,7 +900,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
((CommentViewHolder) holder).scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes,
comment.getScore() + comment.getVoteType()));
VoteThing.voteThing(mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() {
VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() {
@Override
public void onVoteThingSuccess(int position) {
if (newVoteType.equals(RedditUtils.DIR_UPVOTE)) {
@ -957,7 +957,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
((CommentViewHolder) holder).scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes,
comment.getScore() + comment.getVoteType()));
VoteThing.voteThing(mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() {
VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() {
@Override
public void onVoteThingSuccess(int position1) {
if (newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) {
@ -1796,7 +1796,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
mCommentRecyclerViewAdapterCallback.updatePost(mPost);
VoteThing.voteThing(mOauthRetrofit, mAccessToken, new VoteThing.VoteThingWithoutPositionListener() {
VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingWithoutPositionListener() {
@Override
public void onVoteThingSuccess() {
if (newVoteType.equals(RedditUtils.DIR_UPVOTE)) {
@ -1870,7 +1870,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
mCommentRecyclerViewAdapterCallback.updatePost(mPost);
VoteThing.voteThing(mOauthRetrofit, mAccessToken, new VoteThing.VoteThingWithoutPositionListener() {
VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingWithoutPositionListener() {
@Override
public void onVoteThingSuccess() {
if (newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) {

View File

@ -234,7 +234,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
((DataViewHolder) holder).scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes,
comment.getScore() + comment.getVoteType()));
VoteThing.voteThing(mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() {
VoteThing.voteThing(mContext, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() {
@Override
public void onVoteThingSuccess(int position) {
if (newVoteType.equals(RedditUtils.DIR_UPVOTE)) {
@ -286,7 +286,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
((DataViewHolder) holder).scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes,
comment.getScore() + comment.getVoteType()));
VoteThing.voteThing(mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() {
VoteThing.voteThing(mContext, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() {
@Override
public void onVoteThingSuccess(int position1) {
if (newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) {

View File

@ -563,7 +563,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
((PostViewHolder) holder).scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes, post.getScore() + post.getVoteType()));
VoteThing.voteThing(mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() {
VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() {
@Override
public void onVoteThingSuccess(int position1) {
if (newVoteType.equals(RedditUtils.DIR_UPVOTE)) {
@ -634,7 +634,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
((PostViewHolder) holder).scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes, post.getScore() + post.getVoteType()));
VoteThing.voteThing(mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() {
VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() {
@Override
public void onVoteThingSuccess(int position1) {
if (newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) {
@ -1109,7 +1109,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
((PostCompactViewHolder) holder).scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes, post.getScore() + post.getVoteType()));
VoteThing.voteThing(mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() {
VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() {
@Override
public void onVoteThingSuccess(int position1) {
if (newVoteType.equals(RedditUtils.DIR_UPVOTE)) {
@ -1180,7 +1180,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
((PostCompactViewHolder) holder).scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes, post.getScore() + post.getVoteType()));
VoteThing.voteThing(mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() {
VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() {
@Override
public void onVoteThingSuccess(int position1) {
if (newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) {

View File

@ -1,6 +1,6 @@
package ml.docilealligator.infinityforreddit;
import android.app.Application;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
@ -12,9 +12,9 @@ import okhttp3.Interceptor;
import okhttp3.Response;
public class Okhttp3DebugInterceptor implements Interceptor {
private Application context;
private Context context;
public Okhttp3DebugInterceptor(Application context) {
public Okhttp3DebugInterceptor(Context context) {
this.context = context;
}
@ -24,7 +24,9 @@ public class Okhttp3DebugInterceptor implements Interceptor {
Response response = chain.proceed(chain.request());
if (!response.isSuccessful()) {
String message = "No body";
if (response.body() != null) {
if (response.code() == 401) {
message = "Not authorized";
} else if (response.body() != null) {
message = response.body().string();
}
NotificationManagerCompat notificationManager = NotificationUtils.getNotificationManager(context);

View File

@ -1,5 +1,8 @@
package ml.docilealligator.infinityforreddit;
import android.content.Context;
import android.widget.Toast;
import androidx.annotation.NonNull;
import java.util.HashMap;
@ -16,7 +19,7 @@ import retrofit2.Retrofit;
public class VoteThing {
public static void voteThing(final Retrofit retrofit, String accessToken,
public static void voteThing(Context context, final Retrofit retrofit, String accessToken,
final VoteThingListener voteThingListener, final String fullName,
final String point, final int position) {
RedditAPI api = retrofit.create(RedditAPI.class);
@ -34,17 +37,19 @@ public class VoteThing {
voteThingListener.onVoteThingSuccess(position);
} else {
voteThingListener.onVoteThingFail(position);
Toast.makeText(context, "Code " + response.code() + " Body: " + response.body(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
voteThingListener.onVoteThingFail(position);
Toast.makeText(context, "Network error " + "Body: " + t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
public static void voteThing(final Retrofit retrofit, String accessToken,
public static void voteThing(Context context, final Retrofit retrofit, String accessToken,
final VoteThingWithoutPositionListener voteThingWithoutPositionListener,
final String fullName, final String point) {
RedditAPI api = retrofit.create(RedditAPI.class);
@ -62,12 +67,14 @@ public class VoteThing {
voteThingWithoutPositionListener.onVoteThingSuccess();
} else {
voteThingWithoutPositionListener.onVoteThingFail();
Toast.makeText(context, "Code " + response.code() + " Body: " + response.body(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
voteThingWithoutPositionListener.onVoteThingFail();
Toast.makeText(context, "Network error " + "Body: " + t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}