From 75a3a72a845626c0ca0cd9a16b1e1f59c78238b4 Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Wed, 14 Nov 2012 21:44:23 +0400 Subject: [PATCH] functions --- .../CalculatorFunctionsMathRegistry.java | 3 +- .../calculator/ToJsclTextProcessor.java | 6 - .../android/calculator/model/AFunction.java | 58 +++---- .../calculator/model/FunctionsTest.java | 155 ++++++++++++++++++ calculatorpp/res/values-es/text_strings.xml | 2 +- calculatorpp/res/values-it/text_strings.xml | 2 +- calculatorpp/res/values-ru/text_strings.xml | 2 +- calculatorpp/res/values-uk/text_strings.xml | 2 +- .../res/values-zh-rtw/text_strings.xml | 2 +- calculatorpp/res/values/text_strings.xml | 3 +- .../function/FunctionEditDialogFragment.java | 68 +++++--- .../function/FunctionEditorSaver.java | 27 ++- .../math/edit/CalculatorVarsFragment.java | 2 +- .../math/edit/MathEntityRemover.java | 118 ++++++++++--- .../math/edit/VarEditDialogFragment.java | 2 +- 15 files changed, 342 insertions(+), 110 deletions(-) create mode 100644 calculatorpp-core/src/test/java/org/solovyev/android/calculator/model/FunctionsTest.java diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsMathRegistry.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsMathRegistry.java index 9c57ac93..f17af318 100644 --- a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsMathRegistry.java +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsMathRegistry.java @@ -16,6 +16,7 @@ import org.solovyev.android.calculator.model.MathEntityBuilder; import org.solovyev.common.JBuilder; import org.solovyev.common.math.MathRegistry; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -96,7 +97,7 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis final AFunction result = new AFunction(); result.setName(entity.getName()); result.setContent(((CustomFunction) entity).getContent()); - result.setParameterNames(((CustomFunction) entity).getParameterNames()); + result.setParameterNames(new ArrayList(((CustomFunction) entity).getParameterNames())); return result; } else { return null; diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/ToJsclTextProcessor.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/ToJsclTextProcessor.java index 881482ae..efe9d942 100644 --- a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/ToJsclTextProcessor.java +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/ToJsclTextProcessor.java @@ -72,12 +72,6 @@ public class ToJsclTextProcessor implements TextProcessor parameterNames = Collections.emptyList(); + private List parameterNames = new ArrayList(); @Element private boolean system; @Element(required = false) - @Nullable - private String description; + @NotNull + private String description = ""; /* ********************************************************************** @@ -86,7 +88,7 @@ public class AFunction implements IFunction, MathPersistenceEntity { final IFunction that = ((IFunction) mathEntity); this.name = that.getName(); this.content = that.getContent(); - this.description = this.getContent(); + this.description = StringUtils.getNotEmpty(this.getDescription(), ""); this.system = that.isSystem(); if (that.isIdDefined()) { this.id = that.getId(); @@ -104,7 +106,7 @@ public class AFunction implements IFunction, MathPersistenceEntity { /* ********************************************************************** * - * GETTERS/SEETTERS + * GETTERS/SETTERS * ********************************************************************** */ @@ -144,8 +146,8 @@ public class AFunction implements IFunction, MathPersistenceEntity { return content; } - @Nullable - @Override + @NotNull + @Override public String getDescription() { return this.description; } @@ -201,29 +203,15 @@ public class AFunction implements IFunction, MathPersistenceEntity { this.id = function.getId(); } - public Builder(@NotNull IConstant function) { - this.name = function.getName(); + public Builder(@NotNull String name, + @NotNull String value, + @NotNull List parameterNames) { + this.name = name; + this.value = value; + this.parameterNames = parameterNames; + } - this.value = function.getValue(); - - this.system = function.isSystem(); - this.description = function.getDescription(); - if (function.isIdDefined()) { - this.id = function.getId(); - } - } - - public Builder(@NotNull String name, @NotNull Double value) { - this(name, String.valueOf(value)); - } - - public Builder(@NotNull String name, @Nullable String value) { - this.name = name; - this.value = value; - } - - - @NotNull + @NotNull public Builder setName(@NotNull String name) { this.name = name; return this; @@ -262,8 +250,8 @@ public class AFunction implements IFunction, MathPersistenceEntity { result.name = name; result.content = value; result.system = system; - result.description = description; - result.parameterNames = parameterNames; + result.description = StringUtils.getNotEmpty(description, ""); + result.parameterNames = new ArrayList(parameterNames); return result; } diff --git a/calculatorpp-core/src/test/java/org/solovyev/android/calculator/model/FunctionsTest.java b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/model/FunctionsTest.java new file mode 100644 index 00000000..952df63f --- /dev/null +++ b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/model/FunctionsTest.java @@ -0,0 +1,155 @@ +package org.solovyev.android.calculator.model; + +import jscl.util.ExpressionGenerator; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.junit.Assert; +import org.junit.Test; +import org.simpleframework.xml.Serializer; +import org.simpleframework.xml.core.Persister; +import org.solovyev.common.equals.CollectionEqualizer; +import org.solovyev.common.equals.EqualsUtils; +import org.solovyev.common.text.StringUtils; + +import java.io.StringWriter; +import java.util.*; + +/** + * User: serso + * Date: 11/14/12 + * Time: 8:06 PM + */ +public class FunctionsTest { + + private static final String xml = "\n" + + " \n" + + " \n" + + " test\n" + + " x+y\n" + + " \n" + + " x\n" + + " y\n" + + " \n" + + " false\n" + + " description\n" + + " \n" + + " \n" + + " z_2\n" + + " e^(z_1^2+z_2^2)\n" + + " \n" + + " z_1\n" + + " z_2\n" + + " \n" + + " true\n" + + " \n" + + " \n" + + " \n" + + " z_2\n" + + " e^(z_1^2+z_2^2)\n" + + " \n" + + " z_1\n" + + " z_2\n" + + " \n" + + " true\n" + + " \n" + + " \n" + + " \n" + + ""; + + @Test + public void testXml() throws Exception { + final Functions in = new Functions(); + + AFunction first = new AFunction.Builder("test", "x+y", Arrays.asList("x", "y")).setDescription("description").setSystem(false).create(); + in.getEntities().add(first); + + AFunction second = new AFunction.Builder("z_2", "e^(z_1^2+z_2^2)", Arrays.asList("z_1", "z_2")).setSystem(true).create(); + in.getEntities().add(second); + + AFunction third = new AFunction.Builder("z_2", "e^(z_1^2+z_2^2)", Arrays.asList("z_1", "z_2")).setSystem(true).setDescription("").create(); + in.getEntities().add(third); + + final Functions out = testXml(in, xml); + + Assert.assertTrue(!out.getEntities().isEmpty()); + + final AFunction firstOut = out.getEntities().get(0); + final AFunction secondOut = out.getEntities().get(1); + + assertEquals(first, firstOut); + assertEquals(second, secondOut); + + } + + @NotNull + private Functions testXml(@NotNull Functions in, @Nullable String expectedXml) throws Exception { + final String actualXml = toXml(in); + + if (expectedXml != null) { + Assert.assertEquals(expectedXml, actualXml); + } + + final Serializer serializer = new Persister(); + final Functions out = serializer.read(Functions.class, actualXml); + final String actualXml2 = toXml(out); + Assert.assertEquals(actualXml, actualXml2); + return out; + } + + private String toXml(Functions in) throws Exception { + final StringWriter sw = new StringWriter(); + final Serializer serializer = new Persister(); + serializer.write(in, sw); + return sw.toString(); + } + + @Test + public void testRandomXml() throws Exception { + final Functions in = new Functions(); + + final Random random = new Random(new Date().getTime()); + + ExpressionGenerator generator = new ExpressionGenerator(10); + for ( int i = 0; i < 1000; i++ ) { + final String content = generator.generate(); + + final String paramsString = StringUtils.generateRandomString(random.nextInt(10)); + final List parameterNames = new ArrayList(); + for (int j = 0; j < paramsString.length(); j++) { + parameterNames.add(String.valueOf(paramsString.charAt(j))); + } + + final AFunction.Builder builder = new AFunction.Builder("test_" + i, content, parameterNames); + + if ( random.nextBoolean() ) { + builder.setDescription(StringUtils.generateRandomString(random.nextInt(100))); + } + + builder.setSystem(random.nextBoolean()); + + in.getEntities().add(builder.create()); + } + + testXml(in, null); + } + + private void assertEquals(@NotNull final AFunction expected, + @NotNull AFunction actual) { + //Assert.assertEquals(expected.getId(), actual.getId()); + Assert.assertEquals(expected.getContent(), actual.getContent()); + Assert.assertEquals(expected.getDescription(), actual.getDescription()); + Assert.assertEquals(expected.getName(), actual.getName()); + Assert.assertThat(actual.getParameterNames(), new BaseMatcher>() { + @Override + public boolean matches(Object item) { + return EqualsUtils.areEqual(expected.getParameterNames(), (List)item, new CollectionEqualizer(null)); + } + + @Override + public void describeTo(Description description) { + } + }); + } +} diff --git a/calculatorpp/res/values-es/text_strings.xml b/calculatorpp/res/values-es/text_strings.xml index df9aad27..1dfaed43 100644 --- a/calculatorpp/res/values-es/text_strings.xml +++ b/calculatorpp/res/values-es/text_strings.xml @@ -49,7 +49,7 @@ Borrar Si No - Confirmación de Borrado + Confirmación de Borrado ¿Realmente desea borrar la variable \'%s\'? Nombre Valor diff --git a/calculatorpp/res/values-it/text_strings.xml b/calculatorpp/res/values-it/text_strings.xml index 17415dc8..aeee806e 100644 --- a/calculatorpp/res/values-it/text_strings.xml +++ b/calculatorpp/res/values-it/text_strings.xml @@ -49,7 +49,7 @@ Rimuovi No - Conferma rimozione + Conferma rimozione Vuoi davvero cancellare la variabile \'%s\'? Nome Valore diff --git a/calculatorpp/res/values-ru/text_strings.xml b/calculatorpp/res/values-ru/text_strings.xml index eb47996f..9b9f3c4d 100644 --- a/calculatorpp/res/values-ru/text_strings.xml +++ b/calculatorpp/res/values-ru/text_strings.xml @@ -54,7 +54,7 @@ Удалить Да Нет - Подтверждение удаления + Подтверждение удаления Вы действительно хотите удалить переменную \'%s\'? Имя Значение diff --git a/calculatorpp/res/values-uk/text_strings.xml b/calculatorpp/res/values-uk/text_strings.xml index 01a08ed2..7fae09e9 100644 --- a/calculatorpp/res/values-uk/text_strings.xml +++ b/calculatorpp/res/values-uk/text_strings.xml @@ -54,7 +54,7 @@ Вилучити Так Ні - Підтвердження вилучення + Підтвердження вилучення Ви дійсно хочете вилучити змінну \'%s\'? Ім’я Значення diff --git a/calculatorpp/res/values-zh-rtw/text_strings.xml b/calculatorpp/res/values-zh-rtw/text_strings.xml index 982a4802..e1c4e388 100644 --- a/calculatorpp/res/values-zh-rtw/text_strings.xml +++ b/calculatorpp/res/values-zh-rtw/text_strings.xml @@ -54,7 +54,7 @@ 移除 - 確認移除 + 確認移除 您真的要移除變數 \'%s\' 嗎? 名稱 diff --git a/calculatorpp/res/values/text_strings.xml b/calculatorpp/res/values/text_strings.xml index 3a28e6fa..848188ac 100644 --- a/calculatorpp/res/values/text_strings.xml +++ b/calculatorpp/res/values/text_strings.xml @@ -54,7 +54,7 @@ Remove Yes No - Removal confirmation + Removal confirmation Do you really want to delete \'%s\' variable? Name Value @@ -246,5 +246,6 @@ Function name clashes with function name! Function body could not be empty! Function parameter should not be empty! + Do you really want to delete \'%s\' function? \ No newline at end of file diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/function/FunctionEditDialogFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/function/FunctionEditDialogFragment.java index 2d4f69b5..6e027005 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/function/FunctionEditDialogFragment.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/function/FunctionEditDialogFragment.java @@ -6,20 +6,19 @@ import android.support.v4.app.FragmentManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.EditText; import jscl.math.function.CustomFunction; import jscl.math.function.Function; import jscl.math.function.IFunction; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.android.AndroidUtils2; -import org.solovyev.android.calculator.CalculatorEventData; -import org.solovyev.android.calculator.CalculatorEventListener; -import org.solovyev.android.calculator.CalculatorEventType; -import org.solovyev.android.calculator.CalculatorLocatorImpl; -import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.math.edit.MathEntityRemover; import org.solovyev.android.calculator.model.AFunction; +import java.util.List; + /** * User: serso * Date: 11/13/12 @@ -30,10 +29,7 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul @NotNull private final Input input; - @NotNull - private FunctionParamsView paramsView; - - public FunctionEditDialogFragment() { + public FunctionEditDialogFragment() { this(Input.newInstance()); } @@ -50,17 +46,33 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul public void onViewCreated(@NotNull View root, Bundle savedInstanceState) { super.onViewCreated(root, savedInstanceState); - paramsView = (FunctionParamsView) root.findViewById(R.id.function_params_layout); + final FunctionParamsView paramsView = (FunctionParamsView) root.findViewById(R.id.function_params_layout); final AFunction.Builder builder; final IFunction function = input.getFunction(); if (function != null) { builder = new AFunction.Builder(function); - paramsView.init(function.getParameterNames()); } else { builder = new AFunction.Builder(); - paramsView.init(); - } + } + + final List parameterNames = input.getParameterNames(); + if (parameterNames != null) { + paramsView.init(parameterNames); + } else { + paramsView.init(); + } + + final EditText editName = (EditText) root.findViewById(R.id.function_edit_name); + // show soft keyboard automatically + editName.requestFocus(); + editName.setText(input.getName()); + + final EditText editDescription = (EditText) root.findViewById(R.id.function_edit_description); + editDescription.setText(input.getDescription()); + + final EditText editContent = (EditText) root.findViewById(R.id.function_edit_value); + editContent.setText(input.getContent()); root.findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() { @Override @@ -81,7 +93,7 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul getDialog().setTitle(R.string.function_edit_function); Function customFunction = new CustomFunction.Builder(function).create(); - root.findViewById(R.id.remove_button).setOnClickListener(new MathEntityRemover(customFunction, null, CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry(), getActivity(), this)); + root.findViewById(R.id.remove_button).setOnClickListener(MathEntityRemover.newFunctionRemover(customFunction, null, this.getActivity(), FunctionEditDialogFragment.this)); } } @@ -105,7 +117,7 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul case function_removed: case function_added: case function_changed: - if ( calculatorEventData.getSource() == this ) { + if ( calculatorEventData.getSource() == FunctionEditDialogFragment.this ) { dismiss(); } break; @@ -134,11 +146,14 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul private String name; @Nullable - private String value; + private String content; @Nullable private String description; + @Nullable + private List parameterNames; + private Input() { } @@ -157,16 +172,20 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul @NotNull public static Input newFromValue(@Nullable String value) { final Input result = new Input(); - result.value = value; + result.content = value; return result; } @NotNull - public static Input newInstance(@Nullable IFunction function, @Nullable String name, @Nullable String value, @Nullable String description) { + public static Input newInstance(@Nullable IFunction function, + @Nullable String name, + @Nullable String value, + @Nullable String description) { + final Input result = new Input(); result.function = function; result.name = name; - result.value = value; + result.content = value; result.description = description; return result; } @@ -182,13 +201,18 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul } @Nullable - public String getValue() { - return value == null ? (function == null ? null : function.getContent()) : value; + public String getContent() { + return content == null ? (function == null ? null : function.getContent()) : content; } @Nullable public String getDescription() { - return description == null ? (function == null ? null : function.getContent()) : description; + return description == null ? (function == null ? null : function.getDescription()) : description; + } + + @Nullable + public List getParameterNames() { + return parameterNames == null ? (function == null ? null : function.getParameterNames()) : parameterNames; } } } diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/function/FunctionEditorSaver.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/function/FunctionEditorSaver.java index 3fc5d6c8..d6931af4 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/function/FunctionEditorSaver.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/function/FunctionEditorSaver.java @@ -11,7 +11,6 @@ import org.solovyev.android.calculator.CalculatorFunctionsMathRegistry; import org.solovyev.android.calculator.CalculatorLocatorImpl; import org.solovyev.android.calculator.CalculatorMathRegistry; import org.solovyev.android.calculator.R; -import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.math.edit.VarEditorSaver; import org.solovyev.android.calculator.model.AFunction; import org.solovyev.android.calculator.model.MathEntityBuilder; @@ -84,23 +83,17 @@ public class FunctionEditorSaver implements View.OnClickListener { if (canBeSaved) { if (validateParameters(parameterNames)) { - final MathType.Result mathType = MathType.getType(name, 0, false); - if (mathType.getMathType() == MathType.text || mathType.getMathType() == MathType.function ) { - - if (!StringUtils.isEmpty(content)) { - builder.setParameterNames(parameterNames); - builder.setName(name); - builder.setDescription(description); - builder.setValue(content); - error = null; - } else { - error = R.string.function_is_empty; - } - } else { - error = R.string.function_name_clashes; - } - } else { + if (!StringUtils.isEmpty(content)) { + builder.setParameterNames(parameterNames); + builder.setName(name); + builder.setDescription(description); + builder.setValue(content); + error = null; + } else { + error = R.string.function_is_empty; + } + } else { error = R.string.function_param_not_empty; } } else { diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragment.java index 814b1cf8..ffeb2ac6 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragment.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragment.java @@ -234,7 +234,7 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment(constant, null, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), context, context).showConfirmationDialog(); + MathEntityRemover.newConstantRemover(constant, null, context, context).showConfirmationDialog(); } }, diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/MathEntityRemover.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/MathEntityRemover.java index 7a9d20fe..0d6f9815 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/MathEntityRemover.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/MathEntityRemover.java @@ -11,6 +11,8 @@ import android.content.Context; import android.content.DialogInterface; import android.view.View; import android.widget.TextView; +import jscl.math.function.Function; +import jscl.math.function.IConstant; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.CalculatorEventType; @@ -43,43 +45,69 @@ public class MathEntityRemover implements View.OnClickList @NotNull private final Object source; - public MathEntityRemover(@NotNull T mathEntity, - @Nullable DialogInterface.OnClickListener callbackOnCancel, - @NotNull CalculatorMathRegistry varsRegistry, - @NotNull Context context, - @NotNull Object source) { - this(mathEntity, callbackOnCancel, false, varsRegistry, context, source); - } + @NotNull + private final Params params; - public MathEntityRemover(@NotNull T mathEntity, + /* + ********************************************************************** + * + * CONSTRUCTORS + * + ********************************************************************** + */ + + private MathEntityRemover(@NotNull T mathEntity, @Nullable DialogInterface.OnClickListener callbackOnCancel, boolean confirmed, @NotNull CalculatorMathRegistry varsRegistry, @NotNull Context context, - @NotNull Object source) { + @NotNull Object source, + @NotNull Params params) { this.mathEntity = mathEntity; this.callbackOnCancel = callbackOnCancel; this.confirmed = confirmed; this.varsRegistry = varsRegistry; this.context = context; this.source = source; + this.params = params; } + public static MathEntityRemover newConstantRemover(@NotNull IConstant constant, + @Nullable DialogInterface.OnClickListener callbackOnCancel, + @NotNull Context context, + @NotNull Object source) { + return new MathEntityRemover(constant, callbackOnCancel, false, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), context, source, Params.newConstantInstance()); + } + + public static MathEntityRemover newFunctionRemover(@NotNull Function function, + @Nullable DialogInterface.OnClickListener callbackOnCancel, + @NotNull Context context, + @NotNull Object source) { + return new MathEntityRemover(function, callbackOnCancel, false, CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry(), context, source, Params.newFunctionInstance()); + } + + /* + ********************************************************************** + * + * METHODS + * + ********************************************************************** + */ - public void showConfirmationDialog() { + public void showConfirmationDialog() { final TextView question = new TextView(context); - question.setText(String.format(context.getString(R.string.c_var_removal_confirmation_question), mathEntity.getName())); - question.setPadding(6, 6, 6, 6); - final AlertDialog.Builder builder = new AlertDialog.Builder(context) - .setCancelable(true) - .setView(question) - .setTitle(R.string.c_var_removal_confirmation) - .setNegativeButton(R.string.c_no, callbackOnCancel) - .setPositiveButton(R.string.c_yes, new MathEntityRemover(mathEntity, callbackOnCancel, true, varsRegistry, context, source)); + question.setText(String.format(context.getString(params.getRemovalConfirmationQuestionResId()), mathEntity.getName())); + question.setPadding(6, 6, 6, 6); + final AlertDialog.Builder builder = new AlertDialog.Builder(context) + .setCancelable(true) + .setView(question) + .setTitle(params.getRemovalConfirmationTitleResId()) + .setNegativeButton(R.string.c_no, callbackOnCancel) + .setPositiveButton(R.string.c_yes, new MathEntityRemover(mathEntity, callbackOnCancel, true, varsRegistry, context, source, params)); - builder.create().show(); - } + builder.create().show(); + } @Override public void onClick(@Nullable View v) { @@ -89,7 +117,7 @@ public class MathEntityRemover implements View.OnClickList varsRegistry.remove(mathEntity); varsRegistry.save(); - CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.constant_removed, mathEntity, source); + CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(params.getCalculatorEventType(), mathEntity, source); } } @@ -97,4 +125,52 @@ public class MathEntityRemover implements View.OnClickList public void onClick(DialogInterface dialog, int which) { onClick(null); } + + /* + ********************************************************************** + * + * STATIC + * + ********************************************************************** + */ + + private static final class Params { + + private int removalConfirmationTitleResId; + + private int removalConfirmationQuestionResId; + + private CalculatorEventType calculatorEventType; + + private Params() { + } + + public int getRemovalConfirmationTitleResId() { + return removalConfirmationTitleResId; + } + + public int getRemovalConfirmationQuestionResId() { + return removalConfirmationQuestionResId; + } + + public CalculatorEventType getCalculatorEventType() { + return calculatorEventType; + } + + private static Params newConstantInstance() { + final Params result = new Params(); + result.removalConfirmationTitleResId = R.string.removal_confirmation; + result.removalConfirmationQuestionResId = R.string.c_var_removal_confirmation_question; + result.calculatorEventType = CalculatorEventType.constant_removed; + return result; + } + + private static Params newFunctionInstance() { + final Params result = new Params(); + result.removalConfirmationTitleResId = R.string.removal_confirmation; + result.removalConfirmationQuestionResId = R.string.function_removal_confirmation_question; + result.calculatorEventType = CalculatorEventType.function_removed; + return result; + } + } } diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/VarEditDialogFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/VarEditDialogFragment.java index e468b3aa..2f908f9b 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/VarEditDialogFragment.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/VarEditDialogFragment.java @@ -121,7 +121,7 @@ public class VarEditDialogFragment extends DialogFragment implements CalculatorE // EDIT MODE getDialog().setTitle(R.string.c_var_edit_var); - root.findViewById(R.id.remove_button).setOnClickListener(new MathEntityRemover(constant, null, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), getActivity(), this)); + root.findViewById(R.id.remove_button).setOnClickListener(MathEntityRemover.newConstantRemover(constant, null, getActivity(), VarEditDialogFragment.this)); } }