package net.somethingdreadful.MAL.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.NumberPicker;

import com.crashlytics.android.Crashlytics;

import net.somethingdreadful.MAL.R;

public class NumberPickerDialogFragment extends DialogFragment {

    NumberPicker numberPicker;
    private onUpdateClickListener callback;

    private View makeNumberPicker() {
        View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_episode_picker, null);
        int max = 0;
        try {
            max = getArguments().getInt("max");
        } catch (Exception e) {
            Crashlytics.log(Log.ERROR, "MALX", "EpisodesPickerDialogFragment.makeNumberPicker(): " + e.getMessage());
        }
        int current = getArguments().getInt("current");

        numberPicker = (NumberPicker) view.findViewById(R.id.numberPicker);
        numberPicker.setMaxValue(max != 0 ? max : 999);
        numberPicker.setMinValue(0);
        numberPicker.setValue(current);
        return view;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), getTheme());
        builder.setView(makeNumberPicker());
        builder.setTitle(getArguments().getString("title"));
        builder.setPositiveButton(R.string.dialog_label_update, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int whichButton) {
                numberPicker.clearFocus();
                callback.onUpdated(numberPicker.getValue(), getArguments().getInt("id"));
                dismiss();
            }
        });
        builder.setNegativeButton(R.string.dialog_label_cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int whichButton) {
                dismiss();
            }
        });

        return builder.create();
    }

    /**
     * Set the Callback for update purpose.
     *
     * @param callback The activity/fragment where the callback is located
     * @return NumberPickerDialogFragment This will return the dialog itself to make init simple
     */
    public NumberPickerDialogFragment setOnSendClickListener(onUpdateClickListener callback) {
        this.callback = callback;
        return this;
    }

    /**
     * The interface for callback
     */
    public interface onUpdateClickListener {
        public void onUpdated(int number, int id);
    }
}
@@ -669,270 +669,45 @@
 x = 
-0;%0A        try %7B%0A            max = getArguments().getInt(%22max%22);%0A        %7D catch (Exception e) %7B%0A            Crashlytics.log(Log.ERROR, %22MALX%22, %22EpisodesPickerDialogFragment.makeNumberPicker(): %22 + e.getMessage());%0A        %7D%0A        int current = getArguments().
+getInt(%22max%22);%0A        int current = 
 getI
@@ -1905,32 +1905,475 @@
 reate();%0A    %7D%0A%0A
+    /**%0A     * Get the integer from an argument.%0A     *%0A     * @param key The argument name%0A     * @return int The number of the argument%0A     */%0A    public int getInt(String key) %7B%0A        try %7B%0A            return getArguments().getInt(key);%0A        %7D catch (Exception e) %7B%0A            Crashlytics.log(Log.ERROR, %22MALX%22, %22EpisodesPickerDialogFragment.makeNumberPicker(%22 + key + %22): %22 + e.getMessage());%0A            return 0;%0A        %7D%0A    %7D%0A%0A
     /**%0A     * S

Fix numberpicker crashes caused by null values (NumberPickerDialog)