<commit_before>/*
 * Copyright 2015 - 2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.gebatzens.sia.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v4.app.DialogFragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;

import java.util.List;

import de.gebatzens.sia.FilterActivity;
import de.gebatzens.sia.GGApp;
import de.gebatzens.sia.R;
import de.gebatzens.sia.data.Filter;

public class FilterDialog extends DialogFragment {

    FilterActivity activity;

    public static FilterDialog newInstance(boolean mainFilter, int position, int mainPosition) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("main_filter", mainFilter);
        bundle.putInt("update_position", position);
        bundle.putInt("main_filter_position", mainPosition);
        FilterDialog filterDialog = new FilterDialog();
        filterDialog.setArguments(bundle);
        return filterDialog;
    }

    @Override
    public void onAttach(Activity a) {
        activity = (FilterActivity) a;
        super.onAttach(a);
    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle bundle) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(getString(isMainFilterDialog() ? R.string.set_main_filter : (getUpdatePosition() == -1 ? R.string.hide_subject : R.string.edit_filter)));
        builder.setView(View.inflate(getActivity(), R.layout.filter_dialog, null));

        builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                activity.changed = true;
                EditText ed = (EditText) ((Dialog) dialog).findViewById(R.id.filter_text);
                CheckBox cb = (CheckBox) ((Dialog) dialog).findViewById(R.id.checkbox_contains);
                Spinner spinner = (Spinner) ((Dialog) dialog).findViewById(R.id.filter_spinner);

                String filtertext = ed.getText().toString().trim();
                if (filtertext.isEmpty()) {
                    Snackbar.make(getActivity().getWindow().getDecorView().findViewById(R.id.coordinator_layout), getString(R.string.invalid_filter), Snackbar.LENGTH_LONG).show();
                } else {
                    if(isMainFilterDialog()) {
                        Filter.IncludingFilter mainFilter = null;
                        if(getMainFilterPosition() == -1) {
                            mainFilter = new Filter.IncludingFilter(spinner.getSelectedItemPosition() == 0 ? Filter.FilterType.CLASS : Filter.FilterType.TEACHER, filtertext);
                            GGApp.GG_APP.filters.including.add(mainFilter);
                        } else {
                            mainFilter = GGApp.GG_APP.filters.including.get(getMainFilterPosition());
                            mainFilter.setFilter(filtertext);
                            mainFilter.setType(spinner.getSelectedItemPosition() == 0 ? Filter.FilterType.CLASS : Filter.FilterType.TEACHER);<commit_msg>Improve filter dialog<commit_after>/*
 * Copyright 2015 - 2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.gebatzens.sia.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v4.app.DialogFragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;

import java.util.List;

import de.gebatzens.sia.FilterActivity;
import de.gebatzens.sia.GGApp;
import de.gebatzens.sia.R;
import de.gebatzens.sia.data.Filter;

public class FilterDialog extends DialogFragment {

    FilterActivity activity;

    public static FilterDialog newInstance(boolean mainFilter, int position, int mainPosition) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("main_filter", mainFilter);
        bundle.putInt("update_position", position);
        bundle.putInt("main_filter_position", mainPosition);
        FilterDialog filterDialog = new FilterDialog();
        filterDialog.setArguments(bundle);
        return filterDialog;
    }

    @Override
    public void onAttach(Activity a) {
        activity = (FilterActivity) a;
        super.onAttach(a);
    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle bundle) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(getString(isMainFilterDialog() ? (getUpdatePosition() == -1 ? R.string.add_main_filter : R.string.edit_filter) : (getUpdatePosition() == -1 ? R.string.hide_subject : R.string.edit_filter)));
        builder.setView(View.inflate(getActivity(), R.layout.filter_dialog, null));

        builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                activity.changed = true;
                EditText ed = (EditText) ((Dialog) dialog).findViewById(R.id.filter_text);
                CheckBox cb = (CheckBox) ((Dialog) dialog).findViewById(R.id.checkbox_contains);
                Spinner spinner = (Spinner) ((Dialog) dialog).findViewById(R.id.filter_spinner);

                String filtertext = ed.getText().toString().trim();
                if (filtertext.isEmpty()) {
                    Snackbar.make(getActivity().getWindow().getDecorView().findViewById(R.id.coordinator_layout), getString(R.string.invalid_filter), Snackbar.LENGTH_LONG).show();
                } else {
                    if(isMainFilterDialog()) {
                        Filter.IncludingFilter mainFilter = null;
                        if(getMainFilterPosition() == -1) {
                            mainFilter = new Filter.IncludingFilter(spinner.getSelectedItemPosition() == 0 ? Filter.FilterType.CLASS : Filter.FilterType.TEACHER, filtertext);
                            GGApp.GG_APP.filters.including.add(mainFilter);
                        } else {
                            mainFilter = GGApp.GG_APP.filters.including.get(getMainFilterPosition());
                            mainFilter.setFilter(filtertext);
                            mainFilter.setType(spinner.getSelectedItemPosition() == 0 ? Filter.FilterType.CLASS : Filter.FilterType.TEACHER);
{'fraction_non_alphanumeric': 0.1012734990903578, 'fraction_numerical': 0.004244996967859308, 'mean_word_length': 3.0781404549950544, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3520052', 'n_tokens_mistral': 2219, 'n_tokens_neox': 2131, 'n_words': 504}