aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2019-07-28 16:52:08 +0000
committerAlex Ning <chineseperson5@gmail.com>2019-07-28 16:52:08 +0000
commit111ba8e5fc95c3fb5dbbff472e1a8dbbb52a28af (patch)
tree426b3e0188d931189b69e051f54fe3c4bb319c6c /app/src/main/java
parent40fc423b337352e1a47e361dac052213bb81ee26 (diff)
downloadinfinity-for-reddit-111ba8e5fc95c3fb5dbbff472e1a8dbbb52a28af.tar
infinity-for-reddit-111ba8e5fc95c3fb5dbbff472e1a8dbbb52a28af.tar.gz
infinity-for-reddit-111ba8e5fc95c3fb5dbbff472e1a8dbbb52a28af.tar.bz2
infinity-for-reddit-111ba8e5fc95c3fb5dbbff472e1a8dbbb52a28af.tar.lz
infinity-for-reddit-111ba8e5fc95c3fb5dbbff472e1a8dbbb52a28af.tar.xz
infinity-for-reddit-111ba8e5fc95c3fb5dbbff472e1a8dbbb52a28af.tar.zst
infinity-for-reddit-111ba8e5fc95c3fb5dbbff472e1a8dbbb52a28af.zip
Sorting in SearchResultActivity is available. Switch DataSource instead of replacing fragments when changing sorting types. Minor bugs fixed.
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java7
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java7
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java18
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java8
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java4
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java4
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java37
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java6
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SearchPostSortTypeBottomSheetFragment.java77
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java58
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SearchUserAndSubredditSortTypeBottomSheetFragment.java61
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SortTypeBottomSheetFragment.java2
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSource.java15
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSourceFactory.java10
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java24
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingViewModel.java28
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSource.java16
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSourceFactory.java10
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/UserListingFragment.java28
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/UserListingViewModel.java32
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java20
21 files changed, 371 insertions, 101 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java
index b7c5a73e..9b96e087 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java
@@ -2,10 +2,11 @@ package ml.docilealligator.infinityforreddit;
import android.util.Log;
+import androidx.annotation.NonNull;
+
import java.util.ArrayList;
import SubredditDatabase.SubredditData;
-import androidx.annotation.NonNull;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
@@ -56,10 +57,10 @@ class FetchSubredditData {
});
}
- static void fetchSubredditListingData(Retrofit retrofit, String query, String after, final FetchSubredditListingDataListener fetchSubredditListingDataListener) {
+ static void fetchSubredditListingData(Retrofit retrofit, String query, String after, String sortType, final FetchSubredditListingDataListener fetchSubredditListingDataListener) {
RedditAPI api = retrofit.create(RedditAPI.class);
- Call<String> subredditDataCall = api.searchSubreddits(query, after);
+ Call<String> subredditDataCall = api.searchSubreddits(query, after, sortType);
subredditDataCall.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/FetchUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java
index 7f2e2528..8eadcd89 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java
@@ -2,10 +2,11 @@ package ml.docilealligator.infinityforreddit;
import android.util.Log;
+import androidx.annotation.NonNull;
+
import java.util.ArrayList;
import User.UserData;
-import androidx.annotation.NonNull;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
@@ -55,11 +56,11 @@ public class FetchUserData {
});
}
- public static void fetchUserListingData(Retrofit retrofit, String query, String after,
+ public static void fetchUserListingData(Retrofit retrofit, String query, String after, String sortType,
FetchUserListingDataListener fetchUserListingDataListener) {
RedditAPI api = retrofit.create(RedditAPI.class);
- Call<String> userInfo = api.searchUsers(query, after);
+ Call<String> userInfo = api.searchUsers(query, after, sortType);
userInfo.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java
index 08dba87e..f32613f4 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java
@@ -114,7 +114,12 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
startActivityForResult(loginIntent, LOGIN_ACTIVITY_REQUEST_CODE);
} else {
if (savedInstanceState == null) {
- replaceFragment(PostDataSource.SORT_TYPE_BEST);
+ mFragment = new PostFragment();
+ Bundle bundle = new Bundle();
+ bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_FRONT_PAGE);
+ bundle.putString(PostFragment.EXTRA_SORT_TYPE, PostDataSource.SORT_TYPE_BEST);
+ mFragment.setArguments(bundle);
+ getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_content_main, mFragment).commit();
} else {
mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_content_main, mFragment).commit();
@@ -176,15 +181,6 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
fab.setOnClickListener(view -> postTypeBottomSheetFragment.show(getSupportFragmentManager(), postTypeBottomSheetFragment.getTag()));
}
- private void replaceFragment(String sortType) {
- mFragment = new PostFragment();
- Bundle bundle = new Bundle();
- bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_FRONT_PAGE);
- bundle.putString(PostFragment.EXTRA_SORT_TYPE, sortType);
- mFragment.setArguments(bundle);
- getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_content_main, mFragment).commit();
- }
-
private void loadUserData() {
if (!mFetchUserInfoSuccess) {
FetchMyInfo.fetchMyInfo(mOauthRetrofit, mAuthInfoSharedPreferences, new FetchMyInfo.FetchUserMyListener() {
@@ -330,7 +326,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
@Override
public void sortTypeSelected(String sortType) {
- replaceFragment(sortType);
+ ((PostFragment) mFragment).changeSortType(sortType);
}
@Override
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java
index 2a96524e..b596fc75 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java
@@ -31,6 +31,8 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
static final String SORT_TYPE_RISING = "rising";
static final String SORT_TYPE_TOP = "top";
static final String SORT_TYPE_CONTROVERSIAL = "controversial";
+ static final String SORT_TYPE_RELEVANCE = "relevance";
+ static final String SORT_TYPE_COMMENTS = "comments";
private Retrofit retrofit;
private String accessToken;
@@ -459,7 +461,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
Call<String> getPost;
if(subredditName == null) {
- getPost = api.searchPosts(query, null, RedditUtils.getOAuthHeader(accessToken));
+ getPost = api.searchPosts(query, null, sortType, RedditUtils.getOAuthHeader(accessToken));
} else {
getPost = api.searchPostsInSpecificSubreddit(subredditName, query, null, RedditUtils.getOAuthHeader(accessToken));
}
@@ -467,6 +469,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
getPost.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
+ Log.i("initial", call.request().url().toString());
if(response.isSuccessful()) {
ParsePost.parsePosts(response.body(), locale, -1,
new ParsePost.ParsePostsListingListener() {
@@ -507,7 +510,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
Call<String> getPost;
if(subredditName == null) {
- getPost = api.searchPosts(subredditName, params.key, RedditUtils.getOAuthHeader(accessToken));
+ getPost = api.searchPosts(query, params.key, sortType, RedditUtils.getOAuthHeader(accessToken));
} else {
getPost = api.searchPostsInSpecificSubreddit(subredditName, query, params.key, RedditUtils.getOAuthHeader(accessToken));
}
@@ -515,6 +518,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
getPost.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
+ Log.i("after", call.request().url().toString());
if(response.isSuccessful()) {
ParsePost.parsePosts(response.body(), locale, -1, new ParsePost.ParsePostsListingListener() {
@Override
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java
index 31e91c54..79a533e0 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java
@@ -90,4 +90,8 @@ class PostDataSourceFactory extends DataSource.Factory {
PostDataSource getPostDataSource() {
return postDataSource;
}
+
+ void changeSortType(String sortType) {
+ this.sortType = sortType;
+ }
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java
index 7196e640..f78d00b6 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java
@@ -287,6 +287,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
return rootView;
}
+ void changeSortType(String sortType) {
+ mPostViewModel.changeSortType(sortType);
+ }
+
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java
index 3c5548f8..c824567f 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java
@@ -3,6 +3,7 @@ package ml.docilealligator.infinityforreddit;
import androidx.annotation.NonNull;
import androidx.arch.core.util.Function;
import androidx.lifecycle.LiveData;
+import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
@@ -18,6 +19,7 @@ public class PostViewModel extends ViewModel {
private LiveData<NetworkState> paginationNetworkState;
private LiveData<NetworkState> initialLoadingState;
private LiveData<PagedList<Post>> posts;
+ private MutableLiveData<String> sortTypeLiveData;
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType,
PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
@@ -27,13 +29,20 @@ public class PostViewModel extends ViewModel {
(Function<PostDataSource, LiveData<NetworkState>>) PostDataSource::getInitialLoadStateLiveData);
paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
(Function<PostDataSource, LiveData<NetworkState>>) PostDataSource::getPaginationNetworkStateLiveData);
+
+ sortTypeLiveData = new MutableLiveData<>();
+ sortTypeLiveData.postValue(sortType);
+
PagedList.Config pagedListConfig =
(new PagedList.Config.Builder())
.setEnablePlaceholders(false)
.setPageSize(25)
.build();
- posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
+ posts = Transformations.switchMap(sortTypeLiveData, sort -> {
+ postDataSourceFactory.changeSortType(sortTypeLiveData.getValue());
+ return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
+ });
}
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType,
@@ -41,9 +50,12 @@ public class PostViewModel extends ViewModel {
postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName, postType, sortType, onPostFetchedCallback);
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
- dataSource -> dataSource.getInitialLoadStateLiveData());
+ (Function<PostDataSource, LiveData<NetworkState>>) PostDataSource::getInitialLoadStateLiveData);
paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
- dataSource -> dataSource.getPaginationNetworkStateLiveData());
+ (Function<PostDataSource, LiveData<NetworkState>>) PostDataSource::getPaginationNetworkStateLiveData);
+
+ sortTypeLiveData = new MutableLiveData<>();
+ sortTypeLiveData.postValue(sortType);
PagedList.Config pagedListConfig =
(new PagedList.Config.Builder())
@@ -51,7 +63,10 @@ public class PostViewModel extends ViewModel {
.setPageSize(25)
.build();
- posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
+ posts = Transformations.switchMap(sortTypeLiveData, sort -> {
+ postDataSourceFactory.changeSortType(sortTypeLiveData.getValue());
+ return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
+ });
}
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType,
@@ -61,7 +76,7 @@ public class PostViewModel extends ViewModel {
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
dataSource -> dataSource.getInitialLoadStateLiveData());
paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
- dataSource -> dataSource.getPaginationNetworkStateLiveData());
+ (Function<PostDataSource, LiveData<NetworkState>>) PostDataSource::getPaginationNetworkStateLiveData);
PagedList.Config pagedListConfig =
(new PagedList.Config.Builder())
@@ -82,13 +97,19 @@ public class PostViewModel extends ViewModel {
paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
dataSource -> dataSource.getPaginationNetworkStateLiveData());
+ sortTypeLiveData = new MutableLiveData<>();
+ sortTypeLiveData.postValue(sortType);
+
PagedList.Config pagedListConfig =
(new PagedList.Config.Builder())
.setEnablePlaceholders(false)
.setPageSize(25)
.build();
- posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
+ posts = Transformations.switchMap(sortTypeLiveData, sort -> {
+ postDataSourceFactory.changeSortType(sortTypeLiveData.getValue());
+ return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
+ });
}
LiveData<PagedList<Post>> getPosts() {
@@ -115,6 +136,10 @@ public class PostViewModel extends ViewModel {
postDataSourceFactory.getPostDataSource().retryLoadingMore();
}
+ void changeSortType(String sortType) {
+ sortTypeLiveData.postValue(sortType);
+ }
+
public static class Factory extends ViewModelProvider.NewInstanceFactory {
private Retrofit retrofit;
private String accessToken;
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java
index 0aba1d50..f0a9ac5b 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java
@@ -66,13 +66,13 @@ public interface RedditAPI {
Call<String> getInfo(@Path("subredditNamePrefixed") String subredditNamePrefixed, @Query("id") String id);
@GET("subreddits/search.json?raw_json=1&include_over_18=on")
- Call<String> searchSubreddits(@Query("q") String subredditName, @Query("after") String after);
+ Call<String> searchSubreddits(@Query("q") String subredditName, @Query("after") String after, @Query("sort") String sort);
@GET("search.json?raw_json=1&type=user&include_over_18=on")
- Call<String> searchUsers(@Query("q") String profileName, @Query("after") String after);
+ Call<String> searchUsers(@Query("q") String profileName, @Query("after") String after, @Query("sort") String sort);
@GET("search.json?raw_json=1&type=link&include_over_18=on")
- Call<String> searchPosts(@Query("q") String query, @Query("after") String after,
+ Call<String> searchPosts(@Query("q") String query, @Query("after") String after, @Query("sort") String sort,
@HeaderMap Map<String, String> headers);
@GET("r/{subredditName}/search.json?raw_json=1&type=link&restrict_sr=true&include_over_18=on")
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchPostSortTypeBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchPostSortTypeBottomSheetFragment.java
new file mode 100644
index 00000000..1bc862cc
--- /dev/null
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchPostSortTypeBottomSheetFragment.java
@@ -0,0 +1,77 @@
+package ml.docilealligator.infinityforreddit;
+
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.TextView;
+
+import androidx.fragment.app.Fragment;
+
+import com.deishelon.roundedbottomsheet.RoundedBottomSheetDialogFragment;
+
+import butterknife.BindView;
+import butterknife.ButterKnife;
+
+
+/**
+ * A simple {@link Fragment} subclass.
+ */
+public class SearchPostSortTypeBottomSheetFragment extends RoundedBottomSheetDialogFragment {
+
+ interface SearchSortTypeSelectionCallback {
+ void searchSortTypeSelected(String sortType);
+ }
+
+ static final String EXTRA_FRAGMENT_POSITION = "EFP";
+
+ @BindView(R.id.relevance_type_text_view_search_sort_type_bottom_sheet_fragment) TextView relevanceTypeTextView;
+ @BindView(R.id.hot_type_text_view_search_sort_type_bottom_sheet_fragment) TextView hotTypeTextView;
+ @BindView(R.id.top_type_text_view_search_sort_type_bottom_sheet_fragment) TextView topTypeTextView;
+ @BindView(R.id.new_type_text_view_search_sort_type_bottom_sheet_fragment) TextView newTypeTextView;
+ @BindView(R.id.comments_type_text_view_search_sort_type_bottom_sheet_fragment) TextView commentsTypeTextView;
+
+ public SearchPostSortTypeBottomSheetFragment() {
+ // Required empty public constructor
+ }
+
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View rootView = inflater.inflate(R.layout.fragment_search_post_sort_type_bottom_sheet, container, false);
+ ButterKnife.bind(this, rootView);
+
+ Activity activity = getActivity();
+
+ relevanceTypeTextView.setOnClickListener(view -> {
+ ((SearchSortTypeSelectionCallback) activity).searchSortTypeSelected(PostDataSource.SORT_TYPE_RELEVANCE);
+ dismiss();
+ });
+
+ hotTypeTextView.setOnClickListener(view -> {
+ ((SearchSortTypeSelectionCallback) activity).searchSortTypeSelected(PostDataSource.SORT_TYPE_HOT);
+ dismiss();
+ });
+
+ topTypeTextView.setOnClickListener(view -> {
+ ((SearchSortTypeSelectionCallback) activity).searchSortTypeSelected(PostDataSource.SORT_TYPE_TOP);
+ dismiss();
+ });
+
+ newTypeTextView.setOnClickListener(view -> {
+ ((SearchSortTypeSelectionCallback) activity).searchSortTypeSelected(PostDataSource.SORT_TYPE_NEW);
+ dismiss();
+ });
+
+ commentsTypeTextView.setOnClickListener(view -> {
+ ((SearchSortTypeSelectionCallback) activity).searchSortTypeSelected(PostDataSource.SORT_TYPE_COMMENTS);
+ dismiss();
+ });
+
+ return rootView;
+ }
+
+}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java
index b8f304bd..a63f1de2 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java
@@ -19,7 +19,8 @@ import com.google.android.material.tabs.TabLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
-public class SearchResultActivity extends AppCompatActivity {
+public class SearchResultActivity extends AppCompatActivity implements SearchPostSortTypeBottomSheetFragment.SearchSortTypeSelectionCallback,
+ SearchUserAndSubredditSortTypeBottomSheetFragment.SearchUserAndSubredditSortTypeSelectionCallback {
static final String EXTRA_QUERY = "QK";
static final String EXTRA_SUBREDDIT_NAME = "ESN";
@@ -32,6 +33,9 @@ public class SearchResultActivity extends AppCompatActivity {
private SectionsPagerAdapter sectionsPagerAdapter;
+ private SearchPostSortTypeBottomSheetFragment searchPostSortTypeBottomSheetFragment;
+ private SearchUserAndSubredditSortTypeBottomSheetFragment searchUserAndSubredditSortTypeBottomSheetFragment;
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -45,8 +49,16 @@ public class SearchResultActivity extends AppCompatActivity {
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(sectionsPagerAdapter);
viewPager.setOffscreenPageLimit(2);
+
tabLayout.setupWithViewPager(viewPager);
+ searchPostSortTypeBottomSheetFragment = new SearchPostSortTypeBottomSheetFragment();
+ Bundle bundle = new Bundle();
+ bundle.putInt(SearchPostSortTypeBottomSheetFragment.EXTRA_FRAGMENT_POSITION, viewPager.getCurrentItem());
+ searchPostSortTypeBottomSheetFragment.setArguments(bundle);
+
+ searchUserAndSubredditSortTypeBottomSheetFragment = new SearchUserAndSubredditSortTypeBottomSheetFragment();
+
// Get the intent, verify the action and get the query
Intent intent = getIntent();
String query = intent.getExtras().getString(EXTRA_QUERY);
@@ -73,6 +85,21 @@ public class SearchResultActivity extends AppCompatActivity {
case android.R.id.home:
onBackPressed();
return true;
+ case R.id.action_sort_search_result_activity:
+ switch (viewPager.getCurrentItem()) {
+ case 0: {
+ searchPostSortTypeBottomSheetFragment.show(getSupportFragmentManager(), searchPostSortTypeBottomSheetFragment.getTag());
+ break;
+ }
+ case 1:
+ case 2:
+ Bundle bundle = new Bundle();
+ bundle.putInt(SearchUserAndSubredditSortTypeBottomSheetFragment.EXTRA_FRAGMENT_POSITION, viewPager.getCurrentItem());
+ searchUserAndSubredditSortTypeBottomSheetFragment.setArguments(bundle);
+ searchUserAndSubredditSortTypeBottomSheetFragment.show(getSupportFragmentManager(), searchUserAndSubredditSortTypeBottomSheetFragment.getTag());
+ break;
+ }
+ return true;
case R.id.action_search_search_result_activity:
Intent intent = new Intent(this, SearchActivity.class);
finish();
@@ -85,7 +112,18 @@ public class SearchResultActivity extends AppCompatActivity {
return super.onOptionsItemSelected(item);
}
+ @Override
+ public void searchSortTypeSelected(String sortType) {
+ sectionsPagerAdapter.changeSortType(sortType, 0);
+ }
+
+ @Override
+ public void searchUserAndSubredditSortTypeSelected(String sortType, int fragmentPosition) {
+ sectionsPagerAdapter.changeSortType(sortType, fragmentPosition);
+ }
+
private class SectionsPagerAdapter extends FragmentPagerAdapter {
+
private PostFragment postFragment;
private SubredditListingFragment subredditListingFragment;
private UserListingFragment userListingFragment;
@@ -102,7 +140,7 @@ public class SearchResultActivity extends AppCompatActivity {
PostFragment mFragment = new PostFragment();
Bundle bundle = new Bundle();
bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_SEARCH);
- bundle.putString(PostFragment.EXTRA_SORT_TYPE, PostDataSource.SORT_TYPE_BEST);
+ bundle.putString(PostFragment.EXTRA_SORT_TYPE, PostDataSource.SORT_TYPE_RELEVANCE);
bundle.putString(PostFragment.EXTRA_SUBREDDIT_NAME, mSubredditName);
bundle.putString(PostFragment.EXTRA_QUERY, mQuery);
mFragment.setArguments(bundle);
@@ -115,8 +153,7 @@ public class SearchResultActivity extends AppCompatActivity {
mFragment.setArguments(bundle);
return mFragment;
}
- default:
- {
+ default: {
UserListingFragment mFragment = new UserListingFragment();
Bundle bundle = new Bundle();
bundle.putString(UserListingFragment.QUERY_KEY, mQuery);
@@ -162,6 +199,19 @@ public class SearchResultActivity extends AppCompatActivity {
return fragment;
}
+ void changeSortType(String sortType, int fragmentPosition) {
+ switch (fragmentPosition) {
+ case 0:
+ postFragment.changeSortType(sortType);
+ break;
+ case 1:
+ subredditListingFragment.changeSortType(sortType);
+ break;
+ case 2:
+ userListingFragment.changeSortType(sortType);
+ }
+ }
+
public void refresh() {
if(postFragment != null) {
((FragmentCommunicator) postFragment).refresh();
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchUserAndSubredditSortTypeBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchUserAndSubredditSortTypeBottomSheetFragment.java
new file mode 100644
index 00000000..ae652fd4
--- /dev/null
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchUserAndSubredditSortTypeBottomSheetFragment.java
@@ -0,0 +1,61 @@
+package ml.docilealligator.infinityforreddit;
+
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.TextView;
+
+import androidx.fragment.app.Fragment;
+
+import com.deishelon.roundedbottomsheet.RoundedBottomSheetDialogFragment;
+
+import butterknife.BindView;
+import butterknife.ButterKnife;
+
+
+/**
+ * A simple {@link Fragment} subclass.
+ */
+public class SearchUserAndSubredditSortTypeBottomSheetFragment extends RoundedBottomSheetDialogFragment {
+
+ interface SearchUserAndSubredditSortTypeSelectionCallback {
+ void searchUserAndSubredditSortTypeSelected(String sortType, int fragmentPosition);
+ }
+
+ static final String EXTRA_FRAGMENT_POSITION = "EFP";
+
+ @BindView(R.id.relevance_type_text_view_search_user_and_subreddit_sort_type_bottom_sheet_fragment) TextView relevanceTypeTextView;
+ @BindView(R.id.activity_type_text_view_search_user_and_subreddit_sort_type_bottom_sheet_fragment) TextView activityTypeTextView;
+
+ public SearchUserAndSubredditSortTypeBottomSheetFragment() {
+ // Required empty public constructor
+ }
+
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View rootView = inflater.inflate(R.layout.fragment_search_user_and_subreddit_sort_type_bottom_sheet, container, false);
+ ButterKnife.bind(this, rootView);
+
+ Activity activity = getActivity();
+
+ int position = getArguments().getInt(EXTRA_FRAGMENT_POSITION);
+
+ relevanceTypeTextView.setOnClickListener(view -> {
+ ((SearchUserAndSubredditSortTypeSelectionCallback) activity).searchUserAndSubredditSortTypeSelected(PostDataSource.SORT_TYPE_RELEVANCE, position);
+ dismiss();
+ });
+
+ activityTypeTextView.setOnClickListener(view -> {
+ ((SearchUserAndSubredditSortTypeSelectionCallback) activity).searchUserAndSubredditSortTypeSelected(PostDataSource.SORT_TYPE_HOT, position);
+ dismiss();
+ });
+
+ return rootView;
+ }
+
+}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SortTypeBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SortTypeBottomSheetFragment.java
index 8e25c271..16fbb961 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/SortTypeBottomSheetFragment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SortTypeBottomSheetFragment.java
@@ -78,7 +78,7 @@ public class SortTypeBottomSheetFragment extends RoundedBottomSheetDialogFragmen
});
controversialTypeTextView.setOnClickListener(view -> {
- ((SortTypeSelectionCallback) activity).sortTypeSelected(PostDataSource.SORT_TYPE_BEST);
+ ((SortTypeSelectionCallback) activity).sortTypeSelected(PostDataSource.SORT_TYPE_CONTROVERSIAL);
dismiss();
});
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSource.java
index 80e024d6..261c390b 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSource.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSource.java
@@ -1,11 +1,12 @@
package ml.docilealligator.infinityforreddit;
-import java.util.ArrayList;
-
-import SubredditDatabase.SubredditData;
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import androidx.paging.PageKeyedDataSource;
+
+import java.util.ArrayList;
+
+import SubredditDatabase.SubredditData;
import retrofit2.Retrofit;
public class SubredditListingDataSource extends PageKeyedDataSource<String, SubredditData> {
@@ -15,6 +16,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
}
private Retrofit retrofit;
private String query;
+ private String sortType;
private OnSubredditListingDataFetchedCallback onSubredditListingDataFetchedCallback;
private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
@@ -25,10 +27,11 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
private LoadParams<String> params;
private LoadCallback<String, SubredditData> callback;
- SubredditListingDataSource(Retrofit retrofit, String query,
+ SubredditListingDataSource(Retrofit retrofit, String query, String sortType,
OnSubredditListingDataFetchedCallback onSubredditListingDataFetchedCallback) {
this.retrofit = retrofit;
this.query = query;
+ this.sortType = sortType;
this.onSubredditListingDataFetchedCallback = onSubredditListingDataFetchedCallback;
paginationNetworkStateLiveData = new MutableLiveData();
initialLoadStateLiveData = new MutableLiveData();
@@ -49,7 +52,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
initialLoadStateLiveData.postValue(NetworkState.LOADING);
- FetchSubredditData.fetchSubredditListingData(retrofit, query, null, new FetchSubredditData.FetchSubredditListingDataListener() {
+ FetchSubredditData.fetchSubredditListingData(retrofit, query, null, sortType, new FetchSubredditData.FetchSubredditListingDataListener() {
@Override
public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) {
if(subredditData.size() == 0) {
@@ -83,7 +86,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
return;
}
- FetchSubredditData.fetchSubredditListingData(retrofit, query, params.key, new FetchSubredditData.FetchSubredditListingDataListener() {
+ FetchSubredditData.fetchSubredditListingData(retrofit, query, params.key, sortType, new FetchSubredditData.FetchSubredditListingDataListener() {
@Override
public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) {
callback.onResult(subredditData, after);
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSourceFactory.java
index bc64c052..f31cff7a 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSourceFactory.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSourceFactory.java
@@ -8,15 +8,17 @@ import retrofit2.Retrofit;
public class SubredditListingDataSourceFactory extends DataSource.Factory {
private Retrofit retrofit;
private String query;
+ private String sortType;
private SubredditListingDataSource.OnSubredditListingDataFetchedCallback onSubredditListingDataFetchedCallback;
private SubredditListingDataSource subredditListingDataSource;
private MutableLiveData<SubredditListingDataSource> subredditListingDataSourceMutableLiveData;
- SubredditListingDataSourceFactory(Retrofit retrofit, String query,
+ SubredditListingDataSourceFactory(Retrofit retrofit, String query, String sortType,
SubredditListingDataSource.OnSubredditListingDataFetchedCallback onSubredditListingDataFetchedCallback) {
this.retrofit = retrofit;
this.query = query;
+ this.sortType = sortType;
this.onSubredditListingDataFetchedCallback = onSubredditListingDataFetchedCallback;
subredditListingDataSourceMutableLiveData = new MutableLiveData<>();
}
@@ -25,7 +27,7 @@ public class SubredditListingDataSourceFactory extends DataSource.Factory {
@Override
public DataSource create() {
subredditListingDataSource = new SubredditListingDataSource(retrofit,
- query, onSubredditListingDataFetchedCallback);
+ query, sortType, onSubredditListingDataFetchedCallback);
subredditListingDataSourceMutableLiveData.postValue(subredditListingDataSource);
return subredditListingDataSource;
}
@@ -37,4 +39,8 @@ public class SubredditListingDataSourceFactory extends DataSource.Factory {
SubredditListingDataSource getSubredditListingDataSource() {
return subredditListingDataSource;
}
+
+ void changeSortType(String sortType) {
+ this.sortType = sortType;
+ }
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java
index 1972be71..2168bddd 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java
@@ -10,6 +10,12 @@ import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
+import androidx.coordinatorlayout.widget.CoordinatorLayout;
+import androidx.fragment.app.Fragment;
+import androidx.lifecycle.ViewModelProviders;
+import androidx.recyclerview.widget.LinearLayoutManager;
+import androidx.recyclerview.widget.RecyclerView;
+
import com.bumptech.glide.Glide;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;
@@ -17,11 +23,6 @@ import javax.inject.Inject;
import javax.inject.Named;
import SubscribedSubredditDatabase.SubscribedSubredditRoomDatabase;
-import androidx.coordinatorlayout.widget.CoordinatorLayout;
-import androidx.fragment.app.Fragment;
-import androidx.lifecycle.ViewModelProviders;
-import androidx.recyclerview.widget.LinearLayoutManager;
-import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Retrofit;
@@ -79,7 +80,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
mQuery = getArguments().getString(QUERY_KEY);
SubredditListingViewModel.Factory factory = new SubredditListingViewModel.Factory(mRetrofit, mQuery,
- new SubredditListingDataSource.OnSubredditListingDataFetchedCallback() {
+ PostDataSource.SORT_TYPE_RELEVANCE, new SubredditListingDataSource.OnSubredditListingDataFetchedCallback() {
@Override
public void hasSubreddit() {
mFetchSubredditListingInfoLinearLayout.setVisibility(View.GONE);
@@ -87,11 +88,8 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
@Override
public void noSubreddit() {
- mFetchSubredditListingInfoLinearLayout.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- //Do nothing
- }
+ mFetchSubredditListingInfoLinearLayout.setOnClickListener(view -> {
+ //Do nothing
});
showErrorView(R.string.no_subreddits);
}
@@ -135,6 +133,10 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
}
}
+ void changeSortType(String sortType) {
+ mSubredditListingViewModel.changeSortType(sortType);
+ }
+
@Override
public void refresh() {
mSubredditListingViewModel.refresh();
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingViewModel.java
index 1ebb2dc3..3c304b90 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingViewModel.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingViewModel.java
@@ -1,14 +1,16 @@
package ml.docilealligator.infinityforreddit;
-import SubredditDatabase.SubredditData;
import androidx.annotation.NonNull;
import androidx.arch.core.util.Function;
import androidx.lifecycle.LiveData;
+import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.paging.LivePagedListBuilder;
import androidx.paging.PagedList;
+
+import SubredditDatabase.SubredditData;
import retrofit2.Retrofit;
public class SubredditListingViewModel extends ViewModel {
@@ -16,22 +18,30 @@ public class SubredditListingViewModel extends ViewModel {
private LiveData<NetworkState> paginationNetworkState;
private LiveData<NetworkState> initialLoadingState;
private LiveData<PagedList<SubredditData>> subreddits;
+ private MutableLiveData<String> sortTypeLiveData;
- SubredditListingViewModel(Retrofit retrofit, String query,
+ SubredditListingViewModel(Retrofit retrofit, String query, String sortType,
SubredditListingDataSource.OnSubredditListingDataFetchedCallback onSubredditListingDataFetchedCallback) {
- subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, onSubredditListingDataFetchedCallback);
+ subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType, onSubredditListingDataFetchedCallback);
initialLoadingState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(),
(Function<SubredditListingDataSource, LiveData<NetworkState>>) SubredditListingDataSource::getInitialLoadStateLiveData);
paginationNetworkState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(),
(Function<SubredditListingDataSource, LiveData<NetworkState>>) SubredditListingDataSource::getPaginationNetworkStateLiveData);
+
+ sortTypeLiveData = new MutableLiveData<>();
+ sortTypeLiveData.postValue(sortType);
+
PagedList.Config pagedListConfig =
(new PagedList.Config.Builder())
.setEnablePlaceholders(false)
.setPageSize(25)
.build();
- subreddits = (new LivePagedListBuilder(subredditListingDataSourceFactory, pagedListConfig)).build();
+ subreddits = Transformations.switchMap(sortTypeLiveData, sort -> {
+ subredditListingDataSourceFactory.changeSortType(sortTypeLiveData.getValue());
+ return new LivePagedListBuilder(subredditListingDataSourceFactory, pagedListConfig).build();
+ });
}
LiveData<PagedList<SubredditData>> getSubreddits() {
@@ -58,22 +68,28 @@ public class SubredditListingViewModel extends ViewModel {
subredditListingDataSourceFactory.getSubredditListingDataSource().retryLoadingMore();
}
+ void changeSortType(String sortType) {
+ sortTypeLiveData.postValue(sortType);
+ }
+
public static class Factory extends ViewModelProvider.NewInstanceFactory {
private Retrofit retrofit;
private String query;
+ private String sortType;
private SubredditListingDataSource.OnSubredditListingDataFetchedCallback onSubredditListingDataFetchedCallback;
- public Factory(Retrofit retrofit, String query,
+ public Factory(Retrofit retrofit, String query, String sortType,
SubredditListingDataSource.OnSubredditListingDataFetchedCallback onSubredditListingDataFetchedCallback) {
this.retrofit = retrofit;
this.query = query;
+ this.sortType = sortType;
this.onSubredditListingDataFetchedCallback = onSubredditListingDataFetchedCallback;
}
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
- return (T) new SubredditListingViewModel(retrofit, query, onSubredditListingDataFetchedCallback);
+ return (T) new SubredditListingViewModel(retrofit, query, sortType, onSubredditListingDataFetchedCallback);
}
}
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSource.java
index 9273c32b..e2873052 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSource.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSource.java
@@ -1,11 +1,12 @@
package ml.docilealligator.infinityforreddit;
-import java.util.ArrayList;
-
-import User.UserData;
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import androidx.paging.PageKeyedDataSource;
+
+import java.util.ArrayList;
+
+import User.UserData;
import retrofit2.Retrofit;
public class UserListingDataSource extends PageKeyedDataSource<String, UserData> {
@@ -13,8 +14,10 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData>
void hasUser();
void noUser();
}
+
private Retrofit retrofit;
private String query;
+ private String sortType;
private UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback;
private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
@@ -25,10 +28,11 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData>
private PageKeyedDataSource.LoadParams<String> params;
private PageKeyedDataSource.LoadCallback<String, UserData> callback;
- UserListingDataSource(Retrofit retrofit, String query,
+ UserListingDataSource(Retrofit retrofit, String query, String sortType,
UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback) {
this.retrofit = retrofit;
this.query = query;
+ this.sortType = sortType;
this.onUserListingDataFetchedCallback = onUserListingDataFetchedCallback;
paginationNetworkStateLiveData = new MutableLiveData();
initialLoadStateLiveData = new MutableLiveData();
@@ -49,7 +53,7 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData>
initialLoadStateLiveData.postValue(NetworkState.LOADING);
- FetchUserData.fetchUserListingData(retrofit, query, null, new FetchUserData.FetchUserListingDataListener() {
+ FetchUserData.fetchUserListingData(retrofit, query, null, sortType, new FetchUserData.FetchUserListingDataListener() {
@Override
public void onFetchUserListingDataSuccess(ArrayList<UserData> UserData, String after) {
if(UserData.size() == 0) {
@@ -83,7 +87,7 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData>
return;
}
- FetchUserData.fetchUserListingData(retrofit, query, params.key, new FetchUserData.FetchUserListingDataListener() {
+ FetchUserData.fetchUserListingData(retrofit, query, params.key, sortType, new FetchUserData.FetchUserListingDataListener() {
@Override
public void onFetchUserListingDataSuccess(ArrayList<UserData> UserData, String after) {
callback.onResult(UserData, after);
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSourceFactory.java
index 99e1fcfb..c014687c 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSourceFactory.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSourceFactory.java
@@ -8,15 +8,17 @@ import retrofit2.Retrofit;
public class UserListingDataSourceFactory extends DataSource.Factory {
private Retrofit retrofit;
private String query;
+ private String sortType;
private UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback;
private UserListingDataSource userListingDataSource;
private MutableLiveData<UserListingDataSource> userListingDataSourceMutableLiveData;
- UserListingDataSourceFactory(Retrofit retrofit, String query,
+ UserListingDataSourceFactory(Retrofit retrofit, String query, String sortType,
UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback) {
this.retrofit = retrofit;
this.query = query;
+ this.sortType = sortType;
this.onUserListingDataFetchedCallback = onUserListingDataFetchedCallback;
userListingDataSourceMutableLiveData = new MutableLiveData<>();
}
@@ -25,7 +27,7 @@ public class UserListingDataSourceFactory extends DataSource.Factory {
@Override
public DataSource create() {
userListingDataSource = new UserListingDataSource(retrofit,
- query, onUserListingDataFetchedCallback);
+ query, sortType, onUserListingDataFetchedCallback);
userListingDataSourceMutableLiveData.postValue(userListingDataSource);
return userListingDataSource;
}
@@ -37,4 +39,8 @@ public class UserListingDataSourceFactory extends DataSource.Factory {
UserListingDataSource getUserListingDataSource() {
return userListingDataSource;
}
+
+ void changeSortType(String sortType) {
+ this.sortType = sortType;
+ }
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingFragment.java
index 78153fdc..4b111a51 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingFragment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingFragment.java
@@ -1,7 +1,6 @@
package ml.docilealligator.infinityforreddit;
-import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
@@ -11,6 +10,12 @@ import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
+import androidx.coordinatorlayout.widget.CoordinatorLayout;
+import androidx.fragment.app.Fragment;
+import androidx.lifecycle.ViewModelProviders;
+import androidx.recyclerview.widget.LinearLayoutManager;
+import androidx.recyclerview.widget.RecyclerView;
+
import com.bumptech.glide.Glide;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;
@@ -18,11 +23,6 @@ import javax.inject.Inject;
import javax.inject.Named;
import SubscribedUserDatabase.SubscribedUserRoomDatabase;
-import androidx.coordinatorlayout.widget.CoordinatorLayout;
-import androidx.fragment.app.Fragment;
-import androidx.lifecycle.ViewModelProviders;
-import androidx.recyclerview.widget.LinearLayoutManager;
-import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Retrofit;
@@ -85,11 +85,8 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato
mQuery = getArguments().getString(QUERY_KEY);
- String accessToken = getActivity().getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE)
- .getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
-
UserListingViewModel.Factory factory = new UserListingViewModel.Factory(mRetrofit, mQuery,
- new UserListingDataSource.OnUserListingDataFetchedCallback() {
+ PostDataSource.SORT_TYPE_RELEVANCE, new UserListingDataSource.OnUserListingDataFetchedCallback() {
@Override
public void hasUser() {
mFetchUserListingInfoLinearLayout.setVisibility(View.GONE);
@@ -97,11 +94,8 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato
@Override
public void noUser() {
- mFetchUserListingInfoLinearLayout.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- //Do nothing
- }
+ mFetchUserListingInfoLinearLayout.setOnClickListener(view -> {
+ //Do nothing
});
showErrorView(R.string.no_users);
}
@@ -145,6 +139,10 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato
}
}
+ void changeSortType(String sortType) {
+ mUserListingViewModel.changeSortType(sortType);
+ }
+
@Override
public void refresh() {
mUserListingViewModel.refresh();
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingViewModel.java
index 6a1804db..45317ccf 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingViewModel.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingViewModel.java
@@ -1,41 +1,51 @@
package ml.docilealligator.infinityforreddit;
-import User.UserData;
import androidx.annotation.NonNull;
import androidx.arch.core.util.Function;
import androidx.lifecycle.LiveData;
+import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.paging.LivePagedListBuilder;
import androidx.paging.PagedList;
+
+import User.UserData;
import retrofit2.Retrofit;
public class UserListingViewModel extends ViewModel {
private UserListingDataSourceFactory UserListingDataSourceFactory;
private LiveData<NetworkState> paginationNetworkState;
private LiveData<NetworkState> initialLoadingState;
- private LiveData<PagedList<UserData>> Users;
+ private LiveData<PagedList<UserData>> users;
+ private MutableLiveData<String> sortTypeLiveData;
- UserListingViewModel(Retrofit retrofit, String query,
+ UserListingViewModel(Retrofit retrofit, String query, String sortType,
UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback) {
- UserListingDataSourceFactory = new UserListingDataSourceFactory(retrofit, query, onUserListingDataFetchedCallback);
+ UserListingDataSourceFactory = new UserListingDataSourceFactory(retrofit, query, sortType, onUserListingDataFetchedCallback);
initialLoadingState = Transformations.switchMap(UserListingDataSourceFactory.getUserListingDataSourceMutableLiveData(),
(Function<UserListingDataSource, LiveData<NetworkState>>) UserListingDataSource::getInitialLoadStateLiveData);
paginationNetworkState = Transformations.switchMap(UserListingDataSourceFactory.getUserListingDataSourceMutableLiveData(),
(Function<UserListingDataSource, LiveData<NetworkState>>) UserListingDataSource::getPaginationNetworkStateLiveData);
+
+ sortTypeLiveData = new MutableLiveData<>();
+ sortTypeLiveData.postValue(sortType);
+
PagedList.Config pagedListConfig =
(new PagedList.Config.Builder())
.setEnablePlaceholders(false)
.setPageSize(25)
.build();
- Users = (new LivePagedListBuilder(UserListingDataSourceFactory, pagedListConfig)).build();
+ users = Transformations.switchMap(sortTypeLiveData, sort -> {
+ UserListingDataSourceFactory.changeSortType(sortTypeLiveData.getValue());
+ return (new LivePagedListBuilder(UserListingDataSourceFactory, pagedListConfig)).build();
+ });
}
LiveData<PagedList<UserData>> getUsers() {
- return Users;
+ return users;
}
LiveData<NetworkState> getPaginationNetworkState() {
@@ -58,22 +68,28 @@ public class UserListingViewModel extends ViewModel {
UserListingDataSourceFactory.getUserListingDataSource().retryLoadingMore();
}
+ void changeSortType(String sortType) {
+ sortTypeLiveData.postValue(sortType);
+ }
+
public static class Factory extends ViewModelProvider.NewInstanceFactory {
private Retrofit retrofit;
private String query;
+ private String sortType;
private UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback;
- public Factory(Retrofit retrofit, String query,
+ public Factory(Retrofit retrofit, String query, String sortType,
UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback) {
this.retrofit = retrofit;
this.query = query;
+ this.sortType = sortType;
this.onUserListingDataFetchedCallback = onUserListingDataFetchedCallback;
}
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
- return (T) new UserListingViewModel(retrofit, query, onUserListingDataFetchedCallback);
+ return (T) new UserListingViewModel(retrofit, query, sortType, onUserListingDataFetchedCallback);
}
}
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java
index 9996a4af..ef58c820 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java
@@ -253,7 +253,13 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
});
if(savedInstanceState == null) {
- replaceFragment(PostDataSource.SORT_TYPE_BEST);
+ mFragment = new PostFragment();
+ Bundle bundle = new Bundle();
+ bundle.putString(PostFragment.EXTRA_SUBREDDIT_NAME, subredditName);
+ bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT);
+ bundle.putString(PostFragment.EXTRA_SORT_TYPE, PostDataSource.SORT_TYPE_BEST);
+ mFragment.setArguments(bundle);
+ getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_subreddit_detail_activity, mFragment).commit();
} else {
mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE_KEY);
if(mFragment == null) {
@@ -330,16 +336,6 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
return false;
}
- private void replaceFragment(String sortType) {
- mFragment = new PostFragment();
- Bundle bundle = new Bundle();
- bundle.putString(PostFragment.EXTRA_SUBREDDIT_NAME, subredditName);
- bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT);
- bundle.putString(PostFragment.EXTRA_SORT_TYPE, sortType);
- mFragment.setArguments(bundle);
- getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_subreddit_detail_activity, mFragment).commit();
- }
-
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
@@ -353,7 +349,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
@Override
public void sortTypeSelected(String sortType) {
- replaceFragment(sortType);
+ ((PostFragment) mFragment).changeSortType(sortType);
}
@Override