FunctionRemovalDialog

This commit is contained in:
serso 2016-01-29 21:17:50 +01:00
parent 92bbfa1f73
commit 9c71e329cd
7 changed files with 93 additions and 121 deletions

View File

@ -25,10 +25,16 @@ package org.solovyev.android.calculator;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe; 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.jscl.JsclOperation;
import org.solovyev.android.calculator.units.CalculatorNumeralBase; import org.solovyev.android.calculator.units.CalculatorNumeralBase;
import org.solovyev.android.calculator.variables.CppVariable; 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.ConversionException;
import org.solovyev.common.units.Conversions; import org.solovyev.common.units.Conversions;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong; 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 { public class CalculatorImpl implements Calculator, CalculatorEventListener {
// one minute // one minute
@ -500,6 +496,10 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
public void onFunctionsChanged(@Nonnull FunctionsRegistry.ChangedEvent event) { public void onFunctionsChanged(@Nonnull FunctionsRegistry.ChangedEvent event) {
evaluate(); evaluate();
} }
@Subscribe
public void onFunctionsRemoved(@Nonnull FunctionsRegistry.RemovedEvent event) {
evaluate();
}
@Override @Override
public void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, @Nonnull CalculatorEventType calculatorEventType, @Nullable Object data) { 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_added:
case constant_removed: case constant_removed:
case function_removed:
evaluate(); evaluate();
break; break;
case use_constant: case use_constant:

View File

@ -36,49 +36,24 @@ import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.text.Editable; import android.text.Editable;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.ContextMenu; import android.view.*;
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.Button;
import android.widget.EditText; import android.widget.EditText;
import butterknife.Bind;
import butterknife.ButterKnife;
import jscl.math.function.Function;
import org.solovyev.android.Check; import org.solovyev.android.Check;
import org.solovyev.android.calculator.App; import org.solovyev.android.calculator.*;
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.math.edit.FunctionsActivity; import org.solovyev.android.calculator.math.edit.FunctionsActivity;
import org.solovyev.android.calculator.math.edit.VarEditorSaver; import org.solovyev.android.calculator.math.edit.VarEditorSaver;
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 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 butterknife.Bind;
import butterknife.ButterKnife;
import jscl.math.function.Function;
import static org.solovyev.android.calculator.function.CppFunction.NO_ID; 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() { neutral.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { 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) { private void showRemovalDialog(@NonNull final CppFunction function) {
new AlertDialog.Builder(getActivity(), App.getTheme().alertDialogTheme) FunctionRemovalDialog.show(getActivity(), function.name, new DialogInterface.OnClickListener() {
.setCancelable(true) @Override
.setTitle(R.string.removal_confirmation) public void onClick(DialogInterface dialog, int which) {
.setMessage(getString(R.string.function_removal_confirmation_question, function.getName())) Check.isTrue(which == DialogInterface.BUTTON_POSITIVE);
.setNegativeButton(R.string.c_no, null) functionsRegistry.remove(function.toJsclBuilder().create());
.setPositiveButton(R.string.c_yes, new DialogInterface.OnClickListener() { dismiss();
@Override }
public void onClick(DialogInterface dialog, int which) { });
tryRemoveFunction(function, true);
}
})
.create().show();
} }
@Override @Override

View File

@ -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();
}
}

View File

@ -22,6 +22,7 @@
package org.solovyev.android.calculator.math.edit; package org.solovyev.android.calculator.math.edit;
import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentActivity;
@ -30,11 +31,12 @@ import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe; import com.squareup.otto.Subscribe;
import jscl.math.function.Function; import jscl.math.function.Function;
import jscl.math.function.IFunction; import jscl.math.function.IFunction;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.entities.Category; import org.solovyev.android.calculator.entities.Category;
import org.solovyev.android.calculator.function.CppFunction; import org.solovyev.android.calculator.function.CppFunction;
import org.solovyev.android.calculator.function.EditFunctionFragment; 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.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -112,11 +114,6 @@ public class FunctionsFragment extends BaseEntitiesFragment<Function> {
@Override @Override
protected void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull Function function, @NonNull MenuItem.OnMenuItemClickListener listener) { protected void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull Function function, @NonNull MenuItem.OnMenuItemClickListener listener) {
addMenu(menu, R.string.c_use, listener); addMenu(menu, R.string.c_use, listener);
final EntitiesRegistry<Function> functionsRegistry = registry;
if (!Strings.isEmpty(functionsRegistry.getDescription(function.getName()))) {
addMenu(menu, R.string.c_copy_description, listener);
}
if (!function.isSystem()) { if (!function.isSystem()) {
addMenu(menu, R.string.c_edit, listener); addMenu(menu, R.string.c_edit, listener);
addMenu(menu, R.string.c_remove, listener); addMenu(menu, R.string.c_remove, listener);
@ -124,7 +121,7 @@ public class FunctionsFragment extends BaseEntitiesFragment<Function> {
} }
@Override @Override
protected boolean onMenuItemClicked(@Nonnull MenuItem item, @Nonnull Function function) { protected boolean onMenuItemClicked(@Nonnull MenuItem item, @Nonnull final Function function) {
final FragmentActivity activity = getActivity(); final FragmentActivity activity = getActivity();
switch (item.getItemId()) { switch (item.getItemId()) {
case R.string.c_use: case R.string.c_use:
@ -136,10 +133,13 @@ public class FunctionsFragment extends BaseEntitiesFragment<Function> {
} }
return true; return true;
case R.string.c_remove: case R.string.c_remove:
// todo serso: FunctionRemovalDialog.show(getActivity(), function.getName(), new DialogInterface.OnClickListener() {
return true; @Override
case R.string.c_copy_description: public void onClick(DialogInterface dialog, int which) {
copyDescription(function); Check.isTrue(which == DialogInterface.BUTTON_POSITIVE);
registry.remove(function);
}
});
return true; return true;
} }
return false; return false;

View File

@ -74,9 +74,6 @@ public class OperatorsFragment extends BaseEntitiesFragment<Operator> {
@Override @Override
protected void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull Operator operator, @Nonnull MenuItem.OnMenuItemClickListener listener) { protected void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull Operator operator, @Nonnull MenuItem.OnMenuItemClickListener listener) {
addMenu(menu, R.string.c_use, listener); addMenu(menu, R.string.c_use, listener);
if (!Strings.isEmpty(getDescription(operator))) {
addMenu(menu, R.string.c_copy_description, listener);
}
} }
@Override @Override
@ -85,9 +82,6 @@ public class OperatorsFragment extends BaseEntitiesFragment<Operator> {
case R.string.c_use: case R.string.c_use:
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_operator, operator); Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_operator, operator);
return true; return true;
case R.string.c_copy_description:
copyDescription(operator);
return true;
} }
return false; return false;

