From 62dc889867678be046a4d298782cd14305fe5c39 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 14 Aug 2019 17:18:05 +0800 Subject: Sorting user's comments is now available. --- .../infinityforreddit/CommentDataSource.java | 8 ++++--- .../CommentDataSourceFactory.java | 10 +++++++-- .../infinityforreddit/CommentViewModel.java | 25 +++++++++++++++++----- .../infinityforreddit/CommentsListingFragment.java | 7 +++++- .../infinityforreddit/RedditAPI.java | 3 ++- .../infinityforreddit/ViewUserDetailActivity.java | 24 ++++++++++++--------- 6 files changed, 55 insertions(+), 22 deletions(-) (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java index 1d759440..dd33af70 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java @@ -24,6 +24,7 @@ public class CommentDataSource extends PageKeyedDataSource private Retrofit retrofit; private Locale locale; private String username; + private String sortType; private MutableLiveData paginationNetworkStateLiveData; private MutableLiveData initialLoadStateLiveData; @@ -34,10 +35,11 @@ public class CommentDataSource extends PageKeyedDataSource private LoadParams params; private LoadCallback 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 initialLoadStateLiveData.postValue(NetworkState.LOADING); RedditAPI api = retrofit.create(RedditAPI.class); - Call commentsCall = api.getUserComments(username, null); + Call commentsCall = api.getUserComments(username, null, sortType); commentsCall.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { @@ -122,7 +124,7 @@ public class CommentDataSource extends PageKeyedDataSource paginationNetworkStateLiveData.postValue(NetworkState.LOADING); RedditAPI api = retrofit.create(RedditAPI.class); - Call bestPost = api.getUserComments(username, params.key); + Call bestPost = api.getUserComments(username, params.key, sortType); bestPost.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSourceFactory.java index 914af4a4..bfe83a82 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSourceFactory.java @@ -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 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; + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentViewModel.java index b1fd421d..38bc1844 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentViewModel.java @@ -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 initialLoadingState; private LiveData hasCommentLiveData; private LiveData> comments; + private MutableLiveData 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> 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 create(@NonNull Class modelClass) { - return (T) new CommentViewModel(retrofit, locale, username); + return (T) new CommentViewModel(retrofit, locale, username, sortType); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingFragment.java index faffc24f..272ed624 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingFragment.java @@ -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); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index a3abf798..ce351359 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -67,7 +67,8 @@ public interface RedditAPI { Call getUserData(@Path("username") String username); @GET("user/{username}/comments.json?raw_json=1") - Call getUserComments(@Path("username") String username, @Query("after") String after); + Call getUserComments(@Path("username") String username, @Query("after") String after, + @Query("sort") String sortType); @FormUrlEncoded @POST("api/subscribe") diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java index 46cacbd0..bcecd794 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java @@ -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); + } } } } -- cgit v1.2.3