mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-12-30 12:57:12 +01:00
Add authentication for subreddit searching
This commit is contained in:
parent
cdf7eaba26
commit
9e53dbade6
@ -125,7 +125,8 @@ public interface RedditAPI {
|
||||
|
||||
@GET("subreddits/search.json?include_over_18=1&raw_json=1")
|
||||
Call<String> searchSubreddits(@Query("q") String subredditName, @Query("after") String after,
|
||||
@Query("sort") String sort);
|
||||
@Query("sort") String sort,
|
||||
@HeaderMap Map<String, String> headers);
|
||||
|
||||
@GET("search.json?include_over_18=1&raw_json=1&type=user")
|
||||
Call<String> searchUsers(@Query("q") String profileName, @Query("after") String after,
|
||||
|
@ -152,7 +152,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
|
||||
|
||||
mSubredditListingRecyclerView.setAdapter(mAdapter);
|
||||
|
||||
SubredditListingViewModel.Factory factory = new SubredditListingViewModel.Factory(mRetrofit, query, sortType);
|
||||
SubredditListingViewModel.Factory factory = new SubredditListingViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, query, sortType, accessToken);
|
||||
mSubredditListingViewModel = new ViewModelProvider(this, factory).get(SubredditListingViewModel.class);
|
||||
mSubredditListingViewModel.getSubreddits().observe(this, subredditData -> mAdapter.submitList(subredditData));
|
||||
|
||||
|
@ -2,9 +2,13 @@ package ml.docilealligator.infinityforreddit.Subreddit;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.facebook.common.internal.ImmutableMap;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
|
||||
import ml.docilealligator.infinityforreddit.API.RedditAPI;
|
||||
import ml.docilealligator.infinityforreddit.Utils.APIUtils;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
@ -42,11 +46,12 @@ public class FetchSubredditData {
|
||||
});
|
||||
}
|
||||
|
||||
static void fetchSubredditListingData(Retrofit retrofit, String query, String after, String sortType,
|
||||
static void fetchSubredditListingData(Retrofit retrofit, String query, String after, String sortType, String accessToken,
|
||||
final FetchSubredditListingDataListener fetchSubredditListingDataListener) {
|
||||
RedditAPI api = retrofit.create(RedditAPI.class);
|
||||
|
||||
Call<String> subredditDataCall = api.searchSubreddits(query, after, sortType);
|
||||
Map<String, String> headers = accessToken != null ? APIUtils.getOAuthHeader(accessToken) : ImmutableMap.of();
|
||||
Call<String> subredditDataCall = api.searchSubreddits(query, after, sortType, headers);
|
||||
subredditDataCall.enqueue(new Callback<String>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
||||
|
@ -15,6 +15,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
|
||||
private Retrofit retrofit;
|
||||
private String query;
|
||||
private SortType sortType;
|
||||
private String accessToken;
|
||||
|
||||
private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
|
||||
private MutableLiveData<NetworkState> initialLoadStateLiveData;
|
||||
@ -23,10 +24,11 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
|
||||
private LoadParams<String> params;
|
||||
private LoadCallback<String, SubredditData> callback;
|
||||
|
||||
SubredditListingDataSource(Retrofit retrofit, String query, SortType sortType) {
|
||||
SubredditListingDataSource(Retrofit retrofit, String query, SortType sortType, String accessToken) {
|
||||
this.retrofit = retrofit;
|
||||
this.query = query;
|
||||
this.sortType = sortType;
|
||||
this.accessToken = accessToken;
|
||||
paginationNetworkStateLiveData = new MutableLiveData<>();
|
||||
initialLoadStateLiveData = new MutableLiveData<>();
|
||||
hasSubredditLiveData = new MutableLiveData<>();
|
||||
@ -48,7 +50,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,
|
||||
FetchSubredditData.fetchSubredditListingData(retrofit, query, null, sortType.getType().value, accessToken,
|
||||
new FetchSubredditData.FetchSubredditListingDataListener() {
|
||||
@Override
|
||||
public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) {
|
||||
@ -83,7 +85,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
|
||||
return;
|
||||
}
|
||||
|
||||
FetchSubredditData.fetchSubredditListingData(retrofit, query, params.key, sortType.getType().value,
|
||||
FetchSubredditData.fetchSubredditListingData(retrofit, query, params.key, sortType.getType().value, accessToken,
|
||||
new FetchSubredditData.FetchSubredditListingDataListener() {
|
||||
@Override
|
||||
public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) {
|
||||
|
@ -11,21 +11,23 @@ public class SubredditListingDataSourceFactory extends DataSource.Factory {
|
||||
private Retrofit retrofit;
|
||||
private String query;
|
||||
private SortType sortType;
|
||||
private String accessToken;
|
||||
|
||||
private SubredditListingDataSource subredditListingDataSource;
|
||||
private MutableLiveData<SubredditListingDataSource> subredditListingDataSourceMutableLiveData;
|
||||
|
||||
SubredditListingDataSourceFactory(Retrofit retrofit, String query, SortType sortType) {
|
||||
SubredditListingDataSourceFactory(Retrofit retrofit, String query, SortType sortType, String accessToken) {
|
||||
this.retrofit = retrofit;
|
||||
this.query = query;
|
||||
this.sortType = sortType;
|
||||
this.accessToken = accessToken;
|
||||
subredditListingDataSourceMutableLiveData = new MutableLiveData<>();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public DataSource create() {
|
||||
subredditListingDataSource = new SubredditListingDataSource(retrofit, query, sortType);
|
||||
subredditListingDataSource = new SubredditListingDataSource(retrofit, query, sortType, accessToken);
|
||||
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) {
|
||||
subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType);
|
||||
public SubredditListingViewModel(Retrofit retrofit, String query, SortType sortType, String accessToken) {
|
||||
subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType, accessToken);
|
||||
|
||||
initialLoadingState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(),
|
||||
SubredditListingDataSource::getInitialLoadStateLiveData);
|
||||
@ -78,17 +78,19 @@ public class SubredditListingViewModel extends ViewModel {
|
||||
private Retrofit retrofit;
|
||||
private String query;
|
||||
private SortType sortType;
|
||||
private String accessToken;
|
||||
|
||||
public Factory(Retrofit retrofit, String query, SortType sortType) {
|
||||
public Factory(Retrofit retrofit, String query, SortType sortType, String accessToken) {
|
||||
this.retrofit = retrofit;
|
||||
this.query = query;
|
||||
this.sortType = sortType;
|
||||
this.accessToken = accessToken;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
|
||||
return (T) new SubredditListingViewModel(retrofit, query, sortType);
|
||||
return (T) new SubredditListingViewModel(retrofit, query, sortType, accessToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user