From 13bed5d234fdf7fe7335eb3cdeb3516b5dc8b205 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 30 Sep 2020 23:15:32 +0800 Subject: Don't search nsfw users when nsfw is off. --- .../docilealligator/infinityforreddit/API/RedditAPI.java | 4 ++-- .../infinityforreddit/Fragment/UserListingFragment.java | 14 +++++++++----- .../infinityforreddit/User/FetchUserData.java | 4 ++-- .../infinityforreddit/User/UserListingDataSource.java | 8 +++++--- .../User/UserListingDataSourceFactory.java | 6 ++++-- .../infinityforreddit/User/UserListingViewModel.java | 10 ++++++---- 6 files changed, 28 insertions(+), 18 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 e696ccf2..db5798ec 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/API/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/API/RedditAPI.java @@ -128,9 +128,9 @@ public interface RedditAPI { @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") + @GET("search.json?raw_json=1&type=user") Call searchUsers(@Query("q") String profileName, @Query("after") String after, - @Query("sort") String sort); + @Query("sort") String sort, @Query("include_over_18") int nsfw); @GET("search.json?include_over_18=1&raw_json=1&type=link") Call searchPostsOauth(@Query("q") String query, @Query("after") String after, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/UserListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/UserListingFragment.java index 4f0f6720..d39f0d0e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/UserListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/UserListingFragment.java @@ -80,6 +80,9 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato @Named("sort_type") SharedPreferences mSortTypeSharedPreferences; @Inject + @Named("nsfw_and_spoiler") + SharedPreferences mNsfwAndSpoilerSharedPreferences; + @Inject CustomThemeWrapper customThemeWrapper; private LinearLayoutManager mLinearLayoutManager; private String mQuery; @@ -124,6 +127,7 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato String accountName = getArguments().getString(EXTRA_ACCOUNT_NAME); String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_SEARCH_USER, 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 UserListingRecyclerViewAdapter(getActivity(), mOauthRetrofit, mRetrofit, customThemeWrapper, accessToken, accountName, mRedditDataRoomDatabase.subscribedUserDao(), @@ -132,11 +136,11 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato mUserListingRecyclerView.setAdapter(mAdapter); UserListingViewModel.Factory factory = new UserListingViewModel.Factory(mRetrofit, mQuery, - sortType); + sortType, nsfw); mUserListingViewModel = new ViewModelProvider(this, factory).get(UserListingViewModel.class); - mUserListingViewModel.getUsers().observe(this, UserData -> mAdapter.submitList(UserData)); + mUserListingViewModel.getUsers().observe(getViewLifecycleOwner(), UserData -> mAdapter.submitList(UserData)); - mUserListingViewModel.hasUser().observe(this, hasUser -> { + mUserListingViewModel.hasUser().observe(getViewLifecycleOwner(), hasUser -> { mSwipeRefreshLayout.setRefreshing(false); if (hasUser) { mFetchUserListingInfoLinearLayout.setVisibility(View.GONE); @@ -148,7 +152,7 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato } }); - mUserListingViewModel.getInitialLoadingState().observe(this, networkState -> { + mUserListingViewModel.getInitialLoadingState().observe(getViewLifecycleOwner(), networkState -> { if (networkState.getStatus().equals(NetworkState.Status.SUCCESS)) { mSwipeRefreshLayout.setRefreshing(false); } else if (networkState.getStatus().equals(NetworkState.Status.FAILED)) { @@ -160,7 +164,7 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato } }); - mUserListingViewModel.getPaginationNetworkState().observe(this, networkState -> { + mUserListingViewModel.getPaginationNetworkState().observe(getViewLifecycleOwner(), networkState -> { mAdapter.setNetworkState(networkState); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/User/FetchUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/User/FetchUserData.java index 5d9a35d9..f214bec1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/User/FetchUserData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/User/FetchUserData.java @@ -41,11 +41,11 @@ public class FetchUserData { }); } - public static void fetchUserListingData(Retrofit retrofit, String query, String after, String sortType, + public static void fetchUserListingData(Retrofit retrofit, String query, String after, String sortType, boolean nsfw, FetchUserListingDataListener fetchUserListingDataListener) { RedditAPI api = retrofit.create(RedditAPI.class); - Call userInfo = api.searchUsers(query, after, sortType); + Call userInfo = api.searchUsers(query, after, sortType, nsfw ? 1 : 0); userInfo.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingDataSource.java index a7606588..fc6b1f60 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingDataSource.java @@ -15,6 +15,7 @@ public class UserListingDataSource extends PageKeyedDataSource private Retrofit retrofit; private String query; private SortType sortType; + private boolean nsfw; private MutableLiveData paginationNetworkStateLiveData; private MutableLiveData initialLoadStateLiveData; @@ -23,10 +24,11 @@ public class UserListingDataSource extends PageKeyedDataSource private PageKeyedDataSource.LoadParams params; private PageKeyedDataSource.LoadCallback callback; - UserListingDataSource(Retrofit retrofit, String query, SortType sortType) { + UserListingDataSource(Retrofit retrofit, String query, SortType sortType, boolean nsfw) { this.retrofit = retrofit; this.query = query; this.sortType = sortType; + this.nsfw = nsfw; paginationNetworkStateLiveData = new MutableLiveData<>(); initialLoadStateLiveData = new MutableLiveData<>(); hasUserLiveData = new MutableLiveData<>(); @@ -48,7 +50,7 @@ public class UserListingDataSource extends PageKeyedDataSource public void loadInitial(@NonNull PageKeyedDataSource.LoadInitialParams params, @NonNull PageKeyedDataSource.LoadInitialCallback callback) { initialLoadStateLiveData.postValue(NetworkState.LOADING); - FetchUserData.fetchUserListingData(retrofit, query, null, sortType.getType().value, + FetchUserData.fetchUserListingData(retrofit, query, null, sortType.getType().value, nsfw, new FetchUserData.FetchUserListingDataListener() { @Override public void onFetchUserListingDataSuccess(ArrayList UserData, String after) { @@ -83,7 +85,7 @@ public class UserListingDataSource extends PageKeyedDataSource return; } - FetchUserData.fetchUserListingData(retrofit, query, params.key, sortType.getType().value, + FetchUserData.fetchUserListingData(retrofit, query, params.key, sortType.getType().value, nsfw, new FetchUserData.FetchUserListingDataListener() { @Override public void onFetchUserListingDataSuccess(ArrayList UserData, String after) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingDataSourceFactory.java index e0bf6288..ee0a49c0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingDataSourceFactory.java @@ -11,21 +11,23 @@ public class UserListingDataSourceFactory extends DataSource.Factory { private Retrofit retrofit; private String query; private SortType sortType; + private boolean nsfw; private UserListingDataSource userListingDataSource; private MutableLiveData userListingDataSourceMutableLiveData; - UserListingDataSourceFactory(Retrofit retrofit, String query, SortType sortType) { + UserListingDataSourceFactory(Retrofit retrofit, String query, SortType sortType, boolean nsfw) { this.retrofit = retrofit; this.query = query; this.sortType = sortType; + this.nsfw = nsfw; userListingDataSourceMutableLiveData = new MutableLiveData<>(); } @NonNull @Override public DataSource create() { - userListingDataSource = new UserListingDataSource(retrofit, query, sortType); + userListingDataSource = new UserListingDataSource(retrofit, query, sortType, nsfw); userListingDataSourceMutableLiveData.postValue(userListingDataSource); return userListingDataSource; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingViewModel.java index d15bbd62..f2816f3c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingViewModel.java @@ -21,8 +21,8 @@ public class UserListingViewModel extends ViewModel { private LiveData> users; private MutableLiveData sortTypeLiveData; - public UserListingViewModel(Retrofit retrofit, String query, SortType sortType) { - userListingDataSourceFactory = new UserListingDataSourceFactory(retrofit, query, sortType); + public UserListingViewModel(Retrofit retrofit, String query, SortType sortType, boolean nsfw) { + userListingDataSourceFactory = new UserListingDataSourceFactory(retrofit, query, sortType, nsfw); initialLoadingState = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(), UserListingDataSource::getInitialLoadStateLiveData); @@ -78,17 +78,19 @@ public class UserListingViewModel extends ViewModel { private Retrofit retrofit; private String query; private SortType sortType; + private boolean nsfw; - public Factory(Retrofit retrofit, String query, SortType sortType) { + public Factory(Retrofit retrofit, String query, SortType sortType, boolean nsfw) { this.retrofit = retrofit; this.query = query; this.sortType = sortType; + this.nsfw = nsfw; } @NonNull @Override public T create(@NonNull Class modelClass) { - return (T) new UserListingViewModel(retrofit, query, sortType); + return (T) new UserListingViewModel(retrofit, query, sortType, nsfw); } } } -- cgit v1.2.3