Add "Remove" button to PlotEditFunctionFragment

This commit is contained in:
serso 2016-03-02 07:43:08 +01:00
parent c661775772
commit 031feddc1d
8 changed files with 60 additions and 56 deletions

View File

@ -1,15 +1,13 @@
package org.solovyev.android.calculator.entities;
package org.solovyev.android.calculator;
import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.StringRes;
import android.support.v7.app.AlertDialog;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.R;
import javax.annotation.Nonnull;
public class EntityRemovalDialog {
public class RemovalConfirmationDialog {
@Nonnull
private final Activity activity;
@ -20,7 +18,7 @@ public class EntityRemovalDialog {
@StringRes
private final int message;
private EntityRemovalDialog(@Nonnull Activity activity, @Nonnull String name, @Nonnull DialogInterface.OnClickListener listener, int message) {
private RemovalConfirmationDialog(@Nonnull Activity activity, @Nonnull String name, @Nonnull DialogInterface.OnClickListener listener, int message) {
this.activity = activity;
this.name = name;
this.listener = listener;
@ -28,11 +26,11 @@ public class EntityRemovalDialog {
}
public static void showForFunction(@Nonnull Activity activity, @Nonnull String name, @Nonnull DialogInterface.OnClickListener listener) {
new EntityRemovalDialog(activity, name, listener, R.string.function_removal_confirmation_question).show();
new RemovalConfirmationDialog(activity, name, listener, R.string.function_removal_confirmation_question).show();
}
public static void showForVariable(@Nonnull Activity activity, @Nonnull String name, @Nonnull DialogInterface.OnClickListener listener) {
new EntityRemovalDialog(activity, name, listener, R.string.c_var_removal_confirmation_question).show();
new RemovalConfirmationDialog(activity, name, listener, R.string.c_var_removal_confirmation_question).show();
}
private void show() {

View File

@ -38,6 +38,7 @@ import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import butterknife.Bind;
import butterknife.ButterKnife;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.keyboard.FloatingKeyboardWindow;
import org.solovyev.android.calculator.view.EditTextCompat;
@ -113,7 +114,10 @@ public abstract class BaseFunctionFragment extends BaseDialogFragment implements
builder.setNegativeButton(R.string.cpp_cancel, null);
builder.setPositiveButton(R.string.cpp_done, null);
builder.setTitle(isNewFunction() ? R.string.function_create_function :
R.string.function_edit_function);
R.string.function_edit_function);
if (!isNewFunction()) {
builder.setNeutralButton(R.string.cpp_delete, null);
}
}
protected final boolean isNewFunction() {
@ -205,12 +209,18 @@ public abstract class BaseFunctionFragment extends BaseDialogFragment implements
case DialogInterface.BUTTON_POSITIVE:
tryClose();
break;
case DialogInterface.BUTTON_NEUTRAL:
Check.isNotNull(function);
showRemovalDialog(function);
break;
default:
super.onClick(dialog, which);
break;
}
}
protected abstract void showRemovalDialog(@NonNull CppFunction function);
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (v.getId() == R.id.function_body) {

View File

@ -7,13 +7,12 @@ import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import jscl.math.function.Function;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.Engine;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.entities.EntityRemovalDialog;
import org.solovyev.android.calculator.RemovalConfirmationDialog;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -54,39 +53,6 @@ public class EditFunctionFragment extends BaseFunctionFragment {
return fragment;
}
@Override
protected void onPrepareDialog(@NonNull AlertDialog.Builder builder) {
super.onPrepareDialog(builder);
if (!isNewFunction()) {
builder.setNeutralButton(R.string.cpp_delete, null);
}
}
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();
}
});
}
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_NEUTRAL:
Check.isNotNull(function);
showRemovalDialog(function);
break;
default:
super.onClick(dialog, which);
break;
}
}
@Override
protected boolean applyData(@Nonnull @NonNull CppFunction function) {
try {
@ -128,4 +94,16 @@ public class EditFunctionFragment extends BaseFunctionFragment {
clearError(nameLabel);
return true;
}
protected void showRemovalDialog(@NonNull final CppFunction function) {
RemovalConfirmationDialog.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();
}
});
}
}

View File

