From 031feddc1dd3fa0c3832aac8ecf14ea53f49fd72 Mon Sep 17 00:00:00 2001 From: serso Date: Wed, 2 Mar 2016 07:43:08 +0100 Subject: [PATCH] Add "Remove" button to PlotEditFunctionFragment --- ...og.java => RemovalConfirmationDialog.java} | 12 ++--- .../functions/BaseFunctionFragment.java | 12 ++++- .../functions/EditFunctionFragment.java | 48 +++++-------------- .../functions/FunctionsFragment.java | 4 +- .../calculator/plot/ExpressionFunction.java | 11 +++-- .../plot/PlotEditFunctionFragment.java | 21 +++++++- .../variables/EditVariableFragment.java | 4 +- .../variables/VariablesFragment.java | 4 +- 8 files changed, 60 insertions(+), 56 deletions(-) rename app/src/main/java/org/solovyev/android/calculator/{entities/EntityRemovalDialog.java => RemovalConfirmationDialog.java} (74%) diff --git a/app/src/main/java/org/solovyev/android/calculator/entities/EntityRemovalDialog.java b/app/src/main/java/org/solovyev/android/calculator/RemovalConfirmationDialog.java similarity index 74% rename from app/src/main/java/org/solovyev/android/calculator/entities/EntityRemovalDialog.java rename to app/src/main/java/org/solovyev/android/calculator/RemovalConfirmationDialog.java index 6ef347c2..9ffa0644 100644 --- a/app/src/main/java/org/solovyev/android/calculator/entities/EntityRemovalDialog.java +++ b/app/src/main/java/org/solovyev/android/calculator/RemovalConfirmationDialog.java @@ -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() { diff --git a/app/src/main/java/org/solovyev/android/calculator/functions/BaseFunctionFragment.java b/app/src/main/java/org/solovyev/android/calculator/functions/BaseFunctionFragment.java index b1775551..db6ce26f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/functions/BaseFunctionFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/functions/BaseFunctionFragment.java @@ -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) { diff --git a/app/src/main/java/org/solovyev/android/calculator/functions/EditFunctionFragment.java b/app/src/main/java/org/solovyev/android/calculator/functions/EditFunctionFragment.java index 173c7b51..b582f0d4 100644 --- a/app/src/main/java/org/solovyev/android/calculator/functions/EditFunctionFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/functions/EditFunctionFragment.java @@ -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(); + } + }); + } } diff --git a/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsFragment.java b/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsFragment.java index bff7750e..1b71abe9 100644 --- a/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsFragment.java @@ -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 { } 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); diff --git a/app/src/main/java/org/solovyev/android/calculator/plot/ExpressionFunction.java b/app/src/main/java/org/solovyev/android/calculator/plot/ExpressionFunction.java index e4c12a2e..acdefc26 100644 --- a/app/src/main/java/org/solovyev/android/calculator/plot/ExpressionFunction.java +++ b/app/src/main/java/org/solovyev/android/calculator/plot/ExpressionFunction.java @@ -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; } diff --git a/app/src/main/java/org/solovyev/android/calculator/plot/PlotEditFunctionFragment.java b/app/src/main/java/org/solovyev/android/calculator/plot/PlotEditFunctionFragment.java index a924303c..c1b522cd 100644 --- a/app/src/main/java/org/solovyev/android/calculator/plot/PlotEditFunctionFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/plot/PlotEditFunctionFragment.java @@ -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(); + } + }); + } } diff --git a/app/src/main/java/org/solovyev/android/calculator/variables/EditVariableFragment.java b/app/src/main/java/org/solovyev/android/calculator/variables/EditVariableFragment.java index d5cd6212..4c806ffe 100644 --- a/app/src/main/java/org/solovyev/android/calculator/variables/EditVariableFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/variables/EditVariableFragment.java @@ -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) { diff --git a/app/src/main/java/org/solovyev/android/calculator/variables/VariablesFragment.java b/app/src/main/java/org/solovyev/android/calculator/variables/VariablesFragment.java index 6465ddbd..9275131a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/variables/VariablesFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/variables/VariablesFragment.java @@ -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 { 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);