From 480fd5971398fdc800474fc9c2dbd929e016f342 Mon Sep 17 00:00:00 2001 From: zerrium Date: Sun, 21 Mar 2021 01:43:21 +0700 Subject: [PATCH] Refactored Adapter and ViewHolder binder --- .../adapters/KeywordsFilterAdapter.java | 15 +------------ .../KeywordsFilterDialogViewHolder.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java b/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java index da20a23e..d3b0cc70 100644 --- a/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java +++ b/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java @@ -4,18 +4,14 @@ 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; 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 { @@ -36,16 +32,7 @@ public class KeywordsFilterAdapter extends RecyclerView.Adapter { - final String s = items.get(position); - SettingsHelper settingsHelper = new SettingsHelper(context); - items.remove(position); - settingsHelper.putStringSet(Constants.KEYWORD_FILTERS, new HashSet<>(items)); - notifyDataSetChanged(); - final String message = context.getString(R.string.removed_keywords, s); - Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); - }); + holder.bind(items, position, context, this); } @Override 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 a8525313..65fe7b77 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,13 +1,21 @@ package awais.instagrabber.adapters.viewholder.dialogs; +import android.content.Context; import android.view.View; import android.widget.Button; import android.widget.TextView; +import android.widget.Toast; 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.KeywordsFilterAdapter; +import awais.instagrabber.utils.Constants; +import awais.instagrabber.utils.SettingsHelper; public class KeywordsFilterDialogViewHolder extends RecyclerView.ViewHolder { @@ -20,6 +28,19 @@ public class KeywordsFilterDialogViewHolder extends RecyclerView.ViewHolder { item = itemView.findViewById(R.id.keyword_text); } + public void bind(ArrayList items, int position, Context context, KeywordsFilterAdapter adapter){ + item.setText(items.get(position)); + deleteButton.setOnClickListener(view -> { + final String s = items.get(position); + SettingsHelper settingsHelper = new SettingsHelper(context); + items.remove(position); + settingsHelper.putStringSet(Constants.KEYWORD_FILTERS, new HashSet<>(items)); + adapter.notifyDataSetChanged(); + final String message = context.getString(R.string.removed_keywords, s); + Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); + }); + } + public Button getDeleteButton(){ return deleteButton; }