Show function editor

This commit is contained in:
serso 2016-02-28 13:18:32 +01:00
parent f7b66673a5
commit 6b4d1118e0
6 changed files with 43 additions and 143 deletions

View File

@ -40,7 +40,6 @@ import org.solovyev.android.calculator.functions.CppFunction;
import org.solovyev.android.calculator.functions.EditFunctionFragment; import org.solovyev.android.calculator.functions.EditFunctionFragment;
import org.solovyev.android.calculator.functions.FunctionsActivity; import org.solovyev.android.calculator.functions.FunctionsActivity;
import org.solovyev.android.calculator.history.HistoryActivity; import org.solovyev.android.calculator.history.HistoryActivity;
import org.solovyev.android.calculator.matrix.CalculatorMatrixActivity;
import org.solovyev.android.calculator.operators.OperatorsActivity; import org.solovyev.android.calculator.operators.OperatorsActivity;
import org.solovyev.android.calculator.plot.ExpressionFunction; import org.solovyev.android.calculator.plot.ExpressionFunction;
import org.solovyev.android.calculator.plot.PlotActivity; import org.solovyev.android.calculator.plot.PlotActivity;
@ -48,10 +47,8 @@ import org.solovyev.android.calculator.preferences.PreferencesActivity;
import org.solovyev.android.calculator.variables.CppVariable; import org.solovyev.android.calculator.variables.CppVariable;
import org.solovyev.android.calculator.variables.EditVariableFragment; import org.solovyev.android.calculator.variables.EditVariableFragment;
import org.solovyev.android.calculator.variables.VariablesActivity; import org.solovyev.android.calculator.variables.VariablesActivity;
import org.solovyev.android.calculator.variables.VariablesFragment;
import org.solovyev.android.plotter.Plotter; import org.solovyev.android.plotter.Plotter;
import org.solovyev.common.msg.MessageType; import org.solovyev.common.msg.MessageType;
import org.solovyev.common.text.Strings;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -59,10 +56,9 @@ import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
@Singleton @Singleton
public final class ActivityLauncher implements CalculatorEventListener { public final class ActivityLauncher {
@Inject @Inject
Application application; Application application;
@ -93,49 +89,6 @@ public final class ActivityLauncher implements CalculatorEventListener {
context.startActivity(intent); context.startActivity(intent);
} }
public static void tryCreateVar(@Nonnull final Context context) {
final Display display = App.getDisplay();
final DisplayState state = display.getState();
if (state.valid) {
final String value = state.text;
if (!Strings.isEmpty(value)) {
if (VariablesFragment.isValidValue(value)) {
EditVariableFragment.showDialog(CppVariable.builder("").withValue(value).build(), context);
} else {
getNotifier().showMessage(R.string.c_value_is_not_a_number, MessageType.error);
}
} else {
getNotifier().showMessage(R.string.empty_var_error, MessageType.error);
}
} else {
getNotifier().showMessage(R.string.not_valid_result, MessageType.error);
}
}
public static void tryCreateFunction(@Nonnull final Context context) {
final Display display = App.getDisplay();
final DisplayState viewState = display.getState();
if (viewState.valid) {
final String functionBody = viewState.text;
if (!Strings.isEmpty(functionBody)) {
final CppFunction.Builder builder = CppFunction.builder("", functionBody);
final Generic generic = viewState.getResult();
if (generic != null) {
final Set<Constant> constants = generic.getUndefinedConstants(null);
for (Constant constant : constants) {
builder.withParameter(constant.getName());
}
}
EditFunctionFragment.show(builder.build(), context);
} else {
getNotifier().showMessage(R.string.empty_function_error, MessageType.error);
}
} else {
getNotifier().showMessage(R.string.not_valid_result, MessageType.error);
}
}
@Nonnull @Nonnull
private static Notifier getNotifier() { private static Notifier getNotifier() {
return ((CalculatorApplication) App.getApplication()).notifier; return ((CalculatorApplication) App.getApplication()).notifier;
@ -203,47 +156,6 @@ public final class ActivityLauncher implements CalculatorEventListener {
return activity == null ? application : activity; return activity == null ? application : activity;
} }
@Override
public void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, @Nonnull CalculatorEventType calculatorEventType, @Nullable Object data) {
final Context context;
final Object source = calculatorEventData.getSource();
if (source instanceof Context) {
context = ((Context) source);
} else {
context = App.getApplication();
}
switch (calculatorEventType) {
case show_create_matrix_dialog:
App.getUiThreadExecutor().execute(new Runnable() {
@Override
public void run() {
final Intent intent = new Intent(context, CalculatorMatrixActivity.class);
Activities.addIntentFlags(intent, false, context);
context.startActivity(intent);
}
});
break;
case show_create_var_dialog:
App.getUiThreadExecutor().execute(new Runnable() {
@Override
public void run() {
ActivityLauncher.tryCreateVar(context);
}
});
break;
case show_create_function_dialog:
App.getUiThreadExecutor().execute(new Runnable() {
@Override
public void run() {
ActivityLauncher.tryCreateFunction(context);
}
});
break;
}
}
public void showFunctions() { public void showFunctions() {
show(getContext(), FunctionsActivity.getClass(getContext())); show(getContext(), FunctionsActivity.getClass(getContext()));
} }
@ -298,4 +210,29 @@ public final class ActivityLauncher implements CalculatorEventListener {
} }
return true; return true;
} }
public void showConstantEditor() {
final DisplayState state = display.get().getState();
if (!state.valid) {
notifier.showMessage(R.string.not_valid_result);
return;
}
EditVariableFragment.showDialog(CppVariable.builder("").withValue(state.text).build(), getContext());
}
public void showFunctionEditor() {
final DisplayState state = display.get().getState();
if (!state.valid) {
notifier.showMessage(R.string.not_valid_result);
return;
}
final CppFunction.Builder builder = CppFunction.builder("", state.text);
final Generic expression = state.getResult();
if (expression != null) {
for (Constant constant : expression.getUndefinedConstants(variablesRegistry.get())) {
builder.withParameter(constant.getName());
}
}
EditFunctionFragment.show(builder.build(), getContext());
}
} }

