mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-11-07 03:07:26 +01:00
Don't search nsfw subreddits when nsfw is off.
This commit is contained in:
parent
6e4519ef93
commit
773c82a2b7
@ -123,9 +123,9 @@ public interface RedditAPI {
|
||||
@GET("/api/info.json?raw_json=1")
|
||||
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,
|
||||
@Query("sort") String sort,
|
||||
@Query("sort") String sort, @Query("include_over_18") int nsfw,
|
||||
@HeaderMap Map<String, String> headers);
|
||||
|
||||
@GET("search.json?include_over_18=1&raw_json=1&type=user")
|
||||
|
@ -84,6 +84,9 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
|
||||
@Named("sort_type")
|
||||
SharedPreferences mSortTypeSharedPreferences;
|
||||
@Inject
|
||||
@Named("nsfw_and_spoiler")
|
||||
SharedPreferences mNsfwAndSpoilerSharedPreferences;
|
||||
@Inject
|
||||
CustomThemeWrapper customThemeWrapper;
|
||||
private LinearLayoutManager mLinearLayoutManager;
|
||||
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);
|
||||
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,
|
||||
customThemeWrapper, accessToken, accountName, mRedditDataRoomDatabase,
|
||||
@ -152,11 +156,12 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
|
||||
|
||||
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.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);
|
||||
if (hasSubreddit) {
|
||||
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)) {
|
||||
mSwipeRefreshLayout.setRefreshing(false);
|
||||
} 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);
|
||||
});
|
||||
|
||||
|
@ -47,11 +47,11 @@ public class FetchSubredditData {
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
||||
|
@ -27,7 +27,7 @@ class ParseSubredditData {
|
||||
String sidebarDescription = Utils.modifyMarkdown(subredditDataJsonObject.getString(JSONUtils.DESCRIPTION_KEY).trim());
|
||||
long createdUTC = subredditDataJsonObject.getLong(JSONUtils.CREATED_UTC_KEY) * 1000;
|
||||
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;
|
||||
if (subredditDataJsonObject.isNull(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY)) {
|
||||
|
@ -16,6 +16,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
|
||||
private String query;
|
||||
private SortType sortType;
|
||||
private String accessToken;
|
||||
private boolean nsfw;
|
||||
|
||||
private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
|
||||
private MutableLiveData<NetworkState> initialLoadStateLiveData;
|
||||
@ -24,11 +25,12 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
|
||||
private LoadParams<String> params;
|
||||
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.query = query;
|
||||
this.sortType = sortType;
|
||||
this.accessToken = accessToken;
|
||||
this.nsfw = nsfw;
|
||||
paginationNetworkStateLiveData = new MutableLiveData<>();
|
||||
initialLoadStateLiveData = 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) {
|
||||
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() {
|
||||
@Override
|
||||
public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) {
|
||||
@ -85,7 +87,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
|
||||
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() {
|
||||
@Override
|
||||
public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) {
|
||||
|
@ -12,22 +12,24 @@ public class SubredditListingDataSourceFactory extends DataSource.Factory {
|
||||
private String query;
|
||||
private SortType sortType;
|
||||
private String accessToken;
|
||||
private boolean nsfw;
|
||||
|
||||
private SubredditListingDataSource subredditListingDataSource;
|
||||
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.query = query;
|
||||
this.sortType = sortType;
|
||||
this.accessToken = accessToken;
|
||||
this.nsfw = nsfw;
|
||||
subredditListingDataSourceMutableLiveData = new MutableLiveData<>();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public DataSource create() {
|
||||
subredditListingDataSource = new SubredditListingDataSource(retrofit, query, sortType, accessToken);
|
||||
subredditListingDataSource = new SubredditListingDataSource(retrofit, query, sortType, accessToken, nsfw);
|
||||
subredditListingDataSourceMutableLiveData.postValue(subredditListingDataSource);
|
||||
return subredditListingDataSource;
|
||||
}
|
||||
|
@ -21,8 +21,8 @@ public class SubredditListingViewModel extends ViewModel {
|
||||
private LiveData<PagedList<SubredditData>> subreddits;
|
||||
private MutableLiveData<SortType> sortTypeLiveData;
|
||||
|
||||
public SubredditListingViewModel(Retrofit retrofit, String query, SortType sortType, String accessToken) {
|
||||
subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType, accessToken);
|
||||
public SubredditListingViewModel(Retrofit retrofit, String query, SortType sortType, String accessToken, boolean nsfw) {
|
||||
subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType, accessToken, nsfw);
|
||||
|
||||
initialLoadingState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(),
|
||||
SubredditListingDataSource::getInitialLoadStateLiveData);
|
||||
@ -79,18 +79,20 @@ public class SubredditListingViewModel extends ViewModel {
|
||||
private String query;
|
||||
private SortType sortType;
|
||||
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.query = query;
|
||||
this.sortType = sortType;
|
||||
this.accessToken = accessToken;
|
||||
this.nsfw = nsfw;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user