@ -37,7 +37,7 @@ import org.solovyev.android.calculator.Calculator;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.entities.BaseEntitiesFragment;
import org.solovyev.android.calculator.entities.Category;
import org.solovyev.android.calculator.entities.EntityRemovalDialog;
import org.solovyev.android.calculator.RemovalConfirmationDialog;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -90,7 +90,7 @@ public class FunctionsFragment extends BaseEntitiesFragment<Function> {
}
return true;
case R.string.cpp_delete:
EntityRemovalDialog.showForFunction(getActivity(), function.getName(), new DialogInterface.OnClickListener() {
RemovalConfirmationDialog.showForFunction(getActivity(), function.getName(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Check.isTrue(which == DialogInterface.BUTTON_POSITIVE);

View File

@ -1,11 +1,6 @@
package org.solovyev.android.calculator.plot;
import android.text.TextUtils;
import org.solovyev.android.plotter.Function;
import javax.annotation.Nonnull;
import jscl.math.Generic;
import jscl.math.JsclInteger;
import jscl.math.NumericWrapper;
@ -13,6 +8,9 @@ import jscl.math.function.CustomFunction;
import jscl.math.numeric.Complex;
import jscl.math.numeric.Numeric;
import jscl.math.numeric.Real;
import org.solovyev.android.plotter.Function;
import javax.annotation.Nonnull;
public class ExpressionFunction extends Function {
@Nonnull
@ -39,6 +37,9 @@ public class ExpressionFunction extends Function {
} else {
name = function.toString();
}
if (name.length() > 10) {
name = name.substring(0, 10) + "...";
}
}
return imaginary ? "Im(" + name + ")" : name;
}

View File

@ -1,6 +1,7 @@
package org.solovyev.android.calculator.plot;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@ -11,9 +12,11 @@ import android.widget.SeekBar;
import android.widget.TextView;
import butterknife.Bind;
import jscl.math.function.CustomFunction;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.AppComponent;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.RemovalConfirmationDialog;
import org.solovyev.android.calculator.functions.BaseFunctionFragment;
import org.solovyev.android.calculator.functions.CppFunction;
import org.solovyev.android.plotter.Color;
@ -51,8 +54,7 @@ public class PlotEditFunctionFragment extends BaseFunctionFragment
super(R.layout.fragment_plot_function_edit);
}
public static void show(@Nullable PlotFunction function, @Nonnull
FragmentManager fm) {
public static void show(@Nullable PlotFunction function, @Nonnull FragmentManager fm) {
App.showDialog(create(function), "plot-function-editor", fm);
}
@ -184,4 +186,19 @@ public class PlotEditFunctionFragment extends BaseFunctionFragment
public void onStopTrackingTouch(SeekBar seekBar) {
}
protected void showRemovalDialog(@NonNull final CppFunction function) {
Check.isNotNull(plotFunction);
final String functionName = plotFunction.function.getName();
Check.isNotNull(functionName);
RemovalConfirmationDialog.showForFunction(getActivity(), functionName,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Check.isTrue(which == DialogInterface.BUTTON_POSITIVE);
plotter.remove(plotFunction);
dismiss();
}
});
}
}

View File

@ -47,7 +47,7 @@ import dagger.Lazy;
import jscl.math.function.IConstant;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.entities.EntityRemovalDialog;
import org.solovyev.android.calculator.RemovalConfirmationDialog;
import org.solovyev.android.calculator.functions.FunctionsRegistry;
import org.solovyev.android.calculator.keyboard.FloatingKeyboard;
import org.solovyev.android.calculator.keyboard.FloatingKeyboardWindow;
@ -193,7 +193,7 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF
}
private void showRemovalDialog(@NonNull final CppVariable variable) {
EntityRemovalDialog.showForVariable(getActivity(), variable.name,
RemovalConfirmationDialog.showForVariable(getActivity(), variable.name,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

View File

@ -34,7 +34,7 @@ import org.solovyev.android.Check;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.entities.BaseEntitiesFragment;
import org.solovyev.android.calculator.entities.Category;
import org.solovyev.android.calculator.entities.EntityRemovalDialog;
import org.solovyev.android.calculator.RemovalConfirmationDialog;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.common.JPredicate;
import org.solovyev.common.collections.Collections;
@ -118,7 +118,7 @@ public class VariablesFragment extends BaseEntitiesFragment<IConstant> {
EditVariableFragment.showDialog(CppVariable.builder(variable).build(), activity);
return true;
case R.string.cpp_delete:
EntityRemovalDialog.showForVariable(getActivity(), variable.getName(), new DialogInterface.OnClickListener() {
RemovalConfirmationDialog.showForVariable(getActivity(), variable.getName(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Check.isTrue(which == DialogInterface.BUTTON_POSITIVE);