From e5f2e2f8a27a6408dba5c1f9d1462f43c9562cf7 Mon Sep 17 00:00:00 2001 From: Balazs Toldi Date: Tue, 25 Jul 2023 07:48:16 +0200 Subject: [PATCH] Basic search functionality for communities Signed-off-by: Balazs Toldi --- .../toldi/infinityforlemmy/apis/LemmyAPI.java | 16 +++++++++++++++- .../fragments/PostFragment.java | 3 ++- .../fragments/SubredditListingFragment.java | 12 ++++-------- .../post/PostPagingSource.java | 2 +- .../subreddit/FetchSubredditData.java | 18 ++++++------------ .../subreddit/ParseSubredditData.java | 16 ++++++---------- .../subreddit/SubredditListingDataSource.java | 16 ++++++++-------- 7 files changed, 42 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/apis/LemmyAPI.java b/app/src/main/java/eu/toldi/infinityforlemmy/apis/LemmyAPI.java index 06e42961..566569a4 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/apis/LemmyAPI.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/apis/LemmyAPI.java @@ -80,7 +80,21 @@ public interface LemmyAPI { ); @GET("api/v3/search") - ListenableFuture> search( + ListenableFuture> 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 search( @Query("q") String q, @Query("community_id") Integer communityId, @Query("community_name") String communityName, diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/fragments/PostFragment.java b/app/src/main/java/eu/toldi/infinityforlemmy/fragments/PostFragment.java index 48923ee1..b878a089 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/fragments/PostFragment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/fragments/PostFragment.java @@ -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 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); mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mRetrofit.getRetrofit(), mGfycatRetrofit, diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/fragments/SubredditListingFragment.java b/app/src/main/java/eu/toldi/infinityforlemmy/fragments/SubredditListingFragment.java index 2ddcdda7..bc572700 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/fragments/SubredditListingFragment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/fragments/SubredditListingFragment.java @@ -49,7 +49,6 @@ import eu.toldi.infinityforlemmy.customviews.LinearLayoutManagerBugFixed; import eu.toldi.infinityforlemmy.subreddit.SubredditData; import eu.toldi.infinityforlemmy.subreddit.SubredditListingViewModel; import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils; -import retrofit2.Retrofit; /** @@ -80,9 +79,6 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun @Named("no_oauth") RetrofitHolder mRetrofit; @Inject - @Named("oauth") - Retrofit mOauthRetrofit; - @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject @Named("default") @@ -139,11 +135,11 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN); String accountName = getArguments().getString(EXTRA_ACCOUNT_NAME); - String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_SEARCH_SUBREDDIT, SortType.Type.TOP.value); - sortType = new SortType(SortType.Type.valueOf(sort.toUpperCase())); + String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_SEARCH_SUBREDDIT, SortType.Type.TOP_ALL.value); + 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); - mAdapter = new SubredditListingRecyclerViewAdapter(mActivity, mExecutor, mOauthRetrofit, mRetrofit.getRetrofit(), + mAdapter = new SubredditListingRecyclerViewAdapter(mActivity, mExecutor, mRetrofit.getRetrofit(), mRetrofit.getRetrofit(), mCustomThemeWrapper, accessToken, accountName, mRedditDataRoomDatabase, getArguments().getBoolean(EXTRA_IS_MULTI_SELECTION, false), new SubredditListingRecyclerViewAdapter.Callback() { @@ -153,7 +149,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun } @Override - public void subredditSelected(String subredditName, String communityFullName,String iconUrl) { + public void subredditSelected(String subredditName, String communityFullName, String iconUrl) { if (isGettingSubredditInfo) { ((SearchSubredditsResultActivity) mActivity).getSelectedSubreddit(subredditName, iconUrl); } else { diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/post/PostPagingSource.java b/app/src/main/java/eu/toldi/infinityforlemmy/post/PostPagingSource.java index 6792e74b..917b2d8f 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/post/PostPagingSource.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/post/PostPagingSource.java @@ -268,7 +268,7 @@ public class PostPagingSource extends ListenableFuturePagingSource> loadSearchPosts(@NonNull LoadParams loadParams, LemmyAPI api) { ListenableFuture> 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> pageFuture = Futures.transform(searchPosts, this::transformData, executor); diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/subreddit/FetchSubredditData.java b/app/src/main/java/eu/toldi/infinityforlemmy/subreddit/FetchSubredditData.java index dc3892c4..551fef09 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/subreddit/FetchSubredditData.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/subreddit/FetchSubredditData.java @@ -1,18 +1,11 @@ package eu.toldi.infinityforlemmy.subreddit; -import android.text.TextUtils; - import androidx.annotation.NonNull; 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.apis.LemmyAPI; -import eu.toldi.infinityforlemmy.apis.RedditAPI; -import eu.toldi.infinityforlemmy.utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; 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) { - RedditAPI api = retrofit.create(RedditAPI.class); + LemmyAPI api = retrofit.create(LemmyAPI.class); + + + Call subredditDataCall = api.search(query, null, null, null, "Communities", sortType.value, "All", page, 25, accessToken); + - Map map = new HashMap<>(); - Map headers = accessToken != null ? APIUtils.getOAuthHeader(accessToken) : Collections.unmodifiableMap(map); - Call subredditDataCall = api.searchSubreddits(query, after, sortType, nsfw ? 1 : 0, headers); subredditDataCall.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/subreddit/ParseSubredditData.java b/app/src/main/java/eu/toldi/infinityforlemmy/subreddit/ParseSubredditData.java index 349a95db..da1989ff 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/subreddit/ParseSubredditData.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/subreddit/ParseSubredditData.java @@ -11,7 +11,6 @@ import org.json.JSONObject; import java.util.ArrayList; import eu.toldi.infinityforlemmy.utils.JSONUtils; -import eu.toldi.infinityforlemmy.utils.Utils; public class ParseSubredditData { public static void parseSubredditData(String response, ParseSubredditDataListener parseSubredditDataListener) { @@ -32,13 +31,13 @@ public class ParseSubredditData { String title = community.getString(JSONUtils.TITLE_KEY); String bannerImageUrl = ""; - if(!community.isNull("banner")){ + if (!community.isNull("banner")) { bannerImageUrl = community.getString("banner"); } String iconUrl = ""; - if(!community.isNull("banner")){ - bannerImageUrl = community.getString("icon"); + if (!community.isNull("icon")) { + iconUrl = community.getString("icon"); } int id = community.getInt("id"); String name = community.getString("name"); @@ -50,8 +49,6 @@ public class ParseSubredditData { String actorId = community.getString("actor_id"); boolean local = community.getBoolean("local"); - String icon = community.getString("icon"); - String banner = community.getString("banner"); boolean hidden = community.getBoolean("hidden"); boolean postingRestrictedToMods = community.getBoolean("posting_restricted_to_mods"); int instanceId = community.getInt("instance_id"); @@ -138,16 +135,15 @@ public class ParseSubredditData { protected Void doInBackground(Void... voids) { try { if (!parseFailed) { - JSONArray children = jsonResponse.getJSONObject(JSONUtils.DATA_KEY) - .getJSONArray(JSONUtils.CHILDREN_KEY); + JSONArray children = jsonResponse.getJSONArray("communities"); 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); if (subredditData != null) { 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) { e.printStackTrace(); diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/subreddit/SubredditListingDataSource.java b/app/src/main/java/eu/toldi/infinityforlemmy/subreddit/SubredditListingDataSource.java index 26c3c47c..d4764e09 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/subreddit/SubredditListingDataSource.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/subreddit/SubredditListingDataSource.java @@ -10,7 +10,7 @@ import eu.toldi.infinityforlemmy.NetworkState; import eu.toldi.infinityforlemmy.SortType; import retrofit2.Retrofit; -public class SubredditListingDataSource extends PageKeyedDataSource { +public class SubredditListingDataSource extends PageKeyedDataSource { private Retrofit retrofit; private String query; @@ -22,8 +22,8 @@ public class SubredditListingDataSource extends PageKeyedDataSource initialLoadStateLiveData; private MutableLiveData hasSubredditLiveData; - private LoadParams params; - private LoadCallback callback; + private LoadParams params; + private LoadCallback callback; SubredditListingDataSource(Retrofit retrofit, String query, SortType sortType, String accessToken, boolean nsfw) { this.retrofit = retrofit; @@ -49,7 +49,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource params, @NonNull LoadInitialCallback callback) { + public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback callback) { initialLoadStateLiveData.postValue(NetworkState.LOADING); FetchSubredditData.fetchSubredditListingData(retrofit, query, null, sortType.getType(), accessToken, nsfw, @@ -62,7 +62,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource params, @NonNull LoadCallback callback) { + public void loadBefore(@NonNull LoadParams params, @NonNull LoadCallback callback) { } @Override - public void loadAfter(@NonNull LoadParams params, @NonNull LoadCallback callback) { + public void loadAfter(@NonNull LoadParams params, @NonNull LoadCallback callback) { this.params = params; this.callback = callback; @@ -91,7 +91,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource subredditData, String after) { - callback.onResult(subredditData, after); + callback.onResult(subredditData, params.key + 1); paginationNetworkStateLiveData.postValue(NetworkState.LOADED); }