mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-12-26 10:58:23 +01:00
Fix CommentsListingFragment
This allows us to list comments by users
This commit is contained in:
parent
df842d33e1
commit
6eb585800f
@ -46,6 +46,16 @@ public interface LemmyAPI {
|
|||||||
@Query("saved_only") Boolean saved_only,
|
@Query("saved_only") Boolean saved_only,
|
||||||
@Query("auth") String access_token);
|
@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")
|
@GET("api/v3/community/list")
|
||||||
Call<String> listCommunities(
|
Call<String> listCommunities(
|
||||||
@Query("type_") String type_,
|
@Query("type_") String type_,
|
||||||
|
@ -16,16 +16,13 @@ import java.util.Locale;
|
|||||||
|
|
||||||
import eu.toldi.infinityforlemmy.NetworkState;
|
import eu.toldi.infinityforlemmy.NetworkState;
|
||||||
import eu.toldi.infinityforlemmy.SortType;
|
import eu.toldi.infinityforlemmy.SortType;
|
||||||
import eu.toldi.infinityforlemmy.apis.RedditAPI;
|
import eu.toldi.infinityforlemmy.apis.LemmyAPI;
|
||||||
import eu.toldi.infinityforlemmy.post.PostPagingSource;
|
|
||||||
import eu.toldi.infinityforlemmy.utils.APIUtils;
|
|
||||||
import eu.toldi.infinityforlemmy.utils.JSONUtils;
|
|
||||||
import retrofit2.Call;
|
import retrofit2.Call;
|
||||||
import retrofit2.Callback;
|
import retrofit2.Callback;
|
||||||
import retrofit2.Response;
|
import retrofit2.Response;
|
||||||
import retrofit2.Retrofit;
|
import retrofit2.Retrofit;
|
||||||
|
|
||||||
public class CommentDataSource extends PageKeyedDataSource<String, Comment> {
|
public class CommentDataSource extends PageKeyedDataSource<Integer, Comment> {
|
||||||
|
|
||||||
private Retrofit retrofit;
|
private Retrofit retrofit;
|
||||||
private Locale locale;
|
private Locale locale;
|
||||||
@ -39,8 +36,8 @@ public class CommentDataSource extends PageKeyedDataSource<String, Comment> {
|
|||||||
private MutableLiveData<NetworkState> initialLoadStateLiveData;
|
private MutableLiveData<NetworkState> initialLoadStateLiveData;
|
||||||
private MutableLiveData<Boolean> hasPostLiveData;
|
private MutableLiveData<Boolean> hasPostLiveData;
|
||||||
|
|
||||||
private LoadParams<String> params;
|
private LoadParams<Integer> params;
|
||||||
private LoadCallback<String, Comment> callback;
|
private LoadCallback<Integer, Comment> callback;
|
||||||
|
|
||||||
CommentDataSource(Retrofit retrofit, Locale locale, @Nullable String accessToken, String username, SortType sortType,
|
CommentDataSource(Retrofit retrofit, Locale locale, @Nullable String accessToken, String username, SortType sortType,
|
||||||
boolean areSavedComments) {
|
boolean areSavedComments) {
|
||||||
@ -72,42 +69,27 @@ public class CommentDataSource extends PageKeyedDataSource<String, Comment> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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);
|
initialLoadStateLiveData.postValue(NetworkState.LOADING);
|
||||||
|
|
||||||
RedditAPI api = retrofit.create(RedditAPI.class);
|
LemmyAPI api = retrofit.create(LemmyAPI.class);
|
||||||
Call<String> commentsCall;
|
Call<String> commentsCall = api.getUserComments(username, sortType.getType().value, 1, 25, areSavedComments, accessToken);
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
commentsCall.enqueue(new Callback<String>() {
|
commentsCall.enqueue(new Callback<String>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful()) {
|
||||||
new ParseCommentAsyncTask(response.body(), locale, new ParseCommentAsyncTask.ParseCommentAsyncTaskListener() {
|
new ParseCommentAsyncTask(response.body(), locale, new ParseCommentAsyncTask.ParseCommentAsyncTaskListener() {
|
||||||
@Override
|
@Override
|
||||||
public void parseSuccessful(ArrayList<Comment> comments, String after) {
|
public void parseSuccessful(ArrayList<Comment> comments, Integer after) {
|
||||||
if (comments.size() == 0) {
|
if (comments.isEmpty()) {
|
||||||
|
callback.onResult(comments, null, null);
|
||||||
hasPostLiveData.postValue(false);
|
hasPostLiveData.postValue(false);
|
||||||
} else {
|
} else {
|
||||||
hasPostLiveData.postValue(true);
|
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);
|
initialLoadStateLiveData.postValue(NetworkState.LOADED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,39 +111,27 @@ public class CommentDataSource extends PageKeyedDataSource<String, Comment> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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
|
@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.params = params;
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
|
|
||||||
paginationNetworkStateLiveData.postValue(NetworkState.LOADING);
|
paginationNetworkStateLiveData.postValue(NetworkState.LOADING);
|
||||||
|
|
||||||
RedditAPI api = retrofit.create(RedditAPI.class);
|
LemmyAPI api = retrofit.create(LemmyAPI.class);
|
||||||
Call<String> commentsCall;
|
Call<String> commentsCall = api.getUserComments(username, sortType.getType().value, params.key, 25, areSavedComments, accessToken);
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
commentsCall.enqueue(new Callback<String>() {
|
commentsCall.enqueue(new Callback<String>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful()) {
|
||||||
new ParseCommentAsyncTask(response.body(), locale, new ParseCommentAsyncTask.ParseCommentAsyncTaskListener() {
|
new ParseCommentAsyncTask(response.body(), locale, new ParseCommentAsyncTask.ParseCommentAsyncTaskListener() {
|
||||||
@Override
|
@Override
|
||||||
public void parseSuccessful(ArrayList<Comment> comments, String after) {
|
public void parseSuccessful(ArrayList<Comment> comments, Integer after) {
|
||||||
if (after == null || after.equals("") || after.equals("null")) {
|
if (comments.isEmpty()) {
|
||||||
callback.onResult(comments, null);
|
callback.onResult(comments, null);
|
||||||
} else {
|
} else {
|
||||||
callback.onResult(comments, after);
|
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 static class ParseCommentAsyncTask extends AsyncTask<Void, ArrayList<Comment>, ArrayList<Comment>> {
|
||||||
private String after;
|
private Integer after;
|
||||||
private Locale locale;
|
private Locale locale;
|
||||||
private JSONArray commentsJSONArray;
|
private JSONArray commentsJSONArray;
|
||||||
private boolean parseFailed;
|
private boolean parseFailed;
|
||||||
@ -197,9 +167,8 @@ public class CommentDataSource extends PageKeyedDataSource<String, Comment> {
|
|||||||
this.locale = locale;
|
this.locale = locale;
|
||||||
this.parseCommentAsyncTaskListener = parseCommentAsyncTaskListener;
|
this.parseCommentAsyncTaskListener = parseCommentAsyncTaskListener;
|
||||||
try {
|
try {
|
||||||
JSONObject data = new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY);
|
JSONObject data = new JSONObject(response);
|
||||||
commentsJSONArray = data.getJSONArray(JSONUtils.CHILDREN_KEY);
|
commentsJSONArray = data.getJSONArray("comments");
|
||||||
after = data.getString(JSONUtils.AFTER_KEY);
|
|
||||||
parseFailed = false;
|
parseFailed = false;
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
parseFailed = true;
|
parseFailed = true;
|
||||||
@ -216,7 +185,7 @@ public class CommentDataSource extends PageKeyedDataSource<String, Comment> {
|
|||||||
ArrayList<Comment> comments = new ArrayList<>();
|
ArrayList<Comment> comments = new ArrayList<>();
|
||||||
for (int i = 0; i < commentsJSONArray.length(); i++) {
|
for (int i = 0; i < commentsJSONArray.length(); i++) {
|
||||||
try {
|
try {
|
||||||
JSONObject commentJSON = commentsJSONArray.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY);
|
JSONObject commentJSON = commentsJSONArray.getJSONObject(i);
|
||||||
comments.add(ParseComment.parseSingleComment(commentJSON));
|
comments.add(ParseComment.parseSingleComment(commentJSON));
|
||||||
} catch (JSONException ignored) {
|
} catch (JSONException ignored) {
|
||||||
}
|
}
|
||||||
@ -235,7 +204,7 @@ public class CommentDataSource extends PageKeyedDataSource<String, Comment> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface ParseCommentAsyncTaskListener {
|
interface ParseCommentAsyncTaskListener {
|
||||||
void parseSuccessful(ArrayList<Comment> comments, String after);
|
void parseSuccessful(ArrayList<Comment> comments, Integer page);
|
||||||
|
|
||||||
void parseFailed();
|
void parseFailed();
|
||||||
}
|
}
|
||||||
|
@ -52,7 +52,6 @@ import eu.toldi.infinityforlemmy.customtheme.CustomThemeWrapper;
|
|||||||
import eu.toldi.infinityforlemmy.customviews.LinearLayoutManagerBugFixed;
|
import eu.toldi.infinityforlemmy.customviews.LinearLayoutManagerBugFixed;
|
||||||
import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils;
|
import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils;
|
||||||
import eu.toldi.infinityforlemmy.utils.Utils;
|
import eu.toldi.infinityforlemmy.utils.Utils;
|
||||||
import retrofit2.Retrofit;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -82,9 +81,6 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
|
|||||||
@Named("no_oauth")
|
@Named("no_oauth")
|
||||||
RetrofitHolder mRetrofit;
|
RetrofitHolder mRetrofit;
|
||||||
@Inject
|
@Inject
|
||||||
@Named("oauth")
|
|
||||||
Retrofit mOauthRetrofit;
|
|
||||||
@Inject
|
|
||||||
RedditDataRoomDatabase mRedditDataRoomDatabase;
|
RedditDataRoomDatabase mRedditDataRoomDatabase;
|
||||||
@Inject
|
@Inject
|
||||||
@Named("default")
|
@Named("default")
|
||||||
@ -253,6 +249,7 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
|
|||||||
|
|
||||||
mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null);
|
mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null);
|
||||||
|
|
||||||
|
|
||||||
new Handler().postDelayed(() -> bindView(resources), 0);
|
new Handler().postDelayed(() -> bindView(resources), 0);
|
||||||
|
|
||||||
return rootView;
|
return rootView;
|
||||||
@ -263,7 +260,7 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
|
|||||||
mLinearLayoutManager = new LinearLayoutManagerBugFixed(mActivity);
|
mLinearLayoutManager = new LinearLayoutManagerBugFixed(mActivity);
|
||||||
mCommentRecyclerView.setLayoutManager(mLinearLayoutManager);
|
mCommentRecyclerView.setLayoutManager(mLinearLayoutManager);
|
||||||
|
|
||||||
mAdapter = new CommentsListingRecyclerViewAdapter(mActivity, mOauthRetrofit, customThemeWrapper,
|
mAdapter = new CommentsListingRecyclerViewAdapter(mActivity, mRetrofit.getRetrofit(), customThemeWrapper,
|
||||||
getResources().getConfiguration().locale, mSharedPreferences,
|
getResources().getConfiguration().locale, mSharedPreferences,
|
||||||
getArguments().getString(EXTRA_ACCESS_TOKEN), getArguments().getString(EXTRA_ACCOUNT_NAME),
|
getArguments().getString(EXTRA_ACCESS_TOKEN), getArguments().getString(EXTRA_ACCOUNT_NAME),
|
||||||
() -> mCommentViewModel.retryLoadingMore());
|
() -> mCommentViewModel.retryLoadingMore());
|
||||||
@ -294,15 +291,11 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
|
|||||||
|
|
||||||
CommentViewModel.Factory factory;
|
CommentViewModel.Factory factory;
|
||||||
|
|
||||||
if (mAccessToken == null) {
|
|
||||||
factory = new CommentViewModel.Factory(mRetrofit.getRetrofit(),
|
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,
|
resources.getConfiguration().locale, mAccessToken, username, sortType,
|
||||||
getArguments().getBoolean(EXTRA_ARE_SAVED_COMMENTS));
|
getArguments().getBoolean(EXTRA_ARE_SAVED_COMMENTS));
|
||||||
}
|
|
||||||
|
|
||||||
mCommentViewModel = new ViewModelProvider(this, factory).get(CommentViewModel.class);
|
mCommentViewModel = new ViewModelProvider(this, factory).get(CommentViewModel.class);
|
||||||
mCommentViewModel.getComments().observe(getViewLifecycleOwner(), comments -> mAdapter.submitList(comments));
|
mCommentViewModel.getComments().observe(getViewLifecycleOwner(), comments -> mAdapter.submitList(comments));
|
||||||
|
Loading…
Reference in New Issue
Block a user