Fix CommentsListingFragment

This allows us to list comments by users
This commit is contained in:
Balazs Toldi 2023-07-28 07:27:57 +02:00
parent df842d33e1
commit 6eb585800f
No known key found for this signature in database
GPG Key ID: 6C7D440036F99D58
3 changed files with 40 additions and 68 deletions

View File

@ -46,6 +46,16 @@ public interface LemmyAPI {
@Query("saved_only") Boolean saved_only,
@Query("auth") String access_token);
@GET("api/v3/user")
Call<String> getUserComments(
@Query("username") String username,
@Query("sort") String sort,
@Query("page") Integer page,
@Query("limit") Integer limit,
@Query("saved_only") Boolean saved_only,
@Query("auth") String access_token);
@GET("api/v3/community/list")
Call<String> listCommunities(
@Query("type_") String type_,

View File

@ -16,16 +16,13 @@ import java.util.Locale;
import eu.toldi.infinityforlemmy.NetworkState;
import eu.toldi.infinityforlemmy.SortType;
import eu.toldi.infinityforlemmy.apis.RedditAPI;
import eu.toldi.infinityforlemmy.post.PostPagingSource;
import eu.toldi.infinityforlemmy.utils.APIUtils;
import eu.toldi.infinityforlemmy.utils.JSONUtils;
import eu.toldi.infinityforlemmy.apis.LemmyAPI;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
public class CommentDataSource extends PageKeyedDataSource<String, Comment> {
public class CommentDataSource extends PageKeyedDataSource<Integer, Comment> {
private Retrofit retrofit;
private Locale locale;
@ -39,8 +36,8 @@ public class CommentDataSource extends PageKeyedDataSource<String, Comment> {
private MutableLiveData<NetworkState> initialLoadStateLiveData;
private MutableLiveData<Boolean> hasPostLiveData;
private LoadParams<String> params;
private LoadCallback<String, Comment> callback;
private LoadParams<Integer> params;
private LoadCallback<Integer, Comment> callback;
CommentDataSource(Retrofit retrofit, Locale locale, @Nullable String accessToken, String username, SortType sortType,
boolean areSavedComments) {
@ -72,42 +69,27 @@ public class CommentDataSource extends PageKeyedDataSource<String, Comment> {
}
@Override
public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull LoadInitialCallback<String, Comment> callback) {
public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Integer, Comment> callback) {
initialLoadStateLiveData.postValue(NetworkState.LOADING);
RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> commentsCall;
if (areSavedComments) {
commentsCall = api.getUserSavedCommentsOauth(username, PostPagingSource.USER_WHERE_SAVED,
null, sortType.getType(), sortType.getTime(),
APIUtils.getOAuthHeader(accessToken));
} else {
if (accessToken == null) {
commentsCall = api.getUserComments(username, null, sortType.getType(),
sortType.getTime());
} else {
commentsCall = api.getUserCommentsOauth(APIUtils.getOAuthHeader(accessToken), username,
null, sortType.getType(), sortType.getTime());
}
}
LemmyAPI api = retrofit.create(LemmyAPI.class);
Call<String> commentsCall = api.getUserComments(username, sortType.getType().value, 1, 25, areSavedComments, accessToken);
commentsCall.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
new ParseCommentAsyncTask(response.body(), locale, new ParseCommentAsyncTask.ParseCommentAsyncTaskListener() {
@Override
public void parseSuccessful(ArrayList<Comment> comments, String after) {
if (comments.size() == 0) {
public void parseSuccessful(ArrayList<Comment> comments, Integer after) {
if (comments.isEmpty()) {
callback.onResult(comments, null, null);
hasPostLiveData.postValue(false);
} else {
hasPostLiveData.postValue(true);
callback.onResult(comments, null, 2);
}
if (after == null || after.equals("") || after.equals("null")) {
callback.onResult(comments, null, null);
} else {
callback.onResult(comments, null, after);
}
initialLoadStateLiveData.postValue(NetworkState.LOADED);
}
@ -129,39 +111,27 @@ public class CommentDataSource extends PageKeyedDataSource<String, Comment> {
}
@Override
public void loadBefore(@NonNull LoadParams<String> params, @NonNull LoadCallback<String, Comment> callback) {
public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, Comment> callback) {
}
@Override
public void loadAfter(@NonNull LoadParams<String> params, @NonNull LoadCallback<String, Comment> callback) {
public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, Comment> callback) {
this.params = params;
this.callback = callback;
paginationNetworkStateLiveData.postValue(NetworkState.LOADING);
RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> commentsCall;
if (areSavedComments) {
commentsCall = api.getUserSavedCommentsOauth(username, PostPagingSource.USER_WHERE_SAVED, params.key,
sortType.getType(), sortType.getTime(), APIUtils.getOAuthHeader(accessToken));
} else {
if (accessToken == null) {
commentsCall = api.getUserComments(username, params.key, sortType.getType(),
sortType.getTime());
} else {
commentsCall = api.getUserCommentsOauth(APIUtils.getOAuthHeader(accessToken),
username, params.key, sortType.getType(), sortType.getTime());
}
}
LemmyAPI api = retrofit.create(LemmyAPI.class);
Call<String> commentsCall = api.getUserComments(username, sortType.getType().value, params.key, 25, areSavedComments, accessToken);
commentsCall.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
new ParseCommentAsyncTask(response.body(), locale, new ParseCommentAsyncTask.ParseCommentAsyncTaskListener() {
@Override
public void parseSuccessful(ArrayList<Comment> comments, String after) {
if (after == null || after.equals("") || after.equals("null")) {
public void parseSuccessful(ArrayList<Comment> comments, Integer after) {
if (comments.isEmpty()) {
callback.onResult(comments, null);
} else {
callback.onResult(comments, after);
@ -187,7 +157,7 @@ public class CommentDataSource extends PageKeyedDataSource<String, Comment> {
}
private static class ParseCommentAsyncTask extends AsyncTask<Void, ArrayList<Comment>, ArrayList<Comment>> {
private String after;
private Integer after;
private Locale locale;
private JSONArray commentsJSONArray;
private boolean parseFailed;
@ -197,9 +167,8 @@ public class CommentDataSource extends PageKeyedDataSource<String, Comment> {
this.locale = locale;
this.parseCommentAsyncTaskListener = parseCommentAsyncTaskListener;
try {
JSONObject data = new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY);
commentsJSONArray = data.getJSONArray(JSONUtils.CHILDREN_KEY);
after = data.getString(JSONUtils.AFTER_KEY);
JSONObject data = new JSONObject(response);
commentsJSONArray = data.getJSONArray("comments");
parseFailed = false;
} catch (JSONException e) {
parseFailed = true;
@ -216,7 +185,7 @@ public class CommentDataSource extends PageKeyedDataSource<String, Comment> {
ArrayList<Comment> comments = new ArrayList<>();
for (int i = 0; i < commentsJSONArray.length(); i++) {
try {
JSONObject commentJSON = commentsJSONArray.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY);
JSONObject commentJSON = commentsJSONArray.getJSONObject(i);
comments.add(ParseComment.parseSingleComment(commentJSON));
} catch (JSONException ignored) {
}
@ -235,7 +204,7 @@ public class CommentDataSource extends PageKeyedDataSource<String, Comment> {
}
interface ParseCommentAsyncTaskListener {
void parseSuccessful(ArrayList<Comment> comments, String after);
void parseSuccessful(ArrayList<Comment> comments, Integer page);
void parseFailed();
}

View File

@ -52,7 +52,6 @@ import eu.toldi.infinityforlemmy.customtheme.CustomThemeWrapper;
import eu.toldi.infinityforlemmy.customviews.LinearLayoutManagerBugFixed;
import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils;
import eu.toldi.infinityforlemmy.utils.Utils;
import retrofit2.Retrofit;
/**
@ -82,9 +81,6 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
@Named("no_oauth")
RetrofitHolder mRetrofit;
@Inject
@Named("oauth")
Retrofit mOauthRetrofit;
@Inject
RedditDataRoomDatabase mRedditDataRoomDatabase;
@Inject
@Named("default")
@ -253,6 +249,7 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null);
new Handler().postDelayed(() -> bindView(resources), 0);
return rootView;
@ -263,7 +260,7 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
mLinearLayoutManager = new LinearLayoutManagerBugFixed(mActivity);
mCommentRecyclerView.setLayoutManager(mLinearLayoutManager);
mAdapter = new CommentsListingRecyclerViewAdapter(mActivity, mOauthRetrofit, customThemeWrapper,
mAdapter = new CommentsListingRecyclerViewAdapter(mActivity, mRetrofit.getRetrofit(), customThemeWrapper,
getResources().getConfiguration().locale, mSharedPreferences,
getArguments().getString(EXTRA_ACCESS_TOKEN), getArguments().getString(EXTRA_ACCOUNT_NAME),
() -> mCommentViewModel.retryLoadingMore());
@ -294,15 +291,11 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
CommentViewModel.Factory factory;
if (mAccessToken == null) {
factory = new CommentViewModel.Factory(mRetrofit.getRetrofit(),
resources.getConfiguration().locale, null, username, sortType,
getArguments().getBoolean(EXTRA_ARE_SAVED_COMMENTS));
} else {
factory = new CommentViewModel.Factory(mOauthRetrofit,
resources.getConfiguration().locale, mAccessToken, username, sortType,
getArguments().getBoolean(EXTRA_ARE_SAVED_COMMENTS));
}
factory = new CommentViewModel.Factory(mRetrofit.getRetrofit(),
resources.getConfiguration().locale, mAccessToken, username, sortType,
getArguments().getBoolean(EXTRA_ARE_SAVED_COMMENTS));
mCommentViewModel = new ViewModelProvider(this, factory).get(CommentViewModel.class);
mCommentViewModel.getComments().observe(getViewLifecycleOwner(), comments -> mAdapter.submitList(comments));