From 7761d3875673e65cfb6d0cb496a781a9ee8f2db3 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 5 Aug 2019 17:18:30 +0800 Subject: Sorting and refreshing in FilteredPostsActivity are available. --- .../CommentAndPostRecyclerViewAdapter.java | 10 +++ .../infinityforreddit/FilteredPostsActivity.java | 89 ++++++++++++++++++++-- .../infinityforreddit/PostRecyclerViewAdapter.java | 6 +- .../SearchPostSortTypeBottomSheetFragment.java | 2 - .../infinityforreddit/SearchResultActivity.java | 1 - app/src/main/res/layout/item_post.xml | 2 +- app/src/main/res/menu/filtered_posts_activity.xml | 17 +++++ 7 files changed, 114 insertions(+), 13 deletions(-) create mode 100644 app/src/main/res/menu/filtered_posts_activity.xml (limited to 'app') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java index 3d3769d1..dcd2801a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java @@ -305,6 +305,16 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { + Intent intent = new Intent(mActivity, FilteredPostsActivity.class); + intent.putExtra(FilteredPostsActivity.EXTRA_NAME, mSubredditNamePrefixed.substring(2)); + intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); + intent.putExtra(FilteredPostsActivity.EXTRA_SORT_TYPE, PostDataSource.SORT_TYPE_HOT); + intent.putExtra(FilteredPostsActivity.EXTRA_FILTER, mPost.getPostType()); + mActivity.startActivity(intent); + + }); + switch (mPost.getPostType()) { case Post.IMAGE_TYPE: ((PostDetailViewHolder) holder).mTypeChip.setText("IMAGE"); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FilteredPostsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FilteredPostsActivity.java index 4cddd4d9..eae90333 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FilteredPostsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FilteredPostsActivity.java @@ -1,6 +1,7 @@ package ml.docilealligator.infinityforreddit; import android.os.Bundle; +import android.view.Menu; import android.view.MenuItem; import androidx.annotation.NonNull; @@ -11,7 +12,8 @@ import androidx.fragment.app.Fragment; import butterknife.BindView; import butterknife.ButterKnife; -public class FilteredPostsActivity extends AppCompatActivity { +public class FilteredPostsActivity extends AppCompatActivity implements SortTypeBottomSheetFragment.SortTypeSelectionCallback, + SearchPostSortTypeBottomSheetFragment.SearchSortTypeSelectionCallback { static final String EXTRA_NAME = "ESN"; static final String EXTRA_QUERY = "EQ"; @@ -23,8 +25,16 @@ public class FilteredPostsActivity extends AppCompatActivity { @BindView(R.id.toolbar_filtered_posts_activity) Toolbar toolbar; + private String name; + private int postType; + private Fragment mFragment; + private SortTypeBottomSheetFragment bestSortTypeBottomSheetFragment; + private SortTypeBottomSheetFragment popularAndAllSortTypeBottomSheetFragment; + private SortTypeBottomSheetFragment subredditSortTypeBottomSheetFragment; + private SearchPostSortTypeBottomSheetFragment searchPostSortTypeBottomSheetFragment; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -35,21 +45,45 @@ public class FilteredPostsActivity extends AppCompatActivity { setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); - String name = getIntent().getExtras().getString(EXTRA_NAME); + name = getIntent().getExtras().getString(EXTRA_NAME); int filter = getIntent().getExtras().getInt(EXTRA_FILTER); - int postType = getIntent().getExtras().getInt(EXTRA_POST_TYPE); + postType = getIntent().getExtras().getInt(EXTRA_POST_TYPE); String sortType = getIntent().getExtras().getString(EXTRA_SORT_TYPE); switch (postType) { case PostDataSource.TYPE_FRONT_PAGE: getSupportActionBar().setTitle(name); + + bestSortTypeBottomSheetFragment = new SortTypeBottomSheetFragment(); + Bundle bestBundle = new Bundle(); + bestBundle.putBoolean(SortTypeBottomSheetFragment.EXTRA_NO_BEST_TYPE, false); + bestSortTypeBottomSheetFragment.setArguments(bestBundle); break; case PostDataSource.TYPE_SEARCH: getSupportActionBar().setTitle(R.string.search); + + searchPostSortTypeBottomSheetFragment = new SearchPostSortTypeBottomSheetFragment(); + Bundle searchBundle = new Bundle(); + searchPostSortTypeBottomSheetFragment.setArguments(searchBundle); break; case PostDataSource.TYPE_SUBREDDIT: - String subredditNamePrefixed = "r/" + name; - getSupportActionBar().setTitle(subredditNamePrefixed); + if(name.equals("popular") || name.equals("all")) { + getSupportActionBar().setTitle(name.substring(0, 1).toUpperCase() + name.substring(1)); + + popularAndAllSortTypeBottomSheetFragment = new SortTypeBottomSheetFragment(); + Bundle popularBundle = new Bundle(); + popularBundle.putBoolean(SortTypeBottomSheetFragment.EXTRA_NO_BEST_TYPE, true); + popularAndAllSortTypeBottomSheetFragment.setArguments(popularBundle); + } else { + String subredditNamePrefixed = "r/" + name; + getSupportActionBar().setTitle(subredditNamePrefixed); + + subredditSortTypeBottomSheetFragment = new SortTypeBottomSheetFragment(); + Bundle bottomSheetBundle = new Bundle(); + bottomSheetBundle.putBoolean(SortTypeBottomSheetFragment.EXTRA_NO_BEST_TYPE, true); + subredditSortTypeBottomSheetFragment.setArguments(bottomSheetBundle); + } + break; case PostDataSource.TYPE_USER: String usernamePrefixed = "u/" + name; getSupportActionBar().setTitle(usernamePrefixed); @@ -92,11 +126,40 @@ public class FilteredPostsActivity extends AppCompatActivity { } } + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.filtered_posts_activity, menu); + if(postType == PostDataSource.TYPE_USER) { + menu.findItem(R.id.action_sort_filtered_posts_activity).setVisible(false); + } + return true; + } + @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { - if(item.getItemId() == android.R.id.home) { - finish(); - return true; + switch (item.getItemId()) { + case android.R.id.home: + finish(); + return true; + case R.id.action_sort_filtered_posts_activity: + switch (postType) { + case PostDataSource.TYPE_FRONT_PAGE: + bestSortTypeBottomSheetFragment.show(getSupportFragmentManager(), bestSortTypeBottomSheetFragment.getTag()); + break; + case PostDataSource.TYPE_SEARCH: + searchPostSortTypeBottomSheetFragment.show(getSupportFragmentManager(), searchPostSortTypeBottomSheetFragment.getTag()); + break; + case PostDataSource.TYPE_SUBREDDIT: + if(name.equals("popular") || name.equals("all")) { + popularAndAllSortTypeBottomSheetFragment.show(getSupportFragmentManager(), popularAndAllSortTypeBottomSheetFragment.getTag()); + } else { + subredditSortTypeBottomSheetFragment.show(getSupportFragmentManager(), subredditSortTypeBottomSheetFragment.getTag()); + } + } + return true; + case R.id.action_refresh_filtered_posts_activity: + ((FragmentCommunicator) mFragment).refresh(); + return true; } return false; } @@ -108,4 +171,14 @@ public class FilteredPostsActivity extends AppCompatActivity { getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); } } + + @Override + public void searchSortTypeSelected(String sortType) { + ((PostFragment)mFragment).changeSortType(sortType); + } + + @Override + public void sortTypeSelected(String sortType) { + ((PostFragment)mFragment).changeSortType(sortType); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index 43db7760..79759c6c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -361,7 +361,9 @@ class PostRecyclerViewAdapter extends PagedListAdapter callback.typeChipClicked(post.getPostType())); + if(!(mContext instanceof FilteredPostsActivity)) { + ((DataViewHolder) holder).typeChip.setOnClickListener(view -> callback.typeChipClicked(post.getPostType())); + } switch (post.getPostType()) { case Post.IMAGE_TYPE: @@ -379,6 +381,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter + android:visibility="gone" /> + + + + + \ No newline at end of file -- cgit v1.2.3