Don't search nsfw users when nsfw is off.

This commit is contained in:
Alex Ning 2020-09-30 23:15:32 +08:00
parent 773c82a2b7
commit 13bed5d234
6 changed files with 28 additions and 18 deletions

View File

@ -128,9 +128,9 @@ public interface RedditAPI {
@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")
@GET("search.json?raw_json=1&type=user")
Call<String> 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<String> searchPostsOauth(@Query("q") String query, @Query("after") String after,

View File

@ -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);
});

View File

@ -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<String> userInfo = api.searchUsers(query, after, sortType);
Call<String> userInfo = api.searchUsers(query, after, sortType, nsfw ? 1 : 0);
userInfo.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {

View File

@ -15,6 +15,7 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData>
private Retrofit retrofit;
private String query;
private SortType sortType;
private boolean nsfw;
private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
private MutableLiveData<NetworkState> initialLoadStateLiveData;
@ -23,10 +24,11 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData>
private PageKeyedDataSource.LoadParams<String> params;
private PageKeyedDataSource.LoadCallback<String, UserData> 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<String, UserData>
public void loadInitial(@NonNull PageKeyedDataSource.LoadInitialParams<String> params, @NonNull PageKeyedDataSource.LoadInitialCallback<String, UserData> 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> UserData, String after) {
@ -83,7 +85,7 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData>
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> UserData, String after) {

View File

@ -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<UserListingDataSource> 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;
}

View File

@ -21,8 +21,8 @@ public class UserListingViewModel extends ViewModel {
private LiveData<PagedList<UserData>> users;
private MutableLiveData<SortType> 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 extends ViewModel> T create(@NonNull Class<T> modelClass) {
return (T) new UserListingViewModel(retrofit, query, sortType);
return (T) new UserListingViewModel(retrofit, query, sortType, nsfw);
}
}
}