From 10f5d91a42f6e8189e523ff813fcd3b9583de5ff Mon Sep 17 00:00:00 2001 From: zerrium Date: Fri, 19 Mar 2021 18:26:44 +0700 Subject: [PATCH] Fixed Edit Keyword Filters dialog settings --- .../adapters/KeywordsFilterAdapter.java | 8 +++++-- .../KeywordsFilterDialogViewHolder.java | 6 ++--- .../asyncs/FeedPostFetchService.java | 2 +- .../dialogs/KeywordsFilterDialog.java | 24 +++++++++++-------- .../utils/KeywordsFilterUtils.java | 2 +- .../res/layout/dialog_keywords_filter.xml | 15 ++++++------ app/src/main/res/layout/item_keyword.xml | 4 ++-- app/src/main/res/values/strings.xml | 2 ++ 8 files changed, 37 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java b/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java index 9465ce45..99867f6c 100644 --- a/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java +++ b/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java @@ -4,6 +4,7 @@ import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; @@ -37,15 +38,18 @@ public class KeywordsFilterAdapter extends RecyclerView.Adapter { + final String s = items.get(position); SettingsHelper settingsHelper = new SettingsHelper(context); - items.remove(items.get(position)); + items.remove(position); settingsHelper.putStringSet(Constants.KEYWORD_FILTERS, new HashSet<>(items)); notifyDataSetChanged(); + final String message = context.getString(R.string.removed_keywords).replace("{0}", s); + Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); }); } @Override public int getItemCount() { - return 0; + return items.size(); } } 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 index 075f3af0..a8525313 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/dialogs/KeywordsFilterDialogViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/dialogs/KeywordsFilterDialogViewHolder.java @@ -1,7 +1,7 @@ package awais.instagrabber.adapters.viewholder.dialogs; import android.view.View; -import android.widget.ImageView; +import android.widget.Button; import android.widget.TextView; import androidx.annotation.NonNull; @@ -11,7 +11,7 @@ import awais.instagrabber.R; public class KeywordsFilterDialogViewHolder extends RecyclerView.ViewHolder { - private final ImageView deleteButton; + private final Button deleteButton; private final TextView item; public KeywordsFilterDialogViewHolder(@NonNull View itemView) { @@ -20,7 +20,7 @@ public class KeywordsFilterDialogViewHolder extends RecyclerView.ViewHolder { item = itemView.findViewById(R.id.keyword_text); } - public ImageView getDeleteButton(){ + public Button getDeleteButton(){ return deleteButton; } diff --git a/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java b/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java index 54bd2509..ce619b04 100644 --- a/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java +++ b/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java @@ -43,7 +43,7 @@ public class FeedPostFetchService implements PostFetcher.PostFetchService { hasNextPage = result.hasNextPage(); //Check caption if it doesn't contain any specified keywords in filter_keywords.xml - List mediaResults = result.getFeedModels(); + final List mediaResults = result.getFeedModels(); if(!settingsHelper.getBoolean(Constants.TOGGLE_KEYWORD_FILTER)){ feedModels.addAll(mediaResults); }else{ diff --git a/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java b/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java index aa78e08b..473f6f5e 100644 --- a/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java +++ b/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java @@ -6,6 +6,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -16,6 +17,7 @@ import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; import java.util.HashSet; +import awais.instagrabber.R; import awais.instagrabber.adapters.KeywordsFilterAdapter; import awais.instagrabber.databinding.DialogKeywordsFilterBinding; import awais.instagrabber.utils.Constants; @@ -23,24 +25,23 @@ 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 DialogKeywordsFilterBinding dialogKeywordsFilterBinding = DialogKeywordsFilterBinding.inflate(inflater, container, false); final Context context = getContext(); - LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context); - final RecyclerView recyclerView = dialogKeywordsFilterBinding.recycler; + final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context); + final RecyclerView recyclerView = dialogKeywordsFilterBinding.recyclerKeyword; recyclerView.setLayoutManager(linearLayoutManager); - SettingsHelper settingsHelper = new SettingsHelper(context); - ArrayList items = new ArrayList<>(settingsHelper.getStringSet(Constants.KEYWORD_FILTERS)); - KeywordsFilterAdapter adapter = new KeywordsFilterAdapter(context, items); + final SettingsHelper settingsHelper = new SettingsHelper(context); + final ArrayList items = new ArrayList<>(settingsHelper.getStringSet(Constants.KEYWORD_FILTERS)); + final KeywordsFilterAdapter adapter = new KeywordsFilterAdapter(context, items); recyclerView.setAdapter(adapter); final EditText editText = dialogKeywordsFilterBinding.editText; - dialogKeywordsFilterBinding.addIcon.setOnClickListener(view ->{ + dialogKeywordsFilterBinding.btnAdd.setOnClickListener(view ->{ final String s = editText.getText().toString(); if(s.isEmpty()) return; if(items.contains(s)) { @@ -49,13 +50,16 @@ public final class KeywordsFilterDialog extends DialogFragment { } items.add(s); settingsHelper.putStringSet(Constants.KEYWORD_FILTERS, new HashSet<>(items)); - adapter.notifyDataSetChanged(); + adapter.notifyItemInserted(items.size()); + final String message = context.getString(R.string.added_keywords).replace("{0}", s); + Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); + editText.setText(""); }); dialogKeywordsFilterBinding.btnOK.setOnClickListener(view ->{ this.dismiss(); }); - return super.onCreateView(inflater, container, savedInstanceState); + return dialogKeywordsFilterBinding.getRoot(); } } diff --git a/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java b/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java index 52d99db6..a61ac66e 100644 --- a/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java @@ -11,7 +11,7 @@ public final class KeywordsFilterUtils { private final ArrayList keywords; - public KeywordsFilterUtils(ArrayList keywords){ + public KeywordsFilterUtils(final ArrayList keywords){ this.keywords = keywords; } diff --git a/app/src/main/res/layout/dialog_keywords_filter.xml b/app/src/main/res/layout/dialog_keywords_filter.xml index 770147d3..e98d96d7 100644 --- a/app/src/main/res/layout/dialog_keywords_filter.xml +++ b/app/src/main/res/layout/dialog_keywords_filter.xml @@ -17,21 +17,22 @@ - + android:layout_marginStart="-50dp" /> diff --git a/app/src/main/res/layout/item_keyword.xml b/app/src/main/res/layout/item_keyword.xml index 613e00d9..10327042 100644 --- a/app/src/main/res/layout/item_keyword.xml +++ b/app/src/main/res/layout/item_keyword.xml @@ -13,11 +13,11 @@ android:layout_height="wrap_content" android:layout_weight="1" /> - \ 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 814a9838..5cf2cacb 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -469,4 +469,6 @@ Keyword Enable keyword filter Edit keyword filters + Added keyword: {0} to filter list + Removed keyword: {0} from filter list