From a29fcbde6b0b60558ccac11d73c4aea29402c2a2 Mon Sep 17 00:00:00 2001 From: stom79 Date: Wed, 22 Nov 2017 19:09:22 +0100 Subject: [PATCH] Issue #43 - Filter home timeline with regex --- .../mastodon/activities/BaseMainActivity.java | 33 +++++++++++++++++-- .../mastodon/drawers/StatusListAdapter.java | 14 ++++++++ .../gouv/etalab/mastodon/helper/Helper.java | 3 ++ app/src/main/res/layout/filter_regex.xml | 13 ++++++++ app/src/main/res/menu/option_filter_toots.xml | 4 +++ app/src/main/res/values-de/strings.xml | 1 + app/src/main/res/values-fr/strings.xml | 1 + app/src/main/res/values-nl/strings.xml | 1 + app/src/main/res/values-pt/strings.xml | 1 + app/src/main/res/values/strings.xml | 2 ++ 10 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 app/src/main/res/layout/filter_regex.xml diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java index 97883396e..94db587c3 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java @@ -54,6 +54,7 @@ import android.support.v7.widget.Toolbar; import android.view.MenuItem; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; +import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; @@ -144,6 +145,7 @@ public abstract class BaseMainActivity extends AppCompatActivity private Intent streamingIntent; public static String lastHomeId = null, lastNotificationId = null; boolean notif_follow, notif_add, notif_mention, notif_share, show_boosts, show_replies; + String show_filtered; private AppBarLayout appBar; private static boolean activityPaused; @@ -330,11 +332,17 @@ public abstract class BaseMainActivity extends AppCompatActivity Menu menu = popup.getMenu(); final MenuItem itemShowBoosts = menu.findItem(R.id.action_show_boosts); final MenuItem itemShowReplies = menu.findItem(R.id.action_show_replies); + final MenuItem itemFilter = menu.findItem(R.id.action_filter); show_boosts = sharedpreferences.getBoolean(Helper.SET_SHOW_BOOSTS, true); show_replies = sharedpreferences.getBoolean(Helper.SET_SHOW_REPLIES, true); + show_filtered = sharedpreferences.getString(Helper.SET_FILTER_REGEX_HOME, null); itemShowBoosts.setChecked(show_boosts); itemShowReplies.setChecked(show_replies); + if( show_filtered != null && show_filtered.length() > 0){ + itemFilter.setTitle(show_filtered); + } + popup.setOnDismissListener(new PopupMenu.OnDismissListener() { @Override public void onDismiss(PopupMenu menu) { @@ -357,21 +365,42 @@ public abstract class BaseMainActivity extends AppCompatActivity return false; } }); + final SharedPreferences.Editor editor = sharedpreferences.edit(); switch (item.getItemId()) { case R.id.action_show_boosts: - SharedPreferences.Editor editor = sharedpreferences.edit(); + show_boosts = !show_boosts; editor.putBoolean(Helper.SET_SHOW_BOOSTS, show_boosts); itemShowBoosts.setChecked(show_boosts); editor.apply(); break; case R.id.action_show_replies: - editor = sharedpreferences.edit(); show_replies = !show_replies; editor.putBoolean(Helper.SET_SHOW_REPLIES, show_replies); itemShowReplies.setChecked(show_replies); editor.apply(); break; + case R.id.action_filter: + AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(BaseMainActivity.this); + LayoutInflater inflater = getLayoutInflater(); + @SuppressLint("InflateParams") View dialogView = inflater.inflate(R.layout.filter_regex, null); + dialogBuilder.setView(dialogView); + final EditText editText = dialogView.findViewById(R.id.filter_regex); + if( show_filtered != null) { + editText.setText(show_filtered); + editText.setSelection(editText.getText().toString().length()); + } + dialogBuilder.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + itemFilter.setTitle(editText.getText().toString().trim()); + editor.putString(Helper.SET_FILTER_REGEX_HOME, editText.getText().toString().trim()); + editor.apply(); + } + }); + AlertDialog alertDialog = dialogBuilder.create(); + alertDialog.show(); + break; } return false; } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java index 76763468c..e51ea599b 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java @@ -283,6 +283,20 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct Status status = statuses.get(position); SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); int HIDDEN_STATUS = 0; + String filter = sharedpreferences.getString(Helper.SET_FILTER_REGEX_HOME, null); + + if( filter != null && filter.length() > 0){ + Pattern filterPattern = Pattern.compile("(" + filter + ")"); + String content; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) + content = Html.fromHtml(status.getContent(), Html.FROM_HTML_MODE_LEGACY).toString(); + else + //noinspection deprecation + content = Html.fromHtml(status.getContent()).toString(); + Matcher matcher = filterPattern.matcher(content); + if(matcher.find()) + return HIDDEN_STATUS; + } if (status.getReblog() != null && !sharedpreferences.getBoolean(Helper.SET_SHOW_BOOSTS, true)) return HIDDEN_STATUS; else if (status.getIn_reply_to_id() != null && !status.getIn_reply_to_id().equals("null") && !sharedpreferences.getBoolean(Helper.SET_SHOW_REPLIES, true)) { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java index 29b95d914..92bb09b84 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java @@ -239,6 +239,9 @@ public class Helper { public static final String SET_NOTIF_ADD_FILTER = "set_notif_follow_add_filter"; public static final String SET_NOTIF_MENTION_FILTER = "set_notif_follow_mention_filter"; public static final String SET_NOTIF_SHARE_FILTER = "set_notif_follow_share_filter"; + public static final String SET_FILTER_REGEX_HOME = "set_filter_regex_home"; + public static final String SET_FILTER_REGEX_LOCAL = "set_filter_regex_local"; + public static final String SET_FILTER_REGEX_PUBLIC = "set_filter_regex_public"; public static final String SET_NOTIF_VALIDATION = "set_share_validation"; public static final String SET_NOTIF_VALIDATION_FAV = "set_share_validation_fav"; diff --git a/app/src/main/res/layout/filter_regex.xml b/app/src/main/res/layout/filter_regex.xml new file mode 100644 index 000000000..87ce751db --- /dev/null +++ b/app/src/main/res/layout/filter_regex.xml @@ -0,0 +1,13 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/option_filter_toots.xml b/app/src/main/res/menu/option_filter_toots.xml index d5b4edd78..59435db97 100644 --- a/app/src/main/res/menu/option_filter_toots.xml +++ b/app/src/main/res/menu/option_filter_toots.xml @@ -15,4 +15,8 @@ android:title="@string/show_replies" app:actionViewClass="android.widget.CheckBox" app:showAsAction="always" /> + diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 08486942d..c786b269f 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -431,4 +431,5 @@ Emoji einfügen The app did not collect custom emojis for the moment. Live notifications + Mit regulären Ausdrücken filtern diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index d1b0134b9..80ef552cf 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -438,4 +438,5 @@ Insérer un émoji L\'application n\'a pas encore collecté d\'emojis personnalisés Notifications en direct + Filtrer avec une expression rationnelle diff --git a/app/src/main/res/values-nl/strings.xml b/app/src/main/res/values-nl/strings.xml index be4ea8eae..b770473bc 100644 --- a/app/src/main/res/values-nl/strings.xml +++ b/app/src/main/res/values-nl/strings.xml @@ -435,4 +435,5 @@ Emoji toevoegen The app did not collect custom emojis for the moment. Live notifications + Wegfilteren met reguliere expressies diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml index ebfa7415c..d18fd7617 100644 --- a/app/src/main/res/values-pt/strings.xml +++ b/app/src/main/res/values-pt/strings.xml @@ -437,4 +437,5 @@ Inserir Emoji The app did not collect custom emojis for the moment. Live notifications + Filtrar com uma expressão regular diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2610b43ff..c913eb5c4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -443,4 +443,6 @@ Thank you to: + + Filter out by regular expressions \ No newline at end of file