Don't search nsfw subreddits when nsfw is off.

This commit is contained in:
Alex Ning 2020-09-30 22:58:38 +08:00
parent 6e4519ef93
commit 773c82a2b7
7 changed files with 30 additions and 19 deletions

View File

@ -123,9 +123,9 @@ public interface RedditAPI {
@GET("/api/info.json?raw_json=1") @GET("/api/info.json?raw_json=1")
Call<String> getInfoOauth(@Query("id") String id, @HeaderMap Map<String, String> headers); Call<String> getInfoOauth(@Query("id") String id, @HeaderMap Map<String, String> headers);
@GET("subreddits/search.json?include_over_18=1&raw_json=1") @GET("subreddits/search.json?raw_json=1")
Call<String> searchSubreddits(@Query("q") String subredditName, @Query("after") String after, Call<String> searchSubreddits(@Query("q") String subredditName, @Query("after") String after,
@Query("sort") String sort, @Query("sort") String sort, @Query("include_over_18") int nsfw,
@HeaderMap Map<String, String> headers); @HeaderMap Map<String, String> headers);
@GET("search.json?include_over_18=1&raw_json=1&type=user") @GET("search.json?include_over_18=1&raw_json=1&type=user")

View File

@ -84,6 +84,9 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
@Named("sort_type") @Named("sort_type")
SharedPreferences mSortTypeSharedPreferences; SharedPreferences mSortTypeSharedPreferences;
@Inject @Inject
@Named("nsfw_and_spoiler")
SharedPreferences mNsfwAndSpoilerSharedPreferences;
@Inject
CustomThemeWrapper customThemeWrapper; CustomThemeWrapper customThemeWrapper;
private LinearLayoutManager mLinearLayoutManager; private LinearLayoutManager mLinearLayoutManager;
private SubredditListingRecyclerViewAdapter mAdapter; private SubredditListingRecyclerViewAdapter mAdapter;
@ -129,6 +132,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_SEARCH_SUBREDDIT, SortType.Type.RELEVANCE.value); String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_SEARCH_SUBREDDIT, SortType.Type.RELEVANCE.value);
sortType = new SortType(SortType.Type.valueOf(sort.toUpperCase())); sortType = new SortType(SortType.Type.valueOf(sort.toUpperCase()));
boolean nsfw = mNsfwAndSpoilerSharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.NSFW_BASE, false);
mAdapter = new SubredditListingRecyclerViewAdapter(mActivity, mOauthRetrofit, mRetrofit, mAdapter = new SubredditListingRecyclerViewAdapter(mActivity, mOauthRetrofit, mRetrofit,
customThemeWrapper, accessToken, accountName, mRedditDataRoomDatabase, customThemeWrapper, accessToken, accountName, mRedditDataRoomDatabase,
@ -152,11 +156,12 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
mSubredditListingRecyclerView.setAdapter(mAdapter); mSubredditListingRecyclerView.setAdapter(mAdapter);
SubredditListingViewModel.Factory factory = new SubredditListingViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, query, sortType, accessToken); SubredditListingViewModel.Factory factory = new SubredditListingViewModel.Factory(
accessToken == null ? mRetrofit : mOauthRetrofit, query, sortType, accessToken, nsfw);
mSubredditListingViewModel = new ViewModelProvider(this, factory).get(SubredditListingViewModel.class); mSubredditListingViewModel = new ViewModelProvider(this, factory).get(SubredditListingViewModel.class);
mSubredditListingViewModel.getSubreddits().observe(this, subredditData -> mAdapter.submitList(subredditData)); mSubredditListingViewModel.getSubreddits().observe(getViewLifecycleOwner(), subredditData -> mAdapter.submitList(subredditData));
mSubredditListingViewModel.hasSubredditLiveData().observe(this, hasSubreddit -> { mSubredditListingViewModel.hasSubredditLiveData().observe(getViewLifecycleOwner(), hasSubreddit -> {
mSwipeRefreshLayout.setRefreshing(false); mSwipeRefreshLayout.setRefreshing(false);
if (hasSubreddit) { if (hasSubreddit) {
mFetchSubredditListingInfoLinearLayout.setVisibility(View.GONE); mFetchSubredditListingInfoLinearLayout.setVisibility(View.GONE);
@ -168,7 +173,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
} }
}); });
mSubredditListingViewModel.getInitialLoadingState().observe(this, networkState -> { mSubredditListingViewModel.getInitialLoadingState().observe(getViewLifecycleOwner(), networkState -> {
if (networkState.getStatus().equals(NetworkState.Status.SUCCESS)) { if (networkState.getStatus().equals(NetworkState.Status.SUCCESS)) {
mSwipeRefreshLayout.setRefreshing(false); mSwipeRefreshLayout.setRefreshing(false);
} else if (networkState.getStatus().equals(NetworkState.Status.FAILED)) { } else if (networkState.getStatus().equals(NetworkState.Status.FAILED)) {
@ -180,7 +185,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
} }
}); });
mSubredditListingViewModel.getPaginationNetworkState().observe(this, networkState -> { mSubredditListingViewModel.getPaginationNetworkState().observe(getViewLifecycleOwner(), networkState -> {
mAdapter.setNetworkState(networkState); mAdapter.setNetworkState(networkState);
}); });

View File

@ -47,11 +47,11 @@ public class FetchSubredditData {
} }
static void fetchSubredditListingData(Retrofit retrofit, String query, String after, String sortType, String accessToken, static void fetchSubredditListingData(Retrofit retrofit, String query, String after, String sortType, String accessToken,
final FetchSubredditListingDataListener fetchSubredditListingDataListener) { boolean nsfw, final FetchSubredditListingDataListener fetchSubredditListingDataListener) {
RedditAPI api = retrofit.create(RedditAPI.class); RedditAPI api = retrofit.create(RedditAPI.class);
Map<String, String> headers = accessToken != null ? APIUtils.getOAuthHeader(accessToken) : ImmutableMap.of(); Map<String, String> headers = accessToken != null ? APIUtils.getOAuthHeader(accessToken) : ImmutableMap.of();
Call<String> subredditDataCall = api.searchSubreddits(query, after, sortType, headers); Call<String> subredditDataCall = api.searchSubreddits(query, after, sortType, nsfw ? 1 : 0, headers);
subredditDataCall.enqueue(new Callback<String>() { subredditDataCall.enqueue(new Callback<String>() {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {

View File

@ -27,7 +27,7 @@ class ParseSubredditData {
String sidebarDescription = Utils.modifyMarkdown(subredditDataJsonObject.getString(JSONUtils.DESCRIPTION_KEY).trim()); String sidebarDescription = Utils.modifyMarkdown(subredditDataJsonObject.getString(JSONUtils.DESCRIPTION_KEY).trim());
long createdUTC = subredditDataJsonObject.getLong(JSONUtils.CREATED_UTC_KEY) * 1000; long createdUTC = subredditDataJsonObject.getLong(JSONUtils.CREATED_UTC_KEY) * 1000;
String suggestedCommentSort = subredditDataJsonObject.getString(JSONUtils.SUGGESTED_COMMENT_SORT_KEY); String suggestedCommentSort = subredditDataJsonObject.getString(JSONUtils.SUGGESTED_COMMENT_SORT_KEY);
boolean isNSFW = subredditDataJsonObject.getBoolean(JSONUtils.OVER18_KEY); boolean isNSFW = !subredditDataJsonObject.isNull(JSONUtils.OVER18_KEY) && subredditDataJsonObject.getBoolean(JSONUtils.OVER18_KEY);
String bannerImageUrl; String bannerImageUrl;
if (subredditDataJsonObject.isNull(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY)) { if (subredditDataJsonObject.isNull(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY)) {

View File

@ -16,6 +16,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
private String query; private String query;
private SortType sortType; private SortType sortType;
private String accessToken; private String accessToken;
private boolean nsfw;
private MutableLiveData<NetworkState> paginationNetworkStateLiveData; private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
private MutableLiveData<NetworkState> initialLoadStateLiveData; private MutableLiveData<NetworkState> initialLoadStateLiveData;
@ -24,11 +25,12 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
private LoadParams<String> params; private LoadParams<String> params;
private LoadCallback<String, SubredditData> callback; private LoadCallback<String, SubredditData> callback;
SubredditListingDataSource(Retrofit retrofit, String query, SortType sortType, String accessToken) { SubredditListingDataSource(Retrofit retrofit, String query, SortType sortType, String accessToken, boolean nsfw) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.query = query; this.query = query;
this.sortType = sortType; this.sortType = sortType;
this.accessToken = accessToken; this.accessToken = accessToken;
this.nsfw = nsfw;
paginationNetworkStateLiveData = new MutableLiveData<>(); paginationNetworkStateLiveData = new MutableLiveData<>();
initialLoadStateLiveData = new MutableLiveData<>(); initialLoadStateLiveData = new MutableLiveData<>();
hasSubredditLiveData = new MutableLiveData<>(); hasSubredditLiveData = new MutableLiveData<>();
@ -50,7 +52,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull LoadInitialCallback<String, SubredditData> callback) { public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull LoadInitialCallback<String, SubredditData> callback) {
initialLoadStateLiveData.postValue(NetworkState.LOADING); initialLoadStateLiveData.postValue(NetworkState.LOADING);
FetchSubredditData.fetchSubredditListingData(retrofit, query, null, sortType.getType().value, accessToken, FetchSubredditData.fetchSubredditListingData(retrofit, query, null, sortType.getType().value, accessToken, nsfw,
new FetchSubredditData.FetchSubredditListingDataListener() { new FetchSubredditData.FetchSubredditListingDataListener() {
@Override @Override
public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) { public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) {
@ -85,7 +87,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
return; return;
} }
FetchSubredditData.fetchSubredditListingData(retrofit, query, params.key, sortType.getType().value, accessToken, FetchSubredditData.fetchSubredditListingData(retrofit, query, params.key, sortType.getType().value, accessToken, nsfw,
new FetchSubredditData.FetchSubredditListingDataListener() { new FetchSubredditData.FetchSubredditListingDataListener() {
@Override @Override
public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) { public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) {

View File

@ -12,22 +12,24 @@ public class SubredditListingDataSourceFactory extends DataSource.Factory {
private String query; private String query;
private SortType sortType; private SortType sortType;
private String accessToken; private String accessToken;
private boolean nsfw;
private SubredditListingDataSource subredditListingDataSource; private SubredditListingDataSource subredditListingDataSource;
private MutableLiveData<SubredditListingDataSource> subredditListingDataSourceMutableLiveData; private MutableLiveData<SubredditListingDataSource> subredditListingDataSourceMutableLiveData;
SubredditListingDataSourceFactory(Retrofit retrofit, String query, SortType sortType, String accessToken) { SubredditListingDataSourceFactory(Retrofit retrofit, String query, SortType sortType, String accessToken, boolean nsfw) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.query = query; this.query = query;
this.sortType = sortType; this.sortType = sortType;
this.accessToken = accessToken; this.accessToken = accessToken;
this.nsfw = nsfw;
subredditListingDataSourceMutableLiveData = new MutableLiveData<>(); subredditListingDataSourceMutableLiveData = new MutableLiveData<>();
} }
@NonNull @NonNull
@Override @Override
public DataSource create() { public DataSource create() {
subredditListingDataSource = new SubredditListingDataSource(retrofit, query, sortType, accessToken); subredditListingDataSource = new SubredditListingDataSource(retrofit, query, sortType, accessToken, nsfw);
subredditListingDataSourceMutableLiveData.postValue(subredditListingDataSource); subredditListingDataSourceMutableLiveData.postValue(subredditListingDataSource);
return subredditListingDataSource; return subredditListingDataSource;
} }

View File

@ -21,8 +21,8 @@ public class SubredditListingViewModel extends ViewModel {
private LiveData<PagedList<SubredditData>> subreddits; private LiveData<PagedList<SubredditData>> subreddits;
private MutableLiveData<SortType> sortTypeLiveData; private MutableLiveData<SortType> sortTypeLiveData;
public SubredditListingViewModel(Retrofit retrofit, String query, SortType sortType, String accessToken) { public SubredditListingViewModel(Retrofit retrofit, String query, SortType sortType, String accessToken, boolean nsfw) {
subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType, accessToken); subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType, accessToken, nsfw);
initialLoadingState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(), initialLoadingState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(),
SubredditListingDataSource::getInitialLoadStateLiveData); SubredditListingDataSource::getInitialLoadStateLiveData);
@ -79,18 +79,20 @@ public class SubredditListingViewModel extends ViewModel {
private String query; private String query;
private SortType sortType; private SortType sortType;
private String accessToken; private String accessToken;
private boolean nsfw;
public Factory(Retrofit retrofit, String query, SortType sortType, String accessToken) { public Factory(Retrofit retrofit, String query, SortType sortType, String accessToken, boolean nsfw) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.query = query; this.query = query;
this.sortType = sortType; this.sortType = sortType;
this.accessToken = accessToken; this.accessToken = accessToken;
this.nsfw = nsfw;
} }
@NonNull @NonNull
@Override @Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
return (T) new SubredditListingViewModel(retrofit, query, sortType, accessToken); return (T) new SubredditListingViewModel(retrofit, query, sortType, accessToken, nsfw);
} }
} }
} }