<commit_before>package fr.free.nrw.commons.nearby;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import fr.free.nrw.commons.R;
import fr.free.nrw.commons.nearby.presenter.NearbyParentFragmentPresenter;

public class NearbyFilterSearchRecyclerViewAdapter
        extends RecyclerView.Adapter<NearbyFilterSearchRecyclerViewAdapter.RecyclerViewHolder>
        implements Filterable {

    private final LayoutInflater inflater;
    private Context context;
    private RecyclerView recyclerView;
    private ArrayList<Label> labels;
    private ArrayList<Label> displayedLabels;
    public ArrayList<Label> selectedLabels = new ArrayList<>();

    private int state;

    RecyclerView.SmoothScroller smoothScroller;

    public NearbyFilterSearchRecyclerViewAdapter(Context context, ArrayList<Label> labels, RecyclerView recyclerView) {
        this.context = context;
        this.labels = labels;
        this.displayedLabels = labels;
        this.recyclerView = recyclerView;
        smoothScroller = new
                LinearSmoothScroller(context) {
                    @Override protected int getVerticalSnapPreference() {
                        return LinearSmoothScroller.SNAP_TO_START;
                    }
                };
        inflater = LayoutInflater.from(context);
    }

    public class RecyclerViewHolder extends RecyclerView.ViewHolder {
        public TextView placeTypeLabel;
        public ImageView placeTypeIcon;
        public LinearLayout placeTypeLayout;

        public RecyclerViewHolder(View view) {
            super(view);
            placeTypeLabel = view.findViewById(R.id.place_text);
            placeTypeIcon = view.findViewById(R.id.place_icon);
            placeTypeLayout = view.findViewById(R.id.search_list_item);
        }
    }

    @NonNull
    @Override
    public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.nearby_search_list_item, parent, false);
        return new RecyclerViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
        Label label = displayedLabels.get(position);
        holder.placeTypeIcon.setImageResource(label.getIcon());
        holder.placeTypeLabel.setText(label.toString());

        holder.placeTypeLayout.setBackgroundColor(label.isSelected() ? ContextCompat.getColor(context, R.color.divider_grey) : Color.WHITE);
        holder.placeTypeLayout.setOnClickListener(view -> {
            NearbyParentFragmentPresenter.getInstance().setCheckboxUnknown();
            if (label.isSelected()) {
                selectedLabels.remove(label);
            } else {
                selectedLabels.add(label);
                displayedLabels.remove(label);
                displayedLabels.add(selectedLabels.size()-1, label);
                notifyDataSetChanged();
                smoothScroller.setTargetPosition(0);
                recyclerView.getLayoutManager().startSmoothScroll(smoothScroller);
            }
            label.setSelected(!label.isSelected());
            holder.placeTypeLayout.setBackgroundColor(label.isSelected() ? ContextCompat.getColor(context, R.color.divider_grey) : Color.WHITE);
            NearbyParentFragmentPresenter.getInstance().filterByMarkerType(selectedLabels, 0, false, false);
        });
    }

    @Override
    public long getItemId(int position) {
        return displayedLabels.get(position).hashCode();
    }

    @Override
    public int getItemCount() {
        return displayedLabels.size();
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
                ArrayList<Label> filteredArrayList = new ArrayList<>();

                if (labels == null) {
                    labels = new ArrayList<>(displayedLabels);
                }

                if (constraint == null || constraint.length() == 0) {<commit_msg>Prevent NPE (#3241)<commit_after>package fr.free.nrw.commons.nearby;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import fr.free.nrw.commons.R;
import fr.free.nrw.commons.nearby.presenter.NearbyParentFragmentPresenter;

public class NearbyFilterSearchRecyclerViewAdapter
        extends RecyclerView.Adapter<NearbyFilterSearchRecyclerViewAdapter.RecyclerViewHolder>
        implements Filterable {

    private final LayoutInflater inflater;
    private Context context;
    private RecyclerView recyclerView;
    private ArrayList<Label> labels;
    private ArrayList<Label> displayedLabels;
    public ArrayList<Label> selectedLabels = new ArrayList<>();

    private int state;

    RecyclerView.SmoothScroller smoothScroller;

    public NearbyFilterSearchRecyclerViewAdapter(Context context, ArrayList<Label> labels, RecyclerView recyclerView) {
        this.context = context;
        this.labels = labels;
        this.displayedLabels = labels;
        this.recyclerView = recyclerView;
        smoothScroller = new
                LinearSmoothScroller(context) {
                    @Override protected int getVerticalSnapPreference() {
                        return LinearSmoothScroller.SNAP_TO_START;
                    }
                };
        inflater = LayoutInflater.from(context);
    }

    public class RecyclerViewHolder extends RecyclerView.ViewHolder {
        public TextView placeTypeLabel;
        public ImageView placeTypeIcon;
        public LinearLayout placeTypeLayout;

        public RecyclerViewHolder(View view) {
            super(view);
            placeTypeLabel = view.findViewById(R.id.place_text);
            placeTypeIcon = view.findViewById(R.id.place_icon);
            placeTypeLayout = view.findViewById(R.id.search_list_item);
        }
    }

    @NonNull
    @Override
    public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.nearby_search_list_item, parent, false);
        return new RecyclerViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
        Label label = displayedLabels.get(position);
        holder.placeTypeIcon.setImageResource(label.getIcon());
        holder.placeTypeLabel.setText(label.toString());

        holder.placeTypeLayout.setBackgroundColor(label.isSelected() ? ContextCompat.getColor(context, R.color.divider_grey) : Color.WHITE);
        holder.placeTypeLayout.setOnClickListener(view -> {
            NearbyParentFragmentPresenter.getInstance().setCheckboxUnknown();
            if (label.isSelected()) {
                selectedLabels.remove(label);
            } else {
                selectedLabels.add(label);
            }
            label.setSelected(!label.isSelected());
            holder.placeTypeLayout.setBackgroundColor(label.isSelected() ? ContextCompat.getColor(context, R.color.divider_grey) : Color.WHITE);
            NearbyParentFragmentPresenter.getInstance().filterByMarkerType(selectedLabels, 0, false, false);
        });
    }

    @Override
    public long getItemId(int position) {
        return displayedLabels.get(position).hashCode();
    }

    @Override
    public int getItemCount() {
        return displayedLabels.size();
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
                ArrayList<Label> filteredArrayList = new ArrayList<>();

                if (labels == null) {
                    labels = new ArrayList<>(displayedLabels);
                }

                if (constraint == null || constraint.length() == 0) {
{'fraction_non_alphanumeric': 0.08575256811076373, 'fraction_numerical': 0.0011165698972755694, 'mean_word_length': 2.9703014184397163, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3320518', 'n_tokens_mistral': 2381, 'n_tokens_neox': 2245, 'n_words': 438}