Basic search functionality for users

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

View File

@ -60,7 +60,7 @@ public class SortType {
public static Type fromValue(String value) {
for (Type type : values()) {
if (type.value.equals(value)) {
if (type.value.equalsIgnoreCase(value)) {
return type;
}
}

View File

@ -27,7 +27,6 @@ import java.util.concurrent.Executor;
import butterknife.BindView;
import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import eu.toldi.infinityforlemmy.NetworkState;
import eu.toldi.infinityforlemmy.R;
import eu.toldi.infinityforlemmy.RedditDataRoomDatabase;
@ -36,6 +35,8 @@ import eu.toldi.infinityforlemmy.asynctasks.CheckIsFollowingUser;
import eu.toldi.infinityforlemmy.customtheme.CustomThemeWrapper;
import eu.toldi.infinityforlemmy.user.UserData;
import eu.toldi.infinityforlemmy.user.UserFollowing;
import eu.toldi.infinityforlemmy.utils.LemmyUtils;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import pl.droidsonroids.gif.GifImageView;
import retrofit2.Retrofit;
@ -119,7 +120,7 @@ public class UserListingRecyclerViewAdapter extends PagedListAdapter<UserData, R
if (isMultiSelection) {
((DataViewHolder) holder).checkBox.performClick();
} else {
callback.userSelected(userData.getName(), userData.getIconUrl());
callback.userSelected(userData.getName(), userData.getIconUrl(), LemmyUtils.actorID2FullName(userData.getActorId()));
}
});
@ -226,7 +227,7 @@ public class UserListingRecyclerViewAdapter extends PagedListAdapter<UserData, R
public interface Callback {
void retryLoadingMore();
void userSelected(String username, String iconUrl);
void userSelected(String username, String iconUrl, String userQualifiedName);
}
class DataViewHolder extends RecyclerView.ViewHolder {

View File

@ -139,8 +139,8 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato
boolean isGettingUserInfo = getArguments().getBoolean(EXTRA_IS_GETTING_USER_INFO);
String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN);
String accountName = getArguments().getString(EXTRA_ACCOUNT_NAME);
String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_SEARCH_USER, SortType.Type.TOP.value);
sortType = new SortType(SortType.Type.valueOf(sort.toUpperCase()));
String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_SEARCH_USER, 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 UserListingRecyclerViewAdapter(mActivity, mExecutor, mOauthRetrofit, mRetrofit.getRetrofit(),
@ -153,12 +153,13 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato
}
@Override
public void userSelected(String username, String iconUrl) {
public void userSelected(String username, String iconUrl, String userQualifiedName) {
if (isGettingUserInfo) {
((SearchUsersResultActivity) mActivity).getSelectedUser(username, iconUrl);
} else {
Intent intent = new Intent(mActivity, ViewUserDetailActivity.class);
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, username);
intent.putExtra(ViewUserDetailActivity.EXTRA_QUALIFIED_USER_NAME_KEY, userQualifiedName);
mActivity.startActivity(intent);
}
}

View File

