From 773c82a2b7d6860240496b9f184467d675292c0a Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 30 Sep 2020 22:58:38 +0800 Subject: Don't search nsfw subreddits when nsfw is off. --- .../docilealligator/infinityforreddit/API/RedditAPI.java | 4 ++-- .../Fragment/SubredditListingFragment.java | 15 ++++++++++----- .../infinityforreddit/Subreddit/FetchSubredditData.java | 4 ++-- .../infinityforreddit/Subreddit/ParseSubredditData.java | 2 +- .../Subreddit/SubredditListingDataSource.java | 8 +++++--- .../Subreddit/SubredditListingDataSourceFactory.java | 6 ++++-- .../Subreddit/SubredditListingViewModel.java | 10 ++++++---- 7 files changed, 30 insertions(+), 19 deletions(-) (limited to 'app/src') 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 getInfoOauth(@Query("id") String id, @HeaderMap Map headers); - @GET("subreddits/search.json?include_over_18=1&raw_json=1") + @GET("subreddits/search.json?raw_json=1") Call 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 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 headers = accessToken != null ? APIUtils.getOAuthHeader(accessToken) : ImmutableMap.of(); - Call subredditDataCall = api.searchSubreddits(query, after, sortType, headers); + Call subredditDataCall = api.searchSubreddits(query, after, sortType, nsfw ? 1 : 0, headers); subredditDataCall.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response 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 paginationNetworkStateLiveData; private MutableLiveData initialLoadStateLiveData; @@ -24,11 +25,12 @@ public class SubredditListingDataSource extends PageKeyedDataSource params; private LoadCallback 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 params, @NonNull LoadInitialCallback 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, String after) { @@ -85,7 +87,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource 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 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> subreddits; private MutableLiveData 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 create(@NonNull Class modelClass) { - return (T) new SubredditListingViewModel(retrofit, query, sortType, accessToken); + return (T) new SubredditListingViewModel(retrofit, query, sortType, accessToken, nsfw); } } } -- cgit v1.2.3