View File

@ -26,16 +26,18 @@ import android.content.SharedPreferences;
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.JsclArithmeticException;
import jscl.JsclMathEngine;
import jscl.NumeralBase;
import jscl.NumeralBaseException;
import jscl.math.Generic;
import jscl.math.function.Constants;
import jscl.math.function.IConstant;
import jscl.text.ParseInterruptedException;
import org.solovyev.android.Check; import org.solovyev.android.Check;
import org.solovyev.android.calculator.calculations.CalculationCancelledEvent; import org.solovyev.android.calculator.calculations.*;
import org.solovyev.android.calculator.calculations.CalculationFailedEvent;
import org.solovyev.android.calculator.calculations.CalculationFinishedEvent;
import org.solovyev.android.calculator.calculations.ConversionFailedEvent;
import org.solovyev.android.calculator.calculations.ConversionFinishedEvent;
import org.solovyev.android.calculator.functions.FunctionsRegistry; import org.solovyev.android.calculator.functions.FunctionsRegistry;
import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.android.calculator.variables.CppVariable; import org.solovyev.android.calculator.variables.CppVariable;
@ -46,15 +48,11 @@ import org.solovyev.common.msg.MessageType;
import org.solovyev.common.text.Strings; import org.solovyev.common.text.Strings;
import org.solovyev.common.units.ConversionException; import org.solovyev.common.units.ConversionException;
import jscl.JsclArithmeticException; import javax.annotation.Nonnull;
import jscl.JsclMathEngine; import javax.annotation.Nullable;
import jscl.NumeralBase; import javax.inject.Inject;
import jscl.NumeralBaseException; import javax.inject.Named;
import jscl.math.Generic; import javax.inject.Singleton;
import jscl.math.function.Constants;
import jscl.math.function.IConstant;
import jscl.text.ParseInterruptedException;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -62,12 +60,6 @@ import java.util.List;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
@Singleton @Singleton
public class Calculator implements SharedPreferences.OnSharedPreferenceChangeListener { public class Calculator implements SharedPreferences.OnSharedPreferenceChangeListener {
@ -290,14 +282,6 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis
} }
} }
public void addCalculatorEventListener(@Nonnull CalculatorEventListener calculatorEventListener) {
calculatorEventContainer.addCalculatorEventListener(calculatorEventListener);
}
public void removeCalculatorEventListener(@Nonnull CalculatorEventListener calculatorEventListener) {
calculatorEventContainer.removeCalculatorEventListener(calculatorEventListener);
}
public void fireCalculatorEvent(@Nonnull final CalculatorEventData calculatorEventData, @Nonnull final CalculatorEventType calculatorEventType, @Nullable final Object data) { public void fireCalculatorEvent(@Nonnull final CalculatorEventData calculatorEventData, @Nonnull final CalculatorEventType calculatorEventType, @Nullable final Object data) {
ui.execute(new Runnable() { ui.execute(new Runnable() {
@Override @Override

View File

@ -24,15 +24,6 @@ package org.solovyev.android.calculator;
public enum CalculatorEventType { public enum CalculatorEventType {
show_create_var_dialog, ;
show_create_matrix_dialog,
show_create_function_dialog,
plot_graph,
/**
* {@link org.solovyev.android.calculator.plot.PlotData}
*/
plot_data_changed;
} }

View File

@ -36,7 +36,4 @@ public interface CalculatorLocator {
@Nonnull @Nonnull
Engine getEngine(); Engine getEngine();
@Nonnull
Keyboard getKeyboard();
} }

View File

@ -32,8 +32,6 @@ public class Locator implements CalculatorLocator {
private Engine engine; private Engine engine;
@Nonnull @Nonnull
private Calculator calculator; private Calculator calculator;
@Nonnull
private Keyboard keyboard;
public Locator() { public Locator() {
} }
@ -50,7 +48,6 @@ public class Locator implements CalculatorLocator {
this.calculator = calculator; this.calculator = calculator;
this.engine = engine; this.engine = engine;
this.keyboard = keyboard;
} }
@Nonnull @Nonnull
@ -65,10 +62,4 @@ public class Locator implements CalculatorLocator {
return this.calculator; return this.calculator;
} }
@Override
@Nonnull
public Keyboard getKeyboard() {
return keyboard;
}
} }

View File

@ -233,11 +233,11 @@ public class KeyboardUi extends BaseKeyboardUi {
public boolean processDragEvent(@Nonnull DragDirection direction, @Nonnull DragButton button, @Nonnull PointF point, @Nonnull MotionEvent event) { public boolean processDragEvent(@Nonnull DragDirection direction, @Nonnull DragButton button, @Nonnull PointF point, @Nonnull MotionEvent event) {
switch (button.getId()) { switch (button.getId()) {
case R.id.cpp_button_vars: case R.id.cpp_button_vars:
calculator.fireCalculatorEvent(CalculatorEventType.show_create_var_dialog, null); launcher.showConstantEditor();
return true; return true;
case R.id.cpp_button_functions: case R.id.cpp_button_functions:
if (direction == up) { if (direction == up) {
calculator.fireCalculatorEvent(CalculatorEventType.show_create_function_dialog, null); launcher.showFunctionEditor();
return true; return true;
} }
return false; return false;