From 1179cb50fa939c296e14233a9e8109babefe8760 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 9 Aug 2019 16:57:04 +0800 Subject: Browse posts and search posts are now available in anonymous mode. --- .../AccountRecyclerViewAdapter.java | 27 ++++--- .../infinityforreddit/FetchPost.java | 2 +- .../infinityforreddit/MainActivity.java | 85 +++++++++++++++++----- .../infinityforreddit/PostDataSource.java | 62 +++++++++++++--- .../infinityforreddit/PostFragment.java | 27 +++++-- .../infinityforreddit/RedditAPI.java | 36 +++++++-- .../infinityforreddit/SubmitPost.java | 2 +- .../infinityforreddit/ViewPostDetailActivity.java | 2 +- app/src/main/res/values/strings.xml | 1 + 9 files changed, 190 insertions(+), 54 deletions(-) (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AccountRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AccountRecyclerViewAdapter.java index cde80fab..7aba25a7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AccountRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AccountRecyclerViewAdapter.java @@ -77,20 +77,29 @@ class AccountRecyclerViewAdapter extends RecyclerView.Adapter mItemSelectedListener.addAccountSelected()); } else if(position == mAccounts.size() + 1) { - holder.profileImageGifImageView.setColorFilter(ContextCompat.getColor(mContext, R.color.primaryTextColor), android.graphics.PorterDuff.Mode.SRC_IN); - mGlide.load(R.drawable.ic_outline_public_24px) - .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(128, 0))) - .into(holder.profileImageGifImageView); - holder.usernameTextView.setText(R.string.anonymous_account); - holder.itemView.setOnClickListener(view -> mItemSelectedListener.anonymousSelected()); - } else if(position == mAccounts.size() + 2){ + if(mCurrentAccountName != null) { + holder.profileImageGifImageView.setColorFilter(ContextCompat.getColor(mContext, R.color.primaryTextColor), android.graphics.PorterDuff.Mode.SRC_IN); + mGlide.load(R.drawable.ic_outline_public_24px) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(128, 0))) + .into(holder.profileImageGifImageView); + holder.usernameTextView.setText(R.string.anonymous_account); + holder.itemView.setOnClickListener(view -> mItemSelectedListener.anonymousSelected()); + } else { + holder.profileImageGifImageView.setColorFilter(ContextCompat.getColor(mContext, R.color.primaryTextColor), android.graphics.PorterDuff.Mode.SRC_IN); + mGlide.load(R.drawable.ic_outline_settings_24px) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(128, 0))) + .into(holder.profileImageGifImageView); + holder.usernameTextView.setText(R.string.manage_accounts); + holder.itemView.setOnClickListener(view -> mItemSelectedListener.manageAccountSelected()); + } + } else if(position == mAccounts.size() + 2) { holder.profileImageGifImageView.setColorFilter(ContextCompat.getColor(mContext, R.color.primaryTextColor), android.graphics.PorterDuff.Mode.SRC_IN); mGlide.load(R.drawable.ic_outline_settings_24px) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(128, 0))) .into(holder.profileImageGifImageView); holder.usernameTextView.setText(R.string.manage_accounts); holder.itemView.setOnClickListener(view -> mItemSelectedListener.manageAccountSelected()); - } else if(mCurrentAccountName != null) { + } else { holder.profileImageGifImageView.setColorFilter(ContextCompat.getColor(mContext, R.color.primaryTextColor), android.graphics.PorterDuff.Mode.SRC_IN); mGlide.load(R.drawable.ic_outline_block_24px) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(128, 0))) @@ -107,7 +116,7 @@ class AccountRecyclerViewAdapter extends RecyclerView.Adapter postCall = api.getPost(id, RedditUtils.getOAuthHeader(accessToken)); + Call postCall = api.getPostOauth(id, RedditUtils.getOAuthHeader(accessToken)); postCall.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 7fb79cc9..a2e19cdc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -297,8 +297,13 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe loadUserData(); - mAccountNameTextView.setText(mAccountName); - mKarmaTextView.setText(getString(R.string.karma_info, mKarma)); + if(mAccessToken != null) { + mKarmaTextView.setText(getString(R.string.karma_info, mKarma)); + mAccountNameTextView.setText(mAccountName); + } else { + mKarmaTextView.setText(R.string.press_here_to_login); + mAccountNameTextView.setText(R.string.anonymous_account); + } if (mProfileImageUrl != null && !mProfileImageUrl.equals("")) { glide.load(mProfileImageUrl) @@ -516,6 +521,30 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe @NonNull @Override public Fragment getItem(int position) { + if(mAccessToken == null) { + if(position == 0) { + PostFragment fragment = new PostFragment(); + Bundle bundle = new Bundle(); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); + bundle.putString(PostFragment.EXTRA_NAME, "popular"); + bundle.putString(PostFragment.EXTRA_SORT_TYPE, PostDataSource.SORT_TYPE_HOT); + bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); + bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + fragment.setArguments(bundle); + return fragment; + } else { + PostFragment fragment = new PostFragment(); + Bundle bundle = new Bundle(); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); + bundle.putString(PostFragment.EXTRA_NAME, "all"); + bundle.putString(PostFragment.EXTRA_SORT_TYPE, PostDataSource.SORT_TYPE_HOT); + bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); + bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + fragment.setArguments(bundle); + return fragment; + } + } + if (position == 0) { PostFragment fragment = new PostFragment(); Bundle bundle = new Bundle(); @@ -619,34 +648,54 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe } void changeSortType(String sortType) { - switch (viewPager.getCurrentItem()) { - case 0: - frontPagePostFragment.changeSortType(sortType); - break; - case 1: + if(mAccessToken == null) { + if(viewPager.getCurrentItem() == 0) { popularPostFragment.changeSortType(sortType); - break; - case 2: + } else { allPostFragment.changeSortType(sortType); + } + } else { + switch (viewPager.getCurrentItem()) { + case 0: + frontPagePostFragment.changeSortType(sortType); + break; + case 1: + popularPostFragment.changeSortType(sortType); + break; + case 2: + allPostFragment.changeSortType(sortType); + } } } public void refresh() { - switch (viewPager.getCurrentItem()) { - case 0: - if(frontPagePostFragment != null) { - ((FragmentCommunicator) frontPagePostFragment).refresh(); - } - break; - case 1: + if(mAccessToken == null) { + if(viewPager.getCurrentItem() == 0) { if(popularPostFragment != null) { ((FragmentCommunicator) popularPostFragment).refresh(); } - break; - case 2: + } else { if(allPostFragment != null) { ((FragmentCommunicator) allPostFragment).refresh(); } + } + } else { + switch (viewPager.getCurrentItem()) { + case 0: + if(frontPagePostFragment != null) { + ((FragmentCommunicator) frontPagePostFragment).refresh(); + } + break; + case 1: + if(popularPostFragment != null) { + ((FragmentCommunicator) popularPostFragment).refresh(); + } + break; + case 2: + if(allPostFragment != null) { + ((FragmentCommunicator) allPostFragment).refresh(); + } + } } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java index a0f42bbb..e471435f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java @@ -53,8 +53,8 @@ class PostDataSource extends PageKeyedDataSource { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; - paginationNetworkStateLiveData = new MutableLiveData(); - initialLoadStateLiveData = new MutableLiveData(); + paginationNetworkStateLiveData = new MutableLiveData<>(); + initialLoadStateLiveData = new MutableLiveData<>(); hasPostLiveData = new MutableLiveData<>(); this.postType = postType; this.sortType = sortType; @@ -284,7 +284,13 @@ class PostDataSource extends PageKeyedDataSource { private void loadSubredditPostsInitial(@NonNull final LoadInitialCallback callback, String lastItem) { RedditAPI api = retrofit.create(RedditAPI.class); - Call getPost = api.getSubredditBestPosts(subredditOrUserName, sortType, lastItem, RedditUtils.getOAuthHeader(accessToken)); + + Call getPost; + if(accessToken == null) { + getPost = api.getSubredditBestPosts(subredditOrUserName, sortType, lastItem); + } else { + getPost = api.getSubredditBestPostsOauth(subredditOrUserName, sortType, lastItem, RedditUtils.getOAuthHeader(accessToken)); + } getPost.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { @@ -356,7 +362,14 @@ class PostDataSource extends PageKeyedDataSource { String after = lastItem == null ? params.key : lastItem; RedditAPI api = retrofit.create(RedditAPI.class); - Call getPost = api.getSubredditBestPosts(subredditOrUserName, sortType, after, RedditUtils.getOAuthHeader(accessToken)); + + Call getPost; + if(accessToken == null) { + getPost = api.getSubredditBestPosts(subredditOrUserName, sortType, after); + } else { + getPost = api.getSubredditBestPostsOauth(subredditOrUserName, sortType, after, RedditUtils.getOAuthHeader(accessToken)); + } + getPost.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { @@ -394,7 +407,13 @@ class PostDataSource extends PageKeyedDataSource { private void loadUserPostsInitial(@NonNull final LoadInitialCallback callback, String lastItem) { RedditAPI api = retrofit.create(RedditAPI.class); - Call getPost = api.getUserBestPosts(subredditOrUserName, lastItem, RedditUtils.getOAuthHeader(accessToken)); + + Call getPost; + if(accessToken == null) { + getPost = api.getUserBestPosts(subredditOrUserName, lastItem); + } else { + getPost = api.getUserBestPostsOauth(subredditOrUserName, lastItem, RedditUtils.getOAuthHeader(accessToken)); + } getPost.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { @@ -447,7 +466,13 @@ class PostDataSource extends PageKeyedDataSource { String after = lastItem == null ? params.key : lastItem; RedditAPI api = retrofit.create(RedditAPI.class); - Call getPost = api.getUserBestPosts(subredditOrUserName, after, RedditUtils.getOAuthHeader(accessToken)); + + Call getPost; + if(accessToken == null) { + getPost = api.getUserBestPosts(subredditOrUserName, after); + } else { + getPost = api.getUserBestPostsOauth(subredditOrUserName, after, RedditUtils.getOAuthHeader(accessToken)); + } getPost.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { @@ -488,9 +513,18 @@ class PostDataSource extends PageKeyedDataSource { Call getPost; if(subredditOrUserName == null) { - getPost = api.searchPosts(query, null, sortType, RedditUtils.getOAuthHeader(accessToken)); + if(accessToken == null) { + getPost = api.searchPosts(query, lastItem, sortType); + } else { + getPost = api.searchPostsOauth(query, lastItem, sortType, RedditUtils.getOAuthHeader(accessToken)); + } } else { - getPost = api.searchPostsInSpecificSubreddit(subredditOrUserName, query, null, RedditUtils.getOAuthHeader(accessToken)); + if(accessToken == null) { + getPost = api.searchPostsInSpecificSubreddit(subredditOrUserName, query, lastItem); + } else { + getPost = api.searchPostsInSpecificSubredditOauth(subredditOrUserName, query, lastItem, + RedditUtils.getOAuthHeader(accessToken)); + } } getPost.enqueue(new Callback() { @@ -548,9 +582,17 @@ class PostDataSource extends PageKeyedDataSource { Call getPost; if(subredditOrUserName == null) { - getPost = api.searchPosts(query, after, sortType, RedditUtils.getOAuthHeader(accessToken)); + if(accessToken == null) { + getPost = api.searchPosts(query, after, sortType); + } else { + getPost = api.searchPostsOauth(query, after, sortType, RedditUtils.getOAuthHeader(accessToken)); + } } else { - getPost = api.searchPostsInSpecificSubreddit(subredditOrUserName, query, after, RedditUtils.getOAuthHeader(accessToken)); + if(accessToken == null) { + getPost = api.searchPostsInSpecificSubreddit(subredditOrUserName, query, after); + } else { + getPost = api.searchPostsInSpecificSubredditOauth(subredditOrUserName, query, after, RedditUtils.getOAuthHeader(accessToken)); + } } getPost.enqueue(new Callback() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index 3baa0c59..2506dde8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -200,8 +200,13 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } }); - factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, - getResources().getConfiguration().locale, subredditName, query, postType, sortType, filter); + if(accessToken == null) { + factory = new PostViewModel.Factory(mRetrofit, accessToken, + getResources().getConfiguration().locale, subredditName, query, postType, sortType, filter); + } else { + factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, + getResources().getConfiguration().locale, subredditName, query, postType, sortType, filter); + } } else if(postType == PostDataSource.TYPE_SUBREDDIT) { String subredditName = getArguments().getString(EXTRA_NAME); @@ -224,8 +229,13 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } }); - factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, - getResources().getConfiguration().locale, subredditName, postType, sortType, filter); + if(accessToken == null) { + factory = new PostViewModel.Factory(mRetrofit, accessToken, + getResources().getConfiguration().locale, subredditName, postType, sortType, filter); + } else { + factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, + getResources().getConfiguration().locale, subredditName, postType, sortType, filter); + } } else if(postType == PostDataSource.TYPE_USER) { CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mFetchPostInfoLinearLayout.getLayoutParams(); params.height = ViewGroup.LayoutParams.WRAP_CONTENT; @@ -251,8 +261,13 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } }); - factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, - getResources().getConfiguration().locale, username, postType, sortType, filter); + if(accessToken == null) { + factory = new PostViewModel.Factory(mRetrofit, accessToken, + getResources().getConfiguration().locale, username, postType, sortType, filter); + } else { + factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, + getResources().getConfiguration().locale, username, postType, sortType, filter); + } } else { mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, redditDataRoomDatabase, accessToken, postType, true, new PostRecyclerViewAdapter.Callback() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index f0a9ac5b..89d848c1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -39,18 +39,28 @@ public interface RedditAPI { Call voteThing(@HeaderMap Map headers, @FieldMap Map params); @GET("comments/{id}.json?raw_json=1") - Call getPost(@Path("id") String id, @HeaderMap Map headers); + Call getPostOauth(@Path("id") String id, @HeaderMap Map headers); + + @GET("comments/{id}.json?raw_json=1") + Call getPost(@Path("id") String id); @GET("{sortType}?raw_json=1") Call getBestPosts(@Path("sortType") String sortType, @Query("after") String lastItem, @HeaderMap Map headers); + @GET("r/{subredditName}/{sortType}.json?raw_json=1&limit=25") + Call getSubredditBestPostsOauth(@Path("subredditName") String subredditName, @Path("sortType") String sortType, + @Query("after") String lastItem, @HeaderMap Map headers); + @GET("r/{subredditName}/{sortType}.json?raw_json=1&limit=25") Call getSubredditBestPosts(@Path("subredditName") String subredditName, @Path("sortType") String sortType, - @Query("after") String lastItem, @HeaderMap Map headers); + @Query("after") String lastItem); @GET("user/{username}/submitted.json?raw_json=1&limit=25") - Call getUserBestPosts(@Path("username") String username, @Query("after") String lastItem, - @HeaderMap Map headers); + Call getUserBestPostsOauth(@Path("username") String username, @Query("after") String lastItem, + @HeaderMap Map headers); + + @GET("user/{username}/submitted.json?raw_json=1&limit=25") + Call getUserBestPosts(@Path("username") String username, @Query("after") String lastItem); @GET("user/{username}/about.json?raw_json=1") Call getUserData(@Path("username") String username); @@ -72,12 +82,19 @@ public interface RedditAPI { Call searchUsers(@Query("q") String profileName, @Query("after") String after, @Query("sort") String sort); @GET("search.json?raw_json=1&type=link&include_over_18=on") - Call searchPosts(@Query("q") String query, @Query("after") String after, @Query("sort") String sort, - @HeaderMap Map headers); + Call searchPostsOauth(@Query("q") String query, @Query("after") String after, @Query("sort") String sort, + @HeaderMap Map headers); + + @GET("search.json?raw_json=1&type=link&include_over_18=on") + Call searchPosts(@Query("q") String query, @Query("after") String after, @Query("sort") String sort); + + @GET("r/{subredditName}/search.json?raw_json=1&type=link&restrict_sr=true&include_over_18=on") + Call searchPostsInSpecificSubredditOauth(@Path("subredditName") String subredditName, @Query("q") String query, + @Query("after") String after, @HeaderMap Map headers); @GET("r/{subredditName}/search.json?raw_json=1&type=link&restrict_sr=true&include_over_18=on") Call searchPostsInSpecificSubreddit(@Path("subredditName") String subredditName, @Query("q") String query, - @Query("after") String after, @HeaderMap Map headers); + @Query("after") String after); @FormUrlEncoded @POST("api/comment") @@ -102,7 +119,10 @@ public interface RedditAPI { Call getRules(@Path("subredditName") String subredditName); @GET("/comments/{id}.json?raw_json=1") - Call getPostAndCommentsById(@Path("id") String id, @HeaderMap Map headers); + Call getPostAndCommentsByIdOauth(@Path("id") String id, @HeaderMap Map headers); + + @GET("/comments/{id}.json?raw_json=1") + Call getPostAndCommentsById(@Path("id") String id); @Multipart @POST(".") diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java index 60b90560..d55b227b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java @@ -439,7 +439,7 @@ class SubmitPost { RedditAPI api = oauthRetrofit.create(RedditAPI.class); - Call getPostCall = api.getPost(postId, RedditUtils.getOAuthHeader(accessToken)); + Call getPostCall = api.getPostOauth(postId, RedditUtils.getOAuthHeader(accessToken)); getPostCall.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 6c0390da..40f6bc07 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -201,7 +201,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { .getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); RedditAPI api = mOauthRetrofit.create(RedditAPI.class); - Call postAndComments = api.getPostAndCommentsById(subredditId, RedditUtils.getOAuthHeader(accessToken)); + Call postAndComments = api.getPostAndCommentsByIdOauth(subredditId, RedditUtils.getOAuthHeader(accessToken)); postAndComments.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1234b405..32b68cbd 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -176,4 +176,5 @@ Anonymous Manage accounts Log out + Press here to login -- cgit v1.2.3