Basic search post functionality

Signed-off-by: Balazs Toldi <balazs@toldi.eu>
This commit is contained in:
Balazs Toldi 2023-07-24 22:21:21 +02:00
parent 3b91ee0280
commit ad7083b423
No known key found for this signature in database
GPG Key ID: 6C7D440036F99D58
8 changed files with 52 additions and 29 deletions

View File

@ -57,6 +57,15 @@ public class SortType {
this.value = value;
this.fullName = fullName;
}
public static Type fromValue(String value) {
for (Type type : values()) {
if (type.value.equals(value)) {
return type;
}
}
return null;
}
}
public enum Time {

View File

@ -75,6 +75,7 @@ public class SearchActivity extends BaseActivity {
public static final String EXTRA_RETURN_USER_ICON_URL = "ERUIU";
public static final String EXTRA_IS_MULTI_SELECTION = "EIMS";
public static final int SUICIDE_PREVENTION_ACTIVITY_REQUEST_CODE = 101;
public static final String EXTRA_COMMUNITY_FULL_NAME = "ECF";
private static final String SUBREDDIT_NAME_STATE = "SNS";
private static final String SUBREDDIT_IS_USER_STATE = "SIUS";
@ -82,6 +83,7 @@ public class SearchActivity extends BaseActivity {
private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0;
private static final int SUBREDDIT_SEARCH_REQUEST_CODE = 1;
private static final int USER_SEARCH_REQUEST_CODE = 2;
private static final String COMMUNITY_QUALIFIED_NAME = "CQN";
@BindView(R.id.coordinator_layout_search_activity)
CoordinatorLayout coordinatorLayout;
@ -125,6 +127,8 @@ public class SearchActivity extends BaseActivity {
private String mAccessToken;
private String query;
private String subredditName;
private String communityQualifiedName;
private boolean subredditIsUser;
private boolean searchOnlySubreddits;
private boolean searchOnlyUsers;
@ -296,6 +300,7 @@ public class SearchActivity extends BaseActivity {
Intent intent = getIntent();
if (intent.hasExtra(EXTRA_SUBREDDIT_NAME)) {
subredditName = intent.getStringExtra(EXTRA_SUBREDDIT_NAME);
communityQualifiedName = intent.getStringExtra(EXTRA_COMMUNITY_FULL_NAME);
subredditNameTextView.setText(subredditName);
subredditIsUser = intent.getBooleanExtra(EXTRA_SUBREDDIT_IS_USER, false);
}
@ -364,6 +369,7 @@ public class SearchActivity extends BaseActivity {
intent.putExtra(SearchResultActivity.EXTRA_SUBREDDIT_NAME, "u_" + subredditName);
} else {
intent.putExtra(SearchResultActivity.EXTRA_SUBREDDIT_NAME, subredditName);
intent.putExtra(SearchResultActivity.EXTRA_COMMUNITY_QUALIFIED_NAME, communityQualifiedName);
}
}
startActivity(intent);
@ -477,6 +483,7 @@ public class SearchActivity extends BaseActivity {
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(SUBREDDIT_NAME_STATE, subredditName);
outState.putString(COMMUNITY_QUALIFIED_NAME, communityQualifiedName);
outState.putBoolean(SUBREDDIT_IS_USER_STATE, subredditIsUser);
}

View File

@ -90,6 +90,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect
static final String EXTRA_SUBREDDIT_NAME = "ESN";
private static final String INSERT_SEARCH_QUERY_SUCCESS_STATE = "ISQSS";
public static final String EXTRA_COMMUNITY_QUALIFIED_NAME = "ECQN";
@BindView(R.id.coordinator_layout_search_result_activity)
CoordinatorLayout coordinatorLayout;
@BindView(R.id.appbar_layout_search_result_activity)
@ -132,8 +133,11 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect
private Call<String> subredditAutocompleteCall;
private String mAccessToken;
private String mAccountName;
private String mAccountQualifiedName;
private String mQuery;
private String mSubredditName;
private String mCommunityQualifiedName;
private boolean mInsertSearchQuerySuccess;
private FragmentManager fragmentManager;
private SectionsPagerAdapter sectionsPagerAdapter;
@ -192,6 +196,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect
String query = intent.getStringExtra(EXTRA_QUERY);
mSubredditName = intent.getStringExtra(EXTRA_SUBREDDIT_NAME);
mCommunityQualifiedName = intent.getStringExtra(EXTRA_COMMUNITY_QUALIFIED_NAME);
if (query != null) {
mQuery = query;
@ -202,6 +207,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect
mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null);
mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null);
mAccountQualifiedName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_QUALIFIED_NAME, null);
if (savedInstanceState != null) {
mInsertSearchQuerySuccess = savedInstanceState.getBoolean(INSERT_SEARCH_QUERY_SUCCESS_STATE);
@ -386,7 +392,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect
});
if (mAccountName != null && mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_SEARCH_HISTORY, true) && !mInsertSearchQuerySuccess && mQuery != null) {
InsertRecentSearchQuery.insertRecentSearchQueryListener(mRedditDataRoomDatabase, mAccountName,
InsertRecentSearchQuery.insertRecentSearchQueryListener(mRedditDataRoomDatabase, mAccountQualifiedName,
mQuery, () -> mInsertSearchQuerySuccess = true);
}
}
@ -768,7 +774,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect
PostFragment mFragment = new PostFragment();
Bundle bundle = new Bundle();
bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostPagingSource.TYPE_SEARCH);
bundle.putString(PostFragment.EXTRA_NAME, mSubredditName);
bundle.putString(PostFragment.EXTRA_NAME, mCommunityQualifiedName);
bundle.putString(PostFragment.EXTRA_QUERY, mQuery);
bundle.putString(PostFragment.EXTRA_TRENDING_SOURCE, getIntent().getStringExtra(EXTRA_TRENDING_SOURCE));
bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken);

