<commit_before>
package com.battlelancer.seriesguide.ui.dialogs;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.battlelancer.seriesguide.beta.R;
import com.battlelancer.seriesguide.provider.SeriesContract.ListItems;
import com.battlelancer.seriesguide.provider.SeriesContract.Lists;
import com.battlelancer.seriesguide.ui.OnListsChangedListener;

public class ListManageDialogFragment extends DialogFragment {

    public static ListManageDialogFragment newInstance(String listId) {
        ListManageDialogFragment f = new ListManageDialogFragment();

        Bundle args = new Bundle();
        args.putString("listid", listId);
        f.setArguments(args);

        return f;
    }

    private EditText mTitle;
    private OnListsChangedListener mListener;

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

        // hide title, use custom theme
        setStyle(STYLE_NO_TITLE, R.style.SeriesGuideTheme_Dialog);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View layout = inflater.inflate(R.layout.list_manage_dialog, null);

        // title
        mTitle = (EditText) layout.findViewById(R.id.title);

        // buttons
        Button buttonNegative = (Button) layout.findViewById(R.id.buttonNegative);
        buttonNegative.setText(R.string.list_remove);
        buttonNegative.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Cursor lists = getActivity().getContentResolver().query(Lists.CONTENT_URI,
                        new String[] {
                            Lists._ID
                        }, null, null, null);
                int listCount = lists.getCount();
                lists.close();

                // do not remove last list
                if (listCount > 1) {
                    // remove list and items
                    String listId = getArguments().getString("listid");
                    getActivity().getContentResolver().delete(Lists.buildListUri(listId), null,
                            null);
                    getActivity().getContentResolver().delete(ListItems.CONTENT_URI,
                            Lists.LIST_ID + "=?", new String[] {
                                listId
                            });

                    // remove tab from view pager
                    mListener.onListsChanged();
                }

                dismiss();
            }
        });
        Button buttonPositive = (Button) layout.findViewById(R.id.buttonPositive);
        buttonPositive.setText(android.R.string.ok);
        buttonPositive.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // update title
                String listId = getArguments().getString("listid");
                ContentValues values = new ContentValues();
                values.put(Lists.NAME, mTitle.getText().toString());
                getActivity().getContentResolver().update(Lists.buildListUri(listId), values, null,
                        null);

                // refresh view pager
                mListener.onListsChanged();

                dismiss();
            }
        });

        return layout;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {
            mListener = (OnListsChangedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnListsChangedListener");
        }
    }

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

        String listId = getArguments().getString("listid");
        final Cursor list = getActivity().getContentResolver()
                .query(Lists.buildListUri(listId), new String[] {
                        Lists.NAME
                }, null, null, null);
        list.moveToFirst();

        mTitle.setText(list.getString(0));

        list.close();
    }

    /**
     * Display a dialog which allows to edit the title of this list or remove
     * it.
     */
    public static void showListManageDialog(String listId, FragmentManager fm) {
        // DialogFragment.show() will take care of adding the fragment<commit_msg>Disable remove button for last list.<commit_after>
package com.battlelancer.seriesguide.ui.dialogs;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.battlelancer.seriesguide.beta.R;
import com.battlelancer.seriesguide.provider.SeriesContract.ListItems;
import com.battlelancer.seriesguide.provider.SeriesContract.Lists;
import com.battlelancer.seriesguide.ui.OnListsChangedListener;

public class ListManageDialogFragment extends DialogFragment {

    public static ListManageDialogFragment newInstance(String listId) {
        ListManageDialogFragment f = new ListManageDialogFragment();

        Bundle args = new Bundle();
        args.putString("listid", listId);
        f.setArguments(args);

        return f;
    }

    private EditText mTitle;
    private OnListsChangedListener mListener;
    private Button mButtonNegative;

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

        // hide title, use custom theme
        setStyle(STYLE_NO_TITLE, R.style.SeriesGuideTheme_Dialog);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View layout = inflater.inflate(R.layout.list_manage_dialog, null);

        // title
        mTitle = (EditText) layout.findViewById(R.id.title);

        // buttons
        mButtonNegative = (Button) layout.findViewById(R.id.buttonNegative);
        mButtonNegative.setText(R.string.list_remove);
        mButtonNegative.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // remove list and items
                String listId = getArguments().getString("listid");
                getActivity().getContentResolver().delete(Lists.buildListUri(listId), null,
                        null);
                getActivity().getContentResolver().delete(ListItems.CONTENT_URI,
                        Lists.LIST_ID + "=?", new String[] {
                            listId
                        });

                // remove tab from view pager
                mListener.onListsChanged();

                dismiss();
            }
        });
        Button buttonPositive = (Button) layout.findViewById(R.id.buttonPositive);
        buttonPositive.setText(android.R.string.ok);
        buttonPositive.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // update title
                String listId = getArguments().getString("listid");
                ContentValues values = new ContentValues();
                values.put(Lists.NAME, mTitle.getText().toString());
                getActivity().getContentResolver().update(Lists.buildListUri(listId), values, null,
                        null);

                // refresh view pager
                mListener.onListsChanged();

                dismiss();
            }
        });

        return layout;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {
            mListener = (OnListsChangedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnListsChangedListener");
        }
    }

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

        // pre-populate list title
        String listId = getArguments().getString("listid");
        final Cursor list = getActivity().getContentResolver()
                .query(Lists.buildListUri(listId), new String[] {
                        Lists.NAME
                }, null, null, null);
        list.moveToFirst();
        mTitle.setText(list.getString(0));
        list.close();

        // do not allow removing last list, disable remove button
        Cursor lists = getActivity().getContentResolver().query(Lists.CONTENT_URI,
                new String[] {
                    Lists._ID
                }, null, null, null);
        if (lists.getCount() == 1) {
            mButtonNegative.setEnabled(false);
        }
        lists.close();
    }

    /**
     * Display a dialog which allows to edit the title of this list or remove
     * it.
     */
    public static void showListManageDialog(String listId, FragmentManager fm) {
        // DialogFragment.show() will take care of adding the fragment
{'fraction_non_alphanumeric': 0.09312930685042561, 'fraction_numerical': 0.0016214025131738954, 'mean_word_length': 2.1979909267660402, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '4631250', 'n_tokens_mistral': 2590, 'n_tokens_neox': 2380, 'n_words': 533}