Refactor button handling in dialog fragments

This commit is contained in:
serso 2016-02-12 12:46:12 +01:00
parent 9879046288
commit a60817b491
4 changed files with 214 additions and 108 deletions

View File

@ -1,5 +1,7 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import static org.solovyev.android.calculator.App.cast;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.SharedPreferences; import android.content.SharedPreferences;
@ -14,15 +16,20 @@ import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import javax.inject.Inject; import javax.inject.Inject;
import static org.solovyev.android.calculator.App.cast; public abstract class BaseDialogFragment extends DialogFragment implements View.OnClickListener, DialogInterface.OnClickListener {
public abstract class BaseDialogFragment extends DialogFragment {
@Inject @Inject
protected SharedPreferences preferences; protected SharedPreferences preferences;
@Nullable
private Button positiveButton;
@Nullable
private Button negativeButton;
@Nullable
private Button neutralButton;
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
@ -51,6 +58,9 @@ public abstract class BaseDialogFragment extends DialogFragment {
dialog.setOnShowListener(new DialogInterface.OnShowListener() { dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override @Override
public void onShow(DialogInterface d) { 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); onShowDialog(dialog, savedInstanceState == null);
} }
}); });
@ -60,6 +70,15 @@ public abstract class BaseDialogFragment extends DialogFragment {
protected void onShowDialog(@NonNull AlertDialog dialog, boolean firstTime) { 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); protected abstract void onPrepareDialog(@NonNull AlertDialog.Builder builder);
@Nullable @Nullable
@ -86,4 +105,25 @@ public abstract class BaseDialogFragment extends DialogFragment {
final InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); final InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); 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;
}
}
} }

View File

