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.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:

View File

@ -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

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;
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<Function> {
@Override
protected void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull Function function, @NonNull MenuItem.OnMenuItemClickListener 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()) {
addMenu(menu, R.string.c_edit, listener);
addMenu(menu, R.string.c_remove, listener);
@ -124,7 +121,7 @@ public class FunctionsFragment extends BaseEntitiesFragment<Function> {
}
@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<Function> {
}
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;

View File

@ -74,9 +74,6 @@ public class OperatorsFragment extends BaseEntitiesFragment<Operator> {
@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<Operator> {
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;

View File

@ -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<IConstant> implements CalculatorEventListener {
@ -181,10 +165,6 @@ public class VariablesFragment extends BaseEntitiesFragment<IConstant> 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<IConstant> 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;
}

View File

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