mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-01-26 01:34:45 +01:00
Sorting user's comments is now available.
This commit is contained in:
parent
2dbb854bfc
commit
62dc889867
@ -24,6 +24,7 @@ public class CommentDataSource extends PageKeyedDataSource<String, CommentData>
|
|||||||
private Retrofit retrofit;
|
private Retrofit retrofit;
|
||||||
private Locale locale;
|
private Locale locale;
|
||||||
private String username;
|
private String username;
|
||||||
|
private String sortType;
|
||||||
|
|
||||||
private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
|
private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
|
||||||
private MutableLiveData<NetworkState> initialLoadStateLiveData;
|
private MutableLiveData<NetworkState> initialLoadStateLiveData;
|
||||||
@ -34,10 +35,11 @@ public class CommentDataSource extends PageKeyedDataSource<String, CommentData>
|
|||||||
private LoadParams<String> params;
|
private LoadParams<String> params;
|
||||||
private LoadCallback<String, CommentData> callback;
|
private LoadCallback<String, CommentData> callback;
|
||||||
|
|
||||||
CommentDataSource(Retrofit retrofit, Locale locale, String username) {
|
CommentDataSource(Retrofit retrofit, Locale locale, String username, String sortType) {
|
||||||
this.retrofit = retrofit;
|
this.retrofit = retrofit;
|
||||||
this.locale = locale;
|
this.locale = locale;
|
||||||
this.username = username;
|
this.username = username;
|
||||||
|
this.sortType = sortType;
|
||||||
paginationNetworkStateLiveData = new MutableLiveData<>();
|
paginationNetworkStateLiveData = new MutableLiveData<>();
|
||||||
initialLoadStateLiveData = new MutableLiveData<>();
|
initialLoadStateLiveData = new MutableLiveData<>();
|
||||||
hasPostLiveData = new MutableLiveData<>();
|
hasPostLiveData = new MutableLiveData<>();
|
||||||
@ -71,7 +73,7 @@ public class CommentDataSource extends PageKeyedDataSource<String, CommentData>
|
|||||||
initialLoadStateLiveData.postValue(NetworkState.LOADING);
|
initialLoadStateLiveData.postValue(NetworkState.LOADING);
|
||||||
|
|
||||||
RedditAPI api = retrofit.create(RedditAPI.class);
|
RedditAPI api = retrofit.create(RedditAPI.class);
|
||||||
Call<String> commentsCall = api.getUserComments(username, null);
|
Call<String> commentsCall = api.getUserComments(username, null, sortType);
|
||||||
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) {
|
||||||
@ -122,7 +124,7 @@ public class CommentDataSource extends PageKeyedDataSource<String, CommentData>
|
|||||||
paginationNetworkStateLiveData.postValue(NetworkState.LOADING);
|
paginationNetworkStateLiveData.postValue(NetworkState.LOADING);
|
||||||
|
|
||||||
RedditAPI api = retrofit.create(RedditAPI.class);
|
RedditAPI api = retrofit.create(RedditAPI.class);
|
||||||
Call<String> bestPost = api.getUserComments(username, params.key);
|
Call<String> bestPost = api.getUserComments(username, params.key, sortType);
|
||||||
bestPost.enqueue(new Callback<String>() {
|
bestPost.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) {
|
||||||
|
@ -12,21 +12,23 @@ class CommentDataSourceFactory extends DataSource.Factory {
|
|||||||
private Retrofit retrofit;
|
private Retrofit retrofit;
|
||||||
private Locale locale;
|
private Locale locale;
|
||||||
private String username;
|
private String username;
|
||||||
|
private String sortType;
|
||||||
|
|
||||||
private CommentDataSource commentDataSource;
|
private CommentDataSource commentDataSource;
|
||||||
private MutableLiveData<CommentDataSource> commentDataSourceLiveData;
|
private MutableLiveData<CommentDataSource> commentDataSourceLiveData;
|
||||||
|
|
||||||
CommentDataSourceFactory(Retrofit retrofit, Locale locale, String username) {
|
CommentDataSourceFactory(Retrofit retrofit, Locale locale, String username, String sortType) {
|
||||||
this.retrofit = retrofit;
|
this.retrofit = retrofit;
|
||||||
this.locale = locale;
|
this.locale = locale;
|
||||||
this.username = username;
|
this.username = username;
|
||||||
|
this.sortType = sortType;
|
||||||
commentDataSourceLiveData = new MutableLiveData<>();
|
commentDataSourceLiveData = new MutableLiveData<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public DataSource create() {
|
public DataSource create() {
|
||||||
commentDataSource = new CommentDataSource(retrofit, locale, username);
|
commentDataSource = new CommentDataSource(retrofit, locale, username, sortType);
|
||||||
commentDataSourceLiveData.postValue(commentDataSource);
|
commentDataSourceLiveData.postValue(commentDataSource);
|
||||||
return commentDataSource;
|
return commentDataSource;
|
||||||
}
|
}
|
||||||
@ -38,4 +40,8 @@ class CommentDataSourceFactory extends DataSource.Factory {
|
|||||||
CommentDataSource getCommentDataSource() {
|
CommentDataSource getCommentDataSource() {
|
||||||
return commentDataSource;
|
return commentDataSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void changeSortType(String sortType) {
|
||||||
|
this.sortType = sortType;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package ml.docilealligator.infinityforreddit;
|
|||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.lifecycle.LiveData;
|
import androidx.lifecycle.LiveData;
|
||||||
|
import androidx.lifecycle.MutableLiveData;
|
||||||
import androidx.lifecycle.Transformations;
|
import androidx.lifecycle.Transformations;
|
||||||
import androidx.lifecycle.ViewModel;
|
import androidx.lifecycle.ViewModel;
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
@ -18,9 +19,10 @@ public class CommentViewModel extends ViewModel {
|
|||||||
private LiveData<NetworkState> initialLoadingState;
|
private LiveData<NetworkState> initialLoadingState;
|
||||||
private LiveData<Boolean> hasCommentLiveData;
|
private LiveData<Boolean> hasCommentLiveData;
|
||||||
private LiveData<PagedList<CommentData>> comments;
|
private LiveData<PagedList<CommentData>> comments;
|
||||||
|
private MutableLiveData<String> sortTypeLiveData;
|
||||||
|
|
||||||
public CommentViewModel(Retrofit retrofit, Locale locale, String username) {
|
public CommentViewModel(Retrofit retrofit, Locale locale, String username, String sortType) {
|
||||||
commentDataSourceFactory = new CommentDataSourceFactory(retrofit, locale, username);
|
commentDataSourceFactory = new CommentDataSourceFactory(retrofit, locale, username, sortType);
|
||||||
|
|
||||||
initialLoadingState = Transformations.switchMap(commentDataSourceFactory.getCommentDataSourceLiveData(),
|
initialLoadingState = Transformations.switchMap(commentDataSourceFactory.getCommentDataSourceLiveData(),
|
||||||
CommentDataSource::getInitialLoadStateLiveData);
|
CommentDataSource::getInitialLoadStateLiveData);
|
||||||
@ -28,13 +30,20 @@ public class CommentViewModel extends ViewModel {
|
|||||||
CommentDataSource::getPaginationNetworkStateLiveData);
|
CommentDataSource::getPaginationNetworkStateLiveData);
|
||||||
hasCommentLiveData = Transformations.switchMap(commentDataSourceFactory.getCommentDataSourceLiveData(),
|
hasCommentLiveData = Transformations.switchMap(commentDataSourceFactory.getCommentDataSourceLiveData(),
|
||||||
CommentDataSource::hasPostLiveData);
|
CommentDataSource::hasPostLiveData);
|
||||||
|
|
||||||
|
sortTypeLiveData = new MutableLiveData<>();
|
||||||
|
sortTypeLiveData.postValue(sortType);
|
||||||
|
|
||||||
PagedList.Config pagedListConfig =
|
PagedList.Config pagedListConfig =
|
||||||
(new PagedList.Config.Builder())
|
(new PagedList.Config.Builder())
|
||||||
.setEnablePlaceholders(false)
|
.setEnablePlaceholders(false)
|
||||||
.setPageSize(25)
|
.setPageSize(25)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
comments = (new LivePagedListBuilder(commentDataSourceFactory, pagedListConfig)).build();
|
comments = Transformations.switchMap(sortTypeLiveData, sort -> {
|
||||||
|
commentDataSourceFactory.changeSortType(sortTypeLiveData.getValue());
|
||||||
|
return (new LivePagedListBuilder(commentDataSourceFactory, pagedListConfig)).build();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
LiveData<PagedList<CommentData>> getComments() {
|
LiveData<PagedList<CommentData>> getComments() {
|
||||||
@ -65,21 +74,27 @@ public class CommentViewModel extends ViewModel {
|
|||||||
commentDataSourceFactory.getCommentDataSource().retryLoadingMore();
|
commentDataSourceFactory.getCommentDataSource().retryLoadingMore();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void changeSortType(String sortType) {
|
||||||
|
sortTypeLiveData.postValue(sortType);
|
||||||
|
}
|
||||||
|
|
||||||
public static class Factory extends ViewModelProvider.NewInstanceFactory {
|
public static class Factory extends ViewModelProvider.NewInstanceFactory {
|
||||||
private Retrofit retrofit;
|
private Retrofit retrofit;
|
||||||
private Locale locale;
|
private Locale locale;
|
||||||
private String username;
|
private String username;
|
||||||
|
private String sortType;
|
||||||
|
|
||||||
public Factory(Retrofit retrofit, Locale locale, String username) {
|
public Factory(Retrofit retrofit, Locale locale, String username, String sortType) {
|
||||||
this.retrofit = retrofit;
|
this.retrofit = retrofit;
|
||||||
this.locale = locale;
|
this.locale = locale;
|
||||||
this.username = username;
|
this.username = username;
|
||||||
|
this.sortType = sortType;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
|
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
|
||||||
return (T) new CommentViewModel(retrofit, locale, username);
|
return (T) new CommentViewModel(retrofit, locale, username, sortType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -101,7 +101,8 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
|
|||||||
|
|
||||||
mCommentRecyclerView.setAdapter(mAdapter);
|
mCommentRecyclerView.setAdapter(mAdapter);
|
||||||
|
|
||||||
CommentViewModel.Factory factory = new CommentViewModel.Factory(mRetrofit, resources.getConfiguration().locale, username);
|
CommentViewModel.Factory factory = new CommentViewModel.Factory(mRetrofit,
|
||||||
|
resources.getConfiguration().locale, username, PostDataSource.SORT_TYPE_NEW);
|
||||||
mCommentViewModel = new ViewModelProvider(this, factory).get(CommentViewModel.class);
|
mCommentViewModel = new ViewModelProvider(this, factory).get(CommentViewModel.class);
|
||||||
mCommentViewModel.getComments().observe(this, comments -> mAdapter.submitList(comments));
|
mCommentViewModel.getComments().observe(this, comments -> mAdapter.submitList(comments));
|
||||||
|
|
||||||
@ -135,6 +136,10 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
|
|||||||
return rootView;
|
return rootView;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void changeSortType(String sortType) {
|
||||||
|
mCommentViewModel.changeSortType(sortType);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAttach(@NonNull Context context) {
|
public void onAttach(@NonNull Context context) {
|
||||||
super.onAttach(context);
|
super.onAttach(context);
|
||||||
|
@ -67,7 +67,8 @@ public interface RedditAPI {
|
|||||||
Call<String> getUserData(@Path("username") String username);
|
Call<String> getUserData(@Path("username") String username);
|
||||||
|
|
||||||
@GET("user/{username}/comments.json?raw_json=1")
|
@GET("user/{username}/comments.json?raw_json=1")
|
||||||
Call<String> getUserComments(@Path("username") String username, @Query("after") String after);
|
Call<String> getUserComments(@Path("username") String username, @Query("after") String after,
|
||||||
|
@Query("sort") String sortType);
|
||||||
|
|
||||||
@FormUrlEncoded
|
@FormUrlEncoded
|
||||||
@POST("api/subscribe")
|
@POST("api/subscribe")
|
||||||
|
@ -581,21 +581,25 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi
|
|||||||
|
|
||||||
public void refresh() {
|
public void refresh() {
|
||||||
if (viewPager.getCurrentItem() == 0) {
|
if (viewPager.getCurrentItem() == 0) {
|
||||||
postFragment.refresh();
|
if(postFragment != null) {
|
||||||
|
postFragment.refresh();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
commentsListingFragment.refresh();
|
if(commentsListingFragment != null) {
|
||||||
}
|
commentsListingFragment.refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void refreshComments() {
|
|
||||||
if(commentsListingFragment != null) {
|
|
||||||
commentsListingFragment.refresh();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void changeSortType(String sortType) {
|
public void changeSortType(String sortType) {
|
||||||
if(postFragment != null) {
|
if(viewPager.getCurrentItem() == 0) {
|
||||||
postFragment.changeSortType(sortType);
|
if(postFragment != null) {
|
||||||
|
postFragment.changeSortType(sortType);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if(commentsListingFragment != null) {
|
||||||
|
commentsListingFragment.changeSortType(sortType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user