<commit_before>package org.gdg.frisbee.android.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatDialogFragment;
import android.support.v7.widget.SearchView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ListView;

import org.gdg.frisbee.android.R;
import org.gdg.frisbee.android.api.model.Chapter;
import org.gdg.frisbee.android.view.FilterListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ChapterSelectDialog extends AppCompatDialogFragment
    implements AdapterView.OnItemClickListener {

    private static final String EXTRA_CHAPTERS = "EXTRA_CHAPTERS";
    private static final String EXTRA_SELECTED_CHAPTER = "EXTRA_SELECTED_CHAPTER";

    @BindView(R.id.filter) SearchView cityNameSearchView;
    @BindView(android.R.id.list) FilterListView listView;

    private List<Chapter> chapters;

    private Listener listener = Listener.EMPTY;
    private Chapter selectedChapter;

    public static ChapterSelectDialog newInstance(ArrayList<Chapter> chapters,
                                                  @Nullable Chapter selectedChapter) {
        ChapterSelectDialog fragment = new ChapterSelectDialog();
        Bundle args = new Bundle(2);
        args.putParcelableArrayList(EXTRA_CHAPTERS, chapters);
        args.putParcelable(EXTRA_SELECTED_CHAPTER, selectedChapter);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        chapters = getArguments().getParcelableArrayList(EXTRA_CHAPTERS);
        selectedChapter = getArguments().getParcelable(EXTRA_SELECTED_CHAPTER);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View view = getActivity().getLayoutInflater()
            .inflate(R.layout.view_location_list_preference, (ViewGroup) getView(), false);
        ButterKnife.bind(this, view);

        return new AlertDialog.Builder(getActivity())
            .setNegativeButton(R.string.feedback_cancel, null)
            .setView(view)
            .create();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        listView.setOnItemClickListener(this);

        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        final CheckedItemAdapter adapter = new CheckedItemAdapter(
            getContext(),
            android.R.layout.simple_list_item_single_choice,
            android.R.id.text1,
            chapters
        );
        listView.setAdapter(adapter);

        int selectedItemPos = chapters.indexOf(selectedChapter);
        listView.setSelection(selectedItemPos);
        listView.setItemChecked(selectedItemPos, true);
        final Filter.FilterListener filterListener = new Filter.FilterListener() {
            @Override
            public void onFilterComplete(int count) {
                int index = findIndexByValueInFilteredListView(selectedChapter);
                listView.setItemChecked(index, true);
            }
        };

        cityNameSearchView.setOnQueryTextListener(
            new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                    adapter.getFilter().filter(newText, filterListener);
                    return true;
                }
            }
        );
        cityNameSearchView.requestFocus();
    }

    private int findIndexByValueInFilteredListView(Chapter value) {<commit_msg>Fix orientation change bug by setting the filter with the query<commit_after>package org.gdg.frisbee.android.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatDialogFragment;
import android.support.v7.widget.SearchView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ListView;

import org.gdg.frisbee.android.R;
import org.gdg.frisbee.android.api.model.Chapter;
import org.gdg.frisbee.android.view.FilterListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ChapterSelectDialog extends AppCompatDialogFragment
    implements AdapterView.OnItemClickListener {

    private static final String EXTRA_CHAPTERS = "EXTRA_CHAPTERS";
    private static final String EXTRA_SELECTED_CHAPTER = "EXTRA_SELECTED_CHAPTER";

    @BindView(R.id.filter) SearchView cityNameSearchView;
    @BindView(android.R.id.list) FilterListView listView;

    private List<Chapter> chapters;

    private Listener listener = Listener.EMPTY;
    private Chapter selectedChapter;

    public static ChapterSelectDialog newInstance(ArrayList<Chapter> chapters,
                                                  @Nullable Chapter selectedChapter) {
        ChapterSelectDialog fragment = new ChapterSelectDialog();
        Bundle args = new Bundle(2);
        args.putParcelableArrayList(EXTRA_CHAPTERS, chapters);
        args.putParcelable(EXTRA_SELECTED_CHAPTER, selectedChapter);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        chapters = getArguments().getParcelableArrayList(EXTRA_CHAPTERS);
        selectedChapter = getArguments().getParcelable(EXTRA_SELECTED_CHAPTER);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View view = getActivity().getLayoutInflater()
            .inflate(R.layout.view_location_list_preference, (ViewGroup) getView(), false);
        ButterKnife.bind(this, view);

        return new AlertDialog.Builder(getActivity())
            .setNegativeButton(R.string.feedback_cancel, null)
            .setView(view)
            .create();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        listView.setOnItemClickListener(this);

        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        final CheckedItemAdapter adapter = new CheckedItemAdapter(
            getContext(),
            android.R.layout.simple_list_item_single_choice,
            android.R.id.text1,
            chapters
        );
        listView.setAdapter(adapter);

        int selectedItemPos = chapters.indexOf(selectedChapter);
        listView.setSelection(selectedItemPos);
        listView.setItemChecked(selectedItemPos, true);
        final Filter.FilterListener filterListener = new Filter.FilterListener() {
            @Override
            public void onFilterComplete(int count) {
                int index = findIndexByValueInFilteredListView(selectedChapter);
                listView.setItemChecked(index, true);
            }
        };

        cityNameSearchView.setOnQueryTextListener(
            new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                    adapter.getFilter().filter(newText, filterListener);
                    return true;
                }
            }
        );
        cityNameSearchView.requestFocus();
        adapter.getFilter().filter(cityNameSearchView.getQuery(), filterListener);
    }

    private int findIndexByValueInFilteredListView(Chapter value) {
{'fraction_non_alphanumeric': 0.07947412057325595, 'fraction_numerical': 0.0011844131232974062, 'mean_word_length': 2.9792648444863334, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '680907', 'n_tokens_mistral': 2245, 'n_tokens_neox': 2124, 'n_words': 412}