Basic search functionality for communities

Signed-off-by: Balazs Toldi <balazs@toldi.eu>
This commit is contained in:
Balazs Toldi 2023-07-25 07:48:16 +02:00
parent ad7083b423
commit e5f2e2f8a2
No known key found for this signature in database
GPG Key ID: 6C7D440036F99D58
7 changed files with 42 additions and 41 deletions

View File

@ -80,7 +80,21 @@ public interface LemmyAPI {
); );
@GET("api/v3/search") @GET("api/v3/search")
ListenableFuture<Response<String>> search( ListenableFuture<Response<String>> searchLive(
@Query("q") String q,
@Query("community_id") Integer communityId,
@Query("community_name") String communityName,
@Query("creator_id") Integer creatorId,
@Query("type_") String type,
@Query("sort") String sort,
@Query("listing_type") String listingType,
@Query("page") Integer page,
@Query("limit") Integer limit,
@Query("auth") String auth
);
@GET("api/v3/search")
Call<String> search(
@Query("q") String q, @Query("q") String q,
@Query("community_id") Integer communityId, @Query("community_id") Integer communityId,
@Query("community_name") String communityName, @Query("community_name") String communityName,

View File

@ -460,7 +460,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_SEARCH_POST, SortType.Type.TOP_ALL.value); String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_SEARCH_POST, SortType.Type.TOP_ALL.value);
String sortTime = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TIME_SEARCH_POST, SortType.Time.ALL.name()); String sortTime = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TIME_SEARCH_POST, SortType.Time.ALL.name());
sortType = new SortType(SortType.Type.fromValue(sort), SortType.Time.valueOf(sortTime)); SortType.Type st = SortType.Type.fromValue(sort);
sortType = new SortType(st == null ? SortType.Type.TOP_ALL : st, SortType.Time.valueOf(sortTime));
postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_SEARCH_POST, defaultPostLayout); postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_SEARCH_POST, defaultPostLayout);
mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mRetrofit.getRetrofit(), mGfycatRetrofit, mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mRetrofit.getRetrofit(), mGfycatRetrofit,

View File