@ -16,10 +16,33 @@ import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.inputmethod.EditorInfo; 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.Bind;
import butterknife.ButterKnife; 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.annotation.Nonnull;
import javax.inject.Inject; import javax.inject.Inject;
@ -27,12 +50,9 @@ import javax.measure.unit.Dimension;
import javax.measure.unit.NonSI; import javax.measure.unit.NonSI;
import javax.measure.unit.SI; import javax.measure.unit.SI;
import javax.measure.unit.Unit; import javax.measure.unit.Unit;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.*;
public class ConverterFragment extends BaseDialogFragment 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 @Nonnull
private static final DecimalFormat formatter = new DecimalFormat("0.#####E0"); private static final DecimalFormat formatter = new DecimalFormat("0.#####E0");
@ -121,9 +141,9 @@ public class ConverterFragment extends BaseDialogFragment
@Override @Override
protected void onPrepareDialog(@NonNull AlertDialog.Builder builder) { 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.setNegativeButton(R.string.c_cancel, null);
builder.setNeutralButton(R.string.c_copy, this); builder.setNeutralButton(R.string.c_copy, null);
} }
@Override @Override
@ -312,8 +332,35 @@ public class ConverterFragment extends BaseDialogFragment
switch (v.getId()) { switch (v.getId()) {
case R.id.converter_swap_button: case R.id.converter_swap_button:
swap(); swap();
break;
default:
super.onClick(v);
break;
}
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_NEGATIVE) {
dismiss();
return; 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) {
}
} }
private void swap() { private void swap() {
@ -356,23 +403,6 @@ public class ConverterFragment extends BaseDialogFragment
convert(false); 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 { private enum MyDimension {
TIME(Dimension.TIME, "Time"), TIME(Dimension.TIME, "Time"),
AMOUNT_OF_SUBSTANCE(Dimension.AMOUNT_OF_SUBSTANCE, "Amount of substance"), AMOUNT_OF_SUBSTANCE(Dimension.AMOUNT_OF_SUBSTANCE, "Amount of substance"),

View File

@ -22,6 +22,8 @@
package org.solovyev.android.calculator.functions; package org.solovyev.android.calculator.functions;
import static org.solovyev.android.calculator.functions.CppFunction.NO_ID;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
@ -35,27 +37,46 @@ import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.text.Editable; import android.text.Editable;
import android.text.TextUtils; 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.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import butterknife.Bind;
import butterknife.ButterKnife;
import jscl.math.function.Function;
import org.solovyev.android.Activities; import org.solovyev.android.Activities;
import org.solovyev.android.Check; 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.entities.EntityRemovalDialog;
import org.solovyev.android.calculator.keyboard.FloatingKeyboardWindow; import org.solovyev.android.calculator.keyboard.FloatingKeyboardWindow;
import org.solovyev.android.calculator.view.EditTextCompat; import org.solovyev.android.calculator.view.EditTextCompat;
import org.solovyev.common.math.MathRegistry; 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.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.inject.Inject; 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 { 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); intent.putExtra(FunctionsActivity.EXTRA_FUNCTION, function);
context.startActivity(intent); context.startActivity(intent);
} else { } 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) { protected void onPrepareDialog(@NonNull AlertDialog.Builder builder) {
builder.setNegativeButton(R.string.c_cancel, null); builder.setNegativeButton(R.string.c_cancel, null);
builder.setPositiveButton(R.string.ok, 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()) { if (!isNewFunction()) {
builder.setNeutralButton(R.string.c_remove, null); builder.setNeutralButton(R.string.c_remove, null);
} }
@ -158,34 +181,15 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC
@Override @Override
protected void onShowDialog(@NonNull AlertDialog dialog, boolean firstTime) { protected void onShowDialog(@NonNull AlertDialog dialog, boolean firstTime) {
super.onShowDialog(dialog, firstTime);
if (firstTime) { if (firstTime) {
nameView.selectAll(); nameView.selectAll();
showIme(nameView); 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) { private void showRemovalDialog(@NonNull final CppFunction function) {
EntityRemovalDialog.showForFunction(getActivity(), function.name, new DialogInterface.OnClickListener() { EntityRemovalDialog.showForFunction(getActivity(), function.name,
new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
Check.isTrue(which == DialogInterface.BUTTON_POSITIVE); Check.isTrue(which == DialogInterface.BUTTON_POSITIVE);
@ -231,7 +235,8 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC
} }
private void showKeyboard() { private void showKeyboard() {
keyboardWindow.show(new FloatingCalculatorKeyboard(keyboardUser, collectParameters()), getDialog()); keyboardWindow.show(new FloatingCalculatorKeyboard(keyboardUser, collectParameters()),
getDialog());
} }
@Nonnull @Nonnull
@ -247,8 +252,29 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (v.getId() == R.id.function_body) { switch (v.getId()) {
case R.id.function_body:
showKeyboard(); 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;
} }
} }

View File

@ -22,6 +22,8 @@
package org.solovyev.android.calculator.variables; package org.solovyev.android.calculator.variables;
import static org.solovyev.android.calculator.variables.CppVariable.NO_ID;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
@ -41,12 +43,16 @@ import android.view.inputmethod.InputMethodManager;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.PopupWindow; import android.widget.PopupWindow;
import butterknife.Bind;
import butterknife.ButterKnife;
import jscl.math.function.IConstant;
import org.solovyev.android.Activities; import org.solovyev.android.Activities;
import org.solovyev.android.Check; 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.entities.EntityRemovalDialog;
import org.solovyev.android.calculator.functions.FunctionsRegistry; import org.solovyev.android.calculator.functions.FunctionsRegistry;
import org.solovyev.android.calculator.keyboard.FloatingKeyboard; 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.android.calculator.view.EditTextCompat;
import org.solovyev.common.text.Strings; import org.solovyev.common.text.Strings;
import javax.annotation.Nonnull; import butterknife.Bind;
import javax.annotation.Nullable; import butterknife.ButterKnife;
import javax.inject.Inject; import jscl.math.function.IConstant;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; 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 { 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); intent.putExtra(VariablesActivity.EXTRA_VARIABLE, variable);
context.startActivity(intent); context.startActivity(intent);
} else { } else {
EditVariableFragment.showDialog(variable, ((VariablesActivity) context).getSupportFragmentManager()); EditVariableFragment.showDialog(variable,
((VariablesActivity) context).getSupportFragmentManager());
} }
} }
@ -170,34 +180,15 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF
@Override @Override
protected void onShowDialog(@NonNull AlertDialog dialog, boolean firstTime) { protected void onShowDialog(@NonNull AlertDialog dialog, boolean firstTime) {
super.onShowDialog(dialog, firstTime);
if (firstTime) { if (firstTime) {
nameView.selectAll(); nameView.selectAll();
showIme(nameView); 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) { private void showRemovalDialog(@NonNull final CppVariable variable) {
EntityRemovalDialog.showForVariable(getActivity(), variable.name, new DialogInterface.OnClickListener() { EntityRemovalDialog.showForVariable(getActivity(), variable.name,
new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
Check.isTrue(which == DialogInterface.BUTTON_POSITIVE); Check.isTrue(which == DialogInterface.BUTTON_POSITIVE);
@ -352,6 +343,25 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF
showKeyboard(); showKeyboard();
} }
break; 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;
} }
} }