mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-01-12 03:07:11 +01:00
Use Retrofit to send API requests. Rename some methods. Changing the type of some methods related to fetching data to static.
This commit is contained in:
parent
a6a951732d
commit
2a57e4d5b4
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -12,8 +12,6 @@ import android.widget.ImageView;
|
|||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.android.volley.RequestQueue;
|
|
||||||
|
|
||||||
import org.sufficientlysecure.htmltextview.HtmlTextView;
|
import org.sufficientlysecure.htmltextview.HtmlTextView;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -21,15 +19,10 @@ import java.util.ArrayList;
|
|||||||
class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private ArrayList<CommentData> mCommentData;
|
private ArrayList<CommentData> mCommentData;
|
||||||
private RequestQueue mVoteThingRequestQueue;
|
|
||||||
private RequestQueue mAcquireAccessTokenRequestQueue;
|
|
||||||
|
|
||||||
CommentRecyclerViewAdapter(Context context, ArrayList<CommentData> commentData,
|
CommentRecyclerViewAdapter(Context context, ArrayList<CommentData> commentData) {
|
||||||
RequestQueue voteThingRequestQueue, RequestQueue acquireAccessTokenRequestQueue) {
|
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mCommentData = commentData;
|
mCommentData = commentData;
|
||||||
mVoteThingRequestQueue = voteThingRequestQueue;
|
|
||||||
mAcquireAccessTokenRequestQueue = acquireAccessTokenRequestQueue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@ -71,7 +64,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
|
|||||||
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(mCommentData.get(position).getScore() + 1));
|
((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
|
@Override
|
||||||
public void onVoteThingSuccess(int position) {
|
public void onVoteThingSuccess(int position) {
|
||||||
mCommentData.get(position).setVoteType(1);
|
mCommentData.get(position).setVoteType(1);
|
||||||
@ -95,7 +88,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
|
|||||||
((CommentViewHolder) holder).upvoteButton.clearColorFilter();
|
((CommentViewHolder) holder).upvoteButton.clearColorFilter();
|
||||||
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(mCommentData.get(position).getScore() - 1));
|
((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
|
@Override
|
||||||
public void onVoteThingSuccess(int position) {
|
public void onVoteThingSuccess(int position) {
|
||||||
mCommentData.get(position).setVoteType(0);
|
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));
|
((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
|
@Override
|
||||||
public void onVoteThingSuccess(int position) {
|
public void onVoteThingSuccess(int position) {
|
||||||
mCommentData.get(position).setVoteType(-1);
|
mCommentData.get(position).setVoteType(-1);
|
||||||
@ -154,7 +147,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
|
|||||||
((CommentViewHolder) holder).downvoteButton.clearColorFilter();
|
((CommentViewHolder) holder).downvoteButton.clearColorFilter();
|
||||||
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(mCommentData.get(position).getScore() + 1));
|
((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
|
@Override
|
||||||
public void onVoteThingSuccess(int position) {
|
public void onVoteThingSuccess(int position) {
|
||||||
mCommentData.get(position).setVoteType(0);
|
mCommentData.get(position).setVoteType(0);
|
||||||
|
@ -15,15 +15,7 @@ class FetchComment {
|
|||||||
void onFetchCommentFail();
|
void onFetchCommentFail();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String subredditNamePrefixed;
|
static void queryComment(String subredditNamePrefixed, String article, final FetchCommentListener fetchCommentListener) {
|
||||||
private String article;
|
|
||||||
|
|
||||||
FetchComment(String subredditNamePrefixed, String article) {
|
|
||||||
this.subredditNamePrefixed = subredditNamePrefixed;
|
|
||||||
this.article = article;
|
|
||||||
}
|
|
||||||
|
|
||||||
void queryComment(final FetchCommentListener fetchCommentListener) {
|
|
||||||
Retrofit retrofit = new Retrofit.Builder()
|
Retrofit retrofit = new Retrofit.Builder()
|
||||||
.baseUrl(RedditUtils.API_BASE_URI)
|
.baseUrl(RedditUtils.API_BASE_URI)
|
||||||
.addConverterFactory(ScalarsConverterFactory.create())
|
.addConverterFactory(ScalarsConverterFactory.create())
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
package ml.docilealligator.infinityforreddit;
|
package ml.docilealligator.infinityforreddit;
|
||||||
|
|
||||||
import com.android.volley.Request;
|
import android.support.annotation.NonNull;
|
||||||
import com.android.volley.RequestQueue;
|
import android.util.Log;
|
||||||
import com.android.volley.Response;
|
|
||||||
import com.android.volley.VolleyError;
|
import retrofit2.Call;
|
||||||
import com.android.volley.toolbox.StringRequest;
|
import retrofit2.Callback;
|
||||||
|
import retrofit2.Response;
|
||||||
|
import retrofit2.Retrofit;
|
||||||
|
import retrofit2.converter.scalars.ScalarsConverterFactory;
|
||||||
|
|
||||||
class FetchSubredditData {
|
class FetchSubredditData {
|
||||||
interface FetchSubredditDataListener {
|
interface FetchSubredditDataListener {
|
||||||
@ -12,29 +15,26 @@ class FetchSubredditData {
|
|||||||
void onFetchSubredditDataFail();
|
void onFetchSubredditDataFail();
|
||||||
}
|
}
|
||||||
|
|
||||||
private RequestQueue requestQueue;
|
static void fetchSubredditData(String subredditName, final FetchSubredditDataListener fetchSubredditDataListener) {
|
||||||
private String subredditName;
|
Retrofit retrofit = new Retrofit.Builder()
|
||||||
private FetchSubredditDataListener mFetchSubredditDataListener;
|
.baseUrl(RedditUtils.API_BASE_URI)
|
||||||
|
.addConverterFactory(ScalarsConverterFactory.create())
|
||||||
|
.build();
|
||||||
|
|
||||||
FetchSubredditData(RequestQueue requestQueue, String subredditName) {
|
RedditAPI api = retrofit.create(RedditAPI.class);
|
||||||
this.requestQueue = requestQueue;
|
|
||||||
this.subredditName = subredditName;
|
Call<String> subredditData = api.getSubredditData(subredditName);
|
||||||
|
subredditData.enqueue(new Callback<String>() {
|
||||||
|
@Override
|
||||||
|
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
||||||
|
fetchSubredditDataListener.onFetchSubredditDataSuccess(response.body());
|
||||||
}
|
}
|
||||||
|
|
||||||
void querySubredditData(FetchSubredditDataListener fetchSubredditDataListener) {
|
|
||||||
mFetchSubredditDataListener = fetchSubredditDataListener;
|
|
||||||
StringRequest commentRequest = new StringRequest(Request.Method.GET, RedditUtils.getQuerySubredditDataUrl(subredditName), new Response.Listener<String>() {
|
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(String response) {
|
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
|
||||||
mFetchSubredditDataListener.onFetchSubredditDataSuccess(response);
|
Log.i("call failed", t.getMessage());
|
||||||
|
fetchSubredditDataListener.onFetchSubredditDataFail();
|
||||||
}
|
}
|
||||||
}, new Response.ErrorListener() {
|
});
|
||||||
@Override
|
|
||||||
public void onErrorResponse(VolleyError error) {
|
|
||||||
mFetchSubredditDataListener.onFetchSubredditDataFail();
|
|
||||||
}
|
|
||||||
}) {};
|
|
||||||
commentRequest.setTag(FetchSubredditData.class);
|
|
||||||
requestQueue.add(commentRequest);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,62 +1,51 @@
|
|||||||
package ml.docilealligator.infinityforreddit;
|
package ml.docilealligator.infinityforreddit;
|
||||||
|
|
||||||
import android.content.Context;
|
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.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Map;
|
|
||||||
|
import retrofit2.Call;
|
||||||
|
import retrofit2.Callback;
|
||||||
|
import retrofit2.Response;
|
||||||
|
import retrofit2.Retrofit;
|
||||||
|
import retrofit2.converter.scalars.ScalarsConverterFactory;
|
||||||
|
|
||||||
class FetchSubscribedThing {
|
class FetchSubscribedThing {
|
||||||
interface FetchSubscribedSubredditsListener {
|
interface FetchSubscribedThingListener {
|
||||||
void onFetchSubscribedSubredditsSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData,
|
void onFetchSubscribedThingSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData,
|
||||||
ArrayList<SubscribedUserData> subscribedUserData,
|
ArrayList<SubscribedUserData> subscribedUserData,
|
||||||
ArrayList<SubredditData> subredditData);
|
ArrayList<SubredditData> subredditData);
|
||||||
void onFetchSubscribedSubredditsFail();
|
void onFetchSubscribedThingFail();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Context context;
|
static void fetchSubscribedThing(final Context context, final String lastItem,
|
||||||
private RequestQueue requestQueue;
|
final ArrayList<SubscribedSubredditData> subscribedSubredditData,
|
||||||
private FetchSubscribedSubredditsListener mFetchSubscribedSubredditsListener;
|
final ArrayList<SubscribedUserData> subscribedUserData,
|
||||||
private ArrayList<SubscribedSubredditData> mSubscribedSubredditData;
|
final ArrayList<SubredditData> subredditData,
|
||||||
private ArrayList<SubscribedUserData> mSubscribedUserData;
|
final FetchSubscribedThingListener fetchSubscribedThingListener, final int refreshTime) {
|
||||||
private ArrayList<SubredditData> mSubredditData;
|
|
||||||
|
|
||||||
private String mLastItem;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
void fetchSubscribedSubreddits(FetchSubscribedSubredditsListener fetchUserInfoListener, final int refreshTime) {
|
|
||||||
if(refreshTime < 0) {
|
if(refreshTime < 0) {
|
||||||
mFetchSubscribedSubredditsListener.onFetchSubscribedSubredditsFail();
|
fetchSubscribedThingListener.onFetchSubscribedThingFail();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Uri uri = Uri.parse(RedditUtils.OAUTH_API_BASE_URI + RedditUtils.SUBSCRIBED_SUBREDDITS)
|
Retrofit retrofit = new Retrofit.Builder()
|
||||||
.buildUpon().appendQueryParameter(RedditUtils.AFTER_KEY, mLastItem)
|
.baseUrl(RedditUtils.OAUTH_API_BASE_URI)
|
||||||
.appendQueryParameter(RedditUtils.RAW_JSON_KEY, RedditUtils.RAW_JSON_VALUE).build();
|
.addConverterFactory(ScalarsConverterFactory.create())
|
||||||
|
.build();
|
||||||
|
|
||||||
mFetchSubscribedSubredditsListener = fetchUserInfoListener;
|
RedditAPI api = retrofit.create(RedditAPI.class);
|
||||||
StringRequest commentRequest = new StringRequest(Request.Method.GET, uri.toString(), new Response.Listener<String>() {
|
|
||||||
|
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
|
@Override
|
||||||
public void onResponse(String response) {
|
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
||||||
ParseSubscribedThing.parseSubscribedSubreddits(response, mSubscribedSubredditData,
|
ParseSubscribedThing.parseSubscribedSubreddits(response.body(), subscribedSubredditData,
|
||||||
mSubscribedUserData, mSubredditData,
|
subscribedUserData, subredditData,
|
||||||
new ParseSubscribedThing.ParseSubscribedSubredditsListener() {
|
new ParseSubscribedThing.ParseSubscribedSubredditsListener() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -64,49 +53,37 @@ class FetchSubscribedThing {
|
|||||||
ArrayList<SubscribedUserData> subscribedUserData,
|
ArrayList<SubscribedUserData> subscribedUserData,
|
||||||
ArrayList<SubredditData> subredditData,
|
ArrayList<SubredditData> subredditData,
|
||||||
String lastItem) {
|
String lastItem) {
|
||||||
mSubscribedSubredditData = subscribedSubredditData;
|
if(lastItem.equals("null")) {
|
||||||
mSubscribedUserData = subscribedUserData;
|
fetchSubscribedThingListener.onFetchSubscribedThingSuccess(
|
||||||
mSubredditData = subredditData;
|
subscribedSubredditData, subscribedUserData, subredditData);
|
||||||
mLastItem = lastItem;
|
|
||||||
if(mLastItem.equals("null")) {
|
|
||||||
mFetchSubscribedSubredditsListener.onFetchSubscribedSubredditsSuccess(mSubscribedSubredditData,
|
|
||||||
mSubscribedUserData, mSubredditData);
|
|
||||||
} else {
|
} else {
|
||||||
fetchSubscribedSubreddits(mFetchSubscribedSubredditsListener, refreshTime);
|
fetchSubscribedThing(context, lastItem, subscribedSubredditData,
|
||||||
|
subscribedUserData, subredditData,
|
||||||
|
fetchSubscribedThingListener, refreshTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onParseSubscribedSubredditsFail() {
|
public void onParseSubscribedSubredditsFail() {
|
||||||
|
fetchSubscribedThingListener.onFetchSubscribedThingFail();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, new Response.ErrorListener() {
|
|
||||||
@Override
|
@Override
|
||||||
public void onErrorResponse(VolleyError error) {
|
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
|
||||||
if(error instanceof AuthFailureError) {
|
Log.i("call failed", t.getMessage());
|
||||||
new AcquireAccessToken(context).refreshAccessToken(requestQueue, new AcquireAccessToken.AcquireAccessTokenListener() {
|
RefreshAccessToken.refreshAccessToken(context, new RefreshAccessToken.RefreshAccessTokenListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onAcquireAccessTokenSuccess() {
|
public void onRefreshAccessTokenSuccess() {
|
||||||
fetchSubscribedSubreddits(mFetchSubscribedSubredditsListener, refreshTime - 1);
|
fetchSubscribedThing(context, lastItem, subscribedSubredditData,
|
||||||
|
subscribedUserData, subredditData, fetchSubscribedThingListener, refreshTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAcquireAccessTokenFail() {}
|
public void onRefreshAccessTokenFail() {}
|
||||||
});
|
});
|
||||||
} 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);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
commentRequest.setTag(FetchComment.class);
|
|
||||||
requestQueue.add(commentRequest);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,13 @@
|
|||||||
package ml.docilealligator.infinityforreddit;
|
package ml.docilealligator.infinityforreddit;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.net.Uri;
|
import android.support.annotation.NonNull;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import com.android.volley.AuthFailureError;
|
import retrofit2.Call;
|
||||||
import com.android.volley.Request;
|
import retrofit2.Callback;
|
||||||
import com.android.volley.RequestQueue;
|
import retrofit2.Retrofit;
|
||||||
import com.android.volley.Response;
|
import retrofit2.converter.scalars.ScalarsConverterFactory;
|
||||||
import com.android.volley.VolleyError;
|
|
||||||
import com.android.volley.toolbox.StringRequest;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
class FetchUserInfo {
|
class FetchUserInfo {
|
||||||
interface FetchUserInfoListener {
|
interface FetchUserInfoListener {
|
||||||
@ -18,57 +15,41 @@ class FetchUserInfo {
|
|||||||
void onFetchUserInfoFail();
|
void onFetchUserInfoFail();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Context context;
|
static void fetchUserInfo(final Context context, final FetchUserInfoListener fetchUserInfoListener, final int refreshTime) {
|
||||||
private RequestQueue requestQueue;
|
|
||||||
private FetchUserInfoListener mFetchUserInfoListener;
|
|
||||||
|
|
||||||
FetchUserInfo(Context context, RequestQueue requestQueue) {
|
|
||||||
this.context = context;
|
|
||||||
this.requestQueue = requestQueue;
|
|
||||||
}
|
|
||||||
|
|
||||||
void queryUserInfo(FetchUserInfoListener fetchUserInfoListener, final int refreshTime) {
|
|
||||||
if(refreshTime < 0) {
|
if(refreshTime < 0) {
|
||||||
mFetchUserInfoListener.onFetchUserInfoFail();
|
fetchUserInfoListener.onFetchUserInfoFail();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
mFetchUserInfoListener = fetchUserInfoListener;
|
Retrofit retrofit = new Retrofit.Builder()
|
||||||
|
.baseUrl(RedditUtils.OAUTH_API_BASE_URI)
|
||||||
Uri uri = Uri.parse(RedditUtils.OAUTH_API_BASE_URI + RedditUtils.USER_INFO_SUFFIX)
|
.addConverterFactory(ScalarsConverterFactory.create())
|
||||||
.buildUpon().appendQueryParameter(RedditUtils.RAW_JSON_KEY, RedditUtils.RAW_JSON_VALUE)
|
|
||||||
.build();
|
.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
|
@Override
|
||||||
public void onResponse(String response) {
|
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
|
||||||
mFetchUserInfoListener.onFetchUserInfoSuccess(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
|
@Override
|
||||||
public void onAcquireAccessTokenFail() {}
|
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
|
||||||
});
|
Log.i("call failed", t.getMessage());
|
||||||
} else {
|
RefreshAccessToken.refreshAccessToken(context, new RefreshAccessToken.RefreshAccessTokenListener() {
|
||||||
mFetchUserInfoListener.onFetchUserInfoFail();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}) {
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, String> getHeaders() {
|
public void onRefreshAccessTokenSuccess() {
|
||||||
String accessToken = context.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
|
fetchUserInfo(context, fetchUserInfoListener, refreshTime - 1);
|
||||||
return RedditUtils.getOAuthHeader(accessToken);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
commentRequest.setTag(FetchComment.class);
|
@Override
|
||||||
requestQueue.add(commentRequest);
|
public void onRefreshAccessTokenFail() {}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,6 @@ import android.view.View;
|
|||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.android.volley.toolbox.Volley;
|
|
||||||
import com.bumptech.glide.Glide;
|
import com.bumptech.glide.Glide;
|
||||||
import com.bumptech.glide.RequestManager;
|
import com.bumptech.glide.RequestManager;
|
||||||
|
|
||||||
@ -100,15 +99,15 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
queryAccessTokenTime.add(Calendar.SECOND, interval - 300);
|
queryAccessTokenTime.add(Calendar.SECOND, interval - 300);
|
||||||
|
|
||||||
if(now.after(queryAccessTokenTime)) {
|
if(now.after(queryAccessTokenTime)) {
|
||||||
new AcquireAccessToken(this).refreshAccessToken(Volley.newRequestQueue(this),
|
RefreshAccessToken.refreshAccessToken(this,
|
||||||
new AcquireAccessToken.AcquireAccessTokenListener() {
|
new RefreshAccessToken.RefreshAccessTokenListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onAcquireAccessTokenSuccess() {
|
public void onRefreshAccessTokenSuccess() {
|
||||||
loadUserData(savedInstanceState);
|
loadUserData(savedInstanceState);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAcquireAccessTokenFail() {}
|
public void onRefreshAccessTokenFail() {}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
loadUserData(savedInstanceState);
|
loadUserData(savedInstanceState);
|
||||||
@ -179,19 +178,19 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void loadUserData(Bundle savedInstanceState) {
|
private void loadUserData(Bundle savedInstanceState) {
|
||||||
if(savedInstanceState == null) {
|
if (savedInstanceState == null) {
|
||||||
if(!mFetchUserInfoSuccess) {
|
if (!mFetchUserInfoSuccess) {
|
||||||
new FetchUserInfo(this, Volley.newRequestQueue(this)).queryUserInfo(new FetchUserInfo.FetchUserInfoListener() {
|
FetchUserInfo.fetchUserInfo(this, new FetchUserInfo.FetchUserInfoListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onFetchUserInfoSuccess(String response) {
|
public void onFetchUserInfoSuccess(String response) {
|
||||||
ParseUserInfo.parseUserInfo(response, new ParseUserInfo.ParseUserInfoListener() {
|
ParseUserInfo.parseUserInfo(response, new ParseUserInfo.ParseUserInfoListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onParseUserInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) {
|
public void onParseUserInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) {
|
||||||
mNameTextView.setText(name);
|
mNameTextView.setText(name);
|
||||||
if(!mProfileImageUrl.equals("")) {
|
if (!mProfileImageUrl.equals("")) {
|
||||||
glide.load(profileImageUrl).into(mProfileImageView);
|
glide.load(profileImageUrl).into(mProfileImageView);
|
||||||
}
|
}
|
||||||
if(!mBannerImageUrl.equals("")) {
|
if (!mBannerImageUrl.equals("")) {
|
||||||
glide.load(bannerImageUrl).into(mBannerImageView);
|
glide.load(bannerImageUrl).into(mBannerImageView);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -225,12 +224,13 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}, 1);
|
}, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!mInsertSuccess) {
|
if (!mInsertSuccess) {
|
||||||
new FetchSubscribedThing(this, Volley.newRequestQueue(this), new ArrayList<SubscribedSubredditData>(),
|
FetchSubscribedThing.fetchSubscribedThing(this, null,
|
||||||
new ArrayList<SubscribedUserData>(), new ArrayList<SubredditData>())
|
new ArrayList<SubscribedSubredditData>(), new ArrayList<SubscribedUserData>(),
|
||||||
.fetchSubscribedSubreddits(new FetchSubscribedThing.FetchSubscribedSubredditsListener() {
|
new ArrayList<SubredditData>(),
|
||||||
|
new FetchSubscribedThing.FetchSubscribedThingListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onFetchSubscribedSubredditsSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData,
|
public void onFetchSubscribedThingSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData,
|
||||||
ArrayList<SubscribedUserData> subscribedUserData,
|
ArrayList<SubscribedUserData> subscribedUserData,
|
||||||
ArrayList<SubredditData> subredditData) {
|
ArrayList<SubredditData> subredditData) {
|
||||||
new InsertSubscribedThingsAsyncTask(
|
new InsertSubscribedThingsAsyncTask(
|
||||||
@ -249,7 +249,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFetchSubscribedSubredditsFail() {
|
public void onFetchSubscribedThingFail() {
|
||||||
mInsertSuccess = false;
|
mInsertSuccess = false;
|
||||||
}
|
}
|
||||||
}, 1);
|
}, 1);
|
||||||
|
@ -71,7 +71,7 @@ public class PostFragment extends Fragment {
|
|||||||
if(savedInstanceState.containsKey(PostDataParcelableState)) {
|
if(savedInstanceState.containsKey(PostDataParcelableState)) {
|
||||||
mPostData = savedInstanceState.getParcelableArrayList(PostDataParcelableState);
|
mPostData = savedInstanceState.getParcelableArrayList(PostDataParcelableState);
|
||||||
mLastItem = savedInstanceState.getString(lastItemState);
|
mLastItem = savedInstanceState.getString(lastItemState);
|
||||||
mAdapter = new PostRecyclerViewAdapter(getActivity(), mPostData, mPaginationSynchronizer, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue);
|
mAdapter = new PostRecyclerViewAdapter(getActivity(), mPostData, mPaginationSynchronizer, mVoteThingRequestQueue);
|
||||||
mPostRecyclerView.setAdapter(mAdapter);
|
mPostRecyclerView.setAdapter(mAdapter);
|
||||||
mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener(
|
mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener(
|
||||||
getActivity(), mLinearLayoutManager, mAdapter, mLastItem, mPostData,
|
getActivity(), mLinearLayoutManager, mAdapter, mLastItem, mPostData,
|
||||||
@ -82,9 +82,9 @@ public class PostFragment extends Fragment {
|
|||||||
mProgressBar.setVisibility(View.GONE);
|
mProgressBar.setVisibility(View.GONE);
|
||||||
} else {
|
} else {
|
||||||
if(mIsBestPost) {
|
if(mIsBestPost) {
|
||||||
queryBestPost(mQueryPostUrl, 1);
|
queryBestPost(1);
|
||||||
} else {
|
} else {
|
||||||
queryPost(mQueryPostUrl, 1);
|
fetchPost(mQueryPostUrl, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -99,7 +99,7 @@ public class PostFragment extends Fragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(mAcquireAccessTokenRequestQueue != null) {
|
if(mAcquireAccessTokenRequestQueue != null) {
|
||||||
mAcquireAccessTokenRequestQueue.cancelAll(AcquireAccessToken.class);
|
mAcquireAccessTokenRequestQueue.cancelAll(RefreshAccessToken.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(mVoteThingRequestQueue != null) {
|
if(mVoteThingRequestQueue != null) {
|
||||||
@ -156,9 +156,9 @@ public class PostFragment extends Fragment {
|
|||||||
} else {
|
} else {
|
||||||
mPaginationSynchronizer = new PaginationSynchronizer();
|
mPaginationSynchronizer = new PaginationSynchronizer();
|
||||||
if(mIsBestPost) {
|
if(mIsBestPost) {
|
||||||
queryBestPost(mQueryPostUrl, 1);
|
queryBestPost(1);
|
||||||
} else {
|
} else {
|
||||||
queryPost(mQueryPostUrl, 1);
|
fetchPost(mQueryPostUrl, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,7 +181,7 @@ public class PostFragment extends Fragment {
|
|||||||
return rootView;
|
return rootView;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void queryBestPost(final String queryPostUrl, final int refreshTime) {
|
private void queryBestPost(final int refreshTime) {
|
||||||
if(refreshTime < 0) {
|
if(refreshTime < 0) {
|
||||||
showErrorSnackbar();
|
showErrorSnackbar();
|
||||||
return;
|
return;
|
||||||
@ -203,7 +203,7 @@ public class PostFragment extends Fragment {
|
|||||||
public void onParsePostSuccess(ArrayList<PostData> postData, String lastItem) {
|
public void onParsePostSuccess(ArrayList<PostData> postData, String lastItem) {
|
||||||
mPostData = postData;
|
mPostData = postData;
|
||||||
mLastItem = lastItem;
|
mLastItem = lastItem;
|
||||||
mAdapter = new PostRecyclerViewAdapter(getActivity(), postData, mPaginationSynchronizer, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue);
|
mAdapter = new PostRecyclerViewAdapter(getActivity(), postData, mPaginationSynchronizer, mVoteThingRequestQueue);
|
||||||
|
|
||||||
mPostRecyclerView.setAdapter(mAdapter);
|
mPostRecyclerView.setAdapter(mAdapter);
|
||||||
mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener(
|
mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener(
|
||||||
@ -230,15 +230,15 @@ public class PostFragment extends Fragment {
|
|||||||
if (error instanceof AuthFailureError) {
|
if (error instanceof AuthFailureError) {
|
||||||
// Error indicating that there was an Authentication Failure while performing the request
|
// Error indicating that there was an Authentication Failure while performing the request
|
||||||
// Access token expired
|
// Access token expired
|
||||||
new AcquireAccessToken(getActivity()).refreshAccessToken(mAcquireAccessTokenRequestQueue,
|
RefreshAccessToken.refreshAccessToken(getActivity(),
|
||||||
new AcquireAccessToken.AcquireAccessTokenListener() {
|
new RefreshAccessToken.RefreshAccessTokenListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onAcquireAccessTokenSuccess() {
|
public void onRefreshAccessTokenSuccess() {
|
||||||
queryBestPost(queryPostUrl, refreshTime - 1);
|
queryBestPost(refreshTime - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAcquireAccessTokenFail() {}
|
public void onRefreshAccessTokenFail() {}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
Log.i("Post fetch error", error.toString());
|
Log.i("Post fetch error", error.toString());
|
||||||
@ -256,7 +256,7 @@ public class PostFragment extends Fragment {
|
|||||||
mRequestQueue.add(postRequest);
|
mRequestQueue.add(postRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void queryPost(final String queryPostUrl, final int refreshTime) {
|
private void fetchPost(final String queryPostUrl, final int refreshTime) {
|
||||||
if(refreshTime < 0) {
|
if(refreshTime < 0) {
|
||||||
showErrorSnackbar();
|
showErrorSnackbar();
|
||||||
return;
|
return;
|
||||||
@ -282,7 +282,7 @@ public class PostFragment extends Fragment {
|
|||||||
public void onParsePostSuccess(ArrayList<PostData> postData, String lastItem) {
|
public void onParsePostSuccess(ArrayList<PostData> postData, String lastItem) {
|
||||||
mPostData = postData;
|
mPostData = postData;
|
||||||
mLastItem = lastItem;
|
mLastItem = lastItem;
|
||||||
mAdapter = new PostRecyclerViewAdapter(getActivity(), postData, mPaginationSynchronizer, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue);
|
mAdapter = new PostRecyclerViewAdapter(getActivity(), postData, mPaginationSynchronizer, mVoteThingRequestQueue);
|
||||||
|
|
||||||
mPostRecyclerView.setAdapter(mAdapter);
|
mPostRecyclerView.setAdapter(mAdapter);
|
||||||
mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener(
|
mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener(
|
||||||
@ -309,15 +309,15 @@ public class PostFragment extends Fragment {
|
|||||||
if (error instanceof AuthFailureError) {
|
if (error instanceof AuthFailureError) {
|
||||||
// Error indicating that there was an Authentication Failure while performing the request
|
// Error indicating that there was an Authentication Failure while performing the request
|
||||||
// Access token expired
|
// Access token expired
|
||||||
new AcquireAccessToken(getActivity()).refreshAccessToken(mAcquireAccessTokenRequestQueue,
|
RefreshAccessToken.refreshAccessToken(getActivity(),
|
||||||
new AcquireAccessToken.AcquireAccessTokenListener() {
|
new RefreshAccessToken.RefreshAccessTokenListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onAcquireAccessTokenSuccess() {
|
public void onRefreshAccessTokenSuccess() {
|
||||||
queryPost(queryPostUrl, refreshTime - 1);
|
fetchPost(queryPostUrl, refreshTime - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAcquireAccessTokenFail() {}
|
public void onRefreshAccessTokenFail() {}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
Log.i("Post fetch error", error.toString());
|
Log.i("Post fetch error", error.toString());
|
||||||
@ -336,9 +336,9 @@ public class PostFragment extends Fragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
if(mIsBestPost) {
|
if(mIsBestPost) {
|
||||||
queryBestPost(mQueryPostUrl, 1);
|
queryBestPost(1);
|
||||||
} else {
|
} else {
|
||||||
queryPost(mQueryPostUrl, 1);
|
fetchPost(mQueryPostUrl, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -146,15 +146,15 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener {
|
|||||||
public void onErrorResponse(VolleyError error) {
|
public void onErrorResponse(VolleyError error) {
|
||||||
if (error instanceof AuthFailureError) {
|
if (error instanceof AuthFailureError) {
|
||||||
//Access token expired
|
//Access token expired
|
||||||
new AcquireAccessToken(mContext).refreshAccessToken(mAcquireAccessTokenRequestQueue,
|
RefreshAccessToken.refreshAccessToken(mContext,
|
||||||
new AcquireAccessToken.AcquireAccessTokenListener() {
|
new RefreshAccessToken.RefreshAccessTokenListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onAcquireAccessTokenSuccess() {
|
public void onRefreshAccessTokenSuccess() {
|
||||||
fetchBestPost(queryPostUrl, refreshTime - 1);
|
fetchBestPost(queryPostUrl, refreshTime - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAcquireAccessTokenFail() {
|
public void onRefreshAccessTokenFail() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@ -219,15 +219,15 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener {
|
|||||||
public void onErrorResponse(VolleyError error) {
|
public void onErrorResponse(VolleyError error) {
|
||||||
if (error instanceof AuthFailureError) {
|
if (error instanceof AuthFailureError) {
|
||||||
//Access token expired
|
//Access token expired
|
||||||
new AcquireAccessToken(mContext).refreshAccessToken(mAcquireAccessTokenRequestQueue,
|
RefreshAccessToken.refreshAccessToken(mContext,
|
||||||
new AcquireAccessToken.AcquireAccessTokenListener() {
|
new RefreshAccessToken.RefreshAccessTokenListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onAcquireAccessTokenSuccess() {
|
public void onRefreshAccessTokenSuccess() {
|
||||||
fetchPost(queryPostUrl, refreshTime - 1);
|
fetchPost(queryPostUrl, refreshTime - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAcquireAccessTokenFail() {
|
public void onRefreshAccessTokenFail() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
@ -44,7 +44,6 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
|
|||||||
private Context mContext;
|
private Context mContext;
|
||||||
private PaginationSynchronizer mPaginationSynchronizer;
|
private PaginationSynchronizer mPaginationSynchronizer;
|
||||||
private RequestQueue mVoteThingRequestQueue;
|
private RequestQueue mVoteThingRequestQueue;
|
||||||
private RequestQueue mAcquireAccessTokenRequestQueue;
|
|
||||||
private RequestManager glide;
|
private RequestManager glide;
|
||||||
private SubredditDao subredditDao;
|
private SubredditDao subredditDao;
|
||||||
private boolean isLoadingMorePostSuccess;
|
private boolean isLoadingMorePostSuccess;
|
||||||
@ -55,13 +54,12 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
|
|||||||
|
|
||||||
|
|
||||||
PostRecyclerViewAdapter(Context context, ArrayList<PostData> postData, PaginationSynchronizer paginationSynchronizer,
|
PostRecyclerViewAdapter(Context context, ArrayList<PostData> postData, PaginationSynchronizer paginationSynchronizer,
|
||||||
RequestQueue voteThingRequestQueue, RequestQueue acquireAccessTokenRequestQueue) {
|
RequestQueue voteThingRequestQueue) {
|
||||||
if(context != null) {
|
if(context != null) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mPostData = postData;
|
mPostData = postData;
|
||||||
mPaginationSynchronizer = paginationSynchronizer;
|
mPaginationSynchronizer = paginationSynchronizer;
|
||||||
mVoteThingRequestQueue = voteThingRequestQueue;
|
mVoteThingRequestQueue = voteThingRequestQueue;
|
||||||
mAcquireAccessTokenRequestQueue = acquireAccessTokenRequestQueue;
|
|
||||||
isLoadingMorePostSuccess = true;
|
isLoadingMorePostSuccess = true;
|
||||||
canStartActivity = true;
|
canStartActivity = true;
|
||||||
glide = Glide.with(mContext);
|
glide = Glide.with(mContext);
|
||||||
@ -139,7 +137,7 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
|
|||||||
@Override
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class);
|
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));
|
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));
|
((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
|
@Override
|
||||||
public void onVoteThingSuccess(int position) {
|
public void onVoteThingSuccess(int position) {
|
||||||
mPostData.get(position).setVoteType(1);
|
mPostData.get(position).setVoteType(1);
|
||||||
@ -372,7 +370,7 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
|
|||||||
((DataViewHolder) holder).upvoteButton.clearColorFilter();
|
((DataViewHolder) holder).upvoteButton.clearColorFilter();
|
||||||
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(mPostData.get(holder.getAdapterPosition()).getScore() - 1));
|
((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
|
@Override
|
||||||
public void onVoteThingSuccess(int position) {
|
public void onVoteThingSuccess(int position) {
|
||||||
mPostData.get(position).setVoteType(0);
|
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));
|
((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
|
@Override
|
||||||
public void onVoteThingSuccess(int position) {
|
public void onVoteThingSuccess(int position) {
|
||||||
mPostData.get(position).setVoteType(-1);
|
mPostData.get(position).setVoteType(-1);
|
||||||
@ -430,7 +428,7 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
|
|||||||
((DataViewHolder) holder).downvoteButton.clearColorFilter();
|
((DataViewHolder) holder).downvoteButton.clearColorFilter();
|
||||||
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(mPostData.get(holder.getAdapterPosition()).getScore() + 1));
|
((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
|
@Override
|
||||||
public void onVoteThingSuccess(int position) {
|
public void onVoteThingSuccess(int position) {
|
||||||
mPostData.get(position).setVoteType(0);
|
mPostData.get(position).setVoteType(0);
|
||||||
|
@ -1,10 +1,34 @@
|
|||||||
package ml.docilealligator.infinityforreddit;
|
package ml.docilealligator.infinityforreddit;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import retrofit2.Call;
|
import retrofit2.Call;
|
||||||
|
import retrofit2.http.FieldMap;
|
||||||
|
import retrofit2.http.FormUrlEncoded;
|
||||||
import retrofit2.http.GET;
|
import retrofit2.http.GET;
|
||||||
|
import retrofit2.http.HeaderMap;
|
||||||
|
import retrofit2.http.POST;
|
||||||
import retrofit2.http.Path;
|
import retrofit2.http.Path;
|
||||||
|
import retrofit2.http.Query;
|
||||||
|
|
||||||
public interface RedditAPI {
|
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")
|
@GET("{subredditNamePrefixed}/comments/{article}.json?raw_json=1")
|
||||||
Call<String> getComments(@Path("subredditNamePrefixed") String subredditNamePrefixed, @Path("article") String article);
|
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);
|
||||||
}
|
}
|
||||||
|
@ -11,15 +11,11 @@ import java.util.Map;
|
|||||||
|
|
||||||
class RedditUtils {
|
class RedditUtils {
|
||||||
static final String OAUTH_URL ="https://www.reddit.com/api/v1/authorize.compact";
|
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 OAUTH_API_BASE_URI = "https://oauth.reddit.com";
|
||||||
static final String API_BASE_URI = "https://www.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_KEY ="raw_json";
|
||||||
static final String RAW_JSON_VALUE = "1";
|
static final String RAW_JSON_VALUE = "1";
|
||||||
static final String BEST_POST_SUFFIX = "/best";
|
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_KEY = "client_id";
|
||||||
static final String CLIENT_ID = "";
|
static final String CLIENT_ID = "";
|
||||||
@ -70,14 +66,6 @@ class RedditUtils {
|
|||||||
return params;
|
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) {
|
static String getQuerySubredditPostUrl(String subredditName) {
|
||||||
return API_BASE_URI + "/r/" + subredditName + ".json";
|
return API_BASE_URI + "/r/" + subredditName + ".json";
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -38,8 +38,8 @@ class SubscribedSubredditRecyclerViewAdapter extends RecyclerView.Adapter<Recycl
|
|||||||
@Override
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class);
|
Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class);
|
||||||
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME, mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getName());
|
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getName());
|
||||||
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_ID, mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getId());
|
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_ID_KEY, mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getId());
|
||||||
mContext.startActivity(intent);
|
mContext.startActivity(intent);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -24,8 +24,6 @@ import android.widget.RelativeLayout;
|
|||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.android.volley.RequestQueue;
|
|
||||||
import com.android.volley.toolbox.Volley;
|
|
||||||
import com.bumptech.glide.Glide;
|
import com.bumptech.glide.Glide;
|
||||||
import com.bumptech.glide.load.DataSource;
|
import com.bumptech.glide.load.DataSource;
|
||||||
import com.bumptech.glide.load.engine.GlideException;
|
import com.bumptech.glide.load.engine.GlideException;
|
||||||
@ -59,9 +57,6 @@ public class ViewPostDetailActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
private LoadSubredditIconAsyncTask mLoadSubredditIconAsyncTask;
|
private LoadSubredditIconAsyncTask mLoadSubredditIconAsyncTask;
|
||||||
|
|
||||||
private RequestQueue mVoteThingRequestQueue;
|
|
||||||
private RequestQueue mAcquireAccessTokenRequestQueue;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
@ -140,8 +135,6 @@ public class ViewPostDetailActivity extends AppCompatActivity {
|
|||||||
mRecyclerView.setNestedScrollingEnabled(false);
|
mRecyclerView.setNestedScrollingEnabled(false);
|
||||||
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
|
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||||
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
|
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
|
||||||
mVoteThingRequestQueue = Volley.newRequestQueue(this);
|
|
||||||
mAcquireAccessTokenRequestQueue = Volley.newRequestQueue(this);
|
|
||||||
|
|
||||||
subredditTextView.setText(mPostData.getSubredditNamePrefixed());
|
subredditTextView.setText(mPostData.getSubredditNamePrefixed());
|
||||||
postTimeTextView.setText(mPostData.getPostTime());
|
postTimeTextView.setText(mPostData.getPostTime());
|
||||||
@ -385,7 +378,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
|
|||||||
scoreTextView.setText(Integer.toString(mPostData.getScore() + 1));
|
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
|
@Override
|
||||||
public void onVoteThingSuccess() {
|
public void onVoteThingSuccess() {
|
||||||
mPostData.setVoteType(1);
|
mPostData.setVoteType(1);
|
||||||
@ -409,7 +402,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
|
|||||||
upvoteButton.clearColorFilter();
|
upvoteButton.clearColorFilter();
|
||||||
scoreTextView.setText(Integer.toString(mPostData.getScore() - 1));
|
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
|
@Override
|
||||||
public void onVoteThingSuccess() {
|
public void onVoteThingSuccess() {
|
||||||
mPostData.setVoteType(0);
|
mPostData.setVoteType(0);
|
||||||
@ -445,7 +438,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
|
|||||||
scoreTextView.setText(Integer.toString(mPostData.getScore() - 1));
|
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
|
@Override
|
||||||
public void onVoteThingSuccess() {
|
public void onVoteThingSuccess() {
|
||||||
mPostData.setVoteType(-1);
|
mPostData.setVoteType(-1);
|
||||||
@ -469,7 +462,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
|
|||||||
downvoteButton.clearColorFilter();
|
downvoteButton.clearColorFilter();
|
||||||
scoreTextView.setText(Integer.toString(mPostData.getScore() + 1));
|
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
|
@Override
|
||||||
public void onVoteThingSuccess() {
|
public void onVoteThingSuccess() {
|
||||||
mPostData.setVoteType(0);
|
mPostData.setVoteType(0);
|
||||||
@ -492,7 +485,8 @@ public class ViewPostDetailActivity extends AppCompatActivity {
|
|||||||
private void queryComment() {
|
private void queryComment() {
|
||||||
mCommentProgressbar.setVisibility(View.VISIBLE);
|
mCommentProgressbar.setVisibility(View.VISIBLE);
|
||||||
mNoCommentWrapperLinearLayout.setVisibility(View.GONE);
|
mNoCommentWrapperLinearLayout.setVisibility(View.GONE);
|
||||||
new FetchComment(mPostData.getSubredditNamePrefixed(), mPostData.getId()).queryComment(new FetchComment.FetchCommentListener() {
|
FetchComment.queryComment(mPostData.getSubredditNamePrefixed(), mPostData.getId(),
|
||||||
|
new FetchComment.FetchCommentListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onFetchCommentSuccess(String response) {
|
public void onFetchCommentSuccess(String response) {
|
||||||
ParseComment.parseComment(response, new ArrayList<CommentData>(),
|
ParseComment.parseComment(response, new ArrayList<CommentData>(),
|
||||||
@ -503,7 +497,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
|
|||||||
mMoreCommentCount = moreCommentCount;
|
mMoreCommentCount = moreCommentCount;
|
||||||
if (commentData.size() > 0) {
|
if (commentData.size() > 0) {
|
||||||
CommentRecyclerViewAdapter adapter = new CommentRecyclerViewAdapter(
|
CommentRecyclerViewAdapter adapter = new CommentRecyclerViewAdapter(
|
||||||
ViewPostDetailActivity.this, commentData, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue);
|
ViewPostDetailActivity.this, commentData);
|
||||||
mRecyclerView.setAdapter(adapter);
|
mRecyclerView.setAdapter(adapter);
|
||||||
mCommentCardView.setVisibility(View.VISIBLE);
|
mCommentCardView.setVisibility(View.VISIBLE);
|
||||||
} else {
|
} else {
|
||||||
|
@ -18,7 +18,6 @@ import android.widget.ImageView;
|
|||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.android.volley.toolbox.Volley;
|
|
||||||
import com.bumptech.glide.Glide;
|
import com.bumptech.glide.Glide;
|
||||||
import com.bumptech.glide.RequestManager;
|
import com.bumptech.glide.RequestManager;
|
||||||
|
|
||||||
@ -26,8 +25,10 @@ import de.hdodenhof.circleimageview.CircleImageView;
|
|||||||
|
|
||||||
public class ViewSubredditDetailActivity extends AppCompatActivity {
|
public class ViewSubredditDetailActivity extends AppCompatActivity {
|
||||||
|
|
||||||
static final String EXTRA_SUBREDDIT_NAME = "ESN";
|
static final String EXTRA_SUBREDDIT_NAME_KEY = "ESN";
|
||||||
static final String EXTRA_SUBREDDIT_ID = "ESI";
|
static final String EXTRA_SUBREDDIT_ID_KEY = "ESI";
|
||||||
|
|
||||||
|
private static final String FRAGMENT_OUT_STATE_KEY = "FOSK";
|
||||||
|
|
||||||
private Fragment mFragment;
|
private Fragment mFragment;
|
||||||
|
|
||||||
@ -40,7 +41,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity {
|
|||||||
Toolbar toolbar = findViewById(R.id.toolbar);
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
||||||
setSupportActionBar(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;
|
final String title = "r/" + subredditName;
|
||||||
setTitle(title);
|
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 TextView descriptionTextView = findViewById(R.id.description_text_view_view_subreddit_detail_activity);
|
||||||
final RequestManager glide = Glide.with(ViewSubredditDetailActivity.this);
|
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);
|
SubredditViewModel.Factory factory = new SubredditViewModel.Factory(getApplication(), id);
|
||||||
mSubredditViewModel = ViewModelProviders.of(this, factory).get(SubredditViewModel.class);
|
mSubredditViewModel = ViewModelProviders.of(this, factory).get(SubredditViewModel.class);
|
||||||
mSubredditViewModel.getSubredditLiveData().observe(this, new Observer<SubredditData>() {
|
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
|
@Override
|
||||||
public void onFetchSubredditDataSuccess(String response) {
|
public void onFetchSubredditDataSuccess(String response) {
|
||||||
ParseSubredditData.parseComment(response, new ParseSubredditData.ParseSubredditDataListener() {
|
ParseSubredditData.parseComment(response, new ParseSubredditData.ParseSubredditDataListener() {
|
||||||
@ -173,7 +174,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity {
|
|||||||
mFragment.setArguments(bundle);
|
mFragment.setArguments(bundle);
|
||||||
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_subreddit_detail_activity, mFragment).commit();
|
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_subreddit_detail_activity, mFragment).commit();
|
||||||
} else {
|
} 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();
|
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) {
|
protected void onSaveInstanceState(Bundle outState) {
|
||||||
super.onSaveInstanceState(outState);
|
super.onSaveInstanceState(outState);
|
||||||
if(mFragment != null) {
|
if(mFragment != null) {
|
||||||
getSupportFragmentManager().putFragment(outState, "outStateFragment", mFragment);
|
getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE_KEY, mFragment);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
package ml.docilealligator.infinityforreddit;
|
package ml.docilealligator.infinityforreddit;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
import com.android.volley.AuthFailureError;
|
import android.util.Log;
|
||||||
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.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
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.
|
* Created by alex on 3/14/18.
|
||||||
*/
|
*/
|
||||||
@ -28,116 +28,96 @@ class VoteThing {
|
|||||||
void onVoteThingFail();
|
void onVoteThingFail();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Context mContext;
|
static void voteThing(final Context context, final VoteThingListener voteThingListener, final String fullName, final String point, final int position, final int refreshTime) {
|
||||||
private RequestQueue mQueue;
|
if(context != null) {
|
||||||
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) {
|
|
||||||
if(refreshTime < 0) {
|
if(refreshTime < 0) {
|
||||||
voteThingListener.onVoteThingFail(position);
|
voteThingListener.onVoteThingFail(position);
|
||||||
return;
|
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
|
@Override
|
||||||
public void onResponse(String response) {
|
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
|
||||||
voteThingListener.onVoteThingSuccess(position);
|
voteThingListener.onVoteThingSuccess(position);
|
||||||
}
|
}
|
||||||
}, new Response.ErrorListener() {
|
|
||||||
@Override
|
@Override
|
||||||
public void onErrorResponse(VolleyError error) {
|
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
|
||||||
if (error instanceof AuthFailureError) {
|
Log.i("call failed", t.getMessage());
|
||||||
//Access token expired
|
RefreshAccessToken.refreshAccessToken(context,
|
||||||
new AcquireAccessToken(mContext).refreshAccessToken(mAcquireAccessTokenRequestQueue,
|
new RefreshAccessToken.RefreshAccessTokenListener() {
|
||||||
new AcquireAccessToken.AcquireAccessTokenListener() {
|
|
||||||
@Override
|
@Override
|
||||||
public void onAcquireAccessTokenSuccess() {
|
public void onRefreshAccessTokenSuccess() {
|
||||||
votePost(voteThingListener, fullName, point, position, refreshTime - 1);
|
voteThing(context, voteThingListener, fullName, point, position, refreshTime - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAcquireAccessTokenFail() {}
|
public void onRefreshAccessTokenFail() {
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} 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);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
voteRequest.setTag(VoteThing.class);
|
|
||||||
mQueue.add(voteRequest);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void votePost(final VoteThingWithoutPositionListener voteThingWithoutPositionListener, final String fullName, final String point, final int refreshTime) {
|
static void voteThing(final Context context, final VoteThingWithoutPositionListener voteThingWithoutPositionListener, final String fullName, final String point, final int refreshTime) {
|
||||||
if(mContext != null) {
|
if(context != null) {
|
||||||
if(refreshTime < 0) {
|
if(refreshTime < 0) {
|
||||||
voteThingWithoutPositionListener.onVoteThingFail();
|
voteThingWithoutPositionListener.onVoteThingFail();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
StringRequest voteRequest = new StringRequest(Request.Method.POST, RedditUtils.OAUTH_API_BASE_URI + RedditUtils.VOTE_SUFFIX, new Response.Listener<String>() {
|
|
||||||
@Override
|
|
||||||
public void onResponse(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
|
Retrofit retrofit = new Retrofit.Builder()
|
||||||
public void onAcquireAccessTokenFail() {}
|
.baseUrl(RedditUtils.OAUTH_API_BASE_URI)
|
||||||
});
|
.addConverterFactory(ScalarsConverterFactory.create())
|
||||||
} else {
|
.build();
|
||||||
voteThingWithoutPositionListener.onVoteThingFail();
|
|
||||||
}
|
RedditAPI api = retrofit.create(RedditAPI.class);
|
||||||
}
|
|
||||||
}) {
|
String accessToken = context.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE)
|
||||||
@Override
|
.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
|
||||||
protected Map<String, String> getParams() {
|
Map<String, String> params = new HashMap<>();
|
||||||
HashMap<String, String> params = new HashMap<>();
|
|
||||||
params.put(RedditUtils.DIR_KEY, point);
|
params.put(RedditUtils.DIR_KEY, point);
|
||||||
params.put(RedditUtils.ID_KEY, fullName);
|
params.put(RedditUtils.ID_KEY, fullName);
|
||||||
params.put(RedditUtils.RANK_KEY, RedditUtils.RANK);
|
params.put(RedditUtils.RANK_KEY, RedditUtils.RANK);
|
||||||
return params;
|
|
||||||
|
Call<String> voteThingCall = api.voteThing(RedditUtils.getOAuthHeader(accessToken), params);
|
||||||
|
voteThingCall.enqueue(new Callback<String>() {
|
||||||
|
@Override
|
||||||
|
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
|
||||||
|
voteThingWithoutPositionListener.onVoteThingSuccess();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, String> getHeaders() {
|
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
|
||||||
String accessToken = mContext.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
|
Log.i("call failed", t.getMessage());
|
||||||
return RedditUtils.getOAuthHeader(accessToken);
|
RefreshAccessToken.refreshAccessToken(context,
|
||||||
|
new RefreshAccessToken.RefreshAccessTokenListener() {
|
||||||
|
@Override
|
||||||
|
public void onRefreshAccessTokenSuccess() {
|
||||||
|
voteThing(context, voteThingWithoutPositionListener, fullName, point, refreshTime - 1);
|
||||||
}
|
}
|
||||||
};
|
|
||||||
voteRequest.setTag(VoteThing.class);
|
@Override
|
||||||
mQueue.add(voteRequest);
|
public void onRefreshAccessTokenFail() {}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user