@ -7,8 +7,6 @@ import java.util.ArrayList;
import eu.toldi.infinityforlemmy.RedditDataRoomDatabase;
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.Retrofit;
@ -55,11 +53,11 @@ public class FetchUserData {
});
}
public static void fetchUserListingData(Retrofit retrofit, String query, String after, SortType.Type sortType, boolean nsfw,
public static void fetchUserListingData(Retrofit retrofit, String query, Integer page, SortType.Type sortType, boolean nsfw,
FetchUserListingDataListener fetchUserListingDataListener) {
RedditAPI api = retrofit.create(RedditAPI.class);
LemmyAPI api = retrofit.create(LemmyAPI.class);
Call<String> userInfo = api.searchUsers(query, after, sortType, nsfw ? 1 : 0);
Call<String> userInfo = api.search(query, null, null, null, "Users", sortType.value, "All", page, 25, null);
userInfo.enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {

View File

@ -27,7 +27,7 @@ public class ParseUserData {
}
JSONObject personJson = userDataJson.getJSONObject("person_view").getJSONObject("person");
JSONObject personJson = (userDataJson.has("person_view")) ? userDataJson.getJSONObject("person_view").getJSONObject("person") : userDataJson.getJSONObject("person");
String userName = personJson.getString(JSONUtils.NAME_KEY);
String actor_id = personJson.getString("actor_id");
String iconImageUrl = "";
@ -38,7 +38,7 @@ public class ParseUserData {
if (!personJson.isNull("banner")) {
bannerImageUrl = personJson.getString("banner");
}
JSONObject countsJson = userDataJson.getJSONObject("person_view").getJSONObject("counts");
JSONObject countsJson = (userDataJson.has("person_view")) ? userDataJson.getJSONObject("person_view").getJSONObject("counts") : userDataJson.getJSONObject("counts");
int linkKarma = countsJson.getInt(JSONUtils.POST_SCORE_KEY);
int commentKarma = countsJson.getInt(JSONUtils.COMMENT_SCORE_KEY);
@ -150,8 +150,8 @@ public class ParseUserData {
protected Void doInBackground(Void... voids) {
try {
if (!parseFailed) {
after = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
JSONArray children = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
JSONArray children = jsonResponse.getJSONArray("users");
for (int i = 0; i < children.length(); i++) {
try {
UserData userData = parseUserDataBase(children.getJSONObject(i), false);

View File

@ -10,7 +10,7 @@ import eu.toldi.infinityforlemmy.NetworkState;
import eu.toldi.infinityforlemmy.SortType;
import retrofit2.Retrofit;
public class UserListingDataSource extends PageKeyedDataSource<String, UserData> {
public class UserListingDataSource extends PageKeyedDataSource<Integer, UserData> {
private Retrofit retrofit;
private String query;
@ -21,8 +21,8 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData>
private MutableLiveData<NetworkState> initialLoadStateLiveData;
private MutableLiveData<Boolean> hasUserLiveData;
private PageKeyedDataSource.LoadParams<String> params;
private PageKeyedDataSource.LoadCallback<String, UserData> callback;
private PageKeyedDataSource.LoadParams<Integer> params;
private PageKeyedDataSource.LoadCallback<Integer, UserData> callback;
UserListingDataSource(Retrofit retrofit, String query, SortType sortType, boolean nsfw) {
this.retrofit = retrofit;
@ -47,7 +47,7 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData>
}
@Override
public void loadInitial(@NonNull PageKeyedDataSource.LoadInitialParams<String> params, @NonNull PageKeyedDataSource.LoadInitialCallback<String, UserData> callback) {
public void loadInitial(@NonNull PageKeyedDataSource.LoadInitialParams<Integer> params, @NonNull PageKeyedDataSource.LoadInitialCallback<Integer, UserData> callback) {
initialLoadStateLiveData.postValue(NetworkState.LOADING);
FetchUserData.fetchUserListingData(retrofit, query, null, sortType.getType(), nsfw,
@ -56,7 +56,7 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData>
public void onFetchUserListingDataSuccess(ArrayList<UserData> UserData, String after) {
hasUserLiveData.postValue(UserData.size() != 0);
callback.onResult(UserData, null, after);
callback.onResult(UserData, null, 2);
initialLoadStateLiveData.postValue(NetworkState.LOADED);
}
@ -68,12 +68,12 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData>
}
@Override
public void loadBefore(@NonNull PageKeyedDataSource.LoadParams<String> params, @NonNull PageKeyedDataSource.LoadCallback<String, UserData> callback) {
public void loadBefore(@NonNull PageKeyedDataSource.LoadParams<Integer> params, @NonNull PageKeyedDataSource.LoadCallback<Integer, UserData> callback) {
}
@Override
public void loadAfter(@NonNull PageKeyedDataSource.LoadParams<String> params, @NonNull PageKeyedDataSource.LoadCallback<String, UserData> callback) {
public void loadAfter(@NonNull PageKeyedDataSource.LoadParams<Integer> params, @NonNull PageKeyedDataSource.LoadCallback<Integer, UserData> callback) {
this.params = params;
this.callback = callback;
@ -85,7 +85,7 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData>
new FetchUserData.FetchUserListingDataListener() {
@Override
public void onFetchUserListingDataSuccess(ArrayList<UserData> UserData, String after) {
callback.onResult(UserData, after);
callback.onResult(UserData, params.key + 1);
paginationNetworkStateLiveData.postValue(NetworkState.LOADED);
}