diff options
author | vlakreeh <zeb@zebulon.dev> | 2020-07-29 14:59:05 +0000 |
---|---|---|
committer | vlakreeh <zeb@zebulon.dev> | 2020-07-29 14:59:05 +0000 |
commit | 9e53dbade6a2e41a4fc79ab745bf038f97e771d5 (patch) | |
tree | d8100aa9272efdc7e0f214637df6cb35c59c883d | |
parent | cdf7eaba266dcbd80df8264e3400adebb402d7c8 (diff) | |
download | infinity-for-reddit-9e53dbade6a2e41a4fc79ab745bf038f97e771d5.tar infinity-for-reddit-9e53dbade6a2e41a4fc79ab745bf038f97e771d5.tar.gz infinity-for-reddit-9e53dbade6a2e41a4fc79ab745bf038f97e771d5.tar.bz2 infinity-for-reddit-9e53dbade6a2e41a4fc79ab745bf038f97e771d5.tar.lz infinity-for-reddit-9e53dbade6a2e41a4fc79ab745bf038f97e771d5.tar.xz infinity-for-reddit-9e53dbade6a2e41a4fc79ab745bf038f97e771d5.tar.zst infinity-for-reddit-9e53dbade6a2e41a4fc79ab745bf038f97e771d5.zip |
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 fbfdd32a..8bdcbbfa 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); } } } |