Refactor button handling in dialog fragments
This commit is contained in:
parent
9879046288
commit
a60817b491
@ -1,5 +1,7 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import static org.solovyev.android.calculator.App.cast;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.SharedPreferences;
|
||||
@ -14,15 +16,20 @@ import android.support.v7.app.AlertDialog;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
import android.widget.Button;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static org.solovyev.android.calculator.App.cast;
|
||||
|
||||
public abstract class BaseDialogFragment extends DialogFragment {
|
||||
public abstract class BaseDialogFragment extends DialogFragment implements View.OnClickListener, DialogInterface.OnClickListener {
|
||||
|
||||
@Inject
|
||||
protected SharedPreferences preferences;
|
||||
@Nullable
|
||||
private Button positiveButton;
|
||||
@Nullable
|
||||
private Button negativeButton;
|
||||
@Nullable
|
||||
private Button neutralButton;
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
@ -51,6 +58,9 @@ public abstract class BaseDialogFragment extends DialogFragment {
|
||||
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
|
||||
@Override
|
||||
public void onShow(DialogInterface d) {
|
||||
positiveButton = getButton(dialog, AlertDialog.BUTTON_POSITIVE);
|
||||
negativeButton = getButton(dialog, AlertDialog.BUTTON_NEGATIVE);
|
||||
neutralButton = getButton(dialog, AlertDialog.BUTTON_NEUTRAL);
|
||||
onShowDialog(dialog, savedInstanceState == null);
|
||||
}
|
||||
});
|
||||
@ -60,6 +70,15 @@ public abstract class BaseDialogFragment extends DialogFragment {
|
||||
protected void onShowDialog(@NonNull AlertDialog dialog, boolean firstTime) {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Button getButton(@NonNull AlertDialog dialog, int buttonId) {
|
||||
final Button button = dialog.getButton(buttonId);
|
||||
if (button != null) {
|
||||
button.setOnClickListener(this);
|
||||
}
|
||||
return button;
|
||||
}
|
||||
|
||||
protected abstract void onPrepareDialog(@NonNull AlertDialog.Builder builder);
|
||||
|
||||
@Nullable
|
||||
@ -86,4 +105,25 @@ public abstract class BaseDialogFragment extends DialogFragment {
|
||||
final InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (v == positiveButton) {
|
||||
onClick(getDialog(), DialogInterface.BUTTON_POSITIVE);
|
||||
} else if (v == negativeButton) {
|
||||
onClick(getDialog(), DialogInterface.BUTTON_NEGATIVE);
|
||||
} else if (v == neutralButton) {
|
||||
onClick(getDialog(), DialogInterface.BUTTON_NEUTRAL);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
switch (which) {
|
||||
case DialogInterface.BUTTON_POSITIVE:
|
||||
case DialogInterface.BUTTON_NEGATIVE:
|
||||
dismiss();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,10 +16,33 @@ import android.view.KeyEvent;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.inputmethod.EditorInfo;
|
||||
import android.widget.*;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.solovyev.android.calculator.App;
|
||||
import org.solovyev.android.calculator.AppComponent;
|
||||
import org.solovyev.android.calculator.BaseDialogFragment;
|
||||
import org.solovyev.android.calculator.Clipboard;
|
||||
import org.solovyev.android.calculator.Editor;
|
||||
import org.solovyev.android.calculator.R;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
import org.solovyev.android.calculator.*;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.inject.Inject;
|
||||
@ -27,12 +50,9 @@ import javax.measure.unit.Dimension;
|
||||
import javax.measure.unit.NonSI;
|
||||
import javax.measure.unit.SI;
|
||||
import javax.measure.unit.Unit;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.ParseException;
|
||||
import java.util.*;
|
||||
|
||||
public class ConverterFragment extends BaseDialogFragment
|
||||
implements AdapterView.OnItemSelectedListener, View.OnFocusChangeListener, TextView.OnEditorActionListener, View.OnClickListener, TextWatcher, DialogInterface.OnClickListener {
|
||||
implements AdapterView.OnItemSelectedListener, View.OnFocusChangeListener, TextView.OnEditorActionListener, View.OnClickListener, TextWatcher {
|
||||
|
||||
@Nonnull
|
||||
private static final DecimalFormat formatter = new DecimalFormat("0.#####E0");
|
||||
@ -121,9 +141,9 @@ public class ConverterFragment extends BaseDialogFragment
|
||||
|
||||
@Override
|
||||
protected void onPrepareDialog(@NonNull AlertDialog.Builder builder) {
|
||||
builder.setPositiveButton(R.string.c_use, this);
|
||||
builder.setPositiveButton(R.string.c_use, null);
|
||||
builder.setNegativeButton(R.string.c_cancel, null);
|
||||
builder.setNeutralButton(R.string.c_copy, this);
|
||||
builder.setNeutralButton(R.string.c_copy, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -312,7 +332,34 @@ public class ConverterFragment extends BaseDialogFragment
|
||||
switch (v.getId()) {
|
||||
case R.id.converter_swap_button:
|
||||
swap();
|
||||
return;
|
||||
break;
|
||||
default:
|
||||
super.onClick(v);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
if (which == DialogInterface.BUTTON_NEGATIVE) {
|
||||
dismiss();
|
||||
return;
|
||||
}
|
||||
final String text = editTextTo.getText().toString();
|
||||
try {
|
||||
final double value = formatter.parse(text).doubleValue();
|
||||
switch (which) {
|
||||
case DialogInterface.BUTTON_POSITIVE:
|
||||
editor.insert(String.valueOf(value));
|
||||
dismiss();
|
||||
break;
|
||||
case DialogInterface.BUTTON_NEUTRAL:
|
||||
clipboard.setText(String.valueOf(value));
|
||||
Toast.makeText(getActivity(), getString(R.string.c_result_copied),
|
||||
Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
}
|
||||
}
|
||||
|
||||
@ -356,23 +403,6 @@ public class ConverterFragment extends BaseDialogFragment
|
||||
convert(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
final String text = editTextTo.getText().toString();
|
||||
try {
|
||||
final double value = formatter.parse(text).doubleValue();
|
||||
switch (which) {
|
||||
case DialogInterface.BUTTON_POSITIVE:
|
||||
editor.insert(String.valueOf(value));
|
||||
break;
|
||||
case DialogInterface.BUTTON_NEUTRAL:
|
||||
clipboard.setText(String.valueOf(value));
|
||||
break;
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
}
|
||||
}
|
||||
|
||||
private enum MyDimension {
|
||||
TIME(Dimension.TIME, "Time"),
|
||||
AMOUNT_OF_SUBSTANCE(Dimension.AMOUNT_OF_SUBSTANCE, "Amount of substance"),
|
||||
|
@ -22,6 +22,8 @@
|
||||
|
||||
package org.solovyev.android.calculator.functions;
|
||||
|
||||
import static org.solovyev.android.calculator.functions.CppFunction.NO_ID;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
@ -35,27 +37,46 @@ import android.support.v4.app.FragmentManager;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.text.Editable;
|
||||
import android.text.TextUtils;
|
||||
import android.view.*;
|
||||
import android.view.ContextMenu;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.ViewParent;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
import jscl.math.function.Function;
|
||||
|
||||
import org.solovyev.android.Activities;
|
||||
import org.solovyev.android.Check;
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.android.calculator.App;
|
||||
import org.solovyev.android.calculator.AppComponent;
|
||||
import org.solovyev.android.calculator.BaseDialogFragment;
|
||||
import org.solovyev.android.calculator.Calculator;
|
||||
import org.solovyev.android.calculator.Engine;
|
||||
import org.solovyev.android.calculator.FloatingCalculatorKeyboard;
|
||||
import org.solovyev.android.calculator.ParseException;
|
||||
import org.solovyev.android.calculator.R;
|
||||
import org.solovyev.android.calculator.VariablesRegistry;
|
||||
import org.solovyev.android.calculator.entities.EntityRemovalDialog;
|
||||
import org.solovyev.android.calculator.keyboard.FloatingKeyboardWindow;
|
||||
import org.solovyev.android.calculator.view.EditTextCompat;
|
||||
import org.solovyev.common.math.MathRegistry;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
import jscl.math.function.Function;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
import java.util.*;
|
||||
|
||||
import static org.solovyev.android.calculator.functions.CppFunction.NO_ID;
|
||||
|
||||
public class EditFunctionFragment extends BaseDialogFragment implements View.OnClickListener, View.OnFocusChangeListener, View.OnKeyListener {
|
||||
|
||||
@ -111,7 +132,8 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC
|
||||
intent.putExtra(FunctionsActivity.EXTRA_FUNCTION, function);
|
||||
context.startActivity(intent);
|
||||
} else {
|
||||
EditFunctionFragment.show(function, ((FunctionsActivity) context).getSupportFragmentManager());
|
||||
EditFunctionFragment.show(function,
|
||||
((FunctionsActivity) context).getSupportFragmentManager());
|
||||
}
|
||||
}
|
||||
|
||||
@ -138,7 +160,8 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC
|
||||
protected void onPrepareDialog(@NonNull AlertDialog.Builder builder) {
|
||||
builder.setNegativeButton(R.string.c_cancel, null);
|
||||
builder.setPositiveButton(R.string.ok, null);
|
||||
builder.setTitle(isNewFunction() ? R.string.function_create_function : R.string.function_edit_function);
|
||||
builder.setTitle(isNewFunction() ? R.string.function_create_function :
|
||||
R.string.function_edit_function);
|
||||
if (!isNewFunction()) {
|
||||
builder.setNeutralButton(R.string.c_remove, null);
|
||||
}
|
||||
@ -158,41 +181,22 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC
|
||||
|
||||
@Override
|
||||
protected void onShowDialog(@NonNull AlertDialog dialog, boolean firstTime) {
|
||||
super.onShowDialog(dialog, firstTime);
|
||||
|
||||
if (firstTime) {
|
||||
nameView.selectAll();
|
||||
showIme(nameView);
|
||||
}
|
||||
|
||||
final Button ok = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
|
||||
ok.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
tryClose();
|
||||
}
|
||||
});
|
||||
if (!isNewFunction()) {
|
||||
Check.isNotNull(function);
|
||||
final Button neutral = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
|
||||
neutral.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
showRemovalDialog(function);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void showRemovalDialog(@NonNull final CppFunction function) {
|
||||
EntityRemovalDialog.showForFunction(getActivity(), function.name, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
Check.isTrue(which == DialogInterface.BUTTON_POSITIVE);
|
||||
functionsRegistry.remove(function.toJsclBuilder().create());
|
||||
dismiss();
|
||||
}
|
||||
});
|
||||
EntityRemovalDialog.showForFunction(getActivity(), function.name,
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
Check.isTrue(which == DialogInterface.BUTTON_POSITIVE);
|
||||
functionsRegistry.remove(function.toJsclBuilder().create());
|
||||
dismiss();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -231,7 +235,8 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC
|
||||
}
|
||||
|
||||
private void showKeyboard() {
|
||||
keyboardWindow.show(new FloatingCalculatorKeyboard(keyboardUser, collectParameters()), getDialog());
|
||||
keyboardWindow.show(new FloatingCalculatorKeyboard(keyboardUser, collectParameters()),
|
||||
getDialog());
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@ -247,8 +252,29 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (v.getId() == R.id.function_body) {
|
||||
showKeyboard();
|
||||
switch (v.getId()) {
|
||||
case R.id.function_body:
|
||||
showKeyboard();
|
||||
break;
|
||||
default:
|
||||
super.onClick(v);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
switch (which) {
|
||||
case DialogInterface.BUTTON_POSITIVE:
|
||||
tryClose();
|
||||
break;
|
||||
case DialogInterface.BUTTON_NEUTRAL:
|
||||
Check.isNotNull(function);
|
||||
showRemovalDialog(function);
|
||||
break;
|
||||
default:
|
||||
super.onClick(dialog, which);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,6 +22,8 @@
|
||||
|
||||
package org.solovyev.android.calculator.variables;
|
||||
|
||||
import static org.solovyev.android.calculator.variables.CppVariable.NO_ID;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
@ -41,12 +43,16 @@ import android.view.inputmethod.InputMethodManager;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.PopupWindow;
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
import jscl.math.function.IConstant;
|
||||
|
||||
import org.solovyev.android.Activities;
|
||||
import org.solovyev.android.Check;
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.android.calculator.App;
|
||||
import org.solovyev.android.calculator.AppComponent;
|
||||
import org.solovyev.android.calculator.BaseDialogFragment;
|
||||
import org.solovyev.android.calculator.Calculator;
|
||||
import org.solovyev.android.calculator.Engine;
|
||||
import org.solovyev.android.calculator.R;
|
||||
import org.solovyev.android.calculator.VariablesRegistry;
|
||||
import org.solovyev.android.calculator.entities.EntityRemovalDialog;
|
||||
import org.solovyev.android.calculator.functions.FunctionsRegistry;
|
||||
import org.solovyev.android.calculator.keyboard.FloatingKeyboard;
|
||||
@ -55,13 +61,16 @@ import org.solovyev.android.calculator.math.MathType;
|
||||
import org.solovyev.android.calculator.view.EditTextCompat;
|
||||
import org.solovyev.common.text.Strings;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
import jscl.math.function.IConstant;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.solovyev.android.calculator.variables.CppVariable.NO_ID;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class EditVariableFragment extends BaseDialogFragment implements View.OnFocusChangeListener, View.OnKeyListener, View.OnClickListener {
|
||||
|
||||
@ -122,7 +131,8 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF
|
||||
intent.putExtra(VariablesActivity.EXTRA_VARIABLE, variable);
|
||||
context.startActivity(intent);
|
||||
} else {
|
||||
EditVariableFragment.showDialog(variable, ((VariablesActivity) context).getSupportFragmentManager());
|
||||
EditVariableFragment.showDialog(variable,
|
||||
((VariablesActivity) context).getSupportFragmentManager());
|
||||
}
|
||||
}
|
||||
|
||||
@ -170,41 +180,22 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF
|
||||
|
||||
@Override
|
||||
protected void onShowDialog(@NonNull AlertDialog dialog, boolean firstTime) {
|
||||
super.onShowDialog(dialog, firstTime);
|
||||
|
||||
if (firstTime) {
|
||||
nameView.selectAll();
|
||||
showIme(nameView);
|
||||
}
|
||||
|
||||
final Button ok = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
|
||||
ok.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
tryClose();
|
||||
}
|
||||
});
|
||||
if (!isNewVariable()) {
|
||||
Check.isNotNull(variable);
|
||||
final Button neutral = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
|
||||
neutral.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
showRemovalDialog(variable);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void showRemovalDialog(@NonNull final CppVariable variable) {
|
||||
EntityRemovalDialog.showForVariable(getActivity(), variable.name, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
Check.isTrue(which == DialogInterface.BUTTON_POSITIVE);
|
||||
variablesRegistry.remove(variable.toJsclBuilder().create());
|
||||
dismiss();
|
||||
}
|
||||
});
|
||||
EntityRemovalDialog.showForVariable(getActivity(), variable.name,
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
Check.isTrue(which == DialogInterface.BUTTON_POSITIVE);
|
||||
variablesRegistry.remove(variable.toJsclBuilder().create());
|
||||
dismiss();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void tryClose() {
|
||||
@ -352,6 +343,25 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF
|
||||
showKeyboard();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
super.onClick(v);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
switch (which) {
|
||||
case DialogInterface.BUTTON_POSITIVE:
|
||||
tryClose();
|
||||
break;
|
||||
case DialogInterface.BUTTON_NEUTRAL:
|
||||
Check.isNotNull(variable);
|
||||
showRemovalDialog(variable);
|
||||
break;
|
||||
default:
|
||||
super.onClick(dialog, which);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user