From 4a2a277af9bd81f157b2bdcf6041f3f4343fce09 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:41:07 -0500 Subject: Fix unable to search subreddits. --- .../fragments/SubredditListingFragment.java | 2 +- .../infinityforreddit/subreddit/FetchSubredditData.java | 11 ++++++++--- .../subreddit/SubredditListingDataSource.java | 16 +++++++++++----- .../subreddit/SubredditListingDataSourceFactory.java | 11 +++++++++-- .../subreddit/SubredditListingViewModel.java | 14 ++++++++++---- 5 files changed, 39 insertions(+), 15 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubredditListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubredditListingFragment.java index 0cf8b4cb..df44a605 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubredditListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubredditListingFragment.java @@ -180,7 +180,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun } SubredditListingViewModel.Factory factory = new SubredditListingViewModel.Factory( - mOauthRetrofit, query, sortType, accessToken, nsfw); + mOauthRetrofit, query, sortType, accessToken, accountName, nsfw); mSubredditListingViewModel = new ViewModelProvider(this, factory).get(SubredditListingViewModel.class); mSubredditListingViewModel.getSubreddits().observe(getViewLifecycleOwner(), 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 4d02a154..d59c14c6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/FetchSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/FetchSubredditData.java @@ -1,11 +1,15 @@ package ml.docilealligator.infinityforreddit.subreddit; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.Map; import ml.docilealligator.infinityforreddit.SortType; +import ml.docilealligator.infinityforreddit.account.Account; import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.utils.APIUtils; import retrofit2.Call; @@ -51,11 +55,12 @@ public class FetchSubredditData { }); } - static void fetchSubredditListingData(Retrofit retrofit, String query, String after, SortType.Type sortType, String accessToken, - boolean nsfw, final FetchSubredditListingDataListener fetchSubredditListingDataListener) { + static void fetchSubredditListingData(Retrofit retrofit, String query, String after, SortType.Type sortType, @Nullable String accessToken, + @NonNull String accountName, boolean nsfw, final FetchSubredditListingDataListener fetchSubredditListingDataListener) { RedditAPI api = retrofit.create(RedditAPI.class); - Map headers = APIUtils.getOAuthHeader(accessToken); + Map map = new HashMap<>(); + Map headers = accountName.equals(Account.ANONYMOUS_ACCOUNT) ? Collections.unmodifiableMap(map) : APIUtils.getOAuthHeader(accessToken); Call subredditDataCall = api.searchSubreddits(query, after, sortType, nsfw ? 1 : 0, headers); subredditDataCall.enqueue(new Callback() { @Override 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 7ffa9a5c..813dd71b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditListingDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditListingDataSource.java @@ -1,6 +1,7 @@ package ml.docilealligator.infinityforreddit.subreddit; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.lifecycle.MutableLiveData; import androidx.paging.PageKeyedDataSource; @@ -15,7 +16,10 @@ public class SubredditListingDataSource extends PageKeyedDataSource paginationNetworkStateLiveData; @@ -25,11 +29,13 @@ public class SubredditListingDataSource extends PageKeyedDataSource params; private LoadCallback callback; - SubredditListingDataSource(Retrofit retrofit, String query, SortType sortType, String accessToken, boolean nsfw) { + SubredditListingDataSource(Retrofit retrofit, String query, SortType sortType, @Nullable String accessToken, + @NonNull String accountName, boolean nsfw) { this.retrofit = retrofit; this.query = query; this.sortType = sortType; this.accessToken = accessToken; + this.accountName = accountName; this.nsfw = nsfw; paginationNetworkStateLiveData = new MutableLiveData<>(); initialLoadStateLiveData = new MutableLiveData<>(); @@ -52,8 +58,8 @@ public class SubredditListingDataSource extends PageKeyedDataSource params, @NonNull LoadInitialCallback callback) { initialLoadStateLiveData.postValue(NetworkState.LOADING); - FetchSubredditData.fetchSubredditListingData(retrofit, query, null, sortType.getType(), accessToken, nsfw, - new FetchSubredditData.FetchSubredditListingDataListener() { + FetchSubredditData.fetchSubredditListingData(retrofit, query, null, sortType.getType(), accessToken, + accountName, nsfw, new FetchSubredditData.FetchSubredditListingDataListener() { @Override public void onFetchSubredditListingDataSuccess(ArrayList subredditData, String after) { if (subredditData.size() == 0) { @@ -87,8 +93,8 @@ public class SubredditListingDataSource extends PageKeyedDataSource subredditData, String after) { callback.onResult(subredditData, 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 130a7bdf..fadf484a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditListingDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditListingDataSourceFactory.java @@ -1,6 +1,7 @@ package ml.docilealligator.infinityforreddit.subreddit; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.lifecycle.MutableLiveData; import androidx.paging.DataSource; @@ -11,17 +12,22 @@ public class SubredditListingDataSourceFactory extends DataSource.Factory { private final Retrofit retrofit; private final String query; private SortType sortType; + @Nullable private final String accessToken; + @NonNull + private final String accountName; private final boolean nsfw; private SubredditListingDataSource subredditListingDataSource; private final MutableLiveData subredditListingDataSourceMutableLiveData; - SubredditListingDataSourceFactory(Retrofit retrofit, String query, SortType sortType, String accessToken, boolean nsfw) { + SubredditListingDataSourceFactory(Retrofit retrofit, String query, SortType sortType, @Nullable String accessToken, + @NonNull String accountName, boolean nsfw) { this.retrofit = retrofit; this.query = query; this.sortType = sortType; this.accessToken = accessToken; + this.accountName = accountName; this.nsfw = nsfw; subredditListingDataSourceMutableLiveData = new MutableLiveData<>(); } @@ -29,7 +35,8 @@ public class SubredditListingDataSourceFactory extends DataSource.Factory { @NonNull @Override public DataSource create() { - subredditListingDataSource = new SubredditListingDataSource(retrofit, query, sortType, accessToken, nsfw); + subredditListingDataSource = new SubredditListingDataSource(retrofit, query, sortType, accessToken, + accountName, nsfw); 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 137036db..79d9b217 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditListingViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditListingViewModel.java @@ -1,6 +1,7 @@ package ml.docilealligator.infinityforreddit.subreddit; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.Transformations; @@ -21,8 +22,8 @@ public class SubredditListingViewModel extends ViewModel { private final LiveData> subreddits; private final MutableLiveData sortTypeLiveData; - public SubredditListingViewModel(Retrofit retrofit, String query, SortType sortType, String accessToken, boolean nsfw) { - subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType, accessToken, nsfw); + public SubredditListingViewModel(Retrofit retrofit, String query, SortType sortType, @Nullable String accessToken, @NonNull String accountName, boolean nsfw) { + subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType, accessToken, accountName, nsfw); initialLoadingState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(), SubredditListingDataSource::getInitialLoadStateLiveData); @@ -77,21 +78,26 @@ public class SubredditListingViewModel extends ViewModel { private final Retrofit retrofit; private final String query; private final SortType sortType; + @Nullable private final String accessToken; + @NonNull + private final String accountName; private final boolean nsfw; - public Factory(Retrofit retrofit, String query, SortType sortType, String accessToken, boolean nsfw) { + public Factory(Retrofit retrofit, String query, SortType sortType, @Nullable String accessToken, + @NonNull String accountName, boolean nsfw) { this.retrofit = retrofit; this.query = query; this.sortType = sortType; this.accessToken = accessToken; + this.accountName = accountName; this.nsfw = nsfw; } @NonNull @Override public T create(@NonNull Class modelClass) { - return (T) new SubredditListingViewModel(retrofit, query, sortType, accessToken, nsfw); + return (T) new SubredditListingViewModel(retrofit, query, sortType, accessToken, accountName, nsfw); } } } -- cgit v1.2.3