From 9c71e329cd63e3dd9fb7f00f24f1c1d94961f778 Mon Sep 17 00:00:00 2001 From: serso Date: Fri, 29 Jan 2016 21:17:50 +0100 Subject: [PATCH] FunctionRemovalDialog --- .../android/calculator/CalculatorImpl.java | 29 ++++---- .../function/EditFunctionFragment.java | 70 ++++--------------- .../function/FunctionRemovalDialog.java | 45 ++++++++++++ .../math/edit/FunctionsFragment.java | 22 +++--- .../math/edit/OperatorsFragment.java | 6 -- .../math/edit/VariablesFragment.java | 34 ++------- app/src/main/res/xml/ga.xml | 8 +-- 7 files changed, 93 insertions(+), 121 deletions(-) create mode 100644 app/src/main/java/org/solovyev/android/calculator/function/FunctionRemovalDialog.java diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java index 78016c2b..b3db4539 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java @@ -25,10 +25,16 @@ package org.solovyev.android.calculator; import android.support.annotation.NonNull; import android.text.TextUtils; import android.util.Log; - import com.squareup.otto.Bus; import com.squareup.otto.Subscribe; - +import jscl.AbstractJsclArithmeticException; +import jscl.MathEngine; +import jscl.NumeralBase; +import jscl.NumeralBaseException; +import jscl.math.Generic; +import jscl.math.function.IConstant; +import jscl.math.operator.Operator; +import jscl.text.ParseInterruptedException; import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.units.CalculatorNumeralBase; import org.solovyev.android.calculator.variables.CppVariable; @@ -40,24 +46,14 @@ import org.solovyev.common.text.Strings; import org.solovyev.common.units.ConversionException; import org.solovyev.common.units.Conversions; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicLong; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import jscl.AbstractJsclArithmeticException; -import jscl.MathEngine; -import jscl.NumeralBase; -import jscl.NumeralBaseException; -import jscl.math.Generic; -import jscl.math.function.IConstant; -import jscl.math.operator.Operator; -import jscl.text.ParseInterruptedException; - public class CalculatorImpl implements Calculator, CalculatorEventListener { // one minute @@ -500,6 +496,10 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { public void onFunctionsChanged(@Nonnull FunctionsRegistry.ChangedEvent event) { evaluate(); } + @Subscribe + public void onFunctionsRemoved(@Nonnull FunctionsRegistry.RemovedEvent event) { + evaluate(); + } @Override public void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, @Nonnull CalculatorEventType calculatorEventType, @Nullable Object data) { @@ -514,7 +514,6 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { case constant_added: case constant_removed: - case function_removed: evaluate(); break; case use_constant: diff --git a/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java b/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java index 39870fd6..892477c4 100644 --- a/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java @@ -36,49 +36,24 @@ import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.text.Editable; import android.text.TextUtils; -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.*; 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.Check; -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.CalculatorEventType; -import org.solovyev.android.calculator.FunctionsRegistry; -import org.solovyev.android.calculator.KeyboardUi; -import org.solovyev.android.calculator.KeyboardWindow; -import org.solovyev.android.calculator.Locator; -import org.solovyev.android.calculator.ParseException; -import org.solovyev.android.calculator.R; -import org.solovyev.android.calculator.VariablesRegistry; +import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.math.edit.FunctionsActivity; import org.solovyev.android.calculator.math.edit.VarEditorSaver; import org.solovyev.android.calculator.view.EditTextCompat; import org.solovyev.common.math.MathRegistry; -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 butterknife.Bind; -import butterknife.ButterKnife; -import jscl.math.function.Function; +import java.util.*; import static org.solovyev.android.calculator.function.CppFunction.NO_ID; @@ -201,36 +176,21 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC neutral.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - tryRemoveFunction(function, false); + showRemovalDialog(function); } }); } } - private void tryRemoveFunction(@NonNull CppFunction function, boolean confirmed) { - if (!confirmed) { - showRemovalDialog(function); - return; - } - final Function entity = function.toJsclBuilder().create(); - functionsRegistry.remove(entity); - calculator.fireCalculatorEvent(CalculatorEventType.function_removed, entity, this); - dismiss(); - } - private void showRemovalDialog(@NonNull final CppFunction function) { - new AlertDialog.Builder(getActivity(), App.getTheme().alertDialogTheme) - .setCancelable(true) - .setTitle(R.string.removal_confirmation) - .setMessage(getString(R.string.function_removal_confirmation_question, function.getName())) - .setNegativeButton(R.string.c_no, null) - .setPositiveButton(R.string.c_yes, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - tryRemoveFunction(function, true); - } - }) - .create().show(); + FunctionRemovalDialog.show(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 diff --git a/app/src/main/java/org/solovyev/android/calculator/function/FunctionRemovalDialog.java b/app/src/main/java/org/solovyev/android/calculator/function/FunctionRemovalDialog.java new file mode 100644 index 00000000..00a805fb --- /dev/null +++ b/app/src/main/java/org/solovyev/android/calculator/function/FunctionRemovalDialog.java @@ -0,0 +1,45 @@ +package org.solovyev.android.calculator.function; + +import android.app.Activity; +import android.content.DialogInterface; +import android.support.v7.app.AlertDialog; +import org.solovyev.android.calculator.App; +import org.solovyev.android.calculator.R; + +import javax.annotation.Nonnull; + +public class FunctionRemovalDialog { + + @Nonnull + private final Activity activity; + @Nonnull + private final String functionName; + @Nonnull + private final DialogInterface.OnClickListener listener; + + private FunctionRemovalDialog(@Nonnull Activity activity, @Nonnull String functionName, @Nonnull DialogInterface.OnClickListener listener) { + this.activity = activity; + this.functionName = functionName; + this.listener = listener; + } + + public static void show(@Nonnull Activity activity, @Nonnull String functionName, @Nonnull DialogInterface.OnClickListener listener) { + new FunctionRemovalDialog(activity, functionName, listener).show(); + } + + public void show() { + new AlertDialog.Builder(activity, App.getTheme().alertDialogTheme) + .setCancelable(true) + .setTitle(R.string.removal_confirmation) + .setMessage(activity.getString(R.string.function_removal_confirmation_question, functionName)) + .setNegativeButton(R.string.c_no, null) + .setPositiveButton(R.string.c_yes, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + listener.onClick(dialog, DialogInterface.BUTTON_POSITIVE); + dialog.dismiss(); + } + }) + .create().show(); + } +} diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsFragment.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsFragment.java index 81491619..1f209d1f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsFragment.java @@ -22,6 +22,7 @@ package org.solovyev.android.calculator.math.edit; +import android.content.DialogInterface; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.FragmentActivity; @@ -30,11 +31,12 @@ import com.squareup.otto.Bus; import com.squareup.otto.Subscribe; import jscl.math.function.Function; import jscl.math.function.IFunction; +import org.solovyev.android.Check; import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.entities.Category; import org.solovyev.android.calculator.function.CppFunction; import org.solovyev.android.calculator.function.EditFunctionFragment; -import org.solovyev.common.text.Strings; +import org.solovyev.android.calculator.function.FunctionRemovalDialog; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -112,11 +114,6 @@ public class FunctionsFragment extends BaseEntitiesFragment { @Override protected void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull Function function, @NonNull MenuItem.OnMenuItemClickListener listener) { addMenu(menu, R.string.c_use, listener); - final EntitiesRegistry functionsRegistry = registry; - if (!Strings.isEmpty(functionsRegistry.getDescription(function.getName()))) { - addMenu(menu, R.string.c_copy_description, listener); - } - if (!function.isSystem()) { addMenu(menu, R.string.c_edit, listener); addMenu(menu, R.string.c_remove, listener); @@ -124,7 +121,7 @@ public class FunctionsFragment extends BaseEntitiesFragment { } @Override - protected boolean onMenuItemClicked(@Nonnull MenuItem item, @Nonnull Function function) { + protected boolean onMenuItemClicked(@Nonnull MenuItem item, @Nonnull final Function function) { final FragmentActivity activity = getActivity(); switch (item.getItemId()) { case R.string.c_use: @@ -136,10 +133,13 @@ public class FunctionsFragment extends BaseEntitiesFragment { } return true; case R.string.c_remove: - // todo serso: - return true; - case R.string.c_copy_description: - copyDescription(function); + FunctionRemovalDialog.show(getActivity(), function.getName(), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Check.isTrue(which == DialogInterface.BUTTON_POSITIVE); + registry.remove(function); + } + }); return true; } return false; diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsFragment.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsFragment.java index 0c51c80c..f4eda90c 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsFragment.java @@ -74,9 +74,6 @@ public class OperatorsFragment extends BaseEntitiesFragment { @Override protected void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull Operator operator, @Nonnull MenuItem.OnMenuItemClickListener listener) { addMenu(menu, R.string.c_use, listener); - if (!Strings.isEmpty(getDescription(operator))) { - addMenu(menu, R.string.c_copy_description, listener); - } } @Override @@ -85,9 +82,6 @@ public class OperatorsFragment extends BaseEntitiesFragment { case R.string.c_use: Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_operator, operator); return true; - case R.string.c_copy_description: - copyDescription(operator); - return true; } return false; diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/VariablesFragment.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/VariablesFragment.java index a335dd55..77736301 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/VariablesFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/VariablesFragment.java @@ -28,36 +28,20 @@ import android.support.v4.app.FragmentActivity; import android.view.ContextMenu; import android.view.MenuItem; import android.view.View; - import com.squareup.otto.Bus; - -import org.solovyev.android.calculator.AppComponent; -import org.solovyev.android.calculator.Calculator; -import org.solovyev.android.calculator.CalculatorEventData; -import org.solovyev.android.calculator.CalculatorEventListener; -import org.solovyev.android.calculator.CalculatorEventType; -import org.solovyev.android.calculator.CalculatorFragmentType; +import jscl.math.function.IConstant; +import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.entities.Category; -import org.solovyev.android.calculator.Change; -import org.solovyev.android.calculator.Keyboard; -import org.solovyev.android.calculator.Locator; -import org.solovyev.android.calculator.PreparedExpression; -import org.solovyev.android.calculator.R; -import org.solovyev.android.calculator.ToJsclTextProcessor; -import org.solovyev.android.calculator.VariablesRegistry; import org.solovyev.android.calculator.math.MathType; import org.solovyev.common.JPredicate; import org.solovyev.common.collections.Collections; import org.solovyev.common.text.Strings; -import java.util.ArrayList; -import java.util.List; - import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.inject.Inject; - -import jscl.math.function.IConstant; +import java.util.ArrayList; +import java.util.List; public class VariablesFragment extends BaseEntitiesFragment implements CalculatorEventListener { @@ -181,10 +165,6 @@ public class VariablesFragment extends BaseEntitiesFragment implement if (!Strings.isEmpty(constant.getValue())) { addMenu(menu, R.string.c_copy_value, listener); } - - if (!Strings.isEmpty(registry.getDescription(constant.getName()))) { - addMenu(menu, R.string.c_copy_description, listener); - } } @Override @@ -206,12 +186,6 @@ public class VariablesFragment extends BaseEntitiesFragment implement Locator.getInstance().getClipboard().setText(value); } return true; - case R.string.c_copy_description: - final String description = registry.getDescription(constant.getName()); - if (!Strings.isEmpty(description)) { - Locator.getInstance().getClipboard().setText(description); - } - return true; } return false; } diff --git a/app/src/main/res/xml/ga.xml b/app/src/main/res/xml/ga.xml index 9e2323a3..73137bb3 100644 --- a/app/src/main/res/xml/ga.xml +++ b/app/src/main/res/xml/ga.xml @@ -1,8 +1,8 @@ - UA-28724009-2 - true - true - 10.0 + UA-28724009-2 + true + true + 10.0 \ No newline at end of file