From 9e53dbade6a2e41a4fc79ab745bf038f97e771d5 Mon Sep 17 00:00:00 2001 From: vlakreeh Date: Wed, 29 Jul 2020 10:59:05 -0400 Subject: Add authentication for subreddit searching --- .../ml/docilealligator/infinityforreddit/API/RedditAPI.java | 3 ++- .../infinityforreddit/Fragment/SubredditListingFragment.java | 2 +- .../infinityforreddit/Subreddit/FetchSubredditData.java | 9 +++++++-- .../Subreddit/SubredditListingDataSource.java | 8 +++++--- .../Subreddit/SubredditListingDataSourceFactory.java | 6 ++++-- .../infinityforreddit/Subreddit/SubredditListingViewModel.java | 10 ++++++---- 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 searchSubreddits(@Query("q") String subredditName, @Query("after") String after, - @Query("sort") String sort); + @Query("sort") String sort, + @HeaderMap Map headers); @GET("search.json?include_over_18=1&raw_json=1&type=user") Call 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 subredditDataCall = api.searchSubreddits(query, after, sortType); + Map headers = accessToken != null ? APIUtils.getOAuthHeader(accessToken) : ImmutableMap.of(); + Call subredditDataCall = api.searchSubreddits(query, after, sortType, headers); subredditDataCall.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response 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 paginationNetworkStateLiveData; private MutableLiveData initialLoadStateLiveData; @@ -23,10 +24,11 @@ public class SubredditListingDataSource extends PageKeyedDataSource params; private LoadCallback 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 params, @NonNull LoadInitialCallback 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, String after) { @@ -83,7 +85,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource 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 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> subreddits; private MutableLiveData 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 create(@NonNull Class modelClass) { - return (T) new SubredditListingViewModel(retrofit, query, sortType); + return (T) new SubredditListingViewModel(retrofit, query, sortType, accessToken); } } } -- cgit v1.2.3