Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter posts #1226

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public static boolean isRefreshRequired(final Context context, final String key)
R.string.pref_accessibility_say_comment_indent_level_key))
|| key.equals(context.getString(
R.string.pref_behaviour_collapse_sticky_comments_key))
|| key.equals(R.string.pref_behaviour_posts_filter_key)
|| key.equals(context.getString(
R.string.pref_accessibility_concise_mode_key))
|| key.equals(context.getString(
Expand Down Expand Up @@ -1223,6 +1224,12 @@ public static boolean behaviour_block_screenshots() {
false);
}

public static String pref_behaviour_posts_filter() {
return getString(
R.string.pref_behaviour_posts_filter_key,
"");
}

///////////////////////////////
// pref_cache
///////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
import java.io.IOException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.UUID;
Expand Down Expand Up @@ -812,6 +813,10 @@ public void onDataStreamComplete(
&& mPostListingURL.asSubredditPostListURL().type
== SubredditPostListURL.Type.SUBREDDIT);

final String keywordFilter = PrefsUtility.pref_behaviour_posts_filter();

final String[] keywordFilterArray = keywordFilter.split(",");
eikaramba marked this conversation as resolved.
Show resolved Hide resolved

final ArrayList<RedditPostListItem> downloadedPosts
= new ArrayList<>(25);

Expand All @@ -838,6 +843,17 @@ public void onDataStreamComplete(
&& blockedSubreddits.contains(
new SubredditCanonicalId(post.getSubreddit().getDecoded()));

final String postTitle = post.getTitle()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PostListingFragment is already quite cluttered -- it would be nice to split out all this new code into a new class.

.toString().toLowerCase(Locale.US);
eikaramba marked this conversation as resolved.
Show resolved Hide resolved
if (!keywordFilter.isEmpty()) {
final boolean keywordMatched = Arrays.stream(keywordFilterArray)
.anyMatch(keyword -> postTitle
.contains(keyword.toLowerCase(Locale.US)));
if (keywordMatched) {
continue; // Continue the outer loop
}
}

if(!isPostBlocked
&& (!post.getOver_18() || isNsfwAllowed)
&& mPostIds.add(post.getIdAlone())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ public void onCreatePreferences(

final int[] editTextPrefsToUpdate = {
R.string.pref_behaviour_comment_min_key,
R.string.pref_reddit_client_id_override_key
R.string.pref_reddit_client_id_override_key,
R.string.pref_behaviour_posts_filter_key
};

for(final int pref : listPrefsToUpdate) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@
<string name="pref_behaviour_nsfw_key" translatable="false">pref_behaviour_nsfw</string>
<string name="pref_behaviour_nsfw_title">Show NSFW content</string>

<string name="pref_behaviour_posts_filter_key" translatable="false">pref_behaviour_posts_filter</string>
<string name="pref_behaviour_posts_filter_header">Posts</string>
eikaramba marked this conversation as resolved.
Show resolved Hide resolved

<string name="pref_behaviour_posts_filter_title">Post Filtering By Keyword</string>
<string name="pref_behaviour_posts_filter_dialog_title">Remove posts containing keywords (comma-separated list)</string>
<string name="pref_behaviour_posts_filter_default_value"></string>

<!-- Cache Prefs -->

<string name="pref_cache_pruning">Cache Pruning</string>
Expand Down
7 changes: 7 additions & 0 deletions src/main/res/xml/prefs_behaviour.xml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@
android:entryValues="@array/pref_behaviour_postcount_items_return"
android:defaultValue="ALL"/>

<EditTextPreference android:title="@string/pref_behaviour_posts_filter_title"
android:key="@string/pref_behaviour_posts_filter_key"
android:dialogTitle="@string/pref_behaviour_posts_filter_dialog_title"
android:inputType="text"
android:maxLength="2048"
android:defaultValue="@string/pref_behaviour_posts_filter_default_value"/>

</PreferenceCategory>

<PreferenceCategory android:title="@string/pref_behaviour_comments_header">
Expand Down