aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml/docilealligator
diff options
context:
space:
mode:
authorDocile-Alligator <25734209+Docile-Alligator@users.noreply.github.com>2024-01-27 23:41:07 +0000
committerDocile-Alligator <25734209+Docile-Alligator@users.noreply.github.com>2024-01-27 23:41:07 +0000
commit4a2a277af9bd81f157b2bdcf6041f3f4343fce09 (patch)
tree22adcfbdf82978c1f9beb018db4c8aad4c0bcbb1 /app/src/main/java/ml/docilealligator
parent650cfaf924130704ecf5efa31ae4003e633b7599 (diff)
downloadinfinity-for-reddit-4a2a277af9bd81f157b2bdcf6041f3f4343fce09.tar
infinity-for-reddit-4a2a277af9bd81f157b2bdcf6041f3f4343fce09.tar.gz
infinity-for-reddit-4a2a277af9bd81f157b2bdcf6041f3f4343fce09.tar.bz2
infinity-for-reddit-4a2a277af9bd81f157b2bdcf6041f3f4343fce09.tar.lz
infinity-for-reddit-4a2a277af9bd81f157b2bdcf6041f3f4343fce09.tar.xz
infinity-for-reddit-4a2a277af9bd81f157b2bdcf6041f3f4343fce09.tar.zst
infinity-for-reddit-4a2a277af9bd81f157b2bdcf6041f3f4343fce09.zip
Fix unable to search subreddits.
Diffstat (limited to '')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubredditListingFragment.java2
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/FetchSubredditData.java11
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditListingDataSource.java16
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditListingDataSourceFactory.java11
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditListingViewModel.java14
5 files changed, 39 insertions, 15 deletions
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<String, String> headers = APIUtils.getOAuthHeader(accessToken);
+ Map<String, String> map = new HashMap<>();
+ Map<String, String> headers = accountName.equals(Account.ANONYMOUS_ACCOUNT) ? Collections.unmodifiableMap(map) : APIUtils.getOAuthHeader(accessToken);
Call<String> subredditDataCall = api.searchSubreddits(query, after, sortType, nsfw ? 1 : 0, headers);
subredditDataCall.enqueue(new Callback<String>() {
@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<String, Subr
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;
private final MutableLiveData<NetworkState> paginationNetworkStateLiveData;
@@ -25,11 +29,13 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
private LoadParams<String> params;
private LoadCallback<String, SubredditData> 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<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(), accessToken, nsfw,
- new FetchSubredditData.FetchSubredditListingDataListener() {
+ FetchSubredditData.fetchSubredditListingData(retrofit, query, null, sortType.getType(), accessToken,
+ accountName, nsfw, new FetchSubredditData.FetchSubredditListingDataListener() {
@Override
public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) {
if (subredditData.size() == 0) {
@@ -87,8 +93,8 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
return;
}
- FetchSubredditData.fetchSubredditListingData(retrofit, query, params.key, sortType.getType(), accessToken, nsfw,
- new FetchSubredditData.FetchSubredditListingDataListener() {
+ FetchSubredditData.fetchSubredditListingData(retrofit, query, params.key, sortType.getType(), accessToken,
+ accountName, nsfw, new FetchSubredditData.FetchSubredditListingDataListener() {
@Override
public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> 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<SubredditListingDataSource> 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<PagedList<SubredditData>> subreddits;
private final MutableLiveData<SortType> 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 extends ViewModel> T create(@NonNull Class<T> modelClass) {
- return (T) new SubredditListingViewModel(retrofit, query, sortType, accessToken, nsfw);
+ return (T) new SubredditListingViewModel(retrofit, query, sortType, accessToken, accountName, nsfw);
}
}
}