View File

@ -1123,6 +1123,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
} else if (itemId == R.id.action_search_view_subreddit_detail_activity) {
Intent intent = new Intent(this, SearchActivity.class);
intent.putExtra(SearchActivity.EXTRA_SUBREDDIT_NAME, subredditName);
intent.putExtra(SearchActivity.EXTRA_COMMUNITY_FULL_NAME, qualifiedName);
startActivity(intent);
return true;
} else if (itemId == R.id.action_refresh_view_subreddit_detail_activity) {

View File

@ -1130,6 +1130,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
} else if (itemId == R.id.action_search_view_user_detail_activity) {
Intent intent = new Intent(this, SearchActivity.class);
intent.putExtra(SearchActivity.EXTRA_SUBREDDIT_NAME, username);
intent.putExtra(SearchActivity.EXTRA_COMMUNITY_FULL_NAME, qualifiedName);
intent.putExtra(SearchActivity.EXTRA_SUBREDDIT_IS_USER, true);
startActivity(intent);
return true;

View File

@ -78,4 +78,18 @@ public interface LemmyAPI {
@Query("saved_only") Boolean savedOnly,
@Query("auth") String auth
);
@GET("api/v3/search")
ListenableFuture<Response<String>> search(
@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
);
}

View File

@ -458,9 +458,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
usage = PostFilterUsage.SEARCH_TYPE;
nameOfUsage = PostFilterUsage.NO_USAGE;
String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_SEARCH_POST, SortType.Type.TOP.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());
sortType = new SortType(SortType.Type.valueOf(sort), SortType.Time.valueOf(sortTime));
sortType = new SortType(SortType.Type.fromValue(sort), SortType.Time.valueOf(sortTime));
postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_SEARCH_POST, defaultPostLayout);
mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mRetrofit.getRetrofit(), mGfycatRetrofit,

View File

@ -174,9 +174,9 @@ public class PostPagingSource extends ListenableFuturePagingSource<Integer, Post
return loadSubredditPosts(loadParams, api);
case TYPE_USER:
return loadUserPosts(loadParams, api);
/* case TYPE_SEARCH:
case TYPE_SEARCH:
return loadSearchPosts(loadParams, api);
case TYPE_MULTI_REDDIT:
/* case TYPE_MULTI_REDDIT:
return loadMultiRedditPosts(loadParams, api);
default:
return loadAnonymousHomePosts(loadParams, api);*/
@ -264,38 +264,23 @@ public class PostPagingSource extends ListenableFuturePagingSource<Integer, Post
return Futures.catching(partialLoadResultFuture,
IOException.class, LoadResult.Error::new, executor);
}
/*
private ListenableFuture<LoadResult<String, Post>> loadSearchPosts(@NonNull LoadParams<String> loadParams, LemmyAPI api) {
private ListenableFuture<LoadResult<Integer, Post>> loadSearchPosts(@NonNull LoadParams<Integer> loadParams, LemmyAPI api) {
ListenableFuture<Response<String>> searchPosts;
if (subredditOrUserName == null) {
if (accessToken == null) {
searchPosts = api.searchPostsListenableFuture(query, loadParams.getKey(), sortType.getType(), sortType.getTime(),
trendingSource);
} else {
searchPosts = api.searchPostsOauthListenableFuture(query, loadParams.getKey(), sortType.getType(),
sortType.getTime(), trendingSource, APIUtils.getOAuthHeader(accessToken));
}
} else {
if (accessToken == null) {
searchPosts = api.searchPostsInSpecificSubredditListenableFuture(subredditOrUserName, query,
sortType.getType(), sortType.getTime(), loadParams.getKey());
} else {
searchPosts = api.searchPostsInSpecificSubredditOauthListenableFuture(subredditOrUserName, query,
sortType.getType(), sortType.getTime(), loadParams.getKey(),
APIUtils.getOAuthHeader(accessToken));
}
}
ListenableFuture<LoadResult<String, Post>> pageFuture = Futures.transform(searchPosts, this::transformData, executor);
searchPosts = api.search(query, null, subredditOrUserName, null, "Posts", sortType.getType().value, "All", loadParams.getKey(), 25, accessToken);
ListenableFuture<LoadResult<String, Post>> partialLoadResultFuture =
ListenableFuture<LoadResult<Integer, Post>> pageFuture = Futures.transform(searchPosts, this::transformData, executor);
ListenableFuture<LoadResult<Integer, Post>> partialLoadResultFuture =
Futures.catching(pageFuture, HttpException.class,
LoadResult.Error::new, executor);
return Futures.catching(partialLoadResultFuture,
IOException.class, LoadResult.Error::new, executor);
}
/*
private ListenableFuture<LoadResult<String, Post>> loadMultiRedditPosts(@NonNull LoadParams<String> loadParams, LemmyAPI api) {
ListenableFuture<Response<String>> multiRedditPosts;
if (accessToken == null) {