diff options
author | Docile-Alligator <chineseperson5@gmail.com> | 2020-09-08 03:14:56 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-08 03:14:56 +0000 |
commit | 70cc44b0dc6f116d6be76f7b80b961cb758bcaf4 (patch) | |
tree | 936c108cfd2bb4db035a25ab743b98257ecdc623 | |
parent | fc40fb3b243c259347dda749230ef042d9cb3705 (diff) | |
parent | 9e53dbade6a2e41a4fc79ab745bf038f97e771d5 (diff) | |
download | infinity-for-reddit-70cc44b0dc6f116d6be76f7b80b961cb758bcaf4.tar infinity-for-reddit-70cc44b0dc6f116d6be76f7b80b961cb758bcaf4.tar.gz infinity-for-reddit-70cc44b0dc6f116d6be76f7b80b961cb758bcaf4.tar.bz2 infinity-for-reddit-70cc44b0dc6f116d6be76f7b80b961cb758bcaf4.tar.lz infinity-for-reddit-70cc44b0dc6f116d6be76f7b80b961cb758bcaf4.tar.xz infinity-for-reddit-70cc44b0dc6f116d6be76f7b80b961cb758bcaf4.tar.zst infinity-for-reddit-70cc44b0dc6f116d6be76f7b80b961cb758bcaf4.zip |
Merge pull request #170 from vlakreeh/master
Add authentication for subreddit searching
6 files changed, 25 insertions, 13 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 1da58727..003ad955 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/API/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/API/RedditAPI.java @@ -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, 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 3fefdb2d..12a7c067 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubredditListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubredditListingFragment.java @@ -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)); 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 c6bc2914..3f720c67 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/FetchSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/FetchSubredditData.java @@ -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) { 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 05a0b7d2..f75205c1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingDataSource.java @@ -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) { 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 f18b3691..dfdafbca 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingDataSourceFactory.java @@ -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; } 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 ed8df032..df34c349 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<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); } } } |