Browse posts and search posts are now available in anonymous mode.

This commit is contained in:
Alex Ning 2019-08-09 16:57:04 +08:00
parent 17fc426ecb
commit 1179cb50fa
9 changed files with 190 additions and 54 deletions

View File

@ -77,20 +77,29 @@ class AccountRecyclerViewAdapter extends RecyclerView.Adapter<AccountRecyclerVie
holder.usernameTextView.setText(R.string.add_account);
holder.itemView.setOnClickListener(view -> mItemSelectedListener.addAccountSelected());
} else if(position == mAccounts.size() + 1) {
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 if(position == mAccounts.size() + 2){
} 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(mCurrentAccountName != null) {
}
} 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 {
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<AccountRecyclerVie
return 1;
} else {
if(mCurrentAccountName == null) {
return mAccounts.size() + 3;
return mAccounts.size() + 2;
} else {
return mAccounts.size() + 4;
}

View File

@ -19,7 +19,7 @@ class FetchPost {
static void fetchPost(Retrofit oauthRetrofit, String id, String accessToken, Locale locale, FetchPostListener fetchPostListener) {
RedditAPI api = oauthRetrofit.create(RedditAPI.class);
Call<String> postCall = api.getPost(id, RedditUtils.getOAuthHeader(accessToken));
Call<String> postCall = api.getPostOauth(id, RedditUtils.getOAuthHeader(accessToken));
postCall.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {

View File

@ -297,8 +297,13 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
loadUserData();
mAccountNameTextView.setText(mAccountName);
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,6 +648,13 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
}
void changeSortType(String sortType) {
if(mAccessToken == null) {
if(viewPager.getCurrentItem() == 0) {
popularPostFragment.changeSortType(sortType);
} else {
allPostFragment.changeSortType(sortType);
}
} else {
switch (viewPager.getCurrentItem()) {
case 0:
frontPagePostFragment.changeSortType(sortType);
@ -630,8 +666,20 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
allPostFragment.changeSortType(sortType);
}
}
}
public void refresh() {
if(mAccessToken == null) {
if(viewPager.getCurrentItem() == 0) {
if(popularPostFragment != null) {
((FragmentCommunicator) popularPostFragment).refresh();
}
} else {
if(allPostFragment != null) {
((FragmentCommunicator) allPostFragment).refresh();
}
}
} else {
switch (viewPager.getCurrentItem()) {
case 0:
if(frontPagePostFragment != null) {
@ -650,4 +698,5 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
}
}
}
}
}

View File

@ -53,8 +53,8 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
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<String, Post> {
private void loadSubredditPostsInitial(@NonNull final LoadInitialCallback<String, Post> callback, String lastItem) {
RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> getPost = api.getSubredditBestPosts(subredditOrUserName, sortType, lastItem, RedditUtils.getOAuthHeader(accessToken));
Call<String> getPost;
if(accessToken == null) {
getPost = api.getSubredditBestPosts(subredditOrUserName, sortType, lastItem);
} else {
getPost = api.getSubredditBestPostsOauth(subredditOrUserName, sortType, lastItem, RedditUtils.getOAuthHeader(accessToken));
}
getPost.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
@ -356,7 +362,14 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
String after = lastItem == null ? params.key : lastItem;
RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> getPost = api.getSubredditBestPosts(subredditOrUserName, sortType, after, RedditUtils.getOAuthHeader(accessToken));
Call<String> getPost;
if(accessToken == null) {
getPost = api.getSubredditBestPosts(subredditOrUserName, sortType, after);
} else {
getPost = api.getSubredditBestPostsOauth(subredditOrUserName, sortType, after, RedditUtils.getOAuthHeader(accessToken));
}
getPost.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
@ -394,7 +407,13 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
private void loadUserPostsInitial(@NonNull final LoadInitialCallback<String, Post> callback, String lastItem) {
RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> getPost = api.getUserBestPosts(subredditOrUserName, lastItem, RedditUtils.getOAuthHeader(accessToken));
Call<String> getPost;
if(accessToken == null) {
getPost = api.getUserBestPosts(subredditOrUserName, lastItem);
} else {
getPost = api.getUserBestPostsOauth(subredditOrUserName, lastItem, RedditUtils.getOAuthHeader(accessToken));
}
getPost.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
@ -447,7 +466,13 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
String after = lastItem == null ? params.key : lastItem;
RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> getPost = api.getUserBestPosts(subredditOrUserName, after, RedditUtils.getOAuthHeader(accessToken));
Call<String> getPost;
if(accessToken == null) {
getPost = api.getUserBestPosts(subredditOrUserName, after);
} else {
getPost = api.getUserBestPostsOauth(subredditOrUserName, after, RedditUtils.getOAuthHeader(accessToken));
}
getPost.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
@ -488,9 +513,18 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
Call<String> 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.searchPostsInSpecificSubreddit(subredditOrUserName, query, null, RedditUtils.getOAuthHeader(accessToken));
getPost = api.searchPostsOauth(query, lastItem, sortType, RedditUtils.getOAuthHeader(accessToken));
}
} else {
if(accessToken == null) {
getPost = api.searchPostsInSpecificSubreddit(subredditOrUserName, query, lastItem);
} else {
getPost = api.searchPostsInSpecificSubredditOauth(subredditOrUserName, query, lastItem,
RedditUtils.getOAuthHeader(accessToken));
}
}
getPost.enqueue(new Callback<String>() {
@ -548,9 +582,17 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
Call<String> 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.searchPostsInSpecificSubreddit(subredditOrUserName, query, after, RedditUtils.getOAuthHeader(accessToken));
getPost = api.searchPostsOauth(query, after, sortType, RedditUtils.getOAuthHeader(accessToken));
}
} else {
if(accessToken == null) {
getPost = api.searchPostsInSpecificSubreddit(subredditOrUserName, query, after);
} else {
getPost = api.searchPostsInSpecificSubredditOauth(subredditOrUserName, query, after, RedditUtils.getOAuthHeader(accessToken));
}
}
getPost.enqueue(new Callback<String>() {

View File

@ -200,8 +200,13 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
}
});
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 {
}
});
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 {
}
});
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() {

View File

@ -39,19 +39,29 @@ public interface RedditAPI {
Call<String> voteThing(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params);
@GET("comments/{id}.json?raw_json=1")
Call<String> getPost(@Path("id") String id, @HeaderMap Map<String, String> headers);
Call<String> getPostOauth(@Path("id") String id, @HeaderMap Map<String, String> headers);
@GET("comments/{id}.json?raw_json=1")
Call<String> getPost(@Path("id") String id);
@GET("{sortType}?raw_json=1")
Call<String> getBestPosts(@Path("sortType") String sortType, @Query("after") String lastItem, @HeaderMap Map<String, String> headers);
@GET("r/{subredditName}/{sortType}.json?raw_json=1&limit=25")
Call<String> getSubredditBestPosts(@Path("subredditName") String subredditName, @Path("sortType") String sortType,
Call<String> getSubredditBestPostsOauth(@Path("subredditName") String subredditName, @Path("sortType") String sortType,
@Query("after") String lastItem, @HeaderMap Map<String, String> headers);
@GET("r/{subredditName}/{sortType}.json?raw_json=1&limit=25")
Call<String> getSubredditBestPosts(@Path("subredditName") String subredditName, @Path("sortType") String sortType,
@Query("after") String lastItem);
@GET("user/{username}/submitted.json?raw_json=1&limit=25")
Call<String> getUserBestPosts(@Path("username") String username, @Query("after") String lastItem,
Call<String> getUserBestPostsOauth(@Path("username") String username, @Query("after") String lastItem,
@HeaderMap Map<String, String> headers);
@GET("user/{username}/submitted.json?raw_json=1&limit=25")
Call<String> getUserBestPosts(@Path("username") String username, @Query("after") String lastItem);
@GET("user/{username}/about.json?raw_json=1")
Call<String> getUserData(@Path("username") String username);
@ -72,12 +82,19 @@ public interface RedditAPI {
Call<String> 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<String> searchPosts(@Query("q") String query, @Query("after") String after, @Query("sort") String sort,
Call<String> searchPostsOauth(@Query("q") String query, @Query("after") String after, @Query("sort") String sort,
@HeaderMap Map<String, String> headers);
@GET("search.json?raw_json=1&type=link&include_over_18=on")
Call<String> 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<String> searchPostsInSpecificSubredditOauth(@Path("subredditName") String subredditName, @Query("q") String query,
@Query("after") String after, @HeaderMap Map<String, String> headers);
@GET("r/{subredditName}/search.json?raw_json=1&type=link&restrict_sr=true&include_over_18=on")
Call<String> searchPostsInSpecificSubreddit(@Path("subredditName") String subredditName, @Query("q") String query,
@Query("after") String after, @HeaderMap Map<String, String> headers);
@Query("after") String after);
@FormUrlEncoded
@POST("api/comment")
@ -102,7 +119,10 @@ public interface RedditAPI {
Call<String> getRules(@Path("subredditName") String subredditName);
@GET("/comments/{id}.json?raw_json=1")
Call<String> getPostAndCommentsById(@Path("id") String id, @HeaderMap Map<String, String> headers);
Call<String> getPostAndCommentsByIdOauth(@Path("id") String id, @HeaderMap Map<String, String> headers);
@GET("/comments/{id}.json?raw_json=1")
Call<String> getPostAndCommentsById(@Path("id") String id);
@Multipart
@POST(".")

View File

@ -439,7 +439,7 @@ class SubmitPost {
RedditAPI api = oauthRetrofit.create(RedditAPI.class);
Call<String> getPostCall = api.getPost(postId, RedditUtils.getOAuthHeader(accessToken));
Call<String> getPostCall = api.getPostOauth(postId, RedditUtils.getOAuthHeader(accessToken));
getPostCall.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {

View File

@ -201,7 +201,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
RedditAPI api = mOauthRetrofit.create(RedditAPI.class);
Call<String> postAndComments = api.getPostAndCommentsById(subredditId, RedditUtils.getOAuthHeader(accessToken));
Call<String> postAndComments = api.getPostAndCommentsByIdOauth(subredditId, RedditUtils.getOAuthHeader(accessToken));
postAndComments.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {

View File

@ -176,4 +176,5 @@
<string name="anonymous_account">Anonymous</string>
<string name="manage_accounts">Manage accounts</string>
<string name="log_out">Log out</string>
<string name="press_here_to_login">Press here to login</string>
</resources>