mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-11-07 11:17:25 +01:00
Merge pull request #170 from vlakreeh/master
Add authentication for subreddit searching
This commit is contained in:
commit
70cc44b0dc
@ -125,7 +125,8 @@ public interface RedditAPI {
|
|||||||
|
|
||||||
@GET("subreddits/search.json?include_over_18=1&raw_json=1")
|
@GET("subreddits/search.json?include_over_18=1&raw_json=1")
|
||||||
Call<String> searchSubreddits(@Query("q") String subredditName, @Query("after") String after,
|
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")
|
@GET("search.json?include_over_18=1&raw_json=1&type=user")
|
||||||
Call<String> searchUsers(@Query("q") String profileName, @Query("after") String after,
|
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);
|
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 = new ViewModelProvider(this, factory).get(SubredditListingViewModel.class);
|
||||||
mSubredditListingViewModel.getSubreddits().observe(this, subredditData -> mAdapter.submitList(subredditData));
|
mSubredditListingViewModel.getSubreddits().observe(this, subredditData -> mAdapter.submitList(subredditData));
|
||||||
|
|
||||||
|
@ -2,9 +2,13 @@ package ml.docilealligator.infinityforreddit.Subreddit;
|
|||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
|
import com.facebook.common.internal.ImmutableMap;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import ml.docilealligator.infinityforreddit.API.RedditAPI;
|
import ml.docilealligator.infinityforreddit.API.RedditAPI;
|
||||||
|
import ml.docilealligator.infinityforreddit.Utils.APIUtils;
|
||||||
import retrofit2.Call;
|
import retrofit2.Call;
|
||||||
import retrofit2.Callback;
|
import retrofit2.Callback;
|
||||||
import retrofit2.Response;
|
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) {
|
final FetchSubredditListingDataListener fetchSubredditListingDataListener) {
|
||||||
RedditAPI api = retrofit.create(RedditAPI.class);
|
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>() {
|
subredditDataCall.enqueue(new Callback<String>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
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 Retrofit retrofit;
|
||||||
private String query;
|
private String query;
|
||||||
private SortType sortType;
|
private SortType sortType;
|
||||||
|
private String accessToken;
|
||||||
|
|
||||||
private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
|
private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
|
||||||
private MutableLiveData<NetworkState> initialLoadStateLiveData;
|
private MutableLiveData<NetworkState> initialLoadStateLiveData;
|
||||||
@ -23,10 +24,11 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
|
|||||||
private LoadParams<String> params;
|
private LoadParams<String> params;
|
||||||
private LoadCallback<String, SubredditData> callback;
|
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.retrofit = retrofit;
|
||||||
this.query = query;
|
this.query = query;
|
||||||
this.sortType = sortType;
|
this.sortType = sortType;
|
||||||
|
this.accessToken = accessToken;
|
||||||
paginationNetworkStateLiveData = new MutableLiveData<>();
|
paginationNetworkStateLiveData = new MutableLiveData<>();
|
||||||
initialLoadStateLiveData = new MutableLiveData<>();
|
initialLoadStateLiveData = new MutableLiveData<>();
|
||||||
hasSubredditLiveData = 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) {
|
public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull LoadInitialCallback<String, SubredditData> callback) {
|
||||||
initialLoadStateLiveData.postValue(NetworkState.LOADING);
|
initialLoadStateLiveData.postValue(NetworkState.LOADING);
|
||||||
|
|
||||||
FetchSubredditData.fetchSubredditListingData(retrofit, query, null, sortType.getType().value,
|
FetchSubredditData.fetchSubredditListingData(retrofit, query, null, sortType.getType().value, accessToken,
|
||||||
new FetchSubredditData.FetchSubredditListingDataListener() {
|
new FetchSubredditData.FetchSubredditListingDataListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) {
|
public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) {
|
||||||
@ -83,7 +85,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
FetchSubredditData.fetchSubredditListingData(retrofit, query, params.key, sortType.getType().value,
|
FetchSubredditData.fetchSubredditListingData(retrofit, query, params.key, sortType.getType().value, accessToken,
|
||||||
new FetchSubredditData.FetchSubredditListingDataListener() {
|
new FetchSubredditData.FetchSubredditListingDataListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) {
|
public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) {
|
||||||
|
@ -11,21 +11,23 @@ public class SubredditListingDataSourceFactory extends DataSource.Factory {
|
|||||||
private Retrofit retrofit;
|
private Retrofit retrofit;
|
||||||
private String query;
|
private String query;
|
||||||
private SortType sortType;
|
private SortType sortType;
|
||||||
|
private String accessToken;
|
||||||
|
|
||||||
private SubredditListingDataSource subredditListingDataSource;
|
private SubredditListingDataSource subredditListingDataSource;
|
||||||
private MutableLiveData<SubredditListingDataSource> subredditListingDataSourceMutableLiveData;
|
private MutableLiveData<SubredditListingDataSource> subredditListingDataSourceMutableLiveData;
|
||||||
|
|
||||||
SubredditListingDataSourceFactory(Retrofit retrofit, String query, SortType sortType) {
|
SubredditListingDataSourceFactory(Retrofit retrofit, String query, SortType sortType, String accessToken) {
|
||||||
this.retrofit = retrofit;
|
this.retrofit = retrofit;
|
||||||
this.query = query;
|
this.query = query;
|
||||||
this.sortType = sortType;
|
this.sortType = sortType;
|
||||||
|
this.accessToken = accessToken;
|
||||||
subredditListingDataSourceMutableLiveData = new MutableLiveData<>();
|
subredditListingDataSourceMutableLiveData = new MutableLiveData<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public DataSource create() {
|
public DataSource create() {
|
||||||
subredditListingDataSource = new SubredditListingDataSource(retrofit, query, sortType);
|
subredditListingDataSource = new SubredditListingDataSource(retrofit, query, sortType, accessToken);
|
||||||
subredditListingDataSourceMutableLiveData.postValue(subredditListingDataSource);
|
subredditListingDataSourceMutableLiveData.postValue(subredditListingDataSource);
|
||||||
return subredditListingDataSource;
|
return subredditListingDataSource;
|
||||||
}
|
}
|
||||||
|
@ -21,8 +21,8 @@ public class SubredditListingViewModel extends ViewModel {
|
|||||||
private LiveData<PagedList<SubredditData>> subreddits;
|
private LiveData<PagedList<SubredditData>> subreddits;
|
||||||
private MutableLiveData<SortType> sortTypeLiveData;
|
private MutableLiveData<SortType> sortTypeLiveData;
|
||||||
|
|
||||||
public SubredditListingViewModel(Retrofit retrofit, String query, SortType sortType) {
|
public SubredditListingViewModel(Retrofit retrofit, String query, SortType sortType, String accessToken) {
|
||||||
subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType);
|
subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType, accessToken);
|
||||||
|
|
||||||
initialLoadingState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(),
|
initialLoadingState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(),
|
||||||
SubredditListingDataSource::getInitialLoadStateLiveData);
|
SubredditListingDataSource::getInitialLoadStateLiveData);
|
||||||
@ -78,17 +78,19 @@ public class SubredditListingViewModel extends ViewModel {
|
|||||||
private Retrofit retrofit;
|
private Retrofit retrofit;
|
||||||
private String query;
|
private String query;
|
||||||
private SortType sortType;
|
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.retrofit = retrofit;
|
||||||
this.query = query;
|
this.query = query;
|
||||||
this.sortType = sortType;
|
this.sortType = sortType;
|
||||||
|
this.accessToken = accessToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
|
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