mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-01-11 10:47:11 +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 Locale locale;
|
||||
private String username;
|
||||
private String sortType;
|
||||
|
||||
private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
|
||||
private MutableLiveData<NetworkState> initialLoadStateLiveData;
|
||||
@ -34,10 +35,11 @@ public class CommentDataSource extends PageKeyedDataSource<String, CommentData>
|
||||
private LoadParams<String> params;
|
||||
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.locale = locale;
|
||||
this.username = username;
|
||||
this.sortType = sortType;
|
||||
paginationNetworkStateLiveData = new MutableLiveData<>();
|
||||
initialLoadStateLiveData = new MutableLiveData<>();
|
||||
hasPostLiveData = new MutableLiveData<>();
|
||||
@ -71,7 +73,7 @@ public class CommentDataSource extends PageKeyedDataSource<String, CommentData>
|
||||
initialLoadStateLiveData.postValue(NetworkState.LOADING);
|
||||
|
||||
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>() {
|
||||
@Override
|
||||
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);
|
||||
|
||||
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>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
||||
|
@ -12,21 +12,23 @@ class CommentDataSourceFactory extends DataSource.Factory {
|
||||
private Retrofit retrofit;
|
||||
private Locale locale;
|
||||
private String username;
|
||||
private String sortType;
|
||||
|
||||
private CommentDataSource commentDataSource;
|
||||
private MutableLiveData<CommentDataSource> commentDataSourceLiveData;
|
||||
|
||||
CommentDataSourceFactory(Retrofit retrofit, Locale locale, String username) {
|
||||
CommentDataSourceFactory(Retrofit retrofit, Locale locale, String username, String sortType) {
|
||||
this.retrofit = retrofit;
|
||||
this.locale = locale;
|
||||
this.username = username;
|
||||
this.sortType = sortType;
|
||||
commentDataSourceLiveData = new MutableLiveData<>();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public DataSource create() {
|
||||
commentDataSource = new CommentDataSource(retrofit, locale, username);
|
||||
commentDataSource = new CommentDataSource(retrofit, locale, username, sortType);
|
||||
commentDataSourceLiveData.postValue(commentDataSource);
|
||||
return commentDataSource;
|
||||
}
|
||||
@ -38,4 +40,8 @@ class CommentDataSourceFactory extends DataSource.Factory {
|
||||
CommentDataSource getCommentDataSource() {
|
||||
return commentDataSource;
|
||||
}
|
||||
|
||||
void changeSortType(String sortType) {
|
||||
this.sortType = sortType;
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package ml.docilealligator.infinityforreddit;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.Transformations;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
@ -18,9 +19,10 @@ public class CommentViewModel extends ViewModel {
|
||||
private LiveData<NetworkState> initialLoadingState;
|
||||
private LiveData<Boolean> hasCommentLiveData;
|
||||
private LiveData<PagedList<CommentData>> comments;
|
||||
private MutableLiveData<String> sortTypeLiveData;
|
||||
|
||||
public CommentViewModel(Retrofit retrofit, Locale locale, String username) {
|
||||
commentDataSourceFactory = new CommentDataSourceFactory(retrofit, locale, username);
|
||||
public CommentViewModel(Retrofit retrofit, Locale locale, String username, String sortType) {
|
||||
commentDataSourceFactory = new CommentDataSourceFactory(retrofit, locale, username, sortType);
|
||||
|
||||
initialLoadingState = Transformations.switchMap(commentDataSourceFactory.getCommentDataSourceLiveData(),
|
||||
CommentDataSource::getInitialLoadStateLiveData);
|
||||
@ -28,13 +30,20 @@ public class CommentViewModel extends ViewModel {
|
||||
CommentDataSource::getPaginationNetworkStateLiveData);
|
||||
hasCommentLiveData = Transformations.switchMap(commentDataSourceFactory.getCommentDataSourceLiveData(),
|
||||
CommentDataSource::hasPostLiveData);
|
||||
|
||||
sortTypeLiveData = new MutableLiveData<>();
|
||||
sortTypeLiveData.postValue(sortType);
|
||||
|
||||
PagedList.Config pagedListConfig =
|
||||
(new PagedList.Config.Builder())
|
||||
.setEnablePlaceholders(false)
|
||||
.setPageSize(25)
|
||||
.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() {
|
||||
@ -65,21 +74,27 @@ public class CommentViewModel extends ViewModel {
|
||||
commentDataSourceFactory.getCommentDataSource().retryLoadingMore();
|
||||
}
|
||||
|
||||
void changeSortType(String sortType) {
|
||||
sortTypeLiveData.postValue(sortType);
|
||||
}
|
||||
|
||||
public static class Factory extends ViewModelProvider.NewInstanceFactory {
|
||||
private Retrofit retrofit;
|
||||
private Locale locale;
|
||||
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.locale = locale;
|
||||
this.username = username;
|
||||
this.sortType = sortType;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
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);
|
||||
|
||||
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.getComments().observe(this, comments -> mAdapter.submitList(comments));
|
||||
|
||||
@ -135,6 +136,10 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
|
||||
return rootView;
|
||||
}
|
||||
|
||||
void changeSortType(String sortType) {
|
||||
mCommentViewModel.changeSortType(sortType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(@NonNull Context context) {
|
||||
super.onAttach(context);
|
||||
|
@ -67,7 +67,8 @@ public interface RedditAPI {
|
||||
Call<String> getUserData(@Path("username") String username);
|
||||
|
||||
@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
|
||||
@POST("api/subscribe")
|
||||
|
@ -581,21 +581,25 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi
|
||||
|
||||
public void refresh() {
|
||||
if (viewPager.getCurrentItem() == 0) {
|
||||
postFragment.refresh();
|
||||
if(postFragment != null) {
|
||||
postFragment.refresh();
|
||||
}
|
||||
} else {
|
||||
commentsListingFragment.refresh();
|
||||
}
|
||||
}
|
||||
|
||||
public void refreshComments() {
|
||||
if(commentsListingFragment != null) {
|
||||
commentsListingFragment.refresh();
|
||||
if(commentsListingFragment != null) {
|
||||
commentsListingFragment.refresh();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void changeSortType(String sortType) {
|
||||
if(postFragment != null) {
|
||||
postFragment.changeSortType(sortType);
|
||||
if(viewPager.getCurrentItem() == 0) {
|
||||
if(postFragment != null) {
|
||||
postFragment.changeSortType(sortType);
|
||||
}
|
||||
} else {
|
||||
if(commentsListingFragment != null) {
|
||||
commentsListingFragment.changeSortType(sortType);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user