diff options
author | Alex Ning <chineseperson5@gmail.com> | 2018-08-28 15:59:03 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2018-08-28 15:59:03 +0000 |
commit | 2a57e4d5b4d1ea8dc33ce64d659e886a840ca435 (patch) | |
tree | 5fb890c38e5f3bc6ae5807724b09d4f0ef364e9c /app/src/main/java | |
parent | a6a951732d311d1355b8145fca3525d66aad7686 (diff) | |
download | infinity-for-reddit-2a57e4d5b4d1ea8dc33ce64d659e886a840ca435.tar infinity-for-reddit-2a57e4d5b4d1ea8dc33ce64d659e886a840ca435.tar.gz infinity-for-reddit-2a57e4d5b4d1ea8dc33ce64d659e886a840ca435.tar.bz2 infinity-for-reddit-2a57e4d5b4d1ea8dc33ce64d659e886a840ca435.tar.lz infinity-for-reddit-2a57e4d5b4d1ea8dc33ce64d659e886a840ca435.tar.xz infinity-for-reddit-2a57e4d5b4d1ea8dc33ce64d659e886a840ca435.tar.zst infinity-for-reddit-2a57e4d5b4d1ea8dc33ce64d659e886a840ca435.zip |
Use Retrofit to send API requests. Rename some methods. Changing the type of some methods related to fetching data to static.
Diffstat (limited to 'app/src/main/java')
17 files changed, 390 insertions, 474 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AcquireAccessToken.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AcquireAccessToken.java deleted file mode 100644 index 0a9861fd..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AcquireAccessToken.java +++ /dev/null @@ -1,94 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.content.Context; -import android.content.SharedPreferences; -import android.util.Log; -import android.widget.Toast; - -import com.android.volley.Request; -import com.android.volley.RequestQueue; -import com.android.volley.Response; -import com.android.volley.VolleyError; -import com.android.volley.toolbox.StringRequest; - -import org.json.JSONException; -import org.json.JSONObject; - -import java.util.Calendar; -import java.util.HashMap; -import java.util.Map; - -/** - * Created by alex on 3/13/18. - */ - -class AcquireAccessToken { - - interface AcquireAccessTokenListener { - void onAcquireAccessTokenSuccess(); - void onAcquireAccessTokenFail(); - } - - private Context mContext; - private AcquireAccessTokenListener mAcquireAccessTokenListener; - - AcquireAccessToken(Context context) { - mContext = context; - } - - void refreshAccessToken(RequestQueue refreshQueue, AcquireAccessTokenListener acquireAccessTokenListener) { - if(mContext != null) { - mAcquireAccessTokenListener = acquireAccessTokenListener; - final String refreshToken = mContext.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.REFRESH_TOKEN_KEY, ""); - final SharedPreferences.Editor editor = mContext.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE).edit(); - StringRequest newTokenRequest = new StringRequest(Request.Method.POST, RedditUtils.ACQUIRE_ACCESS_TOKEN_URL, new Response.Listener<String>() { - @Override - public void onResponse(String response) { - try { - JSONObject jsonObject = new JSONObject(response); - String newAccessToken = jsonObject.getString(RedditUtils.ACCESS_TOKEN_KEY); - int expireIn = jsonObject.getInt(RedditUtils.EXPIRES_IN_KEY); - editor.putString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, newAccessToken); - editor.putInt(SharedPreferencesUtils.ACCESS_TOKEN_EXPIRE_INTERVAL_KEY, expireIn); - editor.apply(); - - Log.i("access token", newAccessToken); - mAcquireAccessTokenListener.onAcquireAccessTokenSuccess(); - } catch (JSONException e) { - e.printStackTrace(); - mAcquireAccessTokenListener.onAcquireAccessTokenFail(); - Toast.makeText(mContext, "Error parsing JSON object when getting the access token", Toast.LENGTH_SHORT).show(); - Log.i("main activity", "Error parsing JSON object when getting the access token"); - } - } - }, new Response.ErrorListener() { - @Override - public void onErrorResponse(VolleyError error) { - Toast.makeText(mContext, "Error getting the new access token", Toast.LENGTH_SHORT).show(); - mAcquireAccessTokenListener.onAcquireAccessTokenFail(); - Log.i("Error", "getting access token"); - } - }) { - @Override - protected Map<String, String> getParams() { - Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.GRANT_TYPE_KEY, RedditUtils.GRANT_TYPE_REFRESH_TOKEN); - params.put(RedditUtils.REFRESH_TOKEN_KEY, refreshToken); - return params; - } - - @Override - public Map<String, String> getHeaders() { - return RedditUtils.getHttpBasicAuthHeader(); - } - }; - newTokenRequest.setTag(AcquireAccessToken.class); - - long queryAccessTokenTime = Calendar.getInstance().getTimeInMillis(); - editor.putLong(SharedPreferencesUtils.QUERY_ACCESS_TOKEN_TIME_KEY, queryAccessTokenTime); - editor.apply(); - - refreshQueue.add(newTokenRequest); - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java index 8af5a765..5d876dc7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java @@ -12,8 +12,6 @@ import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; -import com.android.volley.RequestQueue; - import org.sufficientlysecure.htmltextview.HtmlTextView; import java.util.ArrayList; @@ -21,15 +19,10 @@ import java.util.ArrayList; class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context mContext; private ArrayList<CommentData> mCommentData; - private RequestQueue mVoteThingRequestQueue; - private RequestQueue mAcquireAccessTokenRequestQueue; - CommentRecyclerViewAdapter(Context context, ArrayList<CommentData> commentData, - RequestQueue voteThingRequestQueue, RequestQueue acquireAccessTokenRequestQueue) { + CommentRecyclerViewAdapter(Context context, ArrayList<CommentData> commentData) { mContext = context; mCommentData = commentData; - mVoteThingRequestQueue = voteThingRequestQueue; - mAcquireAccessTokenRequestQueue = acquireAccessTokenRequestQueue; } @NonNull @@ -71,7 +64,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(mCommentData.get(position).getScore() + 1)); } - new VoteThing(mContext, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingListener() { + VoteThing.voteThing(mContext, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position) { mCommentData.get(position).setVoteType(1); @@ -95,7 +88,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH ((CommentViewHolder) holder).upvoteButton.clearColorFilter(); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(mCommentData.get(position).getScore() - 1)); - new VoteThing(mContext, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingListener() { + VoteThing.voteThing(mContext, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position) { mCommentData.get(position).setVoteType(0); @@ -130,7 +123,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(mCommentData.get(position).getScore() - 1)); } - new VoteThing(mContext, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingListener() { + VoteThing.voteThing(mContext, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position) { mCommentData.get(position).setVoteType(-1); @@ -154,7 +147,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH ((CommentViewHolder) holder).downvoteButton.clearColorFilter(); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(mCommentData.get(position).getScore() + 1)); - new VoteThing(mContext, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingListener() { + VoteThing.voteThing(mContext, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position) { mCommentData.get(position).setVoteType(0); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java index e0fb82e8..20a0d444 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java @@ -15,15 +15,7 @@ class FetchComment { void onFetchCommentFail(); } - private String subredditNamePrefixed; - private String article; - - FetchComment(String subredditNamePrefixed, String article) { - this.subredditNamePrefixed = subredditNamePrefixed; - this.article = article; - } - - void queryComment(final FetchCommentListener fetchCommentListener) { + static void queryComment(String subredditNamePrefixed, String article, final FetchCommentListener fetchCommentListener) { Retrofit retrofit = new Retrofit.Builder() .baseUrl(RedditUtils.API_BASE_URI) .addConverterFactory(ScalarsConverterFactory.create()) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java index af7b6029..5baaede9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java @@ -1,10 +1,13 @@ package ml.docilealligator.infinityforreddit; -import com.android.volley.Request; -import com.android.volley.RequestQueue; -import com.android.volley.Response; -import com.android.volley.VolleyError; -import com.android.volley.toolbox.StringRequest; +import android.support.annotation.NonNull; +import android.util.Log; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; +import retrofit2.converter.scalars.ScalarsConverterFactory; class FetchSubredditData { interface FetchSubredditDataListener { @@ -12,29 +15,26 @@ class FetchSubredditData { void onFetchSubredditDataFail(); } - private RequestQueue requestQueue; - private String subredditName; - private FetchSubredditDataListener mFetchSubredditDataListener; + static void fetchSubredditData(String subredditName, final FetchSubredditDataListener fetchSubredditDataListener) { + Retrofit retrofit = new Retrofit.Builder() + .baseUrl(RedditUtils.API_BASE_URI) + .addConverterFactory(ScalarsConverterFactory.create()) + .build(); - FetchSubredditData(RequestQueue requestQueue, String subredditName) { - this.requestQueue = requestQueue; - this.subredditName = subredditName; - } + RedditAPI api = retrofit.create(RedditAPI.class); - void querySubredditData(FetchSubredditDataListener fetchSubredditDataListener) { - mFetchSubredditDataListener = fetchSubredditDataListener; - StringRequest commentRequest = new StringRequest(Request.Method.GET, RedditUtils.getQuerySubredditDataUrl(subredditName), new Response.Listener<String>() { + Call<String> subredditData = api.getSubredditData(subredditName); + subredditData.enqueue(new Callback<String>() { @Override - public void onResponse(String response) { - mFetchSubredditDataListener.onFetchSubredditDataSuccess(response); + public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { + fetchSubredditDataListener.onFetchSubredditDataSuccess(response.body()); } - }, new Response.ErrorListener() { + @Override - public void onErrorResponse(VolleyError error) { - mFetchSubredditDataListener.onFetchSubredditDataFail(); + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + Log.i("call failed", t.getMessage()); + fetchSubredditDataListener.onFetchSubredditDataFail(); } - }) {}; - commentRequest.setTag(FetchSubredditData.class); - requestQueue.add(commentRequest); + }); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java index 37719bfc..03e30f5e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java @@ -1,62 +1,51 @@ package ml.docilealligator.infinityforreddit; import android.content.Context; -import android.net.Uri; - -import com.android.volley.AuthFailureError; -import com.android.volley.Request; -import com.android.volley.RequestQueue; -import com.android.volley.Response; -import com.android.volley.VolleyError; -import com.android.volley.toolbox.StringRequest; +import android.support.annotation.NonNull; +import android.util.Log; import java.util.ArrayList; -import java.util.Map; - -class FetchSubscribedThing { - interface FetchSubscribedSubredditsListener { - void onFetchSubscribedSubredditsSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData, - ArrayList<SubscribedUserData> subscribedUserData, - ArrayList<SubredditData> subredditData); - void onFetchSubscribedSubredditsFail(); - } - - private Context context; - private RequestQueue requestQueue; - private FetchSubscribedSubredditsListener mFetchSubscribedSubredditsListener; - private ArrayList<SubscribedSubredditData> mSubscribedSubredditData; - private ArrayList<SubscribedUserData> mSubscribedUserData; - private ArrayList<SubredditData> mSubredditData; - private String mLastItem; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; +import retrofit2.converter.scalars.ScalarsConverterFactory; - FetchSubscribedThing(Context context, RequestQueue requestQueue, - ArrayList<SubscribedSubredditData> subscribedSubredditData, - ArrayList<SubscribedUserData> subscribedUserData, - ArrayList<SubredditData> subredditData) { - this.context = context; - this.requestQueue = requestQueue; - mSubscribedSubredditData = subscribedSubredditData; - mSubscribedUserData = subscribedUserData; - mSubredditData = subredditData; +class FetchSubscribedThing { + interface FetchSubscribedThingListener { + void onFetchSubscribedThingSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData, + ArrayList<SubscribedUserData> subscribedUserData, + ArrayList<SubredditData> subredditData); + void onFetchSubscribedThingFail(); } - void fetchSubscribedSubreddits(FetchSubscribedSubredditsListener fetchUserInfoListener, final int refreshTime) { + static void fetchSubscribedThing(final Context context, final String lastItem, + final ArrayList<SubscribedSubredditData> subscribedSubredditData, + final ArrayList<SubscribedUserData> subscribedUserData, + final ArrayList<SubredditData> subredditData, + final FetchSubscribedThingListener fetchSubscribedThingListener, final int refreshTime) { if(refreshTime < 0) { - mFetchSubscribedSubredditsListener.onFetchSubscribedSubredditsFail(); + fetchSubscribedThingListener.onFetchSubscribedThingFail(); return; } - Uri uri = Uri.parse(RedditUtils.OAUTH_API_BASE_URI + RedditUtils.SUBSCRIBED_SUBREDDITS) - .buildUpon().appendQueryParameter(RedditUtils.AFTER_KEY, mLastItem) - .appendQueryParameter(RedditUtils.RAW_JSON_KEY, RedditUtils.RAW_JSON_VALUE).build(); + Retrofit retrofit = new Retrofit.Builder() + .baseUrl(RedditUtils.OAUTH_API_BASE_URI) + .addConverterFactory(ScalarsConverterFactory.create()) + .build(); - mFetchSubscribedSubredditsListener = fetchUserInfoListener; - StringRequest commentRequest = new StringRequest(Request.Method.GET, uri.toString(), new Response.Listener<String>() { + RedditAPI api = retrofit.create(RedditAPI.class); + + String accessToken = context.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE) + .getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); + + Call<String> subredditDataCall = api.getSubscribedThing(lastItem, RedditUtils.getOAuthHeader(accessToken)); + subredditDataCall.enqueue(new Callback<String>() { @Override - public void onResponse(String response) { - ParseSubscribedThing.parseSubscribedSubreddits(response, mSubscribedSubredditData, - mSubscribedUserData, mSubredditData, + public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { + ParseSubscribedThing.parseSubscribedSubreddits(response.body(), subscribedSubredditData, + subscribedUserData, subredditData, new ParseSubscribedThing.ParseSubscribedSubredditsListener() { @Override @@ -64,49 +53,37 @@ class FetchSubscribedThing { ArrayList<SubscribedUserData> subscribedUserData, ArrayList<SubredditData> subredditData, String lastItem) { - mSubscribedSubredditData = subscribedSubredditData; - mSubscribedUserData = subscribedUserData; - mSubredditData = subredditData; - mLastItem = lastItem; - if(mLastItem.equals("null")) { - mFetchSubscribedSubredditsListener.onFetchSubscribedSubredditsSuccess(mSubscribedSubredditData, - mSubscribedUserData, mSubredditData); + if(lastItem.equals("null")) { + fetchSubscribedThingListener.onFetchSubscribedThingSuccess( + subscribedSubredditData, subscribedUserData, subredditData); } else { - fetchSubscribedSubreddits(mFetchSubscribedSubredditsListener, refreshTime); + fetchSubscribedThing(context, lastItem, subscribedSubredditData, + subscribedUserData, subredditData, + fetchSubscribedThingListener, refreshTime); } } @Override public void onParseSubscribedSubredditsFail() { - + fetchSubscribedThingListener.onFetchSubscribedThingFail(); } }); } - }, new Response.ErrorListener() { - @Override - public void onErrorResponse(VolleyError error) { - if(error instanceof AuthFailureError) { - new AcquireAccessToken(context).refreshAccessToken(requestQueue, new AcquireAccessToken.AcquireAccessTokenListener() { - @Override - public void onAcquireAccessTokenSuccess() { - fetchSubscribedSubreddits(mFetchSubscribedSubredditsListener, refreshTime - 1); - } - @Override - public void onAcquireAccessTokenFail() {} - }); - } else { - mFetchSubscribedSubredditsListener.onFetchSubscribedSubredditsFail(); - } - } - }) { @Override - public Map<String, String> getHeaders() { - String accessToken = context.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); - return RedditUtils.getOAuthHeader(accessToken); + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + Log.i("call failed", t.getMessage()); + RefreshAccessToken.refreshAccessToken(context, new RefreshAccessToken.RefreshAccessTokenListener() { + @Override + public void onRefreshAccessTokenSuccess() { + fetchSubscribedThing(context, lastItem, subscribedSubredditData, + subscribedUserData, subredditData, fetchSubscribedThingListener, refreshTime); + } + + @Override + public void onRefreshAccessTokenFail() {} + }); } - }; - commentRequest.setTag(FetchComment.class); - requestQueue.add(commentRequest); + }); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserInfo.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserInfo.java index 274c667e..45957308 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserInfo.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserInfo.java @@ -1,16 +1,13 @@ package ml.docilealligator.infinityforreddit; import android.content.Context; -import android.net.Uri; +import android.support.annotation.NonNull; +import android.util.Log; -import com.android.volley.AuthFailureError; -import com.android.volley.Request; -import com.android.volley.RequestQueue; -import com.android.volley.Response; -import com.android.volley.VolleyError; -import com.android.volley.toolbox.StringRequest; - -import java.util.Map; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Retrofit; +import retrofit2.converter.scalars.ScalarsConverterFactory; class FetchUserInfo { interface FetchUserInfoListener { @@ -18,57 +15,41 @@ class FetchUserInfo { void onFetchUserInfoFail(); } - private Context context; - private RequestQueue requestQueue; - private FetchUserInfoListener mFetchUserInfoListener; - - FetchUserInfo(Context context, RequestQueue requestQueue) { - this.context = context; - this.requestQueue = requestQueue; - } - - void queryUserInfo(FetchUserInfoListener fetchUserInfoListener, final int refreshTime) { + static void fetchUserInfo(final Context context, final FetchUserInfoListener fetchUserInfoListener, final int refreshTime) { if(refreshTime < 0) { - mFetchUserInfoListener.onFetchUserInfoFail(); + fetchUserInfoListener.onFetchUserInfoFail(); return; } - mFetchUserInfoListener = fetchUserInfoListener; - - Uri uri = Uri.parse(RedditUtils.OAUTH_API_BASE_URI + RedditUtils.USER_INFO_SUFFIX) - .buildUpon().appendQueryParameter(RedditUtils.RAW_JSON_KEY, RedditUtils.RAW_JSON_VALUE) + Retrofit retrofit = new Retrofit.Builder() + .baseUrl(RedditUtils.OAUTH_API_BASE_URI) + .addConverterFactory(ScalarsConverterFactory.create()) .build(); - StringRequest commentRequest = new StringRequest(Request.Method.GET, uri.toString(), new Response.Listener<String>() { + RedditAPI api = retrofit.create(RedditAPI.class); + + String accessToken = context.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE) + .getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); + Call<String> userInfo = api.getUserInfo(RedditUtils.getOAuthHeader(accessToken)); + userInfo.enqueue(new Callback<String>() { @Override - public void onResponse(String response) { - mFetchUserInfoListener.onFetchUserInfoSuccess(response); + public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { + fetchUserInfoListener.onFetchUserInfoSuccess(response.body()); } - }, new Response.ErrorListener() { - @Override - public void onErrorResponse(VolleyError error) { - if(error instanceof AuthFailureError) { - new AcquireAccessToken(context).refreshAccessToken(requestQueue, new AcquireAccessToken.AcquireAccessTokenListener() { - @Override - public void onAcquireAccessTokenSuccess() { - queryUserInfo(mFetchUserInfoListener, refreshTime - 1); - } - @Override - public void onAcquireAccessTokenFail() {} - }); - } else { - mFetchUserInfoListener.onFetchUserInfoFail(); - } - } - }) { @Override - public Map<String, String> getHeaders() { - String accessToken = context.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); - return RedditUtils.getOAuthHeader(accessToken); + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + Log.i("call failed", t.getMessage()); + RefreshAccessToken.refreshAccessToken(context, new RefreshAccessToken.RefreshAccessTokenListener() { + @Override + public void onRefreshAccessTokenSuccess() { + fetchUserInfo(context, fetchUserInfoListener, refreshTime - 1); + } + + @Override + public void onRefreshAccessTokenFail() {} + }); } - }; - commentRequest.setTag(FetchComment.class); - requestQueue.add(commentRequest); + }); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index d8bcdf08..181602e5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -21,7 +21,6 @@ import android.view.View; import android.widget.ImageView; import android.widget.TextView; -import com.android.volley.toolbox.Volley; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; @@ -100,15 +99,15 @@ public class MainActivity extends AppCompatActivity { queryAccessTokenTime.add(Calendar.SECOND, interval - 300); if(now.after(queryAccessTokenTime)) { - new AcquireAccessToken(this).refreshAccessToken(Volley.newRequestQueue(this), - new AcquireAccessToken.AcquireAccessTokenListener() { + RefreshAccessToken.refreshAccessToken(this, + new RefreshAccessToken.RefreshAccessTokenListener() { @Override - public void onAcquireAccessTokenSuccess() { + public void onRefreshAccessTokenSuccess() { loadUserData(savedInstanceState); } @Override - public void onAcquireAccessTokenFail() {} + public void onRefreshAccessTokenFail() {} }); } else { loadUserData(savedInstanceState); @@ -179,19 +178,19 @@ public class MainActivity extends AppCompatActivity { } private void loadUserData(Bundle savedInstanceState) { - if(savedInstanceState == null) { - if(!mFetchUserInfoSuccess) { - new FetchUserInfo(this, Volley.newRequestQueue(this)).queryUserInfo(new FetchUserInfo.FetchUserInfoListener() { + if (savedInstanceState == null) { + if (!mFetchUserInfoSuccess) { + FetchUserInfo.fetchUserInfo(this, new FetchUserInfo.FetchUserInfoListener() { @Override public void onFetchUserInfoSuccess(String response) { ParseUserInfo.parseUserInfo(response, new ParseUserInfo.ParseUserInfoListener() { @Override public void onParseUserInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) { mNameTextView.setText(name); - if(!mProfileImageUrl.equals("")) { + if (!mProfileImageUrl.equals("")) { glide.load(profileImageUrl).into(mProfileImageView); } - if(!mBannerImageUrl.equals("")) { + if (!mBannerImageUrl.equals("")) { glide.load(bannerImageUrl).into(mBannerImageView); } @@ -225,14 +224,15 @@ public class MainActivity extends AppCompatActivity { }, 1); } - if(!mInsertSuccess) { - new FetchSubscribedThing(this, Volley.newRequestQueue(this), new ArrayList<SubscribedSubredditData>(), - new ArrayList<SubscribedUserData>(), new ArrayList<SubredditData>()) - .fetchSubscribedSubreddits(new FetchSubscribedThing.FetchSubscribedSubredditsListener() { + if (!mInsertSuccess) { + FetchSubscribedThing.fetchSubscribedThing(this, null, + new ArrayList<SubscribedSubredditData>(), new ArrayList<SubscribedUserData>(), + new ArrayList<SubredditData>(), + new FetchSubscribedThing.FetchSubscribedThingListener() { @Override - public void onFetchSubscribedSubredditsSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData, - ArrayList<SubscribedUserData> subscribedUserData, - ArrayList<SubredditData> subredditData) { + public void onFetchSubscribedThingSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData, + ArrayList<SubscribedUserData> subscribedUserData, + ArrayList<SubredditData> subredditData) { new InsertSubscribedThingsAsyncTask( SubscribedSubredditRoomDatabase.getDatabase(MainActivity.this), SubscribedUserRoomDatabase.getDatabase(MainActivity.this), @@ -249,7 +249,7 @@ public class MainActivity extends AppCompatActivity { } @Override - public void onFetchSubscribedSubredditsFail() { + public void onFetchSubscribedThingFail() { mInsertSuccess = false; } }, 1); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index 7c90c488..585bd85f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -71,7 +71,7 @@ public class PostFragment extends Fragment { if(savedInstanceState.containsKey(PostDataParcelableState)) { mPostData = savedInstanceState.getParcelableArrayList(PostDataParcelableState); mLastItem = savedInstanceState.getString(lastItemState); - mAdapter = new PostRecyclerViewAdapter(getActivity(), mPostData, mPaginationSynchronizer, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue); + mAdapter = new PostRecyclerViewAdapter(getActivity(), mPostData, mPaginationSynchronizer, mVoteThingRequestQueue); mPostRecyclerView.setAdapter(mAdapter); mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener( getActivity(), mLinearLayoutManager, mAdapter, mLastItem, mPostData, @@ -82,9 +82,9 @@ public class PostFragment extends Fragment { mProgressBar.setVisibility(View.GONE); } else { if(mIsBestPost) { - queryBestPost(mQueryPostUrl, 1); + queryBestPost(1); } else { - queryPost(mQueryPostUrl, 1); + fetchPost(mQueryPostUrl, 1); } } } @@ -99,7 +99,7 @@ public class PostFragment extends Fragment { } if(mAcquireAccessTokenRequestQueue != null) { - mAcquireAccessTokenRequestQueue.cancelAll(AcquireAccessToken.class); + mAcquireAccessTokenRequestQueue.cancelAll(RefreshAccessToken.class); } if(mVoteThingRequestQueue != null) { @@ -156,9 +156,9 @@ public class PostFragment extends Fragment { } else { mPaginationSynchronizer = new PaginationSynchronizer(); if(mIsBestPost) { - queryBestPost(mQueryPostUrl, 1); + queryBestPost(1); } else { - queryPost(mQueryPostUrl, 1); + fetchPost(mQueryPostUrl, 1); } } @@ -181,7 +181,7 @@ public class PostFragment extends Fragment { return rootView; } - private void queryBestPost(final String queryPostUrl, final int refreshTime) { + private void queryBestPost(final int refreshTime) { if(refreshTime < 0) { showErrorSnackbar(); return; @@ -203,7 +203,7 @@ public class PostFragment extends Fragment { public void onParsePostSuccess(ArrayList<PostData> postData, String lastItem) { mPostData = postData; mLastItem = lastItem; - mAdapter = new PostRecyclerViewAdapter(getActivity(), postData, mPaginationSynchronizer, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue); + mAdapter = new PostRecyclerViewAdapter(getActivity(), postData, mPaginationSynchronizer, mVoteThingRequestQueue); mPostRecyclerView.setAdapter(mAdapter); mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener( @@ -230,15 +230,15 @@ public class PostFragment extends Fragment { if (error instanceof AuthFailureError) { // Error indicating that there was an Authentication Failure while performing the request // Access token expired - new AcquireAccessToken(getActivity()).refreshAccessToken(mAcquireAccessTokenRequestQueue, - new AcquireAccessToken.AcquireAccessTokenListener() { + RefreshAccessToken.refreshAccessToken(getActivity(), + new RefreshAccessToken.RefreshAccessTokenListener() { @Override - public void onAcquireAccessTokenSuccess() { - queryBestPost(queryPostUrl, refreshTime - 1); + public void onRefreshAccessTokenSuccess() { + queryBestPost(refreshTime - 1); } @Override - public void onAcquireAccessTokenFail() {} + public void onRefreshAccessTokenFail() {} }); } else { Log.i("Post fetch error", error.toString()); @@ -256,7 +256,7 @@ public class PostFragment extends Fragment { mRequestQueue.add(postRequest); } - private void queryPost(final String queryPostUrl, final int refreshTime) { + private void fetchPost(final String queryPostUrl, final int refreshTime) { if(refreshTime < 0) { showErrorSnackbar(); return; @@ -282,7 +282,7 @@ public class PostFragment extends Fragment { public void onParsePostSuccess(ArrayList<PostData> postData, String lastItem) { mPostData = postData; mLastItem = lastItem; - mAdapter = new PostRecyclerViewAdapter(getActivity(), postData, mPaginationSynchronizer, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue); + mAdapter = new PostRecyclerViewAdapter(getActivity(), postData, mPaginationSynchronizer, mVoteThingRequestQueue); mPostRecyclerView.setAdapter(mAdapter); mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener( @@ -309,15 +309,15 @@ public class PostFragment extends Fragment { if (error instanceof AuthFailureError) { // Error indicating that there was an Authentication Failure while performing the request // Access token expired - new AcquireAccessToken(getActivity()).refreshAccessToken(mAcquireAccessTokenRequestQueue, - new AcquireAccessToken.AcquireAccessTokenListener() { + RefreshAccessToken.refreshAccessToken(getActivity(), + new RefreshAccessToken.RefreshAccessTokenListener() { @Override - public void onAcquireAccessTokenSuccess() { - queryPost(queryPostUrl, refreshTime - 1); + public void onRefreshAccessTokenSuccess() { + fetchPost(queryPostUrl, refreshTime - 1); } @Override - public void onAcquireAccessTokenFail() {} + public void onRefreshAccessTokenFail() {} }); } else { Log.i("Post fetch error", error.toString()); @@ -336,9 +336,9 @@ public class PostFragment extends Fragment { @Override public void onClick(View view) { if(mIsBestPost) { - queryBestPost(mQueryPostUrl, 1); + queryBestPost(1); } else { - queryPost(mQueryPostUrl, 1); + fetchPost(mQueryPostUrl, 1); } } }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java index 6aa01cf4..57712c6c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java @@ -146,15 +146,15 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener { public void onErrorResponse(VolleyError error) { if (error instanceof AuthFailureError) { //Access token expired - new AcquireAccessToken(mContext).refreshAccessToken(mAcquireAccessTokenRequestQueue, - new AcquireAccessToken.AcquireAccessTokenListener() { + RefreshAccessToken.refreshAccessToken(mContext, + new RefreshAccessToken.RefreshAccessTokenListener() { @Override - public void onAcquireAccessTokenSuccess() { + public void onRefreshAccessTokenSuccess() { fetchBestPost(queryPostUrl, refreshTime - 1); } @Override - public void onAcquireAccessTokenFail() { + public void onRefreshAccessTokenFail() { } }); } else { @@ -219,15 +219,15 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener { public void onErrorResponse(VolleyError error) { if (error instanceof AuthFailureError) { //Access token expired - new AcquireAccessToken(mContext).refreshAccessToken(mAcquireAccessTokenRequestQueue, - new AcquireAccessToken.AcquireAccessTokenListener() { + RefreshAccessToken.refreshAccessToken(mContext, + new RefreshAccessToken.RefreshAccessTokenListener() { @Override - public void onAcquireAccessTokenSuccess() { + public void onRefreshAccessTokenSuccess() { fetchPost(queryPostUrl, refreshTime - 1); } @Override - public void onAcquireAccessTokenFail() { + public void onRefreshAccessTokenFail() { } }); } else { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index 7dc95034..e1efc0e6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -44,7 +44,6 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold private Context mContext; private PaginationSynchronizer mPaginationSynchronizer; private RequestQueue mVoteThingRequestQueue; - private RequestQueue mAcquireAccessTokenRequestQueue; private RequestManager glide; private SubredditDao subredditDao; private boolean isLoadingMorePostSuccess; @@ -55,13 +54,12 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold PostRecyclerViewAdapter(Context context, ArrayList<PostData> postData, PaginationSynchronizer paginationSynchronizer, - RequestQueue voteThingRequestQueue, RequestQueue acquireAccessTokenRequestQueue) { + RequestQueue voteThingRequestQueue) { if(context != null) { mContext = context; mPostData = postData; mPaginationSynchronizer = paginationSynchronizer; mVoteThingRequestQueue = voteThingRequestQueue; - mAcquireAccessTokenRequestQueue = acquireAccessTokenRequestQueue; isLoadingMorePostSuccess = true; canStartActivity = true; glide = Glide.with(mContext); @@ -139,7 +137,7 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold @Override public void onClick(View view) { Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class); - intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME, + intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, mPostData.get(holder.getAdapterPosition()).getSubredditNamePrefixed().substring(2)); } }); @@ -348,7 +346,7 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(mPostData.get(holder.getAdapterPosition()).getScore() + 1)); } - new VoteThing(mContext, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingListener() { + VoteThing.voteThing(mContext, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position) { mPostData.get(position).setVoteType(1); @@ -372,7 +370,7 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold ((DataViewHolder) holder).upvoteButton.clearColorFilter(); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(mPostData.get(holder.getAdapterPosition()).getScore() - 1)); - new VoteThing(mContext, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingListener() { + VoteThing.voteThing(mContext, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position) { mPostData.get(position).setVoteType(0); @@ -406,7 +404,7 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(mPostData.get(holder.getAdapterPosition()).getScore() - 1)); } - new VoteThing(mContext, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingListener() { + VoteThing.voteThing(mContext, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position) { mPostData.get(position).setVoteType(-1); @@ -430,7 +428,7 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold ((DataViewHolder) holder).downvoteButton.clearColorFilter(); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(mPostData.get(holder.getAdapterPosition()).getScore() + 1)); - new VoteThing(mContext, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingListener() { + VoteThing.voteThing(mContext, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position) { mPostData.get(position).setVoteType(0); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index 5e4c5558..a0985428 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -1,10 +1,34 @@ package ml.docilealligator.infinityforreddit; +import java.util.Map; + import retrofit2.Call; +import retrofit2.http.FieldMap; +import retrofit2.http.FormUrlEncoded; import retrofit2.http.GET; +import retrofit2.http.HeaderMap; +import retrofit2.http.POST; import retrofit2.http.Path; +import retrofit2.http.Query; public interface RedditAPI { + @FormUrlEncoded + @POST("api/v1/access_token") + Call<String> getAccessToken(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params); + @GET("{subredditNamePrefixed}/comments/{article}.json?raw_json=1") Call<String> getComments(@Path("subredditNamePrefixed") String subredditNamePrefixed, @Path("article") String article); + + @GET("r/{subredditName}/about.json?raw_json=1") + Call<String> getSubredditData(@Path("subredditName") String subredditName); + + @GET("subreddits/mine/subscriber?raw_json=1") + Call<String> getSubscribedThing(@Query("after") String lastItem, @HeaderMap Map<String, String> headers); + + @GET("api/v1/me?raw_json=1") + Call<String> getUserInfo(@HeaderMap Map<String, String> headers); + + @FormUrlEncoded + @POST("api/vote") + Call<String> voteThing(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java index c5a13ec3..f6833ee8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java @@ -11,15 +11,11 @@ import java.util.Map; class RedditUtils { static final String OAUTH_URL ="https://www.reddit.com/api/v1/authorize.compact"; - static final String ACQUIRE_ACCESS_TOKEN_URL = "https://www.reddit.com/api/v1/access_token"; static final String OAUTH_API_BASE_URI = "https://oauth.reddit.com"; static final String API_BASE_URI = "https://www.reddit.com"; static final String RAW_JSON_KEY ="raw_json"; static final String RAW_JSON_VALUE = "1"; static final String BEST_POST_SUFFIX = "/best"; - static final String VOTE_SUFFIX = "/api/vote"; - static final String USER_INFO_SUFFIX = "/api/v1/me"; - static final String SUBSCRIBED_SUBREDDITS = "/subreddits/mine/subscriber"; static final String CLIENT_ID_KEY = "client_id"; static final String CLIENT_ID = ""; @@ -70,14 +66,6 @@ class RedditUtils { return params; } - static String getQueryCommentUrl(String subredditName, String article) { - return API_BASE_URI + "/" + subredditName + "/comments/" + article + ".json"; - } - - static String getQuerySubredditDataUrl(String subredditName) { - return API_BASE_URI + "/r/" + subredditName + "/about.json"; - } - static String getQuerySubredditPostUrl(String subredditName) { return API_BASE_URI + "/r/" + subredditName + ".json"; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RefreshAccessToken.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RefreshAccessToken.java new file mode 100644 index 00000000..ac664ef7 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RefreshAccessToken.java @@ -0,0 +1,82 @@ +package ml.docilealligator.infinityforreddit; + +import android.content.Context; +import android.content.SharedPreferences; +import android.support.annotation.NonNull; +import android.util.Log; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.Calendar; +import java.util.HashMap; +import java.util.Map; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Retrofit; +import retrofit2.converter.scalars.ScalarsConverterFactory; + +/** + * Created by alex on 3/13/18. + */ + +class RefreshAccessToken { + + interface RefreshAccessTokenListener { + void onRefreshAccessTokenSuccess(); + void onRefreshAccessTokenFail(); + } + + static void refreshAccessToken(final Context context, final RefreshAccessTokenListener refreshAccessTokenListener) { + if(context != null) { + String refreshToken = context.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.REFRESH_TOKEN_KEY, ""); + + final Retrofit retrofit = new Retrofit.Builder() + .baseUrl(RedditUtils.API_BASE_URI) + .addConverterFactory(ScalarsConverterFactory.create()) + .build(); + + RedditAPI api = retrofit.create(RedditAPI.class); + + Map<String, String> params = new HashMap<>(); + params.put(RedditUtils.GRANT_TYPE_KEY, RedditUtils.GRANT_TYPE_REFRESH_TOKEN); + params.put(RedditUtils.REFRESH_TOKEN_KEY, refreshToken); + + Call<String> accessTokenCall = api.getAccessToken(RedditUtils.getHttpBasicAuthHeader(), params); + accessTokenCall.enqueue(new Callback<String>() { + @Override + public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { + try { + JSONObject jsonObject = new JSONObject(response.body()); + String newAccessToken = jsonObject.getString(RedditUtils.ACCESS_TOKEN_KEY); + int expireIn = jsonObject.getInt(RedditUtils.EXPIRES_IN_KEY); + + SharedPreferences.Editor editor = context.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE).edit(); + editor.putString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, newAccessToken); + editor.putInt(SharedPreferencesUtils.ACCESS_TOKEN_EXPIRE_INTERVAL_KEY, expireIn); + editor.apply(); + + long queryAccessTokenTime = Calendar.getInstance().getTimeInMillis(); + editor.putLong(SharedPreferencesUtils.QUERY_ACCESS_TOKEN_TIME_KEY, queryAccessTokenTime); + editor.apply(); + + Log.i("access token", newAccessToken); + refreshAccessTokenListener.onRefreshAccessTokenSuccess(); + } catch (JSONException e) { + e.printStackTrace(); + refreshAccessTokenListener.onRefreshAccessTokenFail(); + Log.i("main activity", "Error parsing JSON object when getting the access token"); + } + refreshAccessTokenListener.onRefreshAccessTokenSuccess(); + } + + @Override + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + Log.i("call failed", t.getMessage()); + refreshAccessTokenListener.onRefreshAccessTokenFail(); + } + }); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRecyclerViewAdapter.java index 141a3fef..ebc316e5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRecyclerViewAdapter.java @@ -38,8 +38,8 @@ class SubscribedSubredditRecyclerViewAdapter extends RecyclerView.Adapter<Recycl @Override public void onClick(View view) { Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class); - intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME, mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getName()); - intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_ID, mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getId()); + intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getName()); + intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_ID_KEY, mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getId()); mContext.startActivity(intent); } }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 052e675e..10591902 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -24,8 +24,6 @@ import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; -import com.android.volley.RequestQueue; -import com.android.volley.toolbox.Volley; import com.bumptech.glide.Glide; import com.bumptech.glide.load.DataSource; import com.bumptech.glide.load.engine.GlideException; @@ -59,9 +57,6 @@ public class ViewPostDetailActivity extends AppCompatActivity { private LoadSubredditIconAsyncTask mLoadSubredditIconAsyncTask; - private RequestQueue mVoteThingRequestQueue; - private RequestQueue mAcquireAccessTokenRequestQueue; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -140,8 +135,6 @@ public class ViewPostDetailActivity extends AppCompatActivity { mRecyclerView.setNestedScrollingEnabled(false); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); - mVoteThingRequestQueue = Volley.newRequestQueue(this); - mAcquireAccessTokenRequestQueue = Volley.newRequestQueue(this); subredditTextView.setText(mPostData.getSubredditNamePrefixed()); postTimeTextView.setText(mPostData.getPostTime()); @@ -385,7 +378,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { scoreTextView.setText(Integer.toString(mPostData.getScore() + 1)); } - new VoteThing(ViewPostDetailActivity.this, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingWithoutPositionListener() { + VoteThing.voteThing(ViewPostDetailActivity.this, new VoteThing.VoteThingWithoutPositionListener() { @Override public void onVoteThingSuccess() { mPostData.setVoteType(1); @@ -409,7 +402,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { upvoteButton.clearColorFilter(); scoreTextView.setText(Integer.toString(mPostData.getScore() - 1)); - new VoteThing(ViewPostDetailActivity.this, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingWithoutPositionListener() { + VoteThing.voteThing(ViewPostDetailActivity.this, new VoteThing.VoteThingWithoutPositionListener() { @Override public void onVoteThingSuccess() { mPostData.setVoteType(0); @@ -445,7 +438,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { scoreTextView.setText(Integer.toString(mPostData.getScore() - 1)); } - new VoteThing(ViewPostDetailActivity.this, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingWithoutPositionListener() { + VoteThing.voteThing(ViewPostDetailActivity.this, new VoteThing.VoteThingWithoutPositionListener() { @Override public void onVoteThingSuccess() { mPostData.setVoteType(-1); @@ -469,7 +462,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { downvoteButton.clearColorFilter(); scoreTextView.setText(Integer.toString(mPostData.getScore() + 1)); - new VoteThing(ViewPostDetailActivity.this, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingWithoutPositionListener() { + VoteThing.voteThing(ViewPostDetailActivity.this, new VoteThing.VoteThingWithoutPositionListener() { @Override public void onVoteThingSuccess() { mPostData.setVoteType(0); @@ -492,40 +485,41 @@ public class ViewPostDetailActivity extends AppCompatActivity { private void queryComment() { mCommentProgressbar.setVisibility(View.VISIBLE); mNoCommentWrapperLinearLayout.setVisibility(View.GONE); - new FetchComment(mPostData.getSubredditNamePrefixed(), mPostData.getId()).queryComment(new FetchComment.FetchCommentListener() { - @Override - public void onFetchCommentSuccess(String response) { - ParseComment.parseComment(response, new ArrayList<CommentData>(), - getResources().getConfiguration().locale, new ParseComment.ParseCommentListener() { - @Override - public void onParseCommentSuccess(ArrayList<CommentData> commentData, int moreCommentCount) { - mCommentProgressbar.setVisibility(View.GONE); - mMoreCommentCount = moreCommentCount; - if (commentData.size() > 0) { - CommentRecyclerViewAdapter adapter = new CommentRecyclerViewAdapter( - ViewPostDetailActivity.this, commentData, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue); - mRecyclerView.setAdapter(adapter); - mCommentCardView.setVisibility(View.VISIBLE); - } else { - mNoCommentWrapperLinearLayout.setVisibility(View.VISIBLE); - Glide.with(ViewPostDetailActivity.this).load(R.drawable.no_comment_indicator).into(mNoCommentImageView); - } - } + FetchComment.queryComment(mPostData.getSubredditNamePrefixed(), mPostData.getId(), + new FetchComment.FetchCommentListener() { + @Override + public void onFetchCommentSuccess(String response) { + ParseComment.parseComment(response, new ArrayList<CommentData>(), + getResources().getConfiguration().locale, new ParseComment.ParseCommentListener() { + @Override + public void onParseCommentSuccess(ArrayList<CommentData> commentData, int moreCommentCount) { + mCommentProgressbar.setVisibility(View.GONE); + mMoreCommentCount = moreCommentCount; + if (commentData.size() > 0) { + CommentRecyclerViewAdapter adapter = new CommentRecyclerViewAdapter( + ViewPostDetailActivity.this, commentData); + mRecyclerView.setAdapter(adapter); + mCommentCardView.setVisibility(View.VISIBLE); + } else { + mNoCommentWrapperLinearLayout.setVisibility(View.VISIBLE); + Glide.with(ViewPostDetailActivity.this).load(R.drawable.no_comment_indicator).into(mNoCommentImageView); + } + } + + @Override + public void onParseCommentFail() { + mCommentProgressbar.setVisibility(View.GONE); + showRetrySnackbar(); + } + }); + } @Override - public void onParseCommentFail() { + public void onFetchCommentFail() { mCommentProgressbar.setVisibility(View.GONE); showRetrySnackbar(); } }); - } - - @Override - public void onFetchCommentFail() { - mCommentProgressbar.setVisibility(View.GONE); - showRetrySnackbar(); - } - }); } private void showRetrySnackbar() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java index 66fd16f8..f169ad8b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java @@ -18,7 +18,6 @@ import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; -import com.android.volley.toolbox.Volley; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; @@ -26,8 +25,10 @@ import de.hdodenhof.circleimageview.CircleImageView; public class ViewSubredditDetailActivity extends AppCompatActivity { - static final String EXTRA_SUBREDDIT_NAME = "ESN"; - static final String EXTRA_SUBREDDIT_ID = "ESI"; + static final String EXTRA_SUBREDDIT_NAME_KEY = "ESN"; + static final String EXTRA_SUBREDDIT_ID_KEY = "ESI"; + + private static final String FRAGMENT_OUT_STATE_KEY = "FOSK"; private Fragment mFragment; @@ -40,7 +41,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); - final String subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME); + final String subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME_KEY); final String title = "r/" + subredditName; setTitle(title); @@ -74,7 +75,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { final TextView descriptionTextView = findViewById(R.id.description_text_view_view_subreddit_detail_activity); final RequestManager glide = Glide.with(ViewSubredditDetailActivity.this); - String id = getIntent().getExtras().getString(EXTRA_SUBREDDIT_ID); + String id = getIntent().getExtras().getString(EXTRA_SUBREDDIT_ID_KEY); SubredditViewModel.Factory factory = new SubredditViewModel.Factory(getApplication(), id); mSubredditViewModel = ViewModelProviders.of(this, factory).get(SubredditViewModel.class); mSubredditViewModel.getSubredditLiveData().observe(this, new Observer<SubredditData>() { @@ -137,7 +138,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { } }); - new FetchSubredditData(Volley.newRequestQueue(this), subredditName).querySubredditData(new FetchSubredditData.FetchSubredditDataListener() { + FetchSubredditData.fetchSubredditData(subredditName, new FetchSubredditData.FetchSubredditDataListener() { @Override public void onFetchSubredditDataSuccess(String response) { ParseSubredditData.parseComment(response, new ParseSubredditData.ParseSubredditDataListener() { @@ -173,7 +174,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { mFragment.setArguments(bundle); getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_subreddit_detail_activity, mFragment).commit(); } else { - mFragment = getSupportFragmentManager().getFragment(savedInstanceState, "outStateFragment"); + mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE_KEY); getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_subreddit_detail_activity, mFragment).commit(); } } @@ -192,7 +193,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if(mFragment != null) { - getSupportFragmentManager().putFragment(outState, "outStateFragment", mFragment); + getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE_KEY, mFragment); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/VoteThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/VoteThing.java index 0b6ef4e1..c159de08 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/VoteThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/VoteThing.java @@ -1,17 +1,17 @@ package ml.docilealligator.infinityforreddit; import android.content.Context; - -import com.android.volley.AuthFailureError; -import com.android.volley.Request; -import com.android.volley.RequestQueue; -import com.android.volley.Response; -import com.android.volley.VolleyError; -import com.android.volley.toolbox.StringRequest; +import android.support.annotation.NonNull; +import android.util.Log; import java.util.HashMap; import java.util.Map; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Retrofit; +import retrofit2.converter.scalars.ScalarsConverterFactory; + /** * Created by alex on 3/14/18. */ @@ -28,116 +28,96 @@ class VoteThing { void onVoteThingFail(); } - private Context mContext; - private RequestQueue mQueue; - private RequestQueue mAcquireAccessTokenRequestQueue; - - VoteThing(Context context, RequestQueue queue, RequestQueue acquireAccessTokenRequestQueue) { - mContext = context; - mQueue = queue; - mAcquireAccessTokenRequestQueue = acquireAccessTokenRequestQueue; - } - - void votePost(final VoteThingListener voteThingListener, final String fullName, final String point, final int position, final int refreshTime) { - if(mContext != null) { + static void voteThing(final Context context, final VoteThingListener voteThingListener, final String fullName, final String point, final int position, final int refreshTime) { + if(context != null) { if(refreshTime < 0) { voteThingListener.onVoteThingFail(position); return; } - StringRequest voteRequest = new StringRequest(Request.Method.POST, RedditUtils.OAUTH_API_BASE_URI + RedditUtils.VOTE_SUFFIX, new Response.Listener<String>() { + Retrofit retrofit = new Retrofit.Builder() + .baseUrl(RedditUtils.OAUTH_API_BASE_URI) + .addConverterFactory(ScalarsConverterFactory.create()) + .build(); + + RedditAPI api = retrofit.create(RedditAPI.class); + + String accessToken = context.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE) + .getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); + Map<String, String> params = new HashMap<>(); + params.put(RedditUtils.DIR_KEY, point); + params.put(RedditUtils.ID_KEY, fullName); + params.put(RedditUtils.RANK_KEY, RedditUtils.RANK); + + Call<String> voteThingCall = api.voteThing(RedditUtils.getOAuthHeader(accessToken), params); + voteThingCall.enqueue(new Callback<String>() { @Override - public void onResponse(String response) { + public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { voteThingListener.onVoteThingSuccess(position); } - }, new Response.ErrorListener() { - @Override - public void onErrorResponse(VolleyError error) { - if (error instanceof AuthFailureError) { - //Access token expired - new AcquireAccessToken(mContext).refreshAccessToken(mAcquireAccessTokenRequestQueue, - new AcquireAccessToken.AcquireAccessTokenListener() { - @Override - public void onAcquireAccessTokenSuccess() { - votePost(voteThingListener, fullName, point, position, refreshTime - 1); - } - - @Override - public void onAcquireAccessTokenFail() {} - }); - } else { - voteThingListener.onVoteThingFail(position); - } - } - }) { - @Override - protected Map<String, String> getParams() { - HashMap<String, String> params = new HashMap<>(); - params.put(RedditUtils.DIR_KEY, point); - params.put(RedditUtils.ID_KEY, fullName); - params.put(RedditUtils.RANK_KEY, RedditUtils.RANK); - return params; - } @Override - public Map<String, String> getHeaders() { - String accessToken = mContext.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); - return RedditUtils.getOAuthHeader(accessToken); + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + Log.i("call failed", t.getMessage()); + RefreshAccessToken.refreshAccessToken(context, + new RefreshAccessToken.RefreshAccessTokenListener() { + @Override + public void onRefreshAccessTokenSuccess() { + voteThing(context, voteThingListener, fullName, point, position, refreshTime - 1); + } + + @Override + public void onRefreshAccessTokenFail() { + } + }); } - }; - voteRequest.setTag(VoteThing.class); - mQueue.add(voteRequest); + }); } } - void votePost(final VoteThingWithoutPositionListener voteThingWithoutPositionListener, final String fullName, final String point, final int refreshTime) { - if(mContext != null) { + static void voteThing(final Context context, final VoteThingWithoutPositionListener voteThingWithoutPositionListener, final String fullName, final String point, final int refreshTime) { + if(context != null) { if(refreshTime < 0) { voteThingWithoutPositionListener.onVoteThingFail(); return; } - StringRequest voteRequest = new StringRequest(Request.Method.POST, RedditUtils.OAUTH_API_BASE_URI + RedditUtils.VOTE_SUFFIX, new Response.Listener<String>() { + + Retrofit retrofit = new Retrofit.Builder() + .baseUrl(RedditUtils.OAUTH_API_BASE_URI) + .addConverterFactory(ScalarsConverterFactory.create()) + .build(); + + RedditAPI api = retrofit.create(RedditAPI.class); + + String accessToken = context.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE) + .getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); + Map<String, String> params = new HashMap<>(); + params.put(RedditUtils.DIR_KEY, point); + params.put(RedditUtils.ID_KEY, fullName); + params.put(RedditUtils.RANK_KEY, RedditUtils.RANK); + + Call<String> voteThingCall = api.voteThing(RedditUtils.getOAuthHeader(accessToken), params); + voteThingCall.enqueue(new Callback<String>() { @Override - public void onResponse(String response) { + public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { voteThingWithoutPositionListener.onVoteThingSuccess(); } - }, new Response.ErrorListener() { - @Override - public void onErrorResponse(VolleyError error) { - if (error instanceof AuthFailureError) { - //Access token expired - new AcquireAccessToken(mContext).refreshAccessToken(mAcquireAccessTokenRequestQueue, - new AcquireAccessToken.AcquireAccessTokenListener() { - @Override - public void onAcquireAccessTokenSuccess() { - votePost(voteThingWithoutPositionListener, fullName, point, refreshTime - 1); - } - - @Override - public void onAcquireAccessTokenFail() {} - }); - } else { - voteThingWithoutPositionListener.onVoteThingFail(); - } - } - }) { - @Override - protected Map<String, String> getParams() { - HashMap<String, String> params = new HashMap<>(); - params.put(RedditUtils.DIR_KEY, point); - params.put(RedditUtils.ID_KEY, fullName); - params.put(RedditUtils.RANK_KEY, RedditUtils.RANK); - return params; - } @Override - public Map<String, String> getHeaders() { - String accessToken = mContext.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); - return RedditUtils.getOAuthHeader(accessToken); + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + Log.i("call failed", t.getMessage()); + RefreshAccessToken.refreshAccessToken(context, + new RefreshAccessToken.RefreshAccessTokenListener() { + @Override + public void onRefreshAccessTokenSuccess() { + voteThing(context, voteThingWithoutPositionListener, fullName, point, refreshTime - 1); + } + + @Override + public void onRefreshAccessTokenFail() {} + }); } - }; - voteRequest.setTag(VoteThing.class); - mQueue.add(voteRequest); + }); } } } |