aboutsummaryrefslogtreecommitdiff
path: root/app/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java8
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSourceFactory.java10
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/CommentViewModel.java25
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingFragment.java7
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java3
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java24
6 files changed, 55 insertions, 22 deletions
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<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) {
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<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;
+ }
}
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<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);
}
}
}
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<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")
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);
+ }
}
}
}