From a4c99568effa77b9b2be0403904bb938978caac8 Mon Sep 17 00:00:00 2001 From: zerrium Date: Fri, 19 Mar 2021 16:19:27 +0700 Subject: [PATCH] Added settings for KeywordFilter --- .../adapters/KeywordsFilterAdapter.java | 51 ++++++++++++++++ .../KeywordsFilterDialogViewHolder.java | 30 +++++++++ .../dialogs/KeywordsFilterDialog.java | 61 +++++++++++++++++++ .../settings/PostPreferencesFragment.java | 28 ++++++++- .../awais/instagrabber/utils/Constants.java | 4 +- .../utils/KeywordsFilterUtils.java | 47 ++++++++++++++ .../instagrabber/utils/SettingsHelper.java | 3 +- .../res/layout/dialog_keywords_filter.xml | 52 ++++++++++++++++ app/src/main/res/layout/item_keyword.xml | 23 +++++++ app/src/main/res/values/strings.xml | 3 + 10 files changed, 295 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java create mode 100644 app/src/main/java/awais/instagrabber/adapters/viewholder/dialogs/KeywordsFilterDialogViewHolder.java create mode 100644 app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java create mode 100644 app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java create mode 100644 app/src/main/res/layout/dialog_keywords_filter.xml create mode 100644 app/src/main/res/layout/item_keyword.xml diff --git a/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java b/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java new file mode 100644 index 00000000..9465ce45 --- /dev/null +++ b/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java @@ -0,0 +1,51 @@ +package awais.instagrabber.adapters; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.ArrayList; +import java.util.HashSet; + +import awais.instagrabber.R; +import awais.instagrabber.adapters.viewholder.dialogs.KeywordsFilterDialogViewHolder; +import awais.instagrabber.utils.Constants; +import awais.instagrabber.utils.SettingsHelper; + +public class KeywordsFilterAdapter extends RecyclerView.Adapter { + + private final Context context; + private final ArrayList items; + + public KeywordsFilterAdapter(Context context, ArrayList items){ + this.context = context; + this.items = items; + } + + @NonNull + @Override + public KeywordsFilterDialogViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_keyword, parent, false); + return new KeywordsFilterDialogViewHolder(v); + } + + @Override + public void onBindViewHolder(@NonNull KeywordsFilterDialogViewHolder holder, int position) { + holder.getTextView().setText(items.get(position)); + holder.getDeleteButton().setOnClickListener(view -> { + SettingsHelper settingsHelper = new SettingsHelper(context); + items.remove(items.get(position)); + settingsHelper.putStringSet(Constants.KEYWORD_FILTERS, new HashSet<>(items)); + notifyDataSetChanged(); + }); + } + + @Override + public int getItemCount() { + return 0; + } +} diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/dialogs/KeywordsFilterDialogViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/dialogs/KeywordsFilterDialogViewHolder.java new file mode 100644 index 00000000..075f3af0 --- /dev/null +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/dialogs/KeywordsFilterDialogViewHolder.java @@ -0,0 +1,30 @@ +package awais.instagrabber.adapters.viewholder.dialogs; + +import android.view.View; +import android.widget.ImageView; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import awais.instagrabber.R; + +public class KeywordsFilterDialogViewHolder extends RecyclerView.ViewHolder { + + private final ImageView deleteButton; + private final TextView item; + + public KeywordsFilterDialogViewHolder(@NonNull View itemView) { + super(itemView); + deleteButton = itemView.findViewById(R.id.keyword_delete); + item = itemView.findViewById(R.id.keyword_text); + } + + public ImageView getDeleteButton(){ + return deleteButton; + } + + public TextView getTextView(){ + return item; + } +} diff --git a/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java b/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java new file mode 100644 index 00000000..aa78e08b --- /dev/null +++ b/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java @@ -0,0 +1,61 @@ +package awais.instagrabber.dialogs; + +import android.content.Context; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.EditText; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.DialogFragment; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.ArrayList; +import java.util.HashSet; + +import awais.instagrabber.adapters.KeywordsFilterAdapter; +import awais.instagrabber.databinding.DialogKeywordsFilterBinding; +import awais.instagrabber.utils.Constants; +import awais.instagrabber.utils.SettingsHelper; + +public final class KeywordsFilterDialog extends DialogFragment { + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + DialogKeywordsFilterBinding dialogKeywordsFilterBinding = DialogKeywordsFilterBinding.inflate(inflater, container, false); + + final Context context = getContext(); + LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context); + final RecyclerView recyclerView = dialogKeywordsFilterBinding.recycler; + recyclerView.setLayoutManager(linearLayoutManager); + + SettingsHelper settingsHelper = new SettingsHelper(context); + ArrayList items = new ArrayList<>(settingsHelper.getStringSet(Constants.KEYWORD_FILTERS)); + KeywordsFilterAdapter adapter = new KeywordsFilterAdapter(context, items); + recyclerView.setAdapter(adapter); + + final EditText editText = dialogKeywordsFilterBinding.editText; + + dialogKeywordsFilterBinding.addIcon.setOnClickListener(view ->{ + final String s = editText.getText().toString(); + if(s.isEmpty()) return; + if(items.contains(s)) { + editText.setText(""); + return; + } + items.add(s); + settingsHelper.putStringSet(Constants.KEYWORD_FILTERS, new HashSet<>(items)); + adapter.notifyDataSetChanged(); + }); + + dialogKeywordsFilterBinding.btnOK.setOnClickListener(view ->{ + this.dismiss(); + }); + + return super.onCreateView(inflater, container, savedInstanceState); + } +} diff --git a/app/src/main/java/awais/instagrabber/fragments/settings/PostPreferencesFragment.java b/app/src/main/java/awais/instagrabber/fragments/settings/PostPreferencesFragment.java index 07012fe9..39a62004 100644 --- a/app/src/main/java/awais/instagrabber/fragments/settings/PostPreferencesFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/settings/PostPreferencesFragment.java @@ -7,10 +7,10 @@ import androidx.preference.Preference; import androidx.preference.PreferenceScreen; import androidx.preference.SwitchPreferenceCompat; -import awais.instagrabber.R; -import awais.instagrabber.utils.Constants; -import static awais.instagrabber.utils.Utils.settingsHelper; +import awais.instagrabber.R; +import awais.instagrabber.dialogs.KeywordsFilterDialog; +import awais.instagrabber.utils.Constants; public class PostPreferencesFragment extends BasePreferencesFragment { @Override @@ -20,6 +20,8 @@ public class PostPreferencesFragment extends BasePreferencesFragment { // generalCategory.addPreference(getAutoPlayVideosPreference(context)); screen.addPreference(getAlwaysMuteVideosPreference(context)); screen.addPreference(getShowCaptionPreference(context)); + screen.addPreference(getToggleKeywordFilterPreference(context)); + screen.addPreference(getEditKeywordFilterPreference(context)); } private Preference getAutoPlayVideosPreference(@NonNull final Context context) { @@ -46,4 +48,24 @@ public class PostPreferencesFragment extends BasePreferencesFragment { preference.setIconSpaceReserved(false); return preference; } + + private Preference getToggleKeywordFilterPreference(@NonNull final Context context) { + final SwitchPreferenceCompat preference = new SwitchPreferenceCompat(context); + preference.setKey(Constants.TOGGLE_KEYWORD_FILTER); + preference.setDefaultValue(false); + preference.setTitle(R.string.toggle_keyword_filter); + preference.setIconSpaceReserved(false); + return preference; + } + + private Preference getEditKeywordFilterPreference(@NonNull final Context context){ + final Preference preference = new Preference(context); + preference.setTitle(R.string.edit_keyword_filter); + preference.setIconSpaceReserved(false); + preference.setOnPreferenceClickListener(view ->{ + new KeywordsFilterDialog().show(getParentFragmentManager(), null); + return true; + }); + return preference; + } } diff --git a/app/src/main/java/awais/instagrabber/utils/Constants.java b/app/src/main/java/awais/instagrabber/utils/Constants.java index cf63db1c..0c92f0fd 100644 --- a/app/src/main/java/awais/instagrabber/utils/Constants.java +++ b/app/src/main/java/awais/instagrabber/utils/Constants.java @@ -1,8 +1,5 @@ package awais.instagrabber.utils; -import java.util.HashSet; -import java.util.Set; - public final class Constants { // string prefs public static final String FOLDER_PATH = "custom_path"; @@ -20,6 +17,7 @@ public final class Constants { public static final String APP_UA_CODE = "app_ua_code"; // boolean prefs public static final String DOWNLOAD_USER_FOLDER = "download_user_folder"; + public static final String TOGGLE_KEYWORD_FILTER = "toggle_keyword_filter"; // deprecated: public static final String BOTTOM_TOOLBAR = "bottom_toolbar"; public static final String FOLDER_SAVE_TO = "saved_to"; public static final String AUTOPLAY_VIDEOS = "autoplay_videos"; diff --git a/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java b/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java new file mode 100644 index 00000000..52d99db6 --- /dev/null +++ b/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java @@ -0,0 +1,47 @@ +package awais.instagrabber.utils; + +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +import awais.instagrabber.repositories.responses.Caption; +import awais.instagrabber.repositories.responses.Media; + +public final class KeywordsFilterUtils { + + private final ArrayList keywords; + + public KeywordsFilterUtils(ArrayList keywords){ + this.keywords = keywords; + } + + public boolean filter(final String caption){ + if(caption == null) return false; + final String temp = caption.toLowerCase(Locale.getDefault()); + for(final String s:keywords){ + if(temp.contains(s)) return true; + } + return false; + } + + public boolean filter(final Media media){ + if(media == null) return false; + final Caption c = media.getCaption(); + if(c == null) return false; + final String temp = c.getText().toLowerCase(LocaleUtils.getCurrentLocale()); + for(final String s:keywords){ + if(temp.contains(s)) return true; + } + return false; + } + + public List filter(final List media){ + if(media == null) return new ArrayList<>(); + + final List result= new ArrayList<>(); + for(final Media m:media){ + if(filter(m)) result.add(m); + } + return result; + } +} diff --git a/app/src/main/java/awais/instagrabber/utils/SettingsHelper.java b/app/src/main/java/awais/instagrabber/utils/SettingsHelper.java index 9f5ee813..e48a96cd 100755 --- a/app/src/main/java/awais/instagrabber/utils/SettingsHelper.java +++ b/app/src/main/java/awais/instagrabber/utils/SettingsHelper.java @@ -57,6 +57,7 @@ import static awais.instagrabber.utils.Constants.SHOW_QUICK_ACCESS_DIALOG; import static awais.instagrabber.utils.Constants.SKIPPED_VERSION; import static awais.instagrabber.utils.Constants.STORY_SORT; import static awais.instagrabber.utils.Constants.SWAP_DATE_TIME_FORMAT_ENABLED; +import static awais.instagrabber.utils.Constants.TOGGLE_KEYWORD_FILTER; public final class SettingsHelper { private final SharedPreferences sharedPreferences; @@ -155,7 +156,7 @@ public final class SettingsHelper { @StringDef({DOWNLOAD_USER_FOLDER, FOLDER_SAVE_TO, AUTOPLAY_VIDEOS, SHOW_QUICK_ACCESS_DIALOG, MUTED_VIDEOS, SHOW_CAPTIONS, CUSTOM_DATE_TIME_FORMAT_ENABLED, MARK_AS_SEEN, DM_MARK_AS_SEEN, CHECK_ACTIVITY, CHECK_UPDATES, SWAP_DATE_TIME_FORMAT_ENABLED, PREF_ENABLE_DM_NOTIFICATIONS, PREF_ENABLE_DM_AUTO_REFRESH, - FLAG_SECURE}) + FLAG_SECURE, TOGGLE_KEYWORD_FILTER}) public @interface BooleanSettings {} @StringDef({PREV_INSTALL_VERSION, BROWSER_UA_CODE, APP_UA_CODE, PREF_ENABLE_DM_AUTO_REFRESH_FREQ_NUMBER}) diff --git a/app/src/main/res/layout/dialog_keywords_filter.xml b/app/src/main/res/layout/dialog_keywords_filter.xml new file mode 100644 index 00000000..770147d3 --- /dev/null +++ b/app/src/main/res/layout/dialog_keywords_filter.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_keyword.xml b/app/src/main/res/layout/item_keyword.xml new file mode 100644 index 00000000..613e00d9 --- /dev/null +++ b/app/src/main/res/layout/item_keyword.xml @@ -0,0 +1,23 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cdc20d30..814a9838 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -466,4 +466,7 @@ Response is null! Response status is not ok! Request failed! + Keyword + Enable keyword filter + Edit keyword filters