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")
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("community_id") Integer communityId,
@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 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,

View File

@ -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 {

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) {
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);

View File

@ -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<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>() {
@Override
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 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();

View File

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