From 6b4d1118e0ab19b244eca05afa0a9462c8ac9b59 Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 28 Feb 2016 13:18:32 +0100 Subject: [PATCH] Show function editor --- .../android/calculator/ActivityLauncher.java | 115 ++++-------------- .../android/calculator/Calculator.java | 44 +++---- .../calculator/CalculatorEventType.java | 11 +- .../android/calculator/CalculatorLocator.java | 3 - .../solovyev/android/calculator/Locator.java | 9 -- .../calculator/keyboard/KeyboardUi.java | 4 +- 6 files changed, 43 insertions(+), 143 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/ActivityLauncher.java b/app/src/main/java/org/solovyev/android/calculator/ActivityLauncher.java index 23e87af2..ae47475d 100644 --- a/app/src/main/java/org/solovyev/android/calculator/ActivityLauncher.java +++ b/app/src/main/java/org/solovyev/android/calculator/ActivityLauncher.java @@ -40,7 +40,6 @@ import org.solovyev.android.calculator.functions.CppFunction; import org.solovyev.android.calculator.functions.EditFunctionFragment; import org.solovyev.android.calculator.functions.FunctionsActivity; 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.plot.ExpressionFunction; 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.EditVariableFragment; import org.solovyev.android.calculator.variables.VariablesActivity; -import org.solovyev.android.calculator.variables.VariablesFragment; import org.solovyev.android.plotter.Plotter; import org.solovyev.common.msg.MessageType; -import org.solovyev.common.text.Strings; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -59,10 +56,9 @@ import javax.inject.Inject; import javax.inject.Singleton; import java.util.ArrayList; import java.util.List; -import java.util.Set; @Singleton -public final class ActivityLauncher implements CalculatorEventListener { +public final class ActivityLauncher { @Inject Application application; @@ -93,49 +89,6 @@ public final class ActivityLauncher implements CalculatorEventListener { 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 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 private static Notifier getNotifier() { return ((CalculatorApplication) App.getApplication()).notifier; @@ -203,47 +156,6 @@ public final class ActivityLauncher implements CalculatorEventListener { 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() { show(getContext(), FunctionsActivity.getClass(getContext())); } @@ -298,4 +210,29 @@ public final class ActivityLauncher implements CalculatorEventListener { } 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()); + } } diff --git a/app/src/main/java/org/solovyev/android/calculator/Calculator.java b/app/src/main/java/org/solovyev/android/calculator/Calculator.java index b2a1a0e3..74cb06eb 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Calculator.java +++ b/app/src/main/java/org/solovyev/android/calculator/Calculator.java @@ -26,16 +26,18 @@ import android.content.SharedPreferences; 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.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.calculator.calculations.CalculationCancelledEvent; -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.calculations.*; import org.solovyev.android.calculator.functions.FunctionsRegistry; import org.solovyev.android.calculator.jscl.JsclOperation; 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.units.ConversionException; -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 javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; import java.math.BigInteger; import java.util.ArrayList; import java.util.Collections; @@ -62,12 +60,6 @@ import java.util.List; import java.util.concurrent.Executor; 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 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) { ui.execute(new Runnable() { @Override diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorEventType.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorEventType.java index d792c524..e052d8ad 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorEventType.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorEventType.java @@ -24,15 +24,6 @@ package org.solovyev.android.calculator; 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; + ; } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java index eb96f33d..4e0c196f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java @@ -36,7 +36,4 @@ public interface CalculatorLocator { @Nonnull Engine getEngine(); - @Nonnull - Keyboard getKeyboard(); - } diff --git a/app/src/main/java/org/solovyev/android/calculator/Locator.java b/app/src/main/java/org/solovyev/android/calculator/Locator.java index e8213d21..6ac87982 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Locator.java +++ b/app/src/main/java/org/solovyev/android/calculator/Locator.java @@ -32,8 +32,6 @@ public class Locator implements CalculatorLocator { private Engine engine; @Nonnull private Calculator calculator; - @Nonnull - private Keyboard keyboard; public Locator() { } @@ -50,7 +48,6 @@ public class Locator implements CalculatorLocator { this.calculator = calculator; this.engine = engine; - this.keyboard = keyboard; } @Nonnull @@ -65,10 +62,4 @@ public class Locator implements CalculatorLocator { return this.calculator; } - @Override - @Nonnull - public Keyboard getKeyboard() { - return keyboard; - } - } diff --git a/app/src/main/java/org/solovyev/android/calculator/keyboard/KeyboardUi.java b/app/src/main/java/org/solovyev/android/calculator/keyboard/KeyboardUi.java index ee4a2fe3..04dd2b05 100644 --- a/app/src/main/java/org/solovyev/android/calculator/keyboard/KeyboardUi.java +++ b/app/src/main/java/org/solovyev/android/calculator/keyboard/KeyboardUi.java @@ -233,11 +233,11 @@ public class KeyboardUi extends BaseKeyboardUi { public boolean processDragEvent(@Nonnull DragDirection direction, @Nonnull DragButton button, @Nonnull PointF point, @Nonnull MotionEvent event) { switch (button.getId()) { case R.id.cpp_button_vars: - calculator.fireCalculatorEvent(CalculatorEventType.show_create_var_dialog, null); + launcher.showConstantEditor(); return true; case R.id.cpp_button_functions: if (direction == up) { - calculator.fireCalculatorEvent(CalculatorEventType.show_create_function_dialog, null); + launcher.showFunctionEditor(); return true; } return false;