View File

@ -28,36 +28,20 @@ import android.support.v4.app.FragmentActivity;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
import jscl.math.function.IConstant;
import org.solovyev.android.calculator.AppComponent; import org.solovyev.android.calculator.*;
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 org.solovyev.android.calculator.entities.Category; 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.android.calculator.math.MathType;
import org.solovyev.common.JPredicate; import org.solovyev.common.JPredicate;
import org.solovyev.common.collections.Collections; import org.solovyev.common.collections.Collections;
import org.solovyev.common.text.Strings; import org.solovyev.common.text.Strings;
import java.util.ArrayList;
import java.util.List;
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.ArrayList;
import jscl.math.function.IConstant; import java.util.List;
public class VariablesFragment extends BaseEntitiesFragment<IConstant> implements CalculatorEventListener { public class VariablesFragment extends BaseEntitiesFragment<IConstant> implements CalculatorEventListener {
@ -181,10 +165,6 @@ public class VariablesFragment extends BaseEntitiesFragment<IConstant> implement
if (!Strings.isEmpty(constant.getValue())) { if (!Strings.isEmpty(constant.getValue())) {
addMenu(menu, R.string.c_copy_value, listener); addMenu(menu, R.string.c_copy_value, listener);
} }
if (!Strings.isEmpty(registry.getDescription(constant.getName()))) {
addMenu(menu, R.string.c_copy_description, listener);
}
} }
@Override @Override
@ -206,12 +186,6 @@ public class VariablesFragment extends BaseEntitiesFragment<IConstant> implement
Locator.getInstance().getClipboard().setText(value); Locator.getInstance().getClipboard().setText(value);
} }
return true; 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; return false;
} }

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" <resources xmlns:tools="http://schemas.android.com/tools"
tools:ignore="UnusedResources"> tools:ignore="UnusedResources">
<string name="ga_trackingId">UA-28724009-2</string> <string function="ga_trackingId">UA-28724009-2</string>
<bool name="ga_reportUncaughtExceptions">true</bool> <bool function="ga_reportUncaughtExceptions">true</bool>
<bool name="ga_autoActivityTracking">true</bool> <bool function="ga_autoActivityTracking">true</bool>
<string name="ga_sampleFrequency">10.0</string> <string function="ga_sampleFrequency">10.0</string>
</resources> </resources>