Browse Source

Refactored Adapter and ViewHolder binder

renovate/org.robolectric-robolectric-4.x
zerrium 4 years ago
parent
commit
480fd59713
  1. 15
      app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java
  2. 21
      app/src/main/java/awais/instagrabber/adapters/viewholder/dialogs/KeywordsFilterDialogViewHolder.java

15
app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java

@ -4,18 +4,14 @@ import android.content.Context;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import awais.instagrabber.R; import awais.instagrabber.R;
import awais.instagrabber.adapters.viewholder.dialogs.KeywordsFilterDialogViewHolder; import awais.instagrabber.adapters.viewholder.dialogs.KeywordsFilterDialogViewHolder;
import awais.instagrabber.utils.Constants;
import awais.instagrabber.utils.SettingsHelper;
public class KeywordsFilterAdapter extends RecyclerView.Adapter<KeywordsFilterDialogViewHolder> { public class KeywordsFilterAdapter extends RecyclerView.Adapter<KeywordsFilterDialogViewHolder> {
@ -36,16 +32,7 @@ public class KeywordsFilterAdapter extends RecyclerView.Adapter<KeywordsFilterDi
@Override @Override
public void onBindViewHolder(@NonNull KeywordsFilterDialogViewHolder holder, int position) { public void onBindViewHolder(@NonNull KeywordsFilterDialogViewHolder holder, int position) {
holder.getTextView().setText(items.get(position));
holder.getDeleteButton().setOnClickListener(view -> {
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 @Override

21
app/src/main/java/awais/instagrabber/adapters/viewholder/dialogs/KeywordsFilterDialogViewHolder.java

@ -1,13 +1,21 @@
package awais.instagrabber.adapters.viewholder.dialogs; package awais.instagrabber.adapters.viewholder.dialogs;
import android.content.Context;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.HashSet;
import awais.instagrabber.R; 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 { public class KeywordsFilterDialogViewHolder extends RecyclerView.ViewHolder {
@ -20,6 +28,19 @@ public class KeywordsFilterDialogViewHolder extends RecyclerView.ViewHolder {
item = itemView.findViewById(R.id.keyword_text); item = itemView.findViewById(R.id.keyword_text);
} }
public void bind(ArrayList<String> 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(){ public Button getDeleteButton(){
return deleteButton; return deleteButton;
} }

Loading…
Cancel
Save