From 68d8aaf61d8d3f3357fdadb16a44f04acbf5ae86 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 6 Apr 2021 14:30:33 +0800 Subject: Click a flair to see filtered posts. Fix filtering posts that contain certain flairs. --- .../activities/FilteredPostsActivity.java | 8 ++- .../CommentAndPostRecyclerViewAdapter.java | 8 +++ .../adapters/PostRecyclerViewAdapter.java | 24 +++++++++ .../infinityforreddit/fragments/PostFragment.java | 58 ++++++++++++++++++++-- .../infinityforreddit/postfilter/PostFilter.java | 16 ++++-- 5 files changed, 106 insertions(+), 8 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java index 73217aad..e14e8499 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java @@ -63,6 +63,7 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec public static final String EXTRA_NAME = "ESN"; public static final String EXTRA_QUERY = "EQ"; public static final String EXTRA_FILTER = "EF"; + public static final String EXTRA_CONTAIN_FLAIR = "ECF"; public static final String EXTRA_POST_TYPE = "EPT"; public static final String EXTRA_USER_WHERE = "EUW"; @@ -213,6 +214,11 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec break; } + String flair = getIntent().getStringExtra(EXTRA_CONTAIN_FLAIR); + if (flair != null) { + postFilter.containFlairs = flair; + } + if (postType == PostDataSource.TYPE_USER) { userWhere = getIntent().getStringExtra(EXTRA_USER_WHERE); if (userWhere != null && !PostDataSource.USER_WHERE_SUBMITTED.equals(userWhere) && mMenu != null) { @@ -262,7 +268,7 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec private void bindView(PostFilter postFilter, boolean initializeFragment) { switch (postType) { case PostDataSource.TYPE_FRONT_PAGE: - getSupportActionBar().setTitle(name); + getSupportActionBar().setTitle(R.string.home); break; case PostDataSource.TYPE_SEARCH: getSupportActionBar().setTitle(R.string.search); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java index 8b779730..13f9d07f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java @@ -2182,6 +2182,14 @@ public 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_CONTAIN_FLAIR, mPost.getFlair()); + mActivity.startActivity(intent); + }); + mNSFWTextView.setOnClickListener(view -> { Intent intent = new Intent(mActivity, FilteredPostsActivity.class); intent.putExtra(FilteredPostsActivity.EXTRA_NAME, mSubredditNamePrefixed.substring(2)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java index 99df68d9..5e64c959 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -2014,6 +2014,8 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { + int position = getBindingAdapterPosition(); + if (position < 0) { + return; + } + Post post = getItem(position); + if (post != null) { + mCallback.flairChipClicked(post.getFlair()); + } + }); } upvoteButton.setOnClickListener(view -> { @@ -3257,6 +3270,17 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { + int position = getBindingAdapterPosition(); + if (position < 0) { + return; + } + Post post = getItem(position); + if (post != null && !(mActivity instanceof FilteredPostsActivity)) { + mCallback.flairChipClicked(post.getFlair()); + } + }); + imageView.setOnClickListener(view -> { int position = getBindingAdapterPosition(); if (position < 0) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java index 161643a5..2c3f28b8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -457,6 +457,16 @@ public class PostFragment extends Fragment implements FragmentCommunicator { startActivity(intent); } + @Override + public void flairChipClicked(String flair) { + Intent intent = new Intent(activity, FilteredPostsActivity.class); + intent.putExtra(FilteredPostsActivity.EXTRA_NAME, subredditName); + intent.putExtra(FilteredPostsActivity.EXTRA_QUERY, query); + intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, postType); + intent.putExtra(FilteredPostsActivity.EXTRA_CONTAIN_FLAIR, flair); + startActivity(intent); + } + @Override public void nsfwChipClicked() { Intent intent = new Intent(activity, FilteredPostsActivity.class); @@ -525,6 +535,15 @@ public class PostFragment extends Fragment implements FragmentCommunicator { startActivity(intent); } + @Override + public void flairChipClicked(String flair) { + Intent intent = new Intent(activity, FilteredPostsActivity.class); + intent.putExtra(FilteredPostsActivity.EXTRA_NAME, subredditName); + intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, postType); + intent.putExtra(FilteredPostsActivity.EXTRA_CONTAIN_FLAIR, flair); + startActivity(intent); + } + @Override public void nsfwChipClicked() { Intent intent = new Intent(activity, FilteredPostsActivity.class); @@ -592,6 +611,15 @@ public class PostFragment extends Fragment implements FragmentCommunicator { startActivity(intent); } + @Override + public void flairChipClicked(String flair) { + Intent intent = new Intent(activity, FilteredPostsActivity.class); + intent.putExtra(FilteredPostsActivity.EXTRA_NAME, multiRedditPath); + intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, postType); + intent.putExtra(FilteredPostsActivity.EXTRA_CONTAIN_FLAIR, flair); + startActivity(intent); + } + @Override public void nsfwChipClicked() { Intent intent = new Intent(activity, FilteredPostsActivity.class); @@ -660,6 +688,16 @@ public class PostFragment extends Fragment implements FragmentCommunicator { startActivity(intent); } + @Override + public void flairChipClicked(String flair) { + Intent intent = new Intent(activity, FilteredPostsActivity.class); + intent.putExtra(FilteredPostsActivity.EXTRA_NAME, username); + intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, postType); + intent.putExtra(FilteredPostsActivity.EXTRA_USER_WHERE, where); + intent.putExtra(FilteredPostsActivity.EXTRA_CONTAIN_FLAIR, flair); + startActivity(intent); + } + @Override public void nsfwChipClicked() { Intent intent = new Intent(activity, FilteredPostsActivity.class); @@ -709,16 +747,22 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Override public void typeChipClicked(int filter) { Intent intent = new Intent(activity, FilteredPostsActivity.class); - intent.putExtra(FilteredPostsActivity.EXTRA_NAME, activity.getString(R.string.best)); intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, postType); intent.putExtra(FilteredPostsActivity.EXTRA_FILTER, filter); startActivity(intent); } + @Override + public void flairChipClicked(String flair) { + Intent intent = new Intent(activity, FilteredPostsActivity.class); + intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, postType); + intent.putExtra(FilteredPostsActivity.EXTRA_CONTAIN_FLAIR, flair); + startActivity(intent); + } + @Override public void nsfwChipClicked() { Intent intent = new Intent(activity, FilteredPostsActivity.class); - intent.putExtra(FilteredPostsActivity.EXTRA_NAME, activity.getString(R.string.best)); intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, postType); intent.putExtra(FilteredPostsActivity.EXTRA_FILTER, Post.NSFW_TYPE); startActivity(intent); @@ -762,16 +806,22 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Override public void typeChipClicked(int filter) { Intent intent = new Intent(activity, FilteredPostsActivity.class); - intent.putExtra(FilteredPostsActivity.EXTRA_NAME, activity.getString(R.string.best)); intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, postType); intent.putExtra(FilteredPostsActivity.EXTRA_FILTER, filter); startActivity(intent); } + @Override + public void flairChipClicked(String flair) { + Intent intent = new Intent(activity, FilteredPostsActivity.class); + intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, postType); + intent.putExtra(FilteredPostsActivity.EXTRA_CONTAIN_FLAIR, flair); + startActivity(intent); + } + @Override public void nsfwChipClicked() { Intent intent = new Intent(activity, FilteredPostsActivity.class); - intent.putExtra(FilteredPostsActivity.EXTRA_NAME, activity.getString(R.string.best)); intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, postType); intent.putExtra(FilteredPostsActivity.EXTRA_FILTER, Post.NSFW_TYPE); startActivity(intent); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java index 87185b19..9cc0987f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java @@ -213,10 +213,20 @@ public class PostFilter implements Parcelable { } if (postFilter.containFlairs != null && !postFilter.containFlairs.equals("")) { String[] flairs = postFilter.containFlairs.split(",", 0); - for (String f : flairs) { - if (!f.trim().equals("") && post.getFlair().equalsIgnoreCase(f.trim())) { - return false; + if (flairs.length > 0) { + boolean match = false; + for (int i = 0; i < flairs.length; i++) { + String flair = flairs[i].trim(); + if (flair.equals("") && i == flairs.length - 1) { + return false; + } + if (!flair.equals("") && post.getFlair().equalsIgnoreCase(flair)) { + match = true; + break; + } } + + return match; } } -- cgit v1.2.3