diff options
author | Alex Ning <chineseperson5@gmail.com> | 2018-08-27 15:05:56 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2018-08-27 15:05:56 +0000 |
commit | a6a951732d311d1355b8145fca3525d66aad7686 (patch) | |
tree | 37b11095d1bf170c94e78cb39c48190bffbd3326 /app | |
parent | 41b243be7d48e141b082a48e601d2b05a83ea26c (diff) | |
download | infinity-for-reddit-a6a951732d311d1355b8145fca3525d66aad7686.tar infinity-for-reddit-a6a951732d311d1355b8145fca3525d66aad7686.tar.gz infinity-for-reddit-a6a951732d311d1355b8145fca3525d66aad7686.tar.bz2 infinity-for-reddit-a6a951732d311d1355b8145fca3525d66aad7686.tar.lz infinity-for-reddit-a6a951732d311d1355b8145fca3525d66aad7686.tar.xz infinity-for-reddit-a6a951732d311d1355b8145fca3525d66aad7686.tar.zst infinity-for-reddit-a6a951732d311d1355b8145fca3525d66aad7686.zip |
Change some methods related to parsing data to static methods. Preparing to use Retrofit to send API requests. Already made a change to use Retrofit to fetch comments.
Diffstat (limited to '')
15 files changed, 168 insertions, 162 deletions
diff --git a/app/build.gradle b/app/build.gradle index 4dd39fc5..c2f27341 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -57,4 +57,5 @@ dependencies { implementation 'io.reactivex.rxjava2:rxjava:2.2.0' implementation 'org.sufficientlysecure:html-textview:3.6' implementation 'com.squareup.retrofit2:retrofit:2.4.0' + implementation 'com.squareup.retrofit2:converter-scalars:2.4.0' } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java index 6c23036e..e0fb82e8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java @@ -1,12 +1,13 @@ package ml.docilealligator.infinityforreddit; -import android.net.Uri; +import android.support.annotation.NonNull; +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 retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; +import retrofit2.converter.scalars.ScalarsConverterFactory; class FetchComment { interface FetchCommentListener { @@ -14,36 +15,34 @@ class FetchComment { void onFetchCommentFail(); } - private RequestQueue requestQueue; - private String subredditName; + private String subredditNamePrefixed; private String article; - private FetchCommentListener mFetchCommentListener; - FetchComment(RequestQueue requestQueue, String subredditName, String article) { - this.requestQueue = requestQueue; - this.subredditName = subredditName; + FetchComment(String subredditNamePrefixed, String article) { + this.subredditNamePrefixed = subredditNamePrefixed; this.article = article; } - void queryComment(FetchCommentListener fetchCommentListener) { - mFetchCommentListener = fetchCommentListener; - - Uri uri = Uri.parse(RedditUtils.getQueryCommentUrl(subredditName, article)) - .buildUpon().appendQueryParameter(RedditUtils.RAW_JSON_KEY, RedditUtils.RAW_JSON_VALUE) + void queryComment(final FetchCommentListener fetchCommentListener) { + Retrofit retrofit = new Retrofit.Builder() + .baseUrl(RedditUtils.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); + + Call<String> comments = api.getComments(subredditNamePrefixed, article); + comments.enqueue(new Callback<String>() { @Override - public void onResponse(String response) { - mFetchCommentListener.onFetchCommentSuccess(response); + public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { + fetchCommentListener.onFetchCommentSuccess(response.body()); } - }, new Response.ErrorListener() { + @Override - public void onErrorResponse(VolleyError error) { - mFetchCommentListener.onFetchCommentFail(); + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + Log.i("call failed", t.getMessage()); + fetchCommentListener.onFetchCommentFail(); } - }) {}; - commentRequest.setTag(FetchComment.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 be468f7a..37719bfc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java @@ -55,7 +55,7 @@ class FetchSubscribedThing { StringRequest commentRequest = new StringRequest(Request.Method.GET, uri.toString(), new Response.Listener<String>() { @Override public void onResponse(String response) { - new ParseSubscribedThing().parseSubscribedSubreddits(response, mSubscribedSubredditData, + ParseSubscribedThing.parseSubscribedSubreddits(response, mSubscribedSubredditData, mSubscribedUserData, mSubredditData, new ParseSubscribedThing.ParseSubscribedSubredditsListener() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java index 78b2255e..32cbd1bd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java @@ -62,8 +62,8 @@ class JSONUtils { static final String SUBREDDIT_TYPE_KEY = "subreddit_type"; static final String SUBREDDIT_TYPE_VALUE_USER = "user"; static final String SUBSCRIBERS_KEY = "subscribers"; - static final String PUBLIC_DESCRIPTION = "public_description"; - static final String ACTIVE_USER_COUNT = "active_user_count"; - static final String DISPLAY_NAME_PREFIXED = "display_name_prefixed"; - static final String LINK_ID = "link_id"; + static final String PUBLIC_DESCRIPTION_KEY = "public_description"; + static final String ACTIVE_USER_COUNT_KEY = "active_user_count"; + static final String DISPLAY_NAME_PREFIXED_KEY = "display_name_prefixed"; + static final String LINK_ID_KEY = "link_id"; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 09024906..d8bcdf08 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -17,7 +17,6 @@ import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; -import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.TextView; @@ -185,7 +184,7 @@ public class MainActivity extends AppCompatActivity { new FetchUserInfo(this, Volley.newRequestQueue(this)).queryUserInfo(new FetchUserInfo.FetchUserInfoListener() { @Override public void onFetchUserInfoSuccess(String response) { - new ParseUserInfo().parseUserInfo(response, new ParseUserInfo.ParseUserInfoListener() { + ParseUserInfo.parseUserInfo(response, new ParseUserInfo.ParseUserInfoListener() { @Override public void onParseUserInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) { mNameTextView.setText(name); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java index 8b623b76..28355a04 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java @@ -1,6 +1,5 @@ package ml.docilealligator.infinityforreddit; -import android.content.Context; import android.os.AsyncTask; import android.util.Log; @@ -11,6 +10,7 @@ import org.json.JSONObject; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; +import java.util.Locale; class ParseComment { interface ParseCommentListener { @@ -18,31 +18,30 @@ class ParseComment { void onParseCommentFail(); } - private Context mContext; - private ParseCommentListener mParseCommentListener; - - void parseComment(Context context, String response, ArrayList<CommentData> commentData, ParseCommentListener parseCommentListener) { - mParseCommentListener = parseCommentListener; - mContext = context; - new ParseCommentAsyncTask(response, commentData).execute(); + static void parseComment(String response, ArrayList<CommentData> commentData, Locale locale, ParseCommentListener parseCommentListener) { + new ParseCommentAsyncTask(response, commentData, locale, parseCommentListener).execute(); } - private class ParseCommentAsyncTask extends AsyncTask<Void, Void, Void> { + private static class ParseCommentAsyncTask extends AsyncTask<Void, Void, Void> { private JSONArray jsonResponse; private ArrayList<CommentData> commentData; private ArrayList<CommentData> newcommentData; + private Locale locale; + private ParseCommentListener parseCommentListener; private boolean parseFailed; - int moreCommentCount; + private int moreCommentCount; - ParseCommentAsyncTask(String response, ArrayList<CommentData> commentData){ + ParseCommentAsyncTask(String response, ArrayList<CommentData> commentData, Locale locale, ParseCommentListener parseCommentListener){ try { jsonResponse = new JSONArray(response); this.commentData = commentData; newcommentData = new ArrayList<>(); + this.locale = locale; + this.parseCommentListener = parseCommentListener; parseFailed = false; } catch (JSONException e) { Log.i("comment json error", e.getMessage()); - mParseCommentListener.onParseCommentFail(); + parseCommentListener.onParseCommentFail(); } } @@ -69,7 +68,7 @@ class ParseComment { for (int i = 0; i < actualCommentLength; i++) { JSONObject data = allComments.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY); - String fullName = data.getString(JSONUtils.LINK_ID); + String fullName = data.getString(JSONUtils.LINK_ID_KEY); String author = data.getString(JSONUtils.AUTHOR_KEY); boolean isSubmitter = data.getBoolean(JSONUtils.IS_SUBMITTER_KEY); String commentContent = ""; @@ -84,7 +83,7 @@ class ParseComment { Calendar submitTimeCalendar = Calendar.getInstance(); submitTimeCalendar.setTimeInMillis(submitTime); String formattedSubmitTime = new SimpleDateFormat("MMM d, YYYY, HH:mm", - mContext.getResources().getConfiguration().locale).format(submitTimeCalendar.getTime()); + locale).format(submitTimeCalendar.getTime()); int depth = data.getInt(JSONUtils.DEPTH_KEY); boolean collapsed = data.getBoolean(JSONUtils.COLLAPSED_KEY); @@ -103,9 +102,9 @@ class ParseComment { protected void onPostExecute(Void aVoid) { if(!parseFailed) { commentData.addAll(newcommentData); - mParseCommentListener.onParseCommentSuccess(commentData, moreCommentCount); + parseCommentListener.onParseCommentSuccess(commentData, moreCommentCount); } else { - mParseCommentListener.onParseCommentFail(); + parseCommentListener.onParseCommentFail(); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java index 5ef4aab3..381f8044 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java @@ -1,9 +1,7 @@ package ml.docilealligator.infinityforreddit; -import android.content.Context; import android.os.AsyncTask; import android.util.Log; -import android.widget.Toast; import org.json.JSONArray; import org.json.JSONException; @@ -12,6 +10,7 @@ import org.json.JSONObject; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; +import java.util.Locale; /** * Created by alex on 3/21/18. @@ -24,34 +23,31 @@ class ParsePost { void onParsePostFail(); } - private Context mContext; - private ParsePostListener mParseBetPostListener; - - ParsePost(Context context, ParsePostListener parsePostListener) { - mContext = context; - mParseBetPostListener = parsePostListener; - } - - void parsePost(String response, ArrayList<PostData> postData) { - new ParsePostDataAsyncTask(response, postData).execute(); + static void parsePost(String response, ArrayList<PostData> postData, Locale locale, + ParsePostListener parsePostListener) { + new ParsePostDataAsyncTask(response, postData, locale, parsePostListener).execute(); } - private class ParsePostDataAsyncTask extends AsyncTask<Void, Void, Void> { + private static class ParsePostDataAsyncTask extends AsyncTask<Void, Void, Void> { private JSONObject jsonResponse; private ArrayList<PostData> postData; + private Locale locale; + private ParsePostListener parsePostListener; private ArrayList<PostData> newPostData; private String lastItem; private boolean parseFailed; - ParsePostDataAsyncTask(String response, ArrayList<PostData> postData) { + ParsePostDataAsyncTask(String response, ArrayList<PostData> postData, Locale locale, + ParsePostListener parsePostListener) { try { jsonResponse = new JSONObject(response); this.postData = postData; + this.locale = locale; + this.parsePostListener = parsePostListener; newPostData = new ArrayList<>(); parseFailed = false; } catch (JSONException e) { e.printStackTrace(); - Toast.makeText(mContext, "Error converting response to JSON", Toast.LENGTH_SHORT).show(); } } @@ -82,7 +78,7 @@ class ParsePost { Calendar postTimeCalendar = Calendar.getInstance(); postTimeCalendar.setTimeInMillis(postTime); String formattedPostTime = new SimpleDateFormat("MMM d, YYYY, HH:mm", - mContext.getResources().getConfiguration().locale).format(postTimeCalendar.getTime()); + locale).format(postTimeCalendar.getTime()); String permalink = data.getString(JSONUtils.PERMALINK_KEY); String previewUrl = ""; @@ -102,8 +98,7 @@ class ParsePost { } } } catch (JSONException e) { - Log.e("error", e.getMessage()); - Log.i("Best post", "Error parsing data"); + Log.e("best post parse error", e.getMessage()); parseFailed = true; } return null; @@ -113,14 +108,14 @@ class ParsePost { protected void onPostExecute(Void aVoid) { if(!parseFailed) { postData.addAll(newPostData); - mParseBetPostListener.onParsePostSuccess(postData, lastItem); + parsePostListener.onParsePostSuccess(postData, lastItem); } else { - mParseBetPostListener.onParsePostFail(); + parsePostListener.onParsePostFail(); } } } - private void parseData(JSONObject data, String permalink, ArrayList<PostData> bestPostData, + private static void parseData(JSONObject data, String permalink, ArrayList<PostData> bestPostData, String id, String fullName, String subredditNamePrefixed, String formattedPostTime, String title, String previewUrl, int score, int voteType, int gilded, boolean nsfw, boolean stickied, int i) throws JSONException { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java index 9a2fe016..1f9b4a9e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java @@ -12,11 +12,8 @@ class ParseSubredditData { void onParseSubredditDataFail(); } - private ParseSubredditDataListener mParseSubredditDataListener; - - void parseComment(String response, ParseSubredditDataListener parseSubredditDataListener) { - mParseSubredditDataListener = parseSubredditDataListener; - new ParseSubredditDataAsyncTask(response, mParseSubredditDataListener).execute(); + static void parseComment(String response, ParseSubredditDataListener parseSubredditDataListener) { + new ParseSubredditDataAsyncTask(response, parseSubredditDataListener).execute(); } private static class ParseSubredditDataAsyncTask extends AsyncTask<Void, Void, Void> { @@ -42,8 +39,8 @@ class ParseSubredditData { try { JSONObject data = jsonResponse.getJSONObject(JSONUtils.DATA_KEY); String id = data.getString(JSONUtils.NAME_KEY); - String subredditFullName = data.getString(JSONUtils.DISPLAY_NAME_PREFIXED); - String description = data.getString(JSONUtils.PUBLIC_DESCRIPTION).trim(); + String subredditFullName = data.getString(JSONUtils.DISPLAY_NAME_PREFIXED_KEY); + String description = data.getString(JSONUtils.PUBLIC_DESCRIPTION_KEY).trim(); String bannerImageUrl = data.getString(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY); if(bannerImageUrl.equals("") || bannerImageUrl.equals("null")) { bannerImageUrl= data.getString(JSONUtils.BANNER_IMG_KEY); @@ -59,7 +56,7 @@ class ParseSubredditData { } } int nSubscribers = data.getInt(JSONUtils.SUBSCRIBERS_KEY); - int nCurrentOnlineSubscribers = data.getInt(JSONUtils.ACTIVE_USER_COUNT); + int nCurrentOnlineSubscribers = data.getInt(JSONUtils.ACTIVE_USER_COUNT_KEY); subredditData = new SubredditData(id, subredditFullName, iconUrl, bannerImageUrl, description, nSubscribers); mNCurrentOnlineSubscribers = nCurrentOnlineSubscribers; } catch (JSONException e) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java index 5d0c4606..d4cc4030 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java @@ -18,7 +18,7 @@ class ParseSubscribedThing { void onParseSubscribedSubredditsFail(); } - void parseSubscribedSubreddits(String response, ArrayList<SubscribedSubredditData> subscribedSubredditData, + static void parseSubscribedSubreddits(String response, ArrayList<SubscribedSubredditData> subscribedSubredditData, ArrayList<SubscribedUserData> subscribedUserData, ArrayList<SubredditData> subredditData, ParseSubscribedSubredditsListener parseSubscribedSubredditsListener) { @@ -92,8 +92,8 @@ class ParseSubscribedThing { //It's a user newSubscribedUserData.add(new SubscribedUserData(id, name.substring(2), iconUrl)); } else { - String subredditFullName = data.getString(JSONUtils.DISPLAY_NAME_PREFIXED); - String description = data.getString(JSONUtils.PUBLIC_DESCRIPTION).trim(); + String subredditFullName = data.getString(JSONUtils.DISPLAY_NAME_PREFIXED_KEY); + String description = data.getString(JSONUtils.PUBLIC_DESCRIPTION_KEY).trim(); int nSubscribers = data.getInt(JSONUtils.SUBSCRIBERS_KEY); newSubscribedSubredditData.add(new SubscribedSubredditData(id, name, iconUrl)); newSubredditData.add(new SubredditData(id, subredditFullName, iconUrl, bannerImageUrl, description, nSubscribers)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserInfo.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserInfo.java index 2efb8ff8..40e162df 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserInfo.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserInfo.java @@ -13,15 +13,13 @@ class ParseUserInfo { void onParseUserInfoFail(); } - private ParseUserInfoListener mParseUserInfoListener; - - void parseUserInfo(String response, ParseUserInfoListener parseUserInfoListener) { - mParseUserInfoListener = parseUserInfoListener; - new ParseUserInfoAsyncTask(response).execute(); + static void parseUserInfo(String response, ParseUserInfoListener parseUserInfoListener) { + new ParseUserInfoAsyncTask(response, parseUserInfoListener).execute(); } - private class ParseUserInfoAsyncTask extends AsyncTask<Void, Void, Void> { + private static class ParseUserInfoAsyncTask extends AsyncTask<Void, Void, Void> { private JSONObject jsonResponse; + private ParseUserInfoListener parseUserInfoListener; private boolean parseFailed; private String name; @@ -29,13 +27,14 @@ class ParseUserInfo { private String bannerImageUrl; private int karma; - ParseUserInfoAsyncTask(String response){ + ParseUserInfoAsyncTask(String response, ParseUserInfoListener parseUserInfoListener){ try { jsonResponse = new JSONObject(response); + this.parseUserInfoListener = parseUserInfoListener; parseFailed = false; } catch (JSONException e) { Log.i("user info json error", e.getMessage()); - mParseUserInfoListener.onParseUserInfoFail(); + parseUserInfoListener.onParseUserInfoFail(); } } @@ -58,9 +57,9 @@ class ParseUserInfo { @Override protected void onPostExecute(Void aVoid) { if(!parseFailed) { - mParseUserInfoListener.onParseUserInfoSuccess(name, profileImageUrl, bannerImageUrl, karma); + parseUserInfoListener.onParseUserInfoSuccess(name, profileImageUrl, bannerImageUrl, karma); } else { - mParseUserInfoListener.onParseUserInfoFail(); + parseUserInfoListener.onParseUserInfoFail(); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index e8b10896..7c90c488 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -77,7 +77,8 @@ public class PostFragment extends Fragment { getActivity(), mLinearLayoutManager, mAdapter, mLastItem, mPostData, mPaginationSynchronizer, mAcquireAccessTokenRequestQueue, mQueryPostUrl, mIsBestPost, - mPaginationSynchronizer.isLoading(), mPaginationSynchronizer.isLoadSuccess())); + mPaginationSynchronizer.isLoading(), mPaginationSynchronizer.isLoadSuccess(), + getResources().getConfiguration().locale)); mProgressBar.setVisibility(View.GONE); } else { if(mIsBestPost) { @@ -196,29 +197,31 @@ public class PostFragment extends Fragment { ClipData clip = ClipData.newPlainText("response", response); clipboard.setPrimaryClip(clip); //new ParsePostDataAsyncTask(response, accessToken).execute(); - new ParsePost(getActivity(), new ParsePost.ParsePostListener() { - @Override - public void onParsePostSuccess(ArrayList<PostData> postData, String lastItem) { - mPostData = postData; - mLastItem = lastItem; - mAdapter = new PostRecyclerViewAdapter(getActivity(), postData, mPaginationSynchronizer, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue); - - mPostRecyclerView.setAdapter(mAdapter); - mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener( - getActivity(), mLinearLayoutManager, mAdapter, lastItem, postData, - mPaginationSynchronizer, mAcquireAccessTokenRequestQueue, - mQueryPostUrl, mIsBestPost, - mPaginationSynchronizer.isLoading(), mPaginationSynchronizer.isLoadSuccess())); - mProgressBar.setVisibility(View.GONE); - } - - @Override - public void onParsePostFail() { - Toast.makeText(getActivity(), "Error parsing data", Toast.LENGTH_SHORT).show(); - Log.i("Post fetch error", "Error parsing data"); - mProgressBar.setVisibility(View.GONE); - } - }).parsePost(response, new ArrayList<PostData>()); + ParsePost.parsePost(response, new ArrayList<PostData>(), + getResources().getConfiguration().locale, new ParsePost.ParsePostListener() { + @Override + public void onParsePostSuccess(ArrayList<PostData> postData, String lastItem) { + mPostData = postData; + mLastItem = lastItem; + mAdapter = new PostRecyclerViewAdapter(getActivity(), postData, mPaginationSynchronizer, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue); + + mPostRecyclerView.setAdapter(mAdapter); + mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener( + getActivity(), mLinearLayoutManager, mAdapter, lastItem, postData, + mPaginationSynchronizer, mAcquireAccessTokenRequestQueue, + mQueryPostUrl, mIsBestPost, + mPaginationSynchronizer.isLoading(), mPaginationSynchronizer.isLoadSuccess(), + getResources().getConfiguration().locale)); + mProgressBar.setVisibility(View.GONE); + } + + @Override + public void onParsePostFail() { + Toast.makeText(getActivity(), "Error parsing data", Toast.LENGTH_SHORT).show(); + Log.i("Post fetch error", "Error parsing data"); + mProgressBar.setVisibility(View.GONE); + } + }); } } }, new Response.ErrorListener() { @@ -273,29 +276,31 @@ public class PostFragment extends Fragment { ClipData clip = ClipData.newPlainText("response", response); clipboard.setPrimaryClip(clip); //new ParsePostDataAsyncTask(response, accessToken).execute(); - new ParsePost(getActivity(), new ParsePost.ParsePostListener() { - @Override - public void onParsePostSuccess(ArrayList<PostData> postData, String lastItem) { - mPostData = postData; - mLastItem = lastItem; - mAdapter = new PostRecyclerViewAdapter(getActivity(), postData, mPaginationSynchronizer, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue); - - mPostRecyclerView.setAdapter(mAdapter); - mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener( - getActivity(), mLinearLayoutManager, mAdapter, lastItem, postData, - mPaginationSynchronizer, mAcquireAccessTokenRequestQueue, - mQueryPostUrl, mIsBestPost, - mPaginationSynchronizer.isLoading(), mPaginationSynchronizer.isLoadSuccess())); - mProgressBar.setVisibility(View.GONE); - } - - @Override - public void onParsePostFail() { - Toast.makeText(getActivity(), "Error parsing data", Toast.LENGTH_SHORT).show(); - Log.i("Post fetch error", "Error parsing data"); - mProgressBar.setVisibility(View.GONE); - } - }).parsePost(response, new ArrayList<PostData>()); + ParsePost.parsePost(response, new ArrayList<PostData>(), + getResources().getConfiguration().locale, new ParsePost.ParsePostListener() { + @Override + public void onParsePostSuccess(ArrayList<PostData> postData, String lastItem) { + mPostData = postData; + mLastItem = lastItem; + mAdapter = new PostRecyclerViewAdapter(getActivity(), postData, mPaginationSynchronizer, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue); + + mPostRecyclerView.setAdapter(mAdapter); + mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener( + getActivity(), mLinearLayoutManager, mAdapter, lastItem, postData, + mPaginationSynchronizer, mAcquireAccessTokenRequestQueue, + mQueryPostUrl, mIsBestPost, + mPaginationSynchronizer.isLoading(), mPaginationSynchronizer.isLoadSuccess(), + getResources().getConfiguration().locale)); + mProgressBar.setVisibility(View.GONE); + } + + @Override + public void onParsePostFail() { + Toast.makeText(getActivity(), "Error parsing data", Toast.LENGTH_SHORT).show(); + Log.i("Post fetch error", "Error parsing data"); + mProgressBar.setVisibility(View.GONE); + } + }); } } }, new Response.ErrorListener() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java index 2ee6478a..6aa01cf4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java @@ -19,6 +19,7 @@ import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import java.util.ArrayList; +import java.util.Locale; import java.util.Map; /** @@ -39,6 +40,7 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener { private boolean isBestPost; private boolean isLoading; private boolean loadSuccess; + private Locale locale; private String mLastItem; private RequestQueue mRequestQueue; private RequestQueue mAcquireAccessTokenRequestQueue; @@ -46,7 +48,7 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener { PostPaginationScrollListener(Context context, LinearLayoutManager layoutManager, PostRecyclerViewAdapter adapter, String lastItem, ArrayList<PostData> postData, PaginationSynchronizer paginationSynchronizer, RequestQueue acquireAccessTokenRequestQueue, final String queryPostUrl, - final boolean isBestPost, boolean isLoading, boolean loadSuccess) { + final boolean isBestPost, boolean isLoading, boolean loadSuccess, Locale locale) { if(context != null) { this.mContext = context; this.mLayoutManager = layoutManager; @@ -59,6 +61,7 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener { this.isBestPost = isBestPost; this.isLoading = isLoading; this.loadSuccess = loadSuccess; + this.locale = locale; mRequestQueue = Volley.newRequestQueue(mContext); mAcquireAccessTokenRequestQueue = Volley.newRequestQueue(mContext); @@ -117,7 +120,7 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener { ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText("response", response); clipboard.setPrimaryClip(clip); - new ParsePost(mContext, new ParsePost.ParsePostListener() { + ParsePost.parsePost(response, mPostData, locale, new ParsePost.ParsePostListener() { @Override public void onParsePostSuccess(ArrayList<PostData> bestPostData, String lastItem) { mAdapter.notifyDataSetChanged(); @@ -136,7 +139,7 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener { Log.i("Best post", "Error parsing data"); loadFailed(); } - }).parsePost(response, mPostData); + }); } }, new Response.ErrorListener() { @Override @@ -190,7 +193,7 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener { ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText("response", response); clipboard.setPrimaryClip(clip); - new ParsePost(mContext, new ParsePost.ParsePostListener() { + ParsePost.parsePost(response, mPostData, locale, new ParsePost.ParsePostListener() { @Override public void onParsePostSuccess(ArrayList<PostData> bestPostData, String lastItem) { mAdapter.notifyDataSetChanged(); @@ -209,7 +212,7 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener { Log.i("Best post", "Error parsing data"); loadFailed(); } - }).parsePost(response, mPostData); + }); } }, new Response.ErrorListener() { @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java new file mode 100644 index 00000000..5e4c5558 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -0,0 +1,10 @@ +package ml.docilealligator.infinityforreddit; + +import retrofit2.Call; +import retrofit2.http.GET; +import retrofit2.http.Path; + +public interface RedditAPI { + @GET("{subredditNamePrefixed}/comments/{article}.json?raw_json=1") + Call<String> getComments(@Path("subredditNamePrefixed") String subredditNamePrefixed, @Path("article") String article); +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 14323edf..052e675e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -61,7 +61,6 @@ public class ViewPostDetailActivity extends AppCompatActivity { private RequestQueue mVoteThingRequestQueue; private RequestQueue mAcquireAccessTokenRequestQueue; - private RequestQueue mCommentQueue; @Override protected void onCreate(Bundle savedInstanceState) { @@ -143,7 +142,6 @@ public class ViewPostDetailActivity extends AppCompatActivity { mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); mVoteThingRequestQueue = Volley.newRequestQueue(this); mAcquireAccessTokenRequestQueue = Volley.newRequestQueue(this); - mCommentQueue = Volley.newRequestQueue(this); subredditTextView.setText(mPostData.getSubredditNamePrefixed()); postTimeTextView.setText(mPostData.getPostTime()); @@ -494,24 +492,25 @@ public class ViewPostDetailActivity extends AppCompatActivity { private void queryComment() { mCommentProgressbar.setVisibility(View.VISIBLE); mNoCommentWrapperLinearLayout.setVisibility(View.GONE); - new FetchComment(mCommentQueue, mPostData.getSubredditNamePrefixed(), mPostData.getId()).queryComment(new FetchComment.FetchCommentListener() { + new FetchComment(mPostData.getSubredditNamePrefixed(), mPostData.getId()).queryComment(new FetchComment.FetchCommentListener() { @Override public void onFetchCommentSuccess(String response) { - new ParseComment().parseComment(ViewPostDetailActivity.this, response, new ArrayList<CommentData>(), 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); - } - } + 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); + } + } @Override public void onParseCommentFail() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java index 3cced2cb..66fd16f8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java @@ -140,7 +140,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { new FetchSubredditData(Volley.newRequestQueue(this), subredditName).querySubredditData(new FetchSubredditData.FetchSubredditDataListener() { @Override public void onFetchSubredditDataSuccess(String response) { - new ParseSubredditData().parseComment(response, new ParseSubredditData.ParseSubredditDataListener() { + ParseSubredditData.parseComment(response, new ParseSubredditData.ParseSubredditDataListener() { @Override public void onParseSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { new InsertSubredditDataAsyncTask(SubredditRoomDatabase.getDatabase(ViewSubredditDetailActivity.this), subredditData) |