@ -49,7 +49,6 @@ import eu.toldi.infinityforlemmy.customviews.LinearLayoutManagerBugFixed;
import eu.toldi.infinityforlemmy.subreddit.SubredditData; import eu.toldi.infinityforlemmy.subreddit.SubredditData;
import eu.toldi.infinityforlemmy.subreddit.SubredditListingViewModel; import eu.toldi.infinityforlemmy.subreddit.SubredditListingViewModel;
import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils; import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils;
import retrofit2.Retrofit;
/** /**
@ -80,9 +79,6 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
@Named("no_oauth") @Named("no_oauth")
RetrofitHolder mRetrofit; RetrofitHolder mRetrofit;
@Inject @Inject
@Named("oauth")
Retrofit mOauthRetrofit;
@Inject
RedditDataRoomDatabase mRedditDataRoomDatabase; RedditDataRoomDatabase mRedditDataRoomDatabase;
@Inject @Inject
@Named("default") @Named("default")
@ -139,11 +135,11 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN); String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN);
String accountName = getArguments().getString(EXTRA_ACCOUNT_NAME); String accountName = getArguments().getString(EXTRA_ACCOUNT_NAME);
String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_SEARCH_SUBREDDIT, SortType.Type.TOP.value); String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_SEARCH_SUBREDDIT, SortType.Type.TOP_ALL.value);
sortType = new SortType(SortType.Type.valueOf(sort.toUpperCase())); sortType = new SortType(SortType.Type.fromValue(sort));
boolean nsfw = !mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_NSFW_FOREVER, false) && mNsfwAndSpoilerSharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.NSFW_BASE, false); boolean nsfw = !mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_NSFW_FOREVER, false) && mNsfwAndSpoilerSharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.NSFW_BASE, false);
mAdapter = new SubredditListingRecyclerViewAdapter(mActivity, mExecutor, mOauthRetrofit, mRetrofit.getRetrofit(), mAdapter = new SubredditListingRecyclerViewAdapter(mActivity, mExecutor, mRetrofit.getRetrofit(), mRetrofit.getRetrofit(),
mCustomThemeWrapper, accessToken, accountName, mCustomThemeWrapper, accessToken, accountName,
mRedditDataRoomDatabase, getArguments().getBoolean(EXTRA_IS_MULTI_SELECTION, false), mRedditDataRoomDatabase, getArguments().getBoolean(EXTRA_IS_MULTI_SELECTION, false),
new SubredditListingRecyclerViewAdapter.Callback() { new SubredditListingRecyclerViewAdapter.Callback() {
@ -153,7 +149,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
} }
@Override @Override
public void subredditSelected(String subredditName, String communityFullName,String iconUrl) { public void subredditSelected(String subredditName, String communityFullName, String iconUrl) {
if (isGettingSubredditInfo) { if (isGettingSubredditInfo) {
((SearchSubredditsResultActivity) mActivity).getSelectedSubreddit(subredditName, iconUrl); ((SearchSubredditsResultActivity) mActivity).getSelectedSubreddit(subredditName, iconUrl);
} else { } else {

View File

@ -268,7 +268,7 @@ public class PostPagingSource extends ListenableFuturePagingSource<Integer, Post
private ListenableFuture<LoadResult<Integer, Post>> loadSearchPosts(@NonNull LoadParams<Integer> loadParams, LemmyAPI api) { private ListenableFuture<LoadResult<Integer, Post>> loadSearchPosts(@NonNull LoadParams<Integer> loadParams, LemmyAPI api) {
ListenableFuture<Response<String>> searchPosts; ListenableFuture<Response<String>> searchPosts;
searchPosts = api.search(query, null, subredditOrUserName, null, "Posts", sortType.getType().value, "All", loadParams.getKey(), 25, accessToken); searchPosts = api.searchLive(query, null, subredditOrUserName, null, "Posts", sortType.getType().value, "All", loadParams.getKey(), 25, accessToken);
ListenableFuture<LoadResult<Integer, Post>> pageFuture = Futures.transform(searchPosts, this::transformData, executor); ListenableFuture<LoadResult<Integer, Post>> pageFuture = Futures.transform(searchPosts, this::transformData, executor);

View File

@ -1,18 +1,11 @@
package eu.toldi.infinityforlemmy.subreddit; package eu.toldi.infinityforlemmy.subreddit;
import android.text.TextUtils;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import eu.toldi.infinityforlemmy.SortType; import eu.toldi.infinityforlemmy.SortType;
import eu.toldi.infinityforlemmy.apis.LemmyAPI; import eu.toldi.infinityforlemmy.apis.LemmyAPI;
import eu.toldi.infinityforlemmy.apis.RedditAPI;
import eu.toldi.infinityforlemmy.utils.APIUtils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
@ -52,13 +45,14 @@ public class FetchSubredditData {
}); });
} }
static void fetchSubredditListingData(Retrofit retrofit, String query, String after, SortType.Type sortType, String accessToken, static void fetchSubredditListingData(Retrofit retrofit, String query, Integer page, SortType.Type sortType, String accessToken,
boolean nsfw, final FetchSubredditListingDataListener fetchSubredditListingDataListener) { boolean nsfw, final FetchSubredditListingDataListener fetchSubredditListingDataListener) {
RedditAPI api = retrofit.create(RedditAPI.class); LemmyAPI api = retrofit.create(LemmyAPI.class);
Call<String> subredditDataCall = api.search(query, null, null, null, "Communities", sortType.value, "All", page, 25, accessToken);
Map<String, String> map = new HashMap<>();
Map<String, String> headers = accessToken != null ? APIUtils.getOAuthHeader(accessToken) : Collections.unmodifiableMap(map);
Call<String> subredditDataCall = api.searchSubreddits(query, after, sortType, nsfw ? 1 : 0, headers);
subredditDataCall.enqueue(new Callback<String>() { subredditDataCall.enqueue(new Callback<String>() {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {

View File

@ -11,7 +11,6 @@ import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
import eu.toldi.infinityforlemmy.utils.JSONUtils; import eu.toldi.infinityforlemmy.utils.JSONUtils;
import eu.toldi.infinityforlemmy.utils.Utils;
public class ParseSubredditData { public class ParseSubredditData {
public static void parseSubredditData(String response, ParseSubredditDataListener parseSubredditDataListener) { public static void parseSubredditData(String response, ParseSubredditDataListener parseSubredditDataListener) {
@ -32,13 +31,13 @@ public class ParseSubredditData {
String title = community.getString(JSONUtils.TITLE_KEY); String title = community.getString(JSONUtils.TITLE_KEY);
String bannerImageUrl = ""; String bannerImageUrl = "";
if(!community.isNull("banner")){ if (!community.isNull("banner")) {
bannerImageUrl = community.getString("banner"); bannerImageUrl = community.getString("banner");
} }
String iconUrl = ""; String iconUrl = "";
if(!community.isNull("banner")){ if (!community.isNull("icon")) {
bannerImageUrl = community.getString("icon"); iconUrl = community.getString("icon");
} }
int id = community.getInt("id"); int id = community.getInt("id");
String name = community.getString("name"); String name = community.getString("name");
@ -50,8 +49,6 @@ public class ParseSubredditData {
String actorId = community.getString("actor_id"); String actorId = community.getString("actor_id");
boolean local = community.getBoolean("local"); boolean local = community.getBoolean("local");
String icon = community.getString("icon");
String banner = community.getString("banner");
boolean hidden = community.getBoolean("hidden"); boolean hidden = community.getBoolean("hidden");
boolean postingRestrictedToMods = community.getBoolean("posting_restricted_to_mods"); boolean postingRestrictedToMods = community.getBoolean("posting_restricted_to_mods");
int instanceId = community.getInt("instance_id"); int instanceId = community.getInt("instance_id");
@ -138,16 +135,15 @@ public class ParseSubredditData {
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
try { try {
if (!parseFailed) { if (!parseFailed) {
JSONArray children = jsonResponse.getJSONObject(JSONUtils.DATA_KEY) JSONArray children = jsonResponse.getJSONArray("communities");
.getJSONArray(JSONUtils.CHILDREN_KEY);
for (int i = 0; i < children.length(); i++) { for (int i = 0; i < children.length(); i++) {
JSONObject data = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY); JSONObject data = children.getJSONObject(i);
SubredditData subredditData = parseSubredditData(data, nsfw); SubredditData subredditData = parseSubredditData(data, nsfw);
if (subredditData != null) { if (subredditData != null) {
subredditListingData.add(subredditData); subredditListingData.add(subredditData);
} }
} }
after = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); //after = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
} }
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -10,7 +10,7 @@ import eu.toldi.infinityforlemmy.NetworkState;
import eu.toldi.infinityforlemmy.SortType; import eu.toldi.infinityforlemmy.SortType;
import retrofit2.Retrofit; import retrofit2.Retrofit;
public class SubredditListingDataSource extends PageKeyedDataSource<String, SubredditData> { public class SubredditListingDataSource extends PageKeyedDataSource<Integer, SubredditData> {
private Retrofit retrofit; private Retrofit retrofit;
private String query; private String query;
@ -22,8 +22,8 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
private MutableLiveData<NetworkState> initialLoadStateLiveData; private MutableLiveData<NetworkState> initialLoadStateLiveData;
private MutableLiveData<Boolean> hasSubredditLiveData; private MutableLiveData<Boolean> hasSubredditLiveData;
private LoadParams<String> params; private LoadParams<Integer> params;
private LoadCallback<String, SubredditData> callback; private LoadCallback<Integer, SubredditData> callback;
SubredditListingDataSource(Retrofit retrofit, String query, SortType sortType, String accessToken, boolean nsfw) { SubredditListingDataSource(Retrofit retrofit, String query, SortType sortType, String accessToken, boolean nsfw) {
this.retrofit = retrofit; this.retrofit = retrofit;
@ -49,7 +49,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
} }
@Override @Override
public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull LoadInitialCallback<String, SubredditData> callback) { public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Integer, SubredditData> callback) {
initialLoadStateLiveData.postValue(NetworkState.LOADING); initialLoadStateLiveData.postValue(NetworkState.LOADING);
FetchSubredditData.fetchSubredditListingData(retrofit, query, null, sortType.getType(), accessToken, nsfw, FetchSubredditData.fetchSubredditListingData(retrofit, query, null, sortType.getType(), accessToken, nsfw,
@ -62,7 +62,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
hasSubredditLiveData.postValue(true); hasSubredditLiveData.postValue(true);
} }
callback.onResult(subredditData, null, after); callback.onResult(subredditData, null, 2);
initialLoadStateLiveData.postValue(NetworkState.LOADED); initialLoadStateLiveData.postValue(NetworkState.LOADED);
} }
@ -74,12 +74,12 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
} }
@Override @Override
public void loadBefore(@NonNull LoadParams<String> params, @NonNull LoadCallback<String, SubredditData> callback) { public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, SubredditData> callback) {
} }
@Override @Override
public void loadAfter(@NonNull LoadParams<String> params, @NonNull LoadCallback<String, SubredditData> callback) { public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, SubredditData> callback) {
this.params = params; this.params = params;
this.callback = callback; this.callback = callback;
@ -91,7 +91,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
new FetchSubredditData.FetchSubredditListingDataListener() { new FetchSubredditData.FetchSubredditListingDataListener() {
@Override @Override
public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) { public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) {
callback.onResult(subredditData, after); callback.onResult(subredditData, params.key + 1);
paginationNetworkStateLiveData.postValue(NetworkState.LOADED); paginationNetworkStateLiveData.postValue(NetworkState.LOADED);
} }