diff options
author | Alex Ning <chineseperson5@gmail.com> | 2020-09-30 14:58:38 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2020-09-30 14:58:38 +0000 |
commit | 773c82a2b7d6860240496b9f184467d675292c0a (patch) | |
tree | 018ba79d04cfeb3b548e426ef8f142b41a289d14 /app | |
parent | 6e4519ef93f295178e0b850b739bd0085de6aa17 (diff) | |
download | infinity-for-reddit-773c82a2b7d6860240496b9f184467d675292c0a.tar infinity-for-reddit-773c82a2b7d6860240496b9f184467d675292c0a.tar.gz infinity-for-reddit-773c82a2b7d6860240496b9f184467d675292c0a.tar.bz2 infinity-for-reddit-773c82a2b7d6860240496b9f184467d675292c0a.tar.lz infinity-for-reddit-773c82a2b7d6860240496b9f184467d675292c0a.tar.xz infinity-for-reddit-773c82a2b7d6860240496b9f184467d675292c0a.tar.zst infinity-for-reddit-773c82a2b7d6860240496b9f184467d675292c0a.zip |
Don't search nsfw subreddits when nsfw is off.
Diffstat (limited to 'app')
7 files changed, 30 insertions, 19 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/API/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/API/RedditAPI.java index 5f505701..e696ccf2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/API/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/API/RedditAPI.java @@ -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") diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubredditListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubredditListingFragment.java index fa27ce00..3c397a7a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubredditListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubredditListingFragment.java @@ -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); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/FetchSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/FetchSubredditData.java index 3f720c67..f767167b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/FetchSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/FetchSubredditData.java @@ -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) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/ParseSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/ParseSubredditData.java index f2545bd8..720e663b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/ParseSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/ParseSubredditData.java @@ -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)) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingDataSource.java index f75205c1..b260c985 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingDataSource.java @@ -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) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingDataSourceFactory.java index dfdafbca..22e57d26 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingDataSourceFactory.java @@ -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; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingViewModel.java index df34c349..80c1f4b9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingViewModel.java @@ -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); } } } |