diff options
Diffstat (limited to '')
18 files changed, 414 insertions, 186 deletions
diff --git a/.idea/assetWizardSettings.xml b/.idea/assetWizardSettings.xml index ade91cd7..6423ac9f 100644 --- a/.idea/assetWizardSettings.xml +++ b/.idea/assetWizardSettings.xml @@ -23,7 +23,7 @@ <PersistentState> <option name="values"> <map> - <entry key="url" value="jar:file:/home/alex/Android%20Studio/plugins/android/lib/android.jar!/images/material_design_icons/file/ic_file_download_black_24dp.xml" /> + <entry key="url" value="jar:file:/home/alex/Android%20Studio/plugins/android/lib/android.jar!/images/material_design_icons/action/ic_account_circle_black_24dp.xml" /> </map> </option> </PersistentState> @@ -33,8 +33,8 @@ </option> <option name="values"> <map> - <entry key="color" value="ffffff" /> - <entry key="outputName" value="ic_file_download_white_24dp" /> + <entry key="color" value="9e9e9e" /> + <entry key="outputName" value="ic_account_circle_grey_24dp" /> <entry key="sourceFile" value="$USER_HOME$" /> </map> </option> diff --git a/.idea/misc.xml b/.idea/misc.xml index cc51e58e..dc345694 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -25,7 +25,7 @@ </value> </option> </component> - <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> + <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <output url="file://$PROJECT_DIR$/build/classes" /> </component> <component name="ProjectType"> diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AcquireAccessToken.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AcquireAccessToken.java index 969a9680..7895ce0d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AcquireAccessToken.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AcquireAccessToken.java @@ -50,6 +50,7 @@ class AcquireAccessToken { editor.putString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, newAccessToken); editor.apply(); + Log.i("access token", newAccessToken); mAcquireAccessTokenListener.onAcquireAccessTokenSuccess(); } catch (JSONException e) { e.printStackTrace(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostFragment.java index d7b23a23..de4e6eb0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostFragment.java @@ -5,6 +5,7 @@ import android.app.Fragment; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; +import android.net.Uri; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.design.widget.CoordinatorLayout; @@ -169,7 +170,11 @@ public class BestPostFragment extends Fragment { mProgressBar.setVisibility(View.VISIBLE); - StringRequest bestPostRequest = new StringRequest(Request.Method.GET, RedditUtils.OAUTH_API_BASE_URI + RedditUtils.BEST_POST_SUFFIX, new Response.Listener<String>() { + Uri uri = Uri.parse(RedditUtils.OAUTH_API_BASE_URI + RedditUtils.BEST_POST_SUFFIX) + .buildUpon().appendQueryParameter(RedditUtils.RAW_JSON_KEY, RedditUtils.RAW_JSON_VALUE) + .build(); + + StringRequest bestPostRequest = new StringRequest(Request.Method.GET, uri.toString(), new Response.Listener<String>() { @Override public void onResponse(String response) { if(getActivity() != null) { @@ -196,7 +201,7 @@ public class BestPostFragment extends Fragment { Log.i("Best post fetch error", "Error parsing data"); mProgressBar.setVisibility(View.GONE); } - }).parseBestPost(response, null); + }).parseBestPost(response, new ArrayList<BestPostData>()); } } }, new Response.ErrorListener() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostPaginationScrollListener.java b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostPaginationScrollListener.java index d96af7d3..41dbe0af 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostPaginationScrollListener.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostPaginationScrollListener.java @@ -3,6 +3,7 @@ package ml.docilealligator.infinityforreddit; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; +import android.net.Uri; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; @@ -92,7 +93,11 @@ class BestPostPaginationScrollListener extends RecyclerView.OnScrollListener { loadSuccess = false; mPaginationSynchronizer.setLoading(true); - StringRequest bestPostRequest = new StringRequest(Request.Method.GET, RedditUtils.OAUTH_API_BASE_URI + RedditUtils.BEST_POST_SUFFIX + "&" + RedditUtils.AFTER_KEY + "=" + mLastItem, new Response.Listener<String>() { + Uri uri = Uri.parse(RedditUtils.OAUTH_API_BASE_URI + RedditUtils.BEST_POST_SUFFIX) + .buildUpon().appendQueryParameter(RedditUtils.AFTER_KEY, mLastItem) + .appendQueryParameter(RedditUtils.RAW_JSON_KEY, RedditUtils.RAW_JSON_VALUE).build(); + + StringRequest bestPostRequest = new StringRequest(Request.Method.GET, uri.toString(), new Response.Listener<String>() { @Override public void onResponse(String response) { ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedSubreddits.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedSubreddits.java index 76869e0f..999a6dc1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedSubreddits.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedSubreddits.java @@ -1,5 +1,97 @@ package ml.docilealligator.infinityforreddit; +import android.content.Context; +import android.net.Uri; +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.Map; + class FetchSubscribedSubreddits { + interface FetchSubscribedSubredditsListener { + void onFetchSubscribedSubredditsSuccess(ArrayList<SubredditData> subredditData); + void onFetchSubscribedSubredditsFail(); + } + + private Context context; + private RequestQueue requestQueue; + private FetchSubscribedSubredditsListener mFetchSubscribedSubredditsListener; + private ArrayList<SubredditData> mSubredditData; + + private String mLastItem; + + FetchSubscribedSubreddits(Context context, RequestQueue requestQueue, ArrayList<SubredditData> subredditData) { + this.context = context; + this.requestQueue = requestQueue; + mSubredditData = subredditData; + } + + void fetchSubscribedSubreddits(FetchSubscribedSubredditsListener fetchUserInfoListener, final int refreshTime) { + if(refreshTime < 0) { + mFetchSubscribedSubredditsListener.onFetchSubscribedSubredditsFail(); + return; + } + + Uri uri = Uri.parse(RedditUtils.OAUTH_API_BASE_URI + RedditUtils.SUBSCRIBED_SUBREDDITS) + .buildUpon().appendQueryParameter(RedditUtils.AFTER_KEY, mLastItem) + .appendQueryParameter(RedditUtils.RAW_JSON_KEY, RedditUtils.RAW_JSON_VALUE).build(); + + mFetchSubscribedSubredditsListener = fetchUserInfoListener; + StringRequest commentRequest = new StringRequest(Request.Method.GET, uri.toString(), new Response.Listener<String>() { + @Override + public void onResponse(String response) { + new ParseSubscribedSubreddits().parseSubscribedSubreddits(response, mSubredditData, + new ParseSubscribedSubreddits.ParseSubscribedSubredditsListener() { + @Override + public void onParseSubscribedSubredditsSuccess(ArrayList<SubredditData> subredditData, String lastItem) { + mSubredditData = subredditData; + mLastItem = lastItem; + Log.i("last item", lastItem); + if(mLastItem.equals("null")) { + mFetchSubscribedSubredditsListener.onFetchSubscribedSubredditsSuccess(mSubredditData); + } else { + fetchSubscribedSubreddits(mFetchSubscribedSubredditsListener, refreshTime); + } + } + + @Override + public void onParseSubscribedSubredditsFail() { + + } + }); + } + }, new Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + if(error instanceof AuthFailureError) { + new AcquireAccessToken(context).refreshAccessToken(requestQueue, new AcquireAccessToken.AcquireAccessTokenListener() { + @Override + public void onAcquireAccessTokenSuccess() { + fetchSubscribedSubreddits(mFetchSubscribedSubredditsListener, refreshTime - 1); + } + @Override + public void onAcquireAccessTokenFail() {} + }); + } else { + mFetchSubscribedSubredditsListener.onFetchSubscribedSubredditsFail(); + } + } + }) { + @Override + public Map<String, String> getHeaders() { + String accessToken = context.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); + return RedditUtils.getOAuthHeader(accessToken); + } + }; + commentRequest.setTag(FetchComment.class); + requestQueue.add(commentRequest); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserInfo.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserInfo.java index bd3e0b9d..274c667e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserInfo.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserInfo.java @@ -1,6 +1,7 @@ package ml.docilealligator.infinityforreddit; import android.content.Context; +import android.net.Uri; import com.android.volley.AuthFailureError; import com.android.volley.Request; @@ -33,11 +34,15 @@ class FetchUserInfo { } mFetchUserInfoListener = fetchUserInfoListener; - StringRequest commentRequest = new StringRequest(Request.Method.GET, RedditUtils.OAUTH_API_BASE_URI + RedditUtils.USER_INFO_SUFFIX, new Response.Listener<String>() { + + Uri uri = Uri.parse(RedditUtils.OAUTH_API_BASE_URI + RedditUtils.USER_INFO_SUFFIX) + .buildUpon().appendQueryParameter(RedditUtils.RAW_JSON_KEY, RedditUtils.RAW_JSON_VALUE) + .build(); + + StringRequest commentRequest = new StringRequest(Request.Method.GET, uri.toString(), new Response.Listener<String>() { @Override public void onResponse(String response) { mFetchUserInfoListener.onFetchUserInfoSuccess(response); - } }, new Response.ErrorListener() { @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java index 3cdb7bd8..9f5ae9fc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java @@ -56,4 +56,5 @@ class JSONUtils { static final String ICON_IMG_KEY = "icon_img"; static final String LINK_KARMA_KEY = "link_karma"; static final String COMMENT_KARMA_KEY = "comment_karma"; + static final String DISPLAY_NAME = "display_name"; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index ed792b2e..92b73aae 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -6,14 +6,13 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; -import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; 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.view.Menu; -import android.view.MenuItem; import android.view.View; import android.widget.ImageView; import android.widget.TextView; @@ -22,10 +21,13 @@ import com.android.volley.toolbox.Volley; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; + import de.hdodenhof.circleimageview.CircleImageView; -public class MainActivity extends AppCompatActivity - implements NavigationView.OnNavigationItemSelectedListener { +public class MainActivity extends AppCompatActivity { private String nameState = "NS"; private String profileImageUrlState = "PIUS"; @@ -36,6 +38,7 @@ public class MainActivity extends AppCompatActivity private TextView mKarmaTextView; private CircleImageView mProfileImageView; private ImageView mBannerImageView; + private RecyclerView mSubscribedSubredditRecyclerView; private Fragment mFragment; private RequestManager glide; @@ -46,6 +49,8 @@ public class MainActivity extends AppCompatActivity private String mKarma; private boolean mFetchUserInfoSuccess; + private ArrayList<SubredditData> mSubredditData; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -59,15 +64,15 @@ public class MainActivity extends AppCompatActivity drawer.addDrawerListener(toggle); toggle.syncState(); - NavigationView navigationView = findViewById(R.id.nav_view); - navigationView.setNavigationItemSelectedListener(this); - - View header = navigationView.getHeaderView(0); + View header = findViewById(R.id.nav_header_main_activity); mNameTextView = header.findViewById(R.id.name_text_view_nav_header_main); mKarmaTextView = header.findViewById(R.id.karma_text_view_nav_header_main); mProfileImageView = header.findViewById(R.id.profile_image_view_nav_header_main); mBannerImageView = header.findViewById(R.id.banner_image_view_nav_header_main); + mSubscribedSubredditRecyclerView = findViewById(R.id.subscribed_subreddit_recycler_view_main_activity); + mSubscribedSubredditRecyclerView.setLayoutManager(new LinearLayoutManager(this)); + mName = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.USER_KEY, ""); mProfileImageUrl = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.PROFILE_IMAGE_URL_KEY, ""); mBannerImageUrl = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.BANNER_IMAGE_URL_KEY, ""); @@ -102,7 +107,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(MainActivity.this, response, new ParseUserInfo.ParseUserInfoListener() { + new ParseUserInfo().parseUserInfo(response, new ParseUserInfo.ParseUserInfoListener() { @Override public void onParseUserInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) { mNameTextView.setText(name); @@ -142,6 +147,26 @@ public class MainActivity extends AppCompatActivity } }, 1); } + new FetchSubscribedSubreddits(this, Volley.newRequestQueue(this), new ArrayList<SubredditData>()) + .fetchSubscribedSubreddits(new FetchSubscribedSubreddits.FetchSubscribedSubredditsListener() { + @Override + public void onFetchSubscribedSubredditsSuccess(ArrayList<SubredditData> subredditData) { + Collections.sort(subredditData, new Comparator<SubredditData>() { + @Override + public int compare(SubredditData subredditData, SubredditData t1) { + return subredditData.getName().toLowerCase().compareTo(t1.getName().toLowerCase()); + } + }); + mSubredditData = subredditData; + mSubscribedSubredditRecyclerView.setAdapter(new SubscribedSubredditRecyclerViewAdapter( + MainActivity.this, mSubredditData)); + } + + @Override + public void onFetchSubscribedSubredditsFail() { + + } + }, 1); } @Override @@ -155,53 +180,6 @@ public class MainActivity extends AppCompatActivity } @Override - public boolean onCreateOptionsMenu(Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.main, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle action bar item clicks here. The action bar will - // automatically handle clicks on the Home/Up button, so long - // as you specify a parent activity in AndroidManifest.xml. - int id = item.getItemId(); - - //noinspection SimplifiableIfStatement - if (id == R.id.action_settings) { - return true; - } - - return super.onOptionsItemSelected(item); - } - - @SuppressWarnings("StatementWithEmptyBody") - @Override - public boolean onNavigationItemSelected(MenuItem item) { - // Handle navigation view item clicks here. - int id = item.getItemId(); - - if (id == R.id.nav_camera) { - // Handle the camera action - } else if (id == R.id.nav_gallery) { - - } else if (id == R.id.nav_slideshow) { - - } else if (id == R.id.nav_manage) { - - } else if (id == R.id.nav_share) { - - } else if (id == R.id.nav_send) { - - } - - DrawerLayout drawer = findViewById(R.id.drawer_layout); - drawer.closeDrawer(GravityCompat.START); - return true; - } - - @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if(mFragment != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseBestPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseBestPost.java index 3c1121a6..a0e93404 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseBestPost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseBestPost.java @@ -59,9 +59,6 @@ class ParseBestPost { protected Void doInBackground(Void... voids) { try { JSONArray allData = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); - if(bestPostData == null) { - bestPostData = new ArrayList<>(); - } lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); for(int i = 0; i < allData.length(); i++) { @@ -199,106 +196,4 @@ class ParseBestPost { } } } - - /*private void parseData(JSONObject data, String permalink, ArrayList<BestPostData> bestPostData, - String id, String fullName, String subredditName, String formattedPostTime, String title, - int score, int voteType, boolean nsfw, int i) throws JSONException { - boolean isVideo = data.getBoolean(JSONUtils.IS_VIDEO_KEY); - if(!data.has(JSONUtils.PREVIEW_KEY)) { - String url = data.getString(JSONUtils.URL_KEY); - if(url.contains(permalink)) { - //Text post - Log.i("text", Integer.toString(i)); - int postType = BestPostData.TEXT_TYPE; - BestPostData postData = new BestPostData(id, fullName, subredditName, formattedPostTime, title, permalink, score, postType, voteType, nsfw); - postData.setSelfText(data.getString(JSONUtils.SELF_TEXT_KEY).trim()); - bestPostData.add(postData); - } else { - //No preview link post - Log.i("no preview link", Integer.toString(i)); - int postType = BestPostData.NO_PREVIEW_LINK_TYPE; - BestPostData post = new BestPostData(id, fullName, subredditName, formattedPostTime, title, permalink, score, postType, voteType, nsfw); - post.setLinkUrl(url); - bestPostData.add(post); - } - } else if (!isVideo) { - JSONObject variations = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0); - String previewUrl = variations.getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY); - if (variations.has(JSONUtils.VARIANTS_KEY)) { - if (variations.getJSONObject(JSONUtils.VARIANTS_KEY).has(JSONUtils.MP4_KEY)) { - //Gif video - Log.i("gif video", Integer.toString(i)); - int postType = BestPostData.GIF_VIDEO_TYPE; - String videoUrl = variations.getJSONObject(JSONUtils.VARIANTS_KEY).getJSONObject(JSONUtils.MP4_KEY).getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY); - - BestPostData post = new BestPostData(id, fullName, subredditName, formattedPostTime, title, previewUrl, permalink, score, postType, voteType, nsfw); - post.setVideoUrl(videoUrl); - bestPostData.add(post); - } else if (variations.getJSONObject(JSONUtils.VARIANTS_KEY).has(JSONUtils.GIF_KEY)) { - //Gif post - Log.i("gif", Integer.toString(i)); - int postType = BestPostData.GIF_TYPE; - String gifUrl = variations.getJSONObject(JSONUtils.VARIANTS_KEY).getJSONObject(JSONUtils.GIF_KEY).getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY); - - BestPostData post = new BestPostData(id, fullName, subredditName, formattedPostTime, title, previewUrl, permalink, score, postType, voteType, nsfw); - post.setGifUrl(gifUrl); - bestPostData.add(post); - } else { - if(data.getJSONObject(JSONUtils.PREVIEW_KEY).has(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY)) { - //Gif link post - Log.i("gif link", Integer.toString(i)); - int postType = BestPostData.LINK_TYPE; - String gifUrl = data.getString(JSONUtils.URL_KEY); - BestPostData gifLinkPostData = new BestPostData(id, fullName, subredditName, formattedPostTime, title, previewUrl, permalink, score, postType, voteType, nsfw); - gifLinkPostData.setLinkUrl(gifUrl); - bestPostData.add(gifLinkPostData); - } else { - if(!data.isNull(JSONUtils.MEDIA_KEY)) { - //Video link post - Log.i("video link", Integer.toString(i)); - int postType = BestPostData.LINK_TYPE; - String videoUrl = data.getString(JSONUtils.URL_KEY); - BestPostData videoLinkPostData = new BestPostData(id, fullName, subredditName, formattedPostTime, title, previewUrl, permalink, score, postType, voteType, nsfw); - videoLinkPostData.setLinkUrl(videoUrl); - bestPostData.add(videoLinkPostData); - } else { - if(data.getBoolean(JSONUtils.IS_REDDIT_MEDIA_DOMAIN)) { - //Image post - Log.i("image", Integer.toString(i)); - int postType = BestPostData.IMAGE_TYPE; - bestPostData.add(new BestPostData(id, fullName, subredditName, formattedPostTime, title, previewUrl, permalink, score, postType, voteType, nsfw)); - } else { - //Link post - Log.i("link", Integer.toString(i)); - int postType = BestPostData.LINK_TYPE; - String linkUrl = data.getString(JSONUtils.URL_KEY); - BestPostData linkPostData = new BestPostData(id, fullName, subredditName, formattedPostTime, title, previewUrl, permalink, score, postType, voteType, nsfw); - linkPostData.setLinkUrl(linkUrl); - bestPostData.add(linkPostData); - } - } - } - } - } else { - //Image post - Toast.makeText(mContext, "Fixed post" + Integer.toString(i), Toast.LENGTH_SHORT).show(); - Log.i("fixed image", Integer.toString(i)); - int postType = BestPostData.IMAGE_TYPE; - bestPostData.add(new BestPostData(id, fullName, subredditName, formattedPostTime, title, previewUrl, permalink, score, postType, voteType, nsfw)); - } - } else { - //Video post - Log.i("video", Integer.toString(i)); - JSONObject redditVideoObject = data.getJSONObject(JSONUtils.MEDIA_KEY).getJSONObject(JSONUtils.REDDIT_VIDEO_KEY); - int postType = BestPostData.VIDEO_TYPE; - String videoUrl = redditVideoObject.getString(JSONUtils.DASH_URL_KEY); - - String videoPreviewUrl = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0).getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY); - - BestPostData videoPostData = new BestPostData(id, fullName, subredditName, formattedPostTime, title, videoPreviewUrl, permalink, score, postType, voteType, nsfw); - videoPostData.setVideoUrl(videoUrl); - - bestPostData.add(videoPostData); - } - }*/ } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedSubreddits.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedSubreddits.java index 62b5eee9..24d83b40 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedSubreddits.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedSubreddits.java @@ -1,4 +1,72 @@ package ml.docilealligator.infinityforreddit; +import android.os.AsyncTask; +import android.util.Log; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; + class ParseSubscribedSubreddits { + interface ParseSubscribedSubredditsListener { + void onParseSubscribedSubredditsSuccess(ArrayList<SubredditData> subredditData, String lastItem); + void onParseSubscribedSubredditsFail(); + } + + private ParseSubscribedSubredditsListener mParseSubscribedSubredditsListener; + + void parseSubscribedSubreddits(String response, ArrayList<SubredditData> subredditData, + ParseSubscribedSubredditsListener parseSubscribedSubredditsListener) { + mParseSubscribedSubredditsListener = parseSubscribedSubredditsListener; + new ParseSubscribedSubredditsAsyncTask(response, subredditData).execute(); + } + + private class ParseSubscribedSubredditsAsyncTask extends AsyncTask<Void, Void, Void> { + private JSONObject jsonResponse; + private boolean parseFailed; + private String lastItem; + private ArrayList<SubredditData> subredditData; + private ArrayList<SubredditData> newSubredditData; + + ParseSubscribedSubredditsAsyncTask(String response, ArrayList<SubredditData> subredditData){ + try { + jsonResponse = new JSONObject(response); + parseFailed = false; + this.subredditData = subredditData; + newSubredditData = new ArrayList<>(); + } catch (JSONException e) { + Log.i("user info json error", e.getMessage()); + mParseSubscribedSubredditsListener.onParseSubscribedSubredditsFail(); + } + } + + @Override + protected Void doInBackground(Void... voids) { + try { + JSONArray children = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); + for(int i = 0; i < children.length(); i++) { + String name = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.DISPLAY_NAME); + String iconUrl = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.ICON_IMG_KEY); + newSubredditData.add(new SubredditData(name, iconUrl)); + } + lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); + } catch (JSONException e) { + parseFailed = true; + Log.i("parse comment error", e.getMessage()); + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + if(!parseFailed) { + subredditData.addAll(newSubredditData); + mParseSubscribedSubredditsListener.onParseSubscribedSubredditsSuccess(subredditData, lastItem); + } else { + mParseSubscribedSubredditsListener.onParseSubscribedSubredditsFail(); + } + } + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserInfo.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserInfo.java index 49ff057b..2efb8ff8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserInfo.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserInfo.java @@ -1,6 +1,5 @@ package ml.docilealligator.infinityforreddit; -import android.content.Context; import android.os.AsyncTask; import android.text.Html; import android.util.Log; @@ -14,13 +13,11 @@ class ParseUserInfo { void onParseUserInfoFail(); } - private Context mContext; private ParseUserInfoListener mParseUserInfoListener; - void parseUserInfo(Context context, String response, ParseUserInfoListener parseUserInfoListener) { + void parseUserInfo(String response, ParseUserInfoListener parseUserInfoListener) { mParseUserInfoListener = parseUserInfoListener; - mContext = context; - new ParseUserInfo.ParseUserInfoAsyncTask(response).execute(); + new ParseUserInfoAsyncTask(response).execute(); } private class ParseUserInfoAsyncTask extends AsyncTask<Void, Void, Void> { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java index 6d421c09..5fefa9e6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java @@ -14,9 +14,12 @@ class RedditUtils { static final String ACQUIRE_ACCESS_TOKEN_URL = "https://www.reddit.com/api/v1/access_token"; static final String OAUTH_API_BASE_URI = "https://oauth.reddit.com"; static final String API_BASE_URI = "https://www.reddit.com"; - static final String BEST_POST_SUFFIX = "/best?raw_json=1"; + static final String RAW_JSON_KEY ="raw_json"; + static final String RAW_JSON_VALUE = "1"; + static final String BEST_POST_SUFFIX = "/best"; static final String VOTE_SUFFIX = "/api/vote"; - static final String USER_INFO_SUFFIX = "/api/v1/me?raw_json=1"; + 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 = ""; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditData.java index 418fa4de..dcf0cdaf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditData.java @@ -1,4 +1,50 @@ package ml.docilealligator.infinityforreddit; -class SubredditData { +import android.os.Parcel; +import android.os.Parcelable; + +class SubredditData implements Parcelable { + private String name; + private String iconUrl; + + SubredditData(String name, String iconUrl) { + this.name = name; + this.iconUrl = iconUrl; + } + + protected SubredditData(Parcel in) { + name = in.readString(); + iconUrl = in.readString(); + } + + public static final Creator<SubredditData> CREATOR = new Creator<SubredditData>() { + @Override + public SubredditData createFromParcel(Parcel in) { + return new SubredditData(in); + } + + @Override + public SubredditData[] newArray(int size) { + return new SubredditData[size]; + } + }; + + public String getName() { + return name; + } + + public String getIconUrl() { + return iconUrl; + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel parcel, int i) { + parcel.writeString(name); + parcel.writeString(iconUrl); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRecyclerViewAdapter.java index 463e14d7..73f40a40 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRecyclerViewAdapter.java @@ -1,4 +1,65 @@ package ml.docilealligator.infinityforreddit; -class SubscribedSubredditRecyclerViewAdapter { +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import com.bumptech.glide.Glide; +import com.bumptech.glide.RequestManager; + +import java.util.ArrayList; + +import de.hdodenhof.circleimageview.CircleImageView; + +class SubscribedSubredditRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { + Context mContext; + ArrayList<SubredditData> mSubredditData; + RequestManager glide; + + SubscribedSubredditRecyclerViewAdapter(Context context, ArrayList<SubredditData> subredditData) { + mContext = context; + mSubredditData = subredditData; + glide = Glide.with(context); + } + + @NonNull + @Override + public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { + return new SubredditViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_subscribed_subreddit, viewGroup, false)); + } + + @Override + public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) { + if(!mSubredditData.get(i).getIconUrl().equals("")) { + glide.load(mSubredditData.get(i).getIconUrl()).into(((SubredditViewHolder) viewHolder).iconCircleImageView); + } else { + glide.load(R.drawable.subreddit_default_icon).into(((SubredditViewHolder) viewHolder).iconCircleImageView); + } + ((SubredditViewHolder) viewHolder).subredditNameTextView.setText(mSubredditData.get(i).getName()); + } + + @Override + public int getItemCount() { + return mSubredditData.size(); + } + + @Override + public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { + glide.clear(((SubredditViewHolder) holder).iconCircleImageView); + } + + private class SubredditViewHolder extends RecyclerView.ViewHolder { + private CircleImageView iconCircleImageView; + private TextView subredditNameTextView; + + public SubredditViewHolder(View itemView) { + super(itemView); + iconCircleImageView = itemView.findViewById(R.id.subreddit_icon_circle_image_view_item_subscribed_subreddit); + subredditNameTextView = itemView.findViewById(R.id.subreddit_name_text_view_item_subscribed_subreddit); + } + } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index db65bf75..8d43bd79 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -18,9 +18,59 @@ android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" - android:fitsSystemWindows="true" - app:headerLayout="@layout/nav_header_main" - app:menu="@menu/activity_main_drawer" /> + android:fitsSystemWindows="true"> + <LinearLayout + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:orientation="vertical"> + + <include + android:id="@+id/nav_header_main_activity" + layout="@layout/nav_header_main" /> + + <LinearLayout + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:padding="16dp" + android:clickable="true" + android:focusable="true" + android:background="?attr/selectableItemBackground"> + + <ImageView + android:layout_width="24dp" + android:layout_height="24dp" + android:layout_marginEnd="32dp" + android:layout_gravity="center_vertical" + android:src="@drawable/ic_account_circle_grey_24dp"/> + + <TextView + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="@string/profile" + android:textColor="@android:color/black" + android:layout_gravity="center_vertical"/> + + </LinearLayout> + + <View + android:layout_width="match_parent" + android:layout_height="1dp" + android:background="#E0E0E0"/> + + <TextView + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="@string/subscriptions" + android:layout_margin="16dp"/> + + <android.support.v7.widget.RecyclerView + android:id="@+id/subscribed_subreddit_recycler_view_main_activity" + android:layout_width="match_parent" + android:layout_height="wrap_content" /> + + </LinearLayout> + + </android.support.design.widget.NavigationView> </android.support.v4.widget.DrawerLayout> diff --git a/app/src/main/res/layout/item_subscribed_subreddit.xml b/app/src/main/res/layout/item_subscribed_subreddit.xml index d8fb3247..3795aa62 100644 --- a/app/src/main/res/layout/item_subscribed_subreddit.xml +++ b/app/src/main/res/layout/item_subscribed_subreddit.xml @@ -1,6 +1,24 @@ <?xml version="1.0" encoding="utf-8"?> -<android.support.constraint.ConstraintLayout +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" - android:layout_height="match_parent"> + android:layout_height="wrap_content" + android:padding="16dp" + android:clickable="true" + android:focusable="true" + android:background="?attr/selectableItemBackground"> -</android.support.constraint.ConstraintLayout>
\ No newline at end of file + <de.hdodenhof.circleimageview.CircleImageView + android:id="@+id/subreddit_icon_circle_image_view_item_subscribed_subreddit" + android:layout_width="24dp" + android:layout_height="24dp" + android:layout_gravity="center_vertical" + android:layout_marginEnd="32dp"/> + + <TextView + android:id="@+id/subreddit_name_text_view_item_subscribed_subreddit" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:textColor="@android:color/black" + android:layout_gravity="center_vertical"/> + +</LinearLayout>
\ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2f36e7c0..38b51769 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -15,4 +15,7 @@ <string name="nsfw">NSFW</string> <string name="karma_info">Karma: %1$d</string> + + <string name="profile">Profile</string> + <string name="subscriptions">Subscriptions</string> </resources> |