From b5600114ed35886a5f5a0b78a9e5f42bf2fd1b94 Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 17 Jan 2016 18:45:20 +0100 Subject: [PATCH] EntitiesFragment --- ...egistry.java => BaseEntitiesRegistry.java} | 24 +- .../android/calculator/BaseFragment.java | 70 ++++ .../CalculatorActivityLauncher.java | 6 +- .../android/calculator/CalculatorEngine.java | 8 +- .../calculator/CalculatorEngineImpl.java | 35 +- .../calculator/CalculatorFragmentType.java | 12 +- .../android/calculator/CalculatorImpl.java | 2 +- .../CalculatorOperatorsMathRegistry.java | 2 +- .../CalculatorPostfixFunctionsRegistry.java | 2 +- .../calculator/CalculatorVarsRegistry.java | 10 +- ...athRegistry.java => EntitiesRegistry.java} | 11 +- ...thRegistry.java => FunctionsRegistry.java} | 33 +- .../function/FunctionEditDialogFragment.java | 42 +-- .../function/FunctionEditorSaver.java | 10 +- .../history/BaseHistoryFragment.java | 88 ++--- .../edit/AbstractMathEntityListFragment.java | 342 ------------------ .../math/edit/BaseEntitiesFragment.java | 275 ++++++++++++++ .../edit/CalculatorFunctionsActivity.java | 24 +- .../edit/CalculatorOperatorsActivity.java | 2 +- .../edit/CalculatorOperatorsFragment.java | 158 -------- .../math/edit/CalculatorVarsActivity.java | 4 +- ...nsFragment.java => FunctionsFragment.java} | 189 ++++------ .../math/edit/MathEntityRemover.java | 6 +- .../math/edit/OperatorsFragment.java | 107 ++++++ .../calculator/math/edit/VarEditorSaver.java | 8 +- ...torVarsFragment.java => VarsFragment.java} | 196 ++++------ .../android/calculator/model/AFunction.java | 63 +--- .../model/AndroidCalculatorEngine.java | 10 +- ...ies_fragment.xml => fragment_entities.xml} | 7 +- ..._entity.xml => fragment_entities_item.xml} | 10 +- .../calculator/CalculatorTestUtils.java | 2 +- 31 files changed, 729 insertions(+), 1029 deletions(-) rename app/src/main/java/org/solovyev/android/calculator/{AbstractCalculatorMathRegistry.java => BaseEntitiesRegistry.java} (88%) create mode 100644 app/src/main/java/org/solovyev/android/calculator/BaseFragment.java rename app/src/main/java/org/solovyev/android/calculator/{CalculatorMathRegistry.java => EntitiesRegistry.java} (80%) rename app/src/main/java/org/solovyev/android/calculator/{CalculatorFunctionsMathRegistry.java => FunctionsRegistry.java} (88%) delete mode 100644 app/src/main/java/org/solovyev/android/calculator/math/edit/AbstractMathEntityListFragment.java create mode 100644 app/src/main/java/org/solovyev/android/calculator/math/edit/BaseEntitiesFragment.java delete mode 100644 app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsFragment.java rename app/src/main/java/org/solovyev/android/calculator/math/edit/{CalculatorFunctionsFragment.java => FunctionsFragment.java} (51%) create mode 100644 app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsFragment.java rename app/src/main/java/org/solovyev/android/calculator/math/edit/{CalculatorVarsFragment.java => VarsFragment.java} (58%) rename app/src/main/res/layout/{math_entities_fragment.xml => fragment_entities.xml} (87%) rename app/src/main/res/layout/{math_entity.xml => fragment_entities_item.xml} (83%) diff --git a/app/src/main/java/org/solovyev/android/calculator/AbstractCalculatorMathRegistry.java b/app/src/main/java/org/solovyev/android/calculator/BaseEntitiesRegistry.java similarity index 88% rename from app/src/main/java/org/solovyev/android/calculator/AbstractCalculatorMathRegistry.java rename to app/src/main/java/org/solovyev/android/calculator/BaseEntitiesRegistry.java index 6b68142e..8fc53e68 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AbstractCalculatorMathRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/BaseEntitiesRegistry.java @@ -26,19 +26,13 @@ import org.solovyev.common.JBuilder; import org.solovyev.common.math.MathEntity; import org.solovyev.common.math.MathRegistry; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.List; import java.util.Map; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * User: serso - * Date: 10/30/11 - * Time: 1:03 AM - */ -public abstract class AbstractCalculatorMathRegistry implements CalculatorMathRegistry { +public abstract class BaseEntitiesRegistry implements EntitiesRegistry { @Nonnull private final MathRegistry mathRegistry; @@ -49,9 +43,9 @@ public abstract class AbstractCalculatorMathRegistry mathEntityDao; - protected AbstractCalculatorMathRegistry(@Nonnull MathRegistry mathRegistry, - @Nonnull String prefix, - @Nonnull MathEntityDao

mathEntityDao) { + protected BaseEntitiesRegistry(@Nonnull MathRegistry mathRegistry, + @Nonnull String prefix, + @Nonnull MathEntityDao

mathEntityDao) { this.mathRegistry = mathRegistry; this.prefix = prefix; this.mathEntityDao = mathEntityDao; @@ -63,13 +57,13 @@ public abstract class AbstractCalculatorMathRegistry substitutes = getSubstitutes(); - final String substitute = substitutes.get(mathEntityName); + final String substitute = substitutes.get(name); if (substitute == null) { - stringName = prefix + mathEntityName; + stringName = prefix + name; } else { stringName = prefix + substitute; } diff --git a/app/src/main/java/org/solovyev/android/calculator/BaseFragment.java b/app/src/main/java/org/solovyev/android/calculator/BaseFragment.java new file mode 100644 index 00000000..0cc5e44f --- /dev/null +++ b/app/src/main/java/org/solovyev/android/calculator/BaseFragment.java @@ -0,0 +1,70 @@ +package org.solovyev.android.calculator; + +import android.os.Bundle; +import android.support.annotation.StringRes; +import android.support.v4.app.Fragment; +import android.view.*; + +import javax.annotation.Nonnull; + +import static android.view.Menu.NONE; + +public abstract class BaseFragment extends Fragment { + + @Nonnull + protected final FragmentUi ui; + + public BaseFragment(@Nonnull CalculatorFragmentType type) { + ui = new FragmentUi(type.getDefaultLayoutId(), type.getDefaultTitleResId(), false); + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + inject(((CalculatorApplication) getActivity().getApplication()).getComponent()); + ui.onCreate(this); + } + + protected void inject(@Nonnull AppComponent component) { + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return ui.onCreateView(this, inflater, container); + } + + @Override + public void onViewCreated(View root, Bundle savedInstanceState) { + super.onViewCreated(root, savedInstanceState); + ui.onViewCreated(this, root); + } + + @Override + public void onResume() { + super.onResume(); + ui.onResume(this); + } + + @Override + public void onPause() { + ui.onPause(this); + super.onPause(); + } + + @Override + public void onDestroyView() { + ui.onDestroyView(this); + super.onDestroyView(); + } + + @Override + public void onDestroy() { + ui.onDestroy(this); + super.onDestroy(); + } + + @Nonnull + protected static MenuItem addMenu(@Nonnull ContextMenu menu, @StringRes int label, @Nonnull MenuItem.OnMenuItemClickListener listener) { + return menu.add(NONE, label, NONE, label).setOnMenuItemClickListener(listener); + } +} diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java index 525d8e58..6d125725 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java @@ -41,7 +41,7 @@ import org.solovyev.android.calculator.history.CalculatorHistoryActivity; import org.solovyev.android.calculator.math.edit.CalculatorFunctionsActivity; import org.solovyev.android.calculator.math.edit.CalculatorOperatorsActivity; import org.solovyev.android.calculator.math.edit.CalculatorVarsActivity; -import org.solovyev.android.calculator.math.edit.CalculatorVarsFragment; +import org.solovyev.android.calculator.math.edit.VarsFragment; import org.solovyev.android.calculator.math.edit.VarEditDialogFragment; import org.solovyev.android.calculator.matrix.CalculatorMatrixActivity; import org.solovyev.android.calculator.plot.CalculatorPlotActivity; @@ -141,12 +141,12 @@ public final class CalculatorActivityLauncher implements CalculatorEventListener if (viewState.valid) { final String varValue = viewState.text; if (!Strings.isEmpty(varValue)) { - if (CalculatorVarsFragment.isValidValue(varValue)) { + if (VarsFragment.isValidValue(varValue)) { if (context instanceof AppCompatActivity) { VarEditDialogFragment.showDialog(VarEditDialogFragment.Input.newFromValue(varValue), ((AppCompatActivity) context).getSupportFragmentManager()); } else { final Intent intent = new Intent(context, CalculatorVarsActivity.class); - intent.putExtra(CalculatorVarsFragment.CREATE_VAR_EXTRA_STRING, varValue); + intent.putExtra(VarsFragment.CREATE_VAR_EXTRA_STRING, varValue); Activities.addIntentFlags(intent, false, context); context.startActivity(intent); } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorEngine.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorEngine.java index 4a25af88..319cd936 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorEngine.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorEngine.java @@ -39,13 +39,13 @@ public interface CalculatorEngine { void softReset(); @Nonnull - CalculatorMathRegistry getVarsRegistry(); + EntitiesRegistry getVarsRegistry(); @Nonnull - CalculatorMathRegistry getFunctionsRegistry(); + EntitiesRegistry getFunctionsRegistry(); @Nonnull - CalculatorMathRegistry getOperatorsRegistry(); + EntitiesRegistry getOperatorsRegistry(); @Nonnull - CalculatorMathRegistry getPostfixFunctionsRegistry(); + EntitiesRegistry getPostfixFunctionsRegistry(); @Nonnull CalculatorMathEngine getMathEngine(); @Deprecated diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorEngineImpl.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorEngineImpl.java index af349c17..d2116be0 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorEngineImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorEngineImpl.java @@ -22,11 +22,6 @@ package org.solovyev.android.calculator; -import java.text.DecimalFormatSymbols; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - import jscl.AngleUnit; import jscl.JsclMathEngine; import jscl.MathEngine; @@ -37,6 +32,10 @@ import jscl.math.function.IConstant; import jscl.math.operator.Operator; import jscl.text.ParseException; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.text.DecimalFormatSymbols; + public class CalculatorEngineImpl implements CalculatorEngine { /* @@ -63,16 +62,16 @@ public class CalculatorEngineImpl implements CalculatorEngine { private final CalculatorMathEngine mathEngine; @Nonnull - private final CalculatorMathRegistry varsRegistry; + private final EntitiesRegistry varsRegistry; @Nonnull - private final CalculatorMathRegistry functionsRegistry; + private final EntitiesRegistry functionsRegistry; @Nonnull - private final CalculatorMathRegistry operatorsRegistry; + private final EntitiesRegistry operatorsRegistry; @Nonnull - private final CalculatorMathRegistry postfixFunctionsRegistry; + private final EntitiesRegistry postfixFunctionsRegistry; @Nonnull private final Object lock; @@ -90,10 +89,10 @@ public class CalculatorEngineImpl implements CalculatorEngine { private String multiplicationSign = MULTIPLICATION_SIGN_DEFAULT; public CalculatorEngineImpl(@Nonnull JsclMathEngine engine, - @Nonnull CalculatorMathRegistry varsRegistry, - @Nonnull CalculatorMathRegistry functionsRegistry, - @Nonnull CalculatorMathRegistry operatorsRegistry, - @Nonnull CalculatorMathRegistry postfixFunctionsRegistry, + @Nonnull EntitiesRegistry varsRegistry, + @Nonnull EntitiesRegistry functionsRegistry, + @Nonnull EntitiesRegistry operatorsRegistry, + @Nonnull EntitiesRegistry postfixFunctionsRegistry, @Nullable Object lock) { this.engine = engine; @@ -118,25 +117,25 @@ public class CalculatorEngineImpl implements CalculatorEngine { */ @Nonnull @Override - public CalculatorMathRegistry getVarsRegistry() { + public EntitiesRegistry getVarsRegistry() { return this.varsRegistry; } @Nonnull @Override - public CalculatorMathRegistry getFunctionsRegistry() { + public EntitiesRegistry getFunctionsRegistry() { return this.functionsRegistry; } @Nonnull @Override - public CalculatorMathRegistry getOperatorsRegistry() { + public EntitiesRegistry getOperatorsRegistry() { return this.operatorsRegistry; } @Nonnull @Override - public CalculatorMathRegistry getPostfixFunctionsRegistry() { + public EntitiesRegistry getPostfixFunctionsRegistry() { return this.postfixFunctionsRegistry; } @@ -177,7 +176,7 @@ public class CalculatorEngineImpl implements CalculatorEngine { } } - private void safeLoadRegistry(@Nonnull CalculatorMathRegistry registry) { + private void safeLoadRegistry(@Nonnull EntitiesRegistry registry) { try { registry.load(); } catch (Exception e) { diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorFragmentType.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorFragmentType.java index 6f5a8eeb..248fd3be 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorFragmentType.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorFragmentType.java @@ -28,9 +28,9 @@ import org.solovyev.android.calculator.about.CalculatorAboutFragment; import org.solovyev.android.calculator.about.CalculatorReleaseNotesFragment; import org.solovyev.android.calculator.history.RecentHistoryFragment; import org.solovyev.android.calculator.history.SavedHistoryFragment; -import org.solovyev.android.calculator.math.edit.CalculatorFunctionsFragment; -import org.solovyev.android.calculator.math.edit.CalculatorOperatorsFragment; -import org.solovyev.android.calculator.math.edit.CalculatorVarsFragment; +import org.solovyev.android.calculator.math.edit.FunctionsFragment; +import org.solovyev.android.calculator.math.edit.OperatorsFragment; +import org.solovyev.android.calculator.math.edit.VarsFragment; import org.solovyev.android.calculator.matrix.CalculatorMatrixEditFragment; import org.solovyev.android.calculator.plot.CalculatorPlotFragment; import org.solovyev.android.calculator.plot.CalculatorPlotFunctionSettingsActivity; @@ -52,9 +52,9 @@ public enum CalculatorFragmentType { //keyboard(CalculatorHistoryFragment.class, "history", R.layout.fragment_history, R.string.c_history), history(RecentHistoryFragment.class, R.layout.fragment_history, R.string.cpp_history_tab_recent), saved_history(SavedHistoryFragment.class, R.layout.fragment_history, R.string.cpp_history_tab_saved), - variables(CalculatorVarsFragment.class, R.layout.vars_fragment, R.string.c_vars), - functions(CalculatorFunctionsFragment.class, R.layout.math_entities_fragment, R.string.c_functions), - operators(CalculatorOperatorsFragment.class, R.layout.math_entities_fragment, R.string.c_operators), + variables(VarsFragment.class, R.layout.vars_fragment, R.string.c_vars), + functions(FunctionsFragment.class, R.layout.fragment_entities, R.string.c_functions), + operators(OperatorsFragment.class, R.layout.fragment_entities, R.string.c_operators), plotter(CalculatorPlotFragment.class, R.layout.cpp_plotter_fragment, R.string.c_graph), plotter_functions(CalculatorPlotFunctionsActivity.CalculatorPlotFunctionsFragment.class, R.layout.cpp_plot_functions_fragment, R.string.cpp_plot_functions), plotter_function_settings(CalculatorPlotFunctionSettingsActivity.CalculatorPlotFunctionSettingsFragment.class, R.layout.cpp_plot_function_settings_fragment, R.string.cpp_plot_function_settings), 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 7017c0f9..f7c25ef6 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java @@ -498,7 +498,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { if (TextUtils.isEmpty(text)) { return; } - final CalculatorMathRegistry varsRegistry = Locator.getInstance().getEngine().getVarsRegistry(); + final EntitiesRegistry varsRegistry = Locator.getInstance().getEngine().getVarsRegistry(); final IConstant ansVar = varsRegistry.get(CalculatorVarsRegistry.ANS); final Var.Builder builder = ansVar != null ? new Var.Builder(ansVar) : new Var.Builder(); diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorOperatorsMathRegistry.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorOperatorsMathRegistry.java index 7d0c479b..a56b0380 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorOperatorsMathRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorOperatorsMathRegistry.java @@ -37,7 +37,7 @@ import jscl.math.operator.Operator; * Date: 11/17/11 * Time: 11:29 PM */ -public class CalculatorOperatorsMathRegistry extends AbstractCalculatorMathRegistry { +public class CalculatorOperatorsMathRegistry extends BaseEntitiesRegistry { @Nonnull private static final Map substitutes = new HashMap(); diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorPostfixFunctionsRegistry.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorPostfixFunctionsRegistry.java index db3594b8..c42055ff 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorPostfixFunctionsRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorPostfixFunctionsRegistry.java @@ -37,7 +37,7 @@ import jscl.math.operator.Operator; * Date: 11/19/11 * Time: 1:48 PM */ -public class CalculatorPostfixFunctionsRegistry extends AbstractCalculatorMathRegistry { +public class CalculatorPostfixFunctionsRegistry extends BaseEntitiesRegistry { @Nonnull private static final Map substitutes = new HashMap(); diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorVarsRegistry.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorVarsRegistry.java index 972a45d0..c73670c6 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorVarsRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorVarsRegistry.java @@ -42,7 +42,7 @@ import jscl.math.function.IConstant; * Date: 9/29/11 * Time: 4:57 PM */ -public class CalculatorVarsRegistry extends AbstractCalculatorMathRegistry { +public class CalculatorVarsRegistry extends BaseEntitiesRegistry { @Nonnull public static final String ANS = "ans"; @@ -63,7 +63,7 @@ public class CalculatorVarsRegistry extends AbstractCalculatorMathRegistry void saveVariable(@Nonnull CalculatorMathRegistry registry, + public static void saveVariable(@Nonnull EntitiesRegistry registry, @Nonnull MathEntityBuilder builder, @Nullable T editedInstance, @Nonnull Object source, boolean save) { @@ -131,12 +131,12 @@ public class CalculatorVarsRegistry extends AbstractCalculatorMathRegistry extends MathRegistry { +public interface EntitiesRegistry extends MathRegistry { @Nullable - String getDescription(@Nonnull String mathEntityName); + String getDescription(@Nonnull String name); @Nullable - String getCategory(@Nonnull T mathEntity); + String getCategory(@Nonnull E entity); void load(); diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsMathRegistry.java b/app/src/main/java/org/solovyev/android/calculator/FunctionsRegistry.java similarity index 88% rename from app/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsMathRegistry.java rename to app/src/main/java/org/solovyev/android/calculator/FunctionsRegistry.java index e20622b2..e88dc039 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsMathRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/FunctionsRegistry.java @@ -22,6 +22,10 @@ package org.solovyev.android.calculator; +import jscl.CustomFunctionCalculationException; +import jscl.math.function.CustomFunction; +import jscl.math.function.Function; +import jscl.math.function.IFunction; import org.solovyev.android.calculator.function.FunctionBuilderAdapter; import org.solovyev.android.calculator.model.AFunction; import org.solovyev.android.calculator.model.Functions; @@ -30,24 +34,13 @@ import org.solovyev.common.JBuilder; import org.solovyev.common.math.MathRegistry; import org.solovyev.common.text.Strings; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.Arrays; import java.util.HashMap; import java.util.Map; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import jscl.CustomFunctionCalculationException; -import jscl.math.function.CustomFunction; -import jscl.math.function.Function; -import jscl.math.function.IFunction; - -/** - * User: serso - * Date: 11/17/11 - * Time: 11:28 PM - */ -public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegistry { +public class FunctionsRegistry extends BaseEntitiesRegistry { @Nonnull private static final Map substitutes = new HashMap(); @@ -58,12 +51,12 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis substitutes.put("√", "sqrt"); } - public CalculatorFunctionsMathRegistry(@Nonnull MathRegistry functionsRegistry, - @Nonnull MathEntityDao mathEntityDao) { + public FunctionsRegistry(@Nonnull MathRegistry functionsRegistry, + @Nonnull MathEntityDao mathEntityDao) { super(functionsRegistry, FUNCTION_DESCRIPTION_PREFIX, mathEntityDao); } - public static void saveFunction(@Nonnull CalculatorMathRegistry registry, + public static void saveFunction(@Nonnull EntitiesRegistry registry, @Nonnull MathEntityBuilder builder, @Nullable IFunction editedInstance, @Nonnull Object source, boolean save) throws CustomFunctionCalculationException, AFunction.Builder.CreationException { @@ -111,8 +104,8 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis @Nullable @Override - public String getDescription(@Nonnull String functionName) { - final Function function = get(functionName); + public String getDescription(@Nonnull String name) { + final Function function = get(name); String result = null; if (function instanceof CustomFunction) { @@ -120,7 +113,7 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis } if (Strings.isEmpty(result)) { - result = super.getDescription(functionName); + result = super.getDescription(name); } return result; diff --git a/app/src/main/java/org/solovyev/android/calculator/function/FunctionEditDialogFragment.java b/app/src/main/java/org/solovyev/android/calculator/function/FunctionEditDialogFragment.java index 2b6e841d..9bfb091d 100644 --- a/app/src/main/java/org/solovyev/android/calculator/function/FunctionEditDialogFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/function/FunctionEditDialogFragment.java @@ -34,32 +34,22 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; - -import org.solovyev.android.calculator.App; -import org.solovyev.android.calculator.CalculatorEventData; -import org.solovyev.android.calculator.CalculatorEventListener; -import org.solovyev.android.calculator.CalculatorEventType; -import org.solovyev.android.calculator.CalculatorUtils; -import org.solovyev.android.calculator.DisplayState; -import org.solovyev.android.calculator.Locator; -import org.solovyev.android.calculator.R; -import org.solovyev.android.calculator.math.edit.CalculatorFunctionsActivity; -import org.solovyev.android.calculator.math.edit.CalculatorFunctionsFragment; -import org.solovyev.android.calculator.math.edit.MathEntityRemover; -import org.solovyev.android.calculator.model.AFunction; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - import jscl.math.Generic; import jscl.math.function.Constant; import jscl.math.function.CustomFunction; import jscl.math.function.Function; import jscl.math.function.IFunction; +import org.solovyev.android.calculator.*; +import org.solovyev.android.calculator.math.edit.CalculatorFunctionsActivity; +import org.solovyev.android.calculator.math.edit.FunctionsFragment; +import org.solovyev.android.calculator.math.edit.MathEntityRemover; +import org.solovyev.android.calculator.model.AFunction; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; /** * User: serso @@ -91,7 +81,7 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul } else { final Intent intent = new Intent(context, CalculatorFunctionsActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - intent.putExtra(CalculatorFunctionsFragment.CREATE_FUNCTION_EXTRA, input); + intent.putExtra(FunctionsFragment.EXTRA_FUNCTION, input); context.startActivity(intent); } } @@ -193,14 +183,6 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul Locator.getInstance().getCalculator().addCalculatorEventListener(this); } - /* - ********************************************************************** - * - * STATIC - * - ********************************************************************** - */ - @Override public void onPause() { Locator.getInstance().getCalculator().removeCalculatorEventListener(this); diff --git a/app/src/main/java/org/solovyev/android/calculator/function/FunctionEditorSaver.java b/app/src/main/java/org/solovyev/android/calculator/function/FunctionEditorSaver.java index 60cc57fb..8f5729c9 100644 --- a/app/src/main/java/org/solovyev/android/calculator/function/FunctionEditorSaver.java +++ b/app/src/main/java/org/solovyev/android/calculator/function/FunctionEditorSaver.java @@ -25,8 +25,8 @@ package org.solovyev.android.calculator.function; import android.view.View; import android.widget.EditText; -import org.solovyev.android.calculator.CalculatorFunctionsMathRegistry; -import org.solovyev.android.calculator.CalculatorMathRegistry; +import org.solovyev.android.calculator.FunctionsRegistry; +import org.solovyev.android.calculator.EntitiesRegistry; import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.math.edit.VarEditorSaver; @@ -59,13 +59,13 @@ public class FunctionEditorSaver implements View.OnClickListener { private final View view; @Nonnull - private final CalculatorMathRegistry mathRegistry; + private final EntitiesRegistry mathRegistry; public FunctionEditorSaver(@Nonnull AFunction.Builder builder, @Nullable IFunction editedInstance, @Nonnull View view, - @Nonnull CalculatorMathRegistry registry, + @Nonnull EntitiesRegistry registry, @Nonnull Object source) { this.builder = builder; @@ -144,7 +144,7 @@ public class FunctionEditorSaver implements View.OnClickListener { Locator.getInstance().getNotifier().showMessage(error, MessageType.error); } else { try { - CalculatorFunctionsMathRegistry.saveFunction(mathRegistry, new FunctionBuilderAdapter(builder), editedInstance, source, true); + FunctionsRegistry.saveFunction(mathRegistry, new FunctionBuilderAdapter(builder), editedInstance, source, true); } catch (CustomFunctionCalculationException e) { Locator.getInstance().getNotifier().showMessage(e); } catch (AFunction.Builder.CreationException e) { diff --git a/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryFragment.java b/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryFragment.java index 8088d699..49b9adee 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryFragment.java @@ -27,8 +27,6 @@ import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.support.annotation.NonNull; -import android.support.annotation.StringRes; -import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v7.app.AlertDialog; import android.support.v7.widget.LinearLayoutManager; @@ -53,27 +51,32 @@ import javax.annotation.Nullable; import javax.inject.Inject; import java.util.List; -import static android.view.Menu.NONE; import static android.view.View.GONE; import static android.view.View.VISIBLE; -public abstract class BaseHistoryFragment extends Fragment { +public abstract class BaseHistoryFragment extends BaseFragment { private final boolean recentHistory; @Inject History history; @Inject + Editor editor; + @Inject Bus bus; @Bind(R.id.history_recyclerview) RecyclerView recyclerView; @Bind(R.id.history_fab) FloatingActionButton fab; - @Nonnull - private FragmentUi ui; private HistoryAdapter adapter; protected BaseHistoryFragment(@Nonnull CalculatorFragmentType type) { + super(type); recentHistory = type == CalculatorFragmentType.history; - ui = new FragmentUi(type.getDefaultLayoutId(), type.getDefaultTitleResId(), false); + } + + @Override + protected void inject(@Nonnull AppComponent component) { + super.inject(component); + component.inject(this); } @Nonnull @@ -87,23 +90,16 @@ public abstract class BaseHistoryFragment extends Fragment { } public void useState(@Nonnull final HistoryState state) { - App.getEditor().setState(state.editor); + editor.setState(state.editor); final FragmentActivity activity = getActivity(); if (!(activity instanceof CalculatorActivity)) { activity.finish(); } } - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - ((CalculatorApplication) getActivity().getApplication()).getComponent().inject(this); - ui.onCreate(this); - } - @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - final View view = ui.onCreateView(this, inflater, container); + final View view = super.onCreateView(inflater, container, savedInstanceState); ButterKnife.bind(this, view); final Context context = inflater.getContext(); adapter = new HistoryAdapter(context); @@ -121,12 +117,6 @@ public abstract class BaseHistoryFragment extends Fragment { return view; } - @Override - public void onViewCreated(View root, Bundle savedInstanceState) { - super.onViewCreated(root, savedInstanceState); - ui.onViewCreated(this, root); - } - private void showClearHistoryDialog() { new AlertDialog.Builder(getActivity(), ui.getTheme().alertDialogTheme) .setTitle(R.string.cpp_clear_history_title) @@ -146,12 +136,6 @@ public abstract class BaseHistoryFragment extends Fragment { .show(); } - @Override - public void onResume() { - super.onResume(); - ui.onResume(this); - } - @SuppressWarnings("deprecation") protected final void copyResult(@Nonnull HistoryState state) { final Context context = getActivity(); @@ -163,6 +147,12 @@ public abstract class BaseHistoryFragment extends Fragment { clipboard.setText(displayText); } + @Override + public void onDestroyView() { + bus.unregister(adapter); + super.onDestroyView(); + } + @SuppressWarnings("deprecation") protected final void copyExpression(@Nonnull HistoryState state) { final Context context = getActivity(); @@ -178,25 +168,6 @@ public abstract class BaseHistoryFragment extends Fragment { return !state.display.valid || !Strings.isEmpty(state.display.text); } - @Override - public void onPause() { - ui.onPause(this); - super.onPause(); - } - - @Override - public void onDestroyView() { - bus.unregister(adapter); - ui.onDestroyView(this); - super.onDestroyView(); - } - - @Override - public void onDestroy() { - ui.onDestroy(this); - super.onDestroy(); - } - public class HistoryViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener, View.OnClickListener, MenuItem.OnMenuItemClickListener { private static final int DATETIME_FORMAT = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_MONTH | DateUtils.FORMAT_ABBREV_TIME; @@ -234,28 +205,23 @@ public abstract class BaseHistoryFragment extends Fragment { public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { Check.isNotNull(state); if (recentHistory) { - addMenu(menu, R.string.c_use); - addMenu(menu, R.string.c_copy_expression); + addMenu(menu, R.string.c_use, this); + addMenu(menu, R.string.c_copy_expression, this); if (shouldHaveCopyResult(state)) { - addMenu(menu, R.string.c_copy_result); + addMenu(menu, R.string.c_copy_result, this); } - addMenu(menu, R.string.c_save); + addMenu(menu, R.string.c_save, this); } else { - addMenu(menu, R.string.c_use); - addMenu(menu, R.string.c_copy_expression); + addMenu(menu, R.string.c_use, this); + addMenu(menu, R.string.c_copy_expression, this); if (shouldHaveCopyResult(state)) { - addMenu(menu, R.string.c_copy_result); + addMenu(menu, R.string.c_copy_result, this); } - addMenu(menu, R.string.c_edit); - addMenu(menu, R.string.c_remove); + addMenu(menu, R.string.c_edit, this); + addMenu(menu, R.string.c_remove, this); } } - @Nonnull - private MenuItem addMenu(@Nonnull ContextMenu menu, @StringRes int label) { - return menu.add(NONE, label, NONE, label).setOnMenuItemClickListener(this); - } - @Override public void onClick(View v) { Check.isNotNull(state); diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/AbstractMathEntityListFragment.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/AbstractMathEntityListFragment.java deleted file mode 100644 index fcf4e3b2..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/AbstractMathEntityListFragment.java +++ /dev/null @@ -1,342 +0,0 @@ -/* - * Copyright 2013 serso aka se.solovyev - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * Contact details - * - * Email: se.solovyev@gmail.com - * Site: http://se.solovyev.org - */ - -package org.solovyev.android.calculator.math.edit; - -import android.content.Context; -import android.os.Bundle; -import android.os.Handler; -import android.support.v4.app.ListFragment; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.AdapterView; -import android.widget.ArrayAdapter; -import android.widget.ListView; -import android.widget.TextView; -import org.solovyev.android.calculator.*; -import org.solovyev.android.menu.AMenuItem; -import org.solovyev.android.menu.ContextMenuBuilder; -import org.solovyev.android.menu.LabeledMenuItem; -import org.solovyev.android.menu.ListContextMenu; -import org.solovyev.common.JPredicate; -import org.solovyev.common.Objects; -import org.solovyev.common.filter.Filter; -import org.solovyev.common.math.MathEntity; -import org.solovyev.common.text.Strings; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.Comparator; -import java.util.List; - - -/** - * User: serso - * Date: 12/21/11 - * Time: 9:24 PM - */ -public abstract class AbstractMathEntityListFragment extends ListFragment implements CalculatorEventListener { - - /* - ********************************************************************** - * - * CONSTANTS - * - ********************************************************************** - */ - - public static final String MATH_ENTITY_CATEGORY_EXTRA_STRING = "org.solovyev.android.calculator.CalculatorVarsActivity_math_entity_category"; - - - /* - ********************************************************************** - * - * FIELDS - * - ********************************************************************** - */ - @Nonnull - private final FragmentUi ui; - @Nonnull - private final Handler uiHandler = new Handler(); - @Nullable - private MathEntityArrayAdapter adapter; - @Nullable - private String category; - - protected AbstractMathEntityListFragment(@Nonnull CalculatorFragmentType fragmentType) { - ui = new FragmentUi(fragmentType.getDefaultLayoutId(), fragmentType.getDefaultTitleResId()); - } - - @Nonnull - public static Bundle createBundleFor(@Nonnull String categoryId) { - final Bundle result = new Bundle(1); - putCategory(result, categoryId); - return result; - } - - static void putCategory(@Nonnull Bundle bundle, @Nonnull String categoryId) { - bundle.putString(MATH_ENTITY_CATEGORY_EXTRA_STRING, categoryId); - } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - final Bundle bundle = getArguments(); - if (bundle != null) { - category = bundle.getString(MATH_ENTITY_CATEGORY_EXTRA_STRING); - } - - ui.onCreate(this); - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - return ui.onCreateView(this, inflater, container); - } - - @Override - public void onViewCreated(View root, Bundle savedInstanceState) { - super.onViewCreated(root, savedInstanceState); - - ui.onViewCreated(this, root); - - final ListView lv = getListView(); - lv.setTextFilterEnabled(true); - - lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { - public void onItemClick(final AdapterView parent, - final View view, - final int position, - final long id) { - final AMenuItem onClick = getOnClickAction(); - if (onClick != null) { - onClick.onClick(((T) parent.getItemAtPosition(position)), getActivity()); - } - } - }); - - getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { - @Override - public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { - final T item = (T) parent.getItemAtPosition(position); - - final List> menuItems = getMenuItemsOnLongClick(item); - - if (!menuItems.isEmpty()) { - final ContextMenuBuilder, T> menuBuilder = ContextMenuBuilder.newInstance(AbstractMathEntityListFragment.this.getActivity(), "math-entity-menu", ListContextMenu.newInstance(menuItems)); - menuBuilder.build(item).show(); - } - - return true; - } - }); - } - - @Nullable - protected abstract AMenuItem getOnClickAction(); - - @Override - public void onDestroyView() { - ui.onDestroyView(this); - super.onDestroyView(); - } - - @Override - public void onDestroy() { - ui.onDestroy(this); - - super.onDestroy(); - } - - @Nonnull - protected abstract List> getMenuItemsOnLongClick(@Nonnull T item); - - @Override - public void onPause() { - this.ui.onPause(this); - - super.onPause(); - } - - @Override - public void onResume() { - super.onResume(); - - this.ui.onResume(this); - - adapter = new MathEntityArrayAdapter(getDescriptionGetter(), this.getActivity(), getMathEntitiesByCategory()); - setListAdapter(adapter); - - sort(); - } - - @Nonnull - private List getMathEntitiesByCategory() { - final List result = getMathEntities(); - - new Filter(new JPredicate() { - @Override - public boolean apply(T t) { - return !isInCategory(t); - } - }).filter(result.iterator()); - - return result; - } - - protected boolean isInCategory(@Nullable T t) { - return t != null && (category == null || Objects.areEqual(getMathEntityCategory(t), category)); - } - - @Nonnull - protected abstract MathEntityDescriptionGetter getDescriptionGetter(); - - @Nonnull - protected abstract List getMathEntities(); - - @Nullable - abstract String getMathEntityCategory(@Nonnull T t); - - protected void sort() { - final MathEntityArrayAdapter localAdapter = adapter; - if (localAdapter != null) { - localAdapter.sort(new Comparator() { - @Override - public int compare(T function1, T function2) { - return function1.getName().compareTo(function2.getName()); - } - }); - - localAdapter.notifyDataSetChanged(); - } - } - - public void addToAdapter(@Nonnull T mathEntity) { - if (this.adapter != null) { - this.adapter.add(mathEntity); - } - } - - public void removeFromAdapter(@Nonnull T mathEntity) { - if (this.adapter != null) { - this.adapter.remove(mathEntity); - } - } - - public void notifyAdapter() { - if (this.adapter != null) { - this.adapter.notifyDataSetChanged(); - } - } - - @Nullable - protected MathEntityArrayAdapter getAdapter() { - return adapter; - } - - @Nonnull - protected Handler getUiHandler() { - return uiHandler; - } - - @Override - public void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, @Nonnull CalculatorEventType calculatorEventType, @Nullable Object data) { - } - - /* - ********************************************************************** - * - * STATIC - * - ********************************************************************** - */ - - protected static interface MathEntityDescriptionGetter { - - @Nullable - String getDescription(@Nonnull Context context, @Nonnull String mathEntityName); - } - - protected static class MathEntityArrayAdapter extends ArrayAdapter { - - @Nonnull - private final MathEntityDescriptionGetter descriptionGetter; - - private MathEntityArrayAdapter(@Nonnull MathEntityDescriptionGetter descriptionGetter, - @Nonnull Context context, - @Nonnull List objects) { - super(context, R.layout.math_entity, R.id.math_entity_text, objects); - this.descriptionGetter = descriptionGetter; - } - - @Override - public View getView(int position, @Nullable View convertView, ViewGroup parent) { - final ViewGroup result; - - if (convertView == null) { - result = (ViewGroup) super.getView(position, convertView, parent); - fillView(position, result); - } else { - result = (ViewGroup) convertView; - fillView(position, result); - } - - - return result; - } - - private void fillView(int position, @Nonnull ViewGroup result) { - final T mathEntity = getItem(position); - - final TextView text = (TextView) result.findViewById(R.id.math_entity_text); - text.setText(String.valueOf(mathEntity)); - - final String mathEntityDescription = descriptionGetter.getDescription(getContext(), mathEntity.getName()); - - final TextView description = (TextView) result.findViewById(R.id.math_entity_short_description); - if (!Strings.isEmpty(mathEntityDescription)) { - description.setVisibility(View.VISIBLE); - description.setText(mathEntityDescription); - } else { - description.setVisibility(View.GONE); - } - } - } - - protected static class MathEntityDescriptionGetterImpl implements MathEntityDescriptionGetter { - - @Nonnull - private final CalculatorMathRegistry mathRegistry; - - public MathEntityDescriptionGetterImpl(@Nonnull CalculatorMathRegistry mathRegistry) { - this.mathRegistry = mathRegistry; - } - - @Override - public String getDescription(@Nonnull Context context, @Nonnull String mathEntityName) { - return this.mathRegistry.getDescription(mathEntityName); - } - } -} diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/BaseEntitiesFragment.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/BaseEntitiesFragment.java new file mode 100644 index 00000000..230027c4 --- /dev/null +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/BaseEntitiesFragment.java @@ -0,0 +1,275 @@ +/* + * Copyright 2013 serso aka se.solovyev + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * Contact details + * + * Email: se.solovyev@gmail.com + * Site: http://se.solovyev.org + */ + +package org.solovyev.android.calculator.math.edit; + +import android.app.Activity; +import android.content.Context; +import android.os.Bundle; +import android.os.Handler; +import android.support.annotation.NonNull; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.text.ClipboardManager; +import android.text.TextUtils; +import android.view.*; +import android.widget.TextView; +import butterknife.Bind; +import butterknife.ButterKnife; +import com.melnykov.fab.FloatingActionButton; +import org.solovyev.android.Check; +import org.solovyev.android.calculator.*; +import org.solovyev.android.views.llm.DividerItemDecoration; +import org.solovyev.common.math.MathEntity; +import org.solovyev.common.text.Strings; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.Iterator; +import java.util.List; + + +public abstract class BaseEntitiesFragment extends BaseFragment implements CalculatorEventListener { + + public static final String EXTRA_CATEGORY = "category"; + + @Nonnull + private final Handler uiHandler = new Handler(); + @Bind(R.id.entities_fab) + FloatingActionButton fab; + @Bind(R.id.entities_recyclerview) + RecyclerView recyclerView; + @Nullable + private EntitiesAdapter adapter; + @Nullable + private String category; + + protected BaseEntitiesFragment(@Nonnull CalculatorFragmentType type) { + super(type); + } + + @Nonnull + public static Bundle createBundleFor(@Nonnull String categoryId) { + final Bundle result = new Bundle(1); + putCategory(result, categoryId); + return result; + } + + static void putCategory(@Nonnull Bundle bundle, @Nonnull String categoryId) { + bundle.putString(EXTRA_CATEGORY, categoryId); + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + final Bundle bundle = getArguments(); + if (bundle != null) { + category = bundle.getString(EXTRA_CATEGORY); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + final View view = ui.onCreateView(this, inflater, container); + ButterKnife.bind(this, view); + final Context context = inflater.getContext(); + adapter = new EntitiesAdapter(context, TextUtils.isEmpty(category) ? getEntities() : getEntities(category)); + recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)); + recyclerView.setAdapter(adapter); + recyclerView.addItemDecoration(new DividerItemDecoration(context, null)); + return view; + } + + protected abstract void onClick(@Nonnull E entity); + + @Nonnull + private List getEntities(@NonNull String category) { + Check.isNotEmpty(category); + final List entities = getEntities(); + + final Iterator iterator = entities.iterator(); + while (iterator.hasNext()) { + final E entity = iterator.next(); + if (!isInCategory(entity, category)) { + iterator.remove(); + } + } + + return entities; + } + + protected final boolean isInCategory(@NonNull E entity) { + return TextUtils.isEmpty(category) || isInCategory(entity, category); + } + + private boolean isInCategory(@NonNull E entity, @NonNull String category) { + return TextUtils.equals(getCategory(entity), category); + } + + @Nonnull + protected abstract List getEntities(); + + @Nullable + abstract String getCategory(@Nonnull E e); + + protected void sort() { + /* + final EntitiesAdapter localAdapter = adapter; + if (localAdapter != null) { + localAdapter.sort(new Comparator() { + @Override + public int compare(E function1, E function2) { + return function1.getName().compareTo(function2.getName()); + } + }); + + localAdapter.notifyDataSetChanged(); + }*/ + } + + public void addToAdapter(@Nonnull E mathEntity) { + if (this.adapter != null) { + //this.adapter.add(mathEntity); + } + } + + public void removeFromAdapter(@Nonnull E mathEntity) { + if (this.adapter != null) { + //this.adapter.remove(mathEntity); + } + } + + public void notifyAdapter() { + if (this.adapter != null) { + this.adapter.notifyDataSetChanged(); + } + } + + @Nullable + protected EntitiesAdapter getAdapter() { + return adapter; + } + + @Nonnull + protected Handler getUiHandler() { + return uiHandler; + } + + @Override + public void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, @Nonnull CalculatorEventType calculatorEventType, @Nullable Object data) { + } + + public class EntityViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, MenuItem.OnMenuItemClickListener { + @Bind(R.id.entity_text) + TextView textView; + @Bind(R.id.entity_description) + TextView descriptionView; + @Nullable + private E entity; + + public EntityViewHolder(@Nonnull View view) { + super(view); + ButterKnife.bind(this, view); + view.setOnClickListener(this); + view.setOnCreateContextMenuListener(this); + } + + public void bind(@Nonnull E entity) { + this.entity = entity; + textView.setText(String.valueOf(entity)); + + final String description = getDescription(entity); + if (!Strings.isEmpty(description)) { + descriptionView.setVisibility(View.VISIBLE); + descriptionView.setText(description); + } else { + descriptionView.setVisibility(View.GONE); + } + } + + @Override + public void onClick(View v) { + Check.isNotNull(entity); + BaseEntitiesFragment.this.onClick(entity); + } + + @Override + public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { + Check.isNotNull(entity); + BaseEntitiesFragment.this.onCreateContextMenu(menu, entity, this); + } + + @Override + public boolean onMenuItemClick(MenuItem item) { + Check.isNotNull(entity); + return BaseEntitiesFragment.this.onMenuItemClicked(item, entity); + } + } + + @SuppressWarnings("deprecation") + protected final void copyDescription(@Nonnull E entity) { + final String description = getDescription(entity); + if (!Strings.isEmpty(description)) { + final ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Activity.CLIPBOARD_SERVICE); + clipboard.setText(description); + } + } + + @Nullable + protected abstract String getDescription(@NonNull E entity); + protected abstract void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull E entity, @Nonnull MenuItem.OnMenuItemClickListener listener); + protected abstract boolean onMenuItemClicked(@Nonnull MenuItem item, @Nonnull E entity); + + public class EntitiesAdapter extends RecyclerView.Adapter { + @Nonnull + private final LayoutInflater inflater; + @Nonnull + private final List list; + + private EntitiesAdapter(@Nonnull Context context, + @Nonnull List list) { + this.list = list; + this.inflater = LayoutInflater.from(context); + } + + @Override + public EntityViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + return new EntityViewHolder(inflater.inflate(R.layout.fragment_entities_item, parent, false)); + } + + @Override + public void onBindViewHolder(EntityViewHolder holder, int position) { + holder.bind(list.get(position)); + } + + @Override + public int getItemCount() { + return list.size(); + } + + @Nonnull + public E getItem(int position) { + return list.get(position); + } + } +} diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java index 1593722f..4d611655 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java @@ -22,18 +22,9 @@ package org.solovyev.android.calculator.math.edit; -import android.content.Intent; import android.os.Bundle; import android.util.Log; - -import org.solovyev.android.calculator.AndroidFunctionCategory; -import org.solovyev.android.calculator.BaseActivity; -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.FunctionCategory; -import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.*; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -48,14 +39,7 @@ public class CalculatorFunctionsActivity extends BaseActivity implements Calcula public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - final Bundle bundle; - - final Intent intent = getIntent(); - if (intent != null) { - bundle = intent.getExtras(); - } else { - bundle = null; - } + final Bundle bundle = getIntent().getExtras(); final CalculatorFragmentType fragmentType = CalculatorFragmentType.functions; @@ -66,10 +50,10 @@ public class CalculatorFunctionsActivity extends BaseActivity implements Calcula final Bundle fragmentParameters; if (category == FunctionCategory.my && bundle != null) { - AbstractMathEntityListFragment.putCategory(bundle, category.name()); + BaseEntitiesFragment.putCategory(bundle, category.name()); fragmentParameters = bundle; } else { - fragmentParameters = AbstractMathEntityListFragment.createBundleFor(category.name()); + fragmentParameters = BaseEntitiesFragment.createBundleFor(category.name()); } ui.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), fragmentParameters, androidCategory.getCaptionId(), R.id.main_layout); diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsActivity.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsActivity.java index 60338ec7..bb5bbdcc 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsActivity.java @@ -51,7 +51,7 @@ public class CalculatorOperatorsActivity extends BaseActivity implements Calcula for (OperatorCategory category : OperatorCategory.getCategoriesByTabOrder()) { final AndroidOperatorCategory androidCategory = AndroidOperatorCategory.valueOf(category); if (androidCategory != null) { - ui.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), AbstractMathEntityListFragment.createBundleFor(category.name()), androidCategory.getCaptionId(), R.id.main_layout); + ui.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), BaseEntitiesFragment.createBundleFor(category.name()), androidCategory.getCaptionId(), R.id.main_layout); } else { ui.logError("Unable to find android operator category for " + category); } diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsFragment.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsFragment.java deleted file mode 100644 index f6017815..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsFragment.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Copyright 2013 serso aka se.solovyev - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * Contact details - * - * Email: se.solovyev@gmail.com - * Site: http://se.solovyev.org - */ - -package org.solovyev.android.calculator.math.edit; - -import android.app.Activity; -import android.content.Context; -import android.text.ClipboardManager; - -import org.solovyev.android.calculator.CalculatorEventType; -import org.solovyev.android.calculator.CalculatorFragmentType; -import org.solovyev.android.calculator.Locator; -import org.solovyev.android.calculator.R; -import org.solovyev.android.menu.AMenuItem; -import org.solovyev.android.menu.LabeledMenuItem; -import org.solovyev.common.text.Strings; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import javax.annotation.Nonnull; - -import jscl.math.operator.Operator; - -/** - * User: serso - * Date: 11/17/11 - * Time: 1:53 PM - */ - -public class CalculatorOperatorsFragment extends AbstractMathEntityListFragment { - - public CalculatorOperatorsFragment() { - super(CalculatorFragmentType.operators); - } - - @Override - protected AMenuItem getOnClickAction() { - return LongClickMenuItem.use; - } - - @Nonnull - @Override - protected List> getMenuItemsOnLongClick(@Nonnull Operator item) { - final List> result = new ArrayList>(Arrays.asList(LongClickMenuItem.values())); - - if (Strings.isEmpty(OperatorDescriptionGetter.instance.getDescription(this.getActivity(), item.getName()))) { - result.remove(LongClickMenuItem.copy_description); - } - - return result; - } - - @Nonnull - @Override - protected MathEntityDescriptionGetter getDescriptionGetter() { - return OperatorDescriptionGetter.instance; - } - - - @Nonnull - @Override - protected List getMathEntities() { - final List result = new ArrayList(); - - result.addAll(Locator.getInstance().getEngine().getOperatorsRegistry().getEntities()); - result.addAll(Locator.getInstance().getEngine().getPostfixFunctionsRegistry().getEntities()); - - return result; - } - - @Override - protected String getMathEntityCategory(@Nonnull Operator operator) { - String result = Locator.getInstance().getEngine().getOperatorsRegistry().getCategory(operator); - if (result == null) { - result = Locator.getInstance().getEngine().getPostfixFunctionsRegistry().getCategory(operator); - } - - return result; - } - - private static enum OperatorDescriptionGetter implements MathEntityDescriptionGetter { - - instance; - - @Override - public String getDescription(@Nonnull Context context, @Nonnull String mathEntityName) { - String result = Locator.getInstance().getEngine().getOperatorsRegistry().getDescription(mathEntityName); - if (Strings.isEmpty(result)) { - result = Locator.getInstance().getEngine().getPostfixFunctionsRegistry().getDescription(mathEntityName); - } - - return result; - } - } - - /* - ********************************************************************** - * - * STATIC - * - ********************************************************************** - */ - - private static enum LongClickMenuItem implements LabeledMenuItem { - - use(R.string.c_use) { - @Override - public void onClick(@Nonnull Operator data, @Nonnull Context context) { - Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_operator, data); - } - }, - - copy_description(R.string.c_copy_description) { - @Override - public void onClick(@Nonnull Operator data, @Nonnull Context context) { - final String text = OperatorDescriptionGetter.instance.getDescription(context, data.getName()); - if (!Strings.isEmpty(text)) { - final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); - clipboard.setText(text); - } - } - }; - private final int captionId; - - LongClickMenuItem(int captionId) { - this.captionId = captionId; - } - - @Nonnull - @Override - public String getCaption(@Nonnull Context context) { - return context.getString(captionId); - } - } - -} - diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java index ac031744..36372465 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java @@ -63,10 +63,10 @@ public class CalculatorVarsActivity extends BaseActivity implements CalculatorEv final Bundle fragmentParameters; if (category == VarCategory.my && bundle != null) { - AbstractMathEntityListFragment.putCategory(bundle, category.name()); + BaseEntitiesFragment.putCategory(bundle, category.name()); fragmentParameters = bundle; } else { - fragmentParameters = AbstractMathEntityListFragment.createBundleFor(category.name()); + fragmentParameters = BaseEntitiesFragment.createBundleFor(category.name()); } diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsFragment.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsFragment.java similarity index 51% rename from app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsFragment.java rename to app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsFragment.java index e15c8607..f6d8b147 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsFragment.java @@ -22,49 +22,30 @@ package org.solovyev.android.calculator.math.edit; -import android.app.Activity; -import android.content.Context; import android.os.Bundle; -import android.os.Parcelable; -import android.support.v7.app.ActionBarActivity; -import android.text.ClipboardManager; +import android.support.annotation.NonNull; +import android.support.v4.app.FragmentActivity; +import android.view.ContextMenu; +import android.view.MenuItem; import android.view.View; -import android.widget.ListView; - -import com.melnykov.fab.FloatingActionButton; - -import org.solovyev.android.calculator.CalculatorEventData; -import org.solovyev.android.calculator.CalculatorEventType; -import org.solovyev.android.calculator.CalculatorFragmentType; -import org.solovyev.android.calculator.CalculatorMathRegistry; -import org.solovyev.android.calculator.Change; -import org.solovyev.android.calculator.Locator; -import org.solovyev.android.calculator.R; +import jscl.math.function.Function; +import jscl.math.function.IFunction; +import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.function.FunctionEditDialogFragment; -import org.solovyev.android.menu.AMenuItem; -import org.solovyev.android.menu.LabeledMenuItem; import org.solovyev.common.text.Strings; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.List; -import jscl.math.function.Function; -import jscl.math.function.IFunction; +public class FunctionsFragment extends BaseEntitiesFragment { -/** - * User: serso - * Date: 10/29/11 - * Time: 4:55 PM - */ -public class CalculatorFunctionsFragment extends AbstractMathEntityListFragment { + public static final String EXTRA_FUNCTION = "function"; + @NonNull + private final EntitiesRegistry registry = Locator.getInstance().getEngine().getFunctionsRegistry(); - public static final String CREATE_FUNCTION_EXTRA = "create_function"; - - public CalculatorFunctionsFragment() { + public FunctionsFragment() { super(CalculatorFragmentType.functions); } @@ -74,27 +55,21 @@ public class CalculatorFunctionsFragment extends AbstractMathEntityListFragment< final Bundle bundle = getArguments(); if (bundle != null) { - final Parcelable parcelable = bundle.getParcelable(CREATE_FUNCTION_EXTRA); - if (parcelable instanceof FunctionEditDialogFragment.Input) { - FunctionEditDialogFragment.showDialog((FunctionEditDialogFragment.Input) parcelable, getFragmentManager()); - + final FunctionEditDialogFragment.Input input = bundle.getParcelable(EXTRA_FUNCTION); + if (input != null) { + FunctionEditDialogFragment.showDialog(input, getFragmentManager()); // in order to stop intent for other tabs - bundle.remove(CREATE_FUNCTION_EXTRA); + bundle.remove(EXTRA_FUNCTION); } } - - setHasOptionsMenu(true); - } @Override public void onViewCreated(View root, Bundle savedInstanceState) { super.onViewCreated(root, savedInstanceState); - final ListView lv = getListView(); - final FloatingActionButton fab = (FloatingActionButton) root.findViewById(R.id.fab); fab.setVisibility(View.VISIBLE); - fab.attachToListView(lv); + fab.attachToRecyclerView(recyclerView); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -104,44 +79,55 @@ public class CalculatorFunctionsFragment extends AbstractMathEntityListFragment< } @Override - protected AMenuItem getOnClickAction() { - return LongClickMenuItem.use; + protected void onClick(@NonNull @Nonnull Function function) { + Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_function, function); } - @Nonnull @Override - protected List> getMenuItemsOnLongClick(@Nonnull Function item) { - List> result = new ArrayList>(Arrays.asList(LongClickMenuItem.values())); - - final CalculatorMathRegistry functionsRegistry = Locator.getInstance().getEngine().getFunctionsRegistry(); - if (Strings.isEmpty(functionsRegistry.getDescription(item.getName()))) { - result.remove(LongClickMenuItem.copy_description); + 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); } - final Function function = functionsRegistry.get(item.getName()); - if (function == null || function.isSystem()) { - result.remove(LongClickMenuItem.edit); - result.remove(LongClickMenuItem.remove); + if (!function.isSystem()) { + addMenu(menu, R.string.c_edit, listener); + addMenu(menu, R.string.c_remove, listener); } + } - return result; + @Override + protected boolean onMenuItemClicked(@Nonnull MenuItem item, @Nonnull Function function) { + final FragmentActivity activity = getActivity(); + switch (item.getItemId()) { + case R.string.c_use: + Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_function, function); + return true; + case R.string.c_edit: + if (function instanceof IFunction) { + FunctionEditDialogFragment.showDialog(FunctionEditDialogFragment.Input.newFromFunction((IFunction) function), activity.getSupportFragmentManager()); + } + return true; + case R.string.c_remove: + MathEntityRemover.newFunctionRemover(function, null, activity, activity).showConfirmationDialog(); + return true; + case R.string.c_copy_description: + copyDescription(function); + return true; + } + return false; } @Nonnull @Override - protected MathEntityDescriptionGetter getDescriptionGetter() { - return new MathEntityDescriptionGetterImpl(Locator.getInstance().getEngine().getFunctionsRegistry()); - } - - @Nonnull - @Override - protected List getMathEntities() { - return new ArrayList(Locator.getInstance().getEngine().getFunctionsRegistry().getEntities()); + protected List getEntities() { + return new ArrayList<>(registry.getEntities()); } @Override - protected String getMathEntityCategory(@Nonnull Function function) { - return Locator.getInstance().getEngine().getFunctionsRegistry().getCategory(function); + protected String getCategory(@Nonnull Function function) { + return registry.getCategory(function); } @Override @@ -163,6 +149,12 @@ public class CalculatorFunctionsFragment extends AbstractMathEntityListFragment< } } + @Nullable + @Override + protected String getDescription(@NonNull Function function) { + return registry.getDescription(function.getName()); + } + private void processFunctionRemoved(@Nonnull final Function function) { if (this.isInCategory(function)) { @@ -189,12 +181,12 @@ public class CalculatorFunctionsFragment extends AbstractMathEntityListFragment< IFunction oldValue = change.getOldValue(); if (oldValue.isIdDefined()) { - final MathEntityArrayAdapter adapter = getAdapter(); + final EntitiesAdapter adapter = getAdapter(); if (adapter != null) { - for (int i = 0; i < adapter.getCount(); i++) { + for (int i = 0; i < adapter.getItemCount(); i++) { final Function functionFromAdapter = adapter.getItem(i); if (functionFromAdapter.isIdDefined() && oldValue.getId().equals(functionFromAdapter.getId())) { - adapter.remove(functionFromAdapter); + //adapter.remove(functionFromAdapter); break; } } @@ -222,59 +214,4 @@ public class CalculatorFunctionsFragment extends AbstractMathEntityListFragment< }); } } - - /* - ********************************************************************** - * - * STATIC - * - ********************************************************************** - */ - - private static enum LongClickMenuItem implements LabeledMenuItem { - use(R.string.c_use) { - @Override - public void onClick(@Nonnull Function function, @Nonnull Context context) { - Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_function, function); - } - }, - - edit(R.string.c_edit) { - @Override - public void onClick(@Nonnull Function function, @Nonnull Context context) { - if (function instanceof IFunction) { - FunctionEditDialogFragment.showDialog(FunctionEditDialogFragment.Input.newFromFunction((IFunction) function), ((ActionBarActivity) context).getSupportFragmentManager()); - } - } - }, - - remove(R.string.c_remove) { - @Override - public void onClick(@Nonnull Function function, @Nonnull Context context) { - MathEntityRemover.newFunctionRemover(function, null, context, context).showConfirmationDialog(); - } - }, - - copy_description(R.string.c_copy_description) { - @Override - public void onClick(@Nonnull Function function, @Nonnull Context context) { - final String text = Locator.getInstance().getEngine().getFunctionsRegistry().getDescription(function.getName()); - if (!Strings.isEmpty(text)) { - final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); - clipboard.setText(text); - } - } - }; - private final int captionId; - - LongClickMenuItem(int captionId) { - this.captionId = captionId; - } - - @Nonnull - @Override - public String getCaption(@Nonnull Context context) { - return context.getString(captionId); - } - } } diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/MathEntityRemover.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/MathEntityRemover.java index 919f5c3d..84e01c29 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/MathEntityRemover.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/MathEntityRemover.java @@ -29,7 +29,7 @@ import android.view.View; import android.widget.TextView; import org.solovyev.android.calculator.CalculatorEventType; -import org.solovyev.android.calculator.CalculatorMathRegistry; +import org.solovyev.android.calculator.EntitiesRegistry; import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.R; import org.solovyev.common.math.MathEntity; @@ -56,7 +56,7 @@ public class MathEntityRemover implements View.OnClickList private final boolean confirmed; @Nonnull - private final CalculatorMathRegistry varsRegistry; + private final EntitiesRegistry varsRegistry; @Nonnull private final Object source; @Nonnull @@ -75,7 +75,7 @@ public class MathEntityRemover implements View.OnClickList private MathEntityRemover(@Nonnull T mathEntity, @Nullable DialogInterface.OnClickListener callbackOnCancel, boolean confirmed, - @Nonnull CalculatorMathRegistry varsRegistry, + @Nonnull EntitiesRegistry varsRegistry, @Nonnull Context context, @Nonnull Object source, @Nonnull Params params) { 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 new file mode 100644 index 00000000..d8cbfad8 --- /dev/null +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsFragment.java @@ -0,0 +1,107 @@ +/* + * Copyright 2013 serso aka se.solovyev + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * Contact details + * + * Email: se.solovyev@gmail.com + * Site: http://se.solovyev.org + */ + +package org.solovyev.android.calculator.math.edit; + +import android.support.annotation.NonNull; +import android.text.TextUtils; +import android.view.ContextMenu; +import android.view.MenuItem; +import jscl.math.operator.Operator; +import org.solovyev.android.calculator.*; +import org.solovyev.common.text.Strings; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.List; + +public class OperatorsFragment extends BaseEntitiesFragment { + + @NonNull + private final EntitiesRegistry operatorsRegistry = Locator.getInstance().getEngine().getOperatorsRegistry(); + @NonNull + private final EntitiesRegistry postfixFunctionsRegistry = Locator.getInstance().getEngine().getPostfixFunctionsRegistry(); + + public OperatorsFragment() { + super(CalculatorFragmentType.operators); + } + + @Override + protected void onClick(@NonNull Operator operator) { + Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_operator, operator); + } + + @Nonnull + @Override + protected List getEntities() { + final List result = new ArrayList(); + + result.addAll(operatorsRegistry.getEntities()); + result.addAll(postfixFunctionsRegistry.getEntities()); + + return result; + } + + @Override + protected String getCategory(@Nonnull Operator operator) { + final String result = operatorsRegistry.getCategory(operator); + if (!TextUtils.isEmpty(result)) { + return result; + } + return postfixFunctionsRegistry.getCategory(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 + protected boolean onMenuItemClicked(@Nonnull MenuItem item, @Nonnull Operator operator) { + switch (item.getItemId()) { + 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; + } + + @Nullable + @Override + protected String getDescription(@NonNull Operator operator) { + final String name = operator.getName(); + final String result = operatorsRegistry.getDescription(name); + if (!Strings.isEmpty(result)) { + return result; + } + return postfixFunctionsRegistry.getDescription(name); + } +} + diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/VarEditorSaver.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/VarEditorSaver.java index 0f11bf51..f1596c77 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/VarEditorSaver.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/VarEditorSaver.java @@ -25,7 +25,7 @@ package org.solovyev.android.calculator.math.edit; import android.view.View; import android.widget.EditText; -import org.solovyev.android.calculator.CalculatorMathRegistry; +import org.solovyev.android.calculator.EntitiesRegistry; import org.solovyev.android.calculator.CalculatorVarsRegistry; import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.R; @@ -57,7 +57,7 @@ public class VarEditorSaver implements View.OnClickListene private final T editedInstance; @Nonnull - private final CalculatorMathRegistry mathRegistry; + private final EntitiesRegistry mathRegistry; @Nonnull private final Object source; @@ -68,7 +68,7 @@ public class VarEditorSaver implements View.OnClickListene public VarEditorSaver(@Nonnull MathEntityBuilder varBuilder, @Nullable T editedInstance, @Nonnull View editView, - @Nonnull CalculatorMathRegistry mathRegistry, + @Nonnull EntitiesRegistry mathRegistry, @Nonnull Object source) { this.varBuilder = varBuilder; this.editedInstance = editedInstance; @@ -130,7 +130,7 @@ public class VarEditorSaver implements View.OnClickListene error = null; } else { // value is not empty => must be a number - boolean valid = CalculatorVarsFragment.isValidValue(value); + boolean valid = VarsFragment.isValidValue(value); if (valid) { varBuilder.setName(name); diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragment.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/VarsFragment.java similarity index 58% rename from app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragment.java rename to app/src/main/java/org/solovyev/android/calculator/math/edit/VarsFragment.java index bb5e7a4e..e9742224 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/VarsFragment.java @@ -22,49 +22,32 @@ package org.solovyev.android.calculator.math.edit; -import android.content.Context; import android.os.Bundle; -import android.support.v7.app.ActionBarActivity; +import android.support.annotation.NonNull; +import android.support.v4.app.FragmentActivity; +import android.view.ContextMenu; +import android.view.MenuItem; import android.view.View; -import android.widget.ListView; - import com.melnykov.fab.FloatingActionButton; - -import org.solovyev.android.calculator.CalculatorEventData; -import org.solovyev.android.calculator.CalculatorEventType; -import org.solovyev.android.calculator.CalculatorFragmentType; -import org.solovyev.android.calculator.CalculatorParseException; -import org.solovyev.android.calculator.Change; -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 jscl.math.function.IConstant; +import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.math.MathType; -import org.solovyev.android.menu.AMenuItem; -import org.solovyev.android.menu.LabeledMenuItem; import org.solovyev.common.JPredicate; import org.solovyev.common.collections.Collections; import org.solovyev.common.text.Strings; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.List; -import jscl.math.function.IConstant; - -/** - * User: serso - * Date: 9/28/11 - * Time: 10:55 PM - */ -public class CalculatorVarsFragment extends AbstractMathEntityListFragment { +public class VarsFragment extends BaseEntitiesFragment { public static final String CREATE_VAR_EXTRA_STRING = "create_var"; + @NonNull + private final EntitiesRegistry registry = Locator.getInstance().getEngine().getVarsRegistry(); - public CalculatorVarsFragment() { + public VarsFragment() { super(CalculatorFragmentType.variables); } @@ -102,10 +85,9 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment getOnClickAction() { - return LongClickMenuItem.use; - } - - @Nonnull - @Override - protected List> getMenuItemsOnLongClick(@Nonnull IConstant item) { - final List> result = new ArrayList>(Arrays.asList(LongClickMenuItem.values())); - - if (item.isSystem()) { - result.remove(LongClickMenuItem.edit); - result.remove(LongClickMenuItem.remove); - } - - if (Strings.isEmpty(Locator.getInstance().getEngine().getVarsRegistry().getDescription(item.getName()))) { - result.remove(LongClickMenuItem.copy_description); - } - - if (Strings.isEmpty(item.getValue())) { - result.remove(LongClickMenuItem.copy_value); - } - - return result; - } - - @Nonnull - @Override - protected MathEntityDescriptionGetter getDescriptionGetter() { - return new MathEntityDescriptionGetterImpl(Locator.getInstance().getEngine().getVarsRegistry()); + protected void onClick(@NonNull IConstant constant) { + Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_constant, constant); } @SuppressWarnings({"UnusedDeclaration"}) @@ -153,8 +108,8 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment getMathEntities() { - final List result = new ArrayList(Locator.getInstance().getEngine().getVarsRegistry().getEntities()); + protected List getEntities() { + final List result = new ArrayList(registry.getEntities()); Collections.removeAll(result, new JPredicate() { @Override @@ -167,8 +122,8 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment { - use(R.string.c_use) { - @Override - public void onClick(@Nonnull IConstant data, @Nonnull Context context) { - Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_constant, data); - } - }, - - edit(R.string.c_edit) { - @Override - public void onClick(@Nonnull IConstant constant, @Nonnull Context context) { - VarEditDialogFragment.showDialog(VarEditDialogFragment.Input.newFromConstant(constant), ((ActionBarActivity) context).getSupportFragmentManager()); - } - }, - - remove(R.string.c_remove) { - @Override - public void onClick(@Nonnull IConstant constant, @Nonnull Context context) { - MathEntityRemover.newConstantRemover(constant, null, context, context).showConfirmationDialog(); - } - }, - - copy_value(R.string.c_copy_value) { - @Override - public void onClick(@Nonnull IConstant data, @Nonnull Context context) { - final String text = data.getValue(); - if (!Strings.isEmpty(text)) { - if (text == null) throw new AssertionError(); - Locator.getInstance().getClipboard().setText(text); - } - } - }, - - copy_description(R.string.c_copy_description) { - @Override - public void onClick(@Nonnull IConstant data, @Nonnull Context context) { - final String text = Locator.getInstance().getEngine().getVarsRegistry().getDescription(data.getName()); - if (!Strings.isEmpty(text)) { - if (text == null) throw new AssertionError(); - Locator.getInstance().getClipboard().setText(text); - } - } - }; - private final int captionId; - - LongClickMenuItem(int captionId) { - this.captionId = captionId; - } - - @Nonnull - @Override - public String getCaption(@Nonnull Context context) { - return context.getString(captionId); - } + @Nullable + @Override + protected String getDescription(@NonNull IConstant constant) { + return registry.getDescription(constant.getName()); } + } diff --git a/app/src/main/java/org/solovyev/android/calculator/model/AFunction.java b/app/src/main/java/org/solovyev/android/calculator/model/AFunction.java index ae22dc6b..22d0c089 100644 --- a/app/src/main/java/org/solovyev/android/calculator/model/AFunction.java +++ b/app/src/main/java/org/solovyev/android/calculator/model/AFunction.java @@ -22,6 +22,7 @@ package org.solovyev.android.calculator.model; +import jscl.math.function.IFunction; import org.simpleframework.xml.Element; import org.simpleframework.xml.ElementList; import org.simpleframework.xml.Root; @@ -34,33 +35,17 @@ import org.solovyev.common.msg.Message; import org.solovyev.common.msg.MessageLevel; import org.solovyev.common.text.Strings; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Locale; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import jscl.math.function.IFunction; - -/** - * User: serso - * Date: 12/22/11 - * Time: 5:25 PM - */ - @Root(name = "function") public class AFunction implements IFunction, MathPersistenceEntity, Serializable { - /* - ********************************************************************** - * - * FIELDS - * - ********************************************************************** - */ @Transient private Integer id; @@ -83,15 +68,7 @@ public class AFunction implements IFunction, MathPersistenceEntity, Serializable @Nonnull private String description = ""; - /* - ********************************************************************** - * - * CONSTRUCTORS - * - ********************************************************************** - */ - - public AFunction() { + public AFunction() { } public AFunction(Integer id) { @@ -106,15 +83,7 @@ public class AFunction implements IFunction, MathPersistenceEntity, Serializable return result; } - /* - ********************************************************************** - * - * METHODS - * - ********************************************************************** - */ - - private static void copy(@Nonnull AFunction target, + private static void copy(@Nonnull AFunction target, @Nonnull IFunction source) { target.name = source.getName(); target.content = source.getContent(); @@ -123,7 +92,7 @@ public class AFunction implements IFunction, MathPersistenceEntity, Serializable if (source.isIdDefined()) { target.id = source.getId(); } - target.parameterNames = new ArrayList(source.getParameterNames()); + target.parameterNames = new ArrayList<>(source.getParameterNames()); } @Override @@ -149,15 +118,7 @@ public class AFunction implements IFunction, MathPersistenceEntity, Serializable '}'; } - /* - ********************************************************************** - * - * GETTERS/SETTERS - * - ********************************************************************** - */ - - @Nonnull + @Nonnull public String getName() { return name; } @@ -211,15 +172,7 @@ public class AFunction implements IFunction, MathPersistenceEntity, Serializable this.parameterNames = parameterNames; } - /* - ********************************************************************** - * - * STATIC - * - ********************************************************************** - */ - - public static class Builder implements MathEntityBuilder { + public static class Builder implements MathEntityBuilder { @Nonnull private String name; diff --git a/app/src/main/java/org/solovyev/android/calculator/model/AndroidCalculatorEngine.java b/app/src/main/java/org/solovyev/android/calculator/model/AndroidCalculatorEngine.java index 88a06190..806d17a8 100644 --- a/app/src/main/java/org/solovyev/android/calculator/model/AndroidCalculatorEngine.java +++ b/app/src/main/java/org/solovyev/android/calculator/model/AndroidCalculatorEngine.java @@ -91,7 +91,7 @@ public class AndroidCalculatorEngine implements CalculatorEngine, SharedPreferen final JsclMathEngine engine = JsclMathEngine.getInstance(); this.calculatorEngine = new CalculatorEngineImpl(engine, new CalculatorVarsRegistry(engine.getConstantsRegistry(), new AndroidMathEntityDao("org.solovyev.android.calculator.CalculatorModel_vars", application, Vars.class)), - new CalculatorFunctionsMathRegistry(engine.getFunctionsRegistry(), new AndroidMathEntityDao("org.solovyev.android.calculator.CalculatorModel_functions", application, Functions.class)), + new FunctionsRegistry(engine.getFunctionsRegistry(), new AndroidMathEntityDao("org.solovyev.android.calculator.CalculatorModel_functions", application, Functions.class)), new CalculatorOperatorsMathRegistry(engine.getOperatorsRegistry(), new AndroidMathEntityDao(null, application, null)), new CalculatorPostfixFunctionsRegistry(engine.getPostfixFunctionsRegistry(), new AndroidMathEntityDao(null, application, null)), this.lock); @@ -109,25 +109,25 @@ public class AndroidCalculatorEngine implements CalculatorEngine, SharedPreferen @Override @Nonnull - public CalculatorMathRegistry getVarsRegistry() { + public EntitiesRegistry getVarsRegistry() { return calculatorEngine.getVarsRegistry(); } @Override @Nonnull - public CalculatorMathRegistry getFunctionsRegistry() { + public EntitiesRegistry getFunctionsRegistry() { return calculatorEngine.getFunctionsRegistry(); } @Override @Nonnull - public CalculatorMathRegistry getOperatorsRegistry() { + public EntitiesRegistry getOperatorsRegistry() { return calculatorEngine.getOperatorsRegistry(); } @Override @Nonnull - public CalculatorMathRegistry getPostfixFunctionsRegistry() { + public EntitiesRegistry getPostfixFunctionsRegistry() { return calculatorEngine.getPostfixFunctionsRegistry(); } diff --git a/app/src/main/res/layout/math_entities_fragment.xml b/app/src/main/res/layout/fragment_entities.xml similarity index 87% rename from app/src/main/res/layout/math_entities_fragment.xml rename to app/src/main/res/layout/fragment_entities.xml index f9091f03..470f566a 100644 --- a/app/src/main/res/layout/math_entities_fragment.xml +++ b/app/src/main/res/layout/fragment_entities.xml @@ -41,10 +41,13 @@ a:layout_width="match_parent" a:layout_height="match_parent"> - + diff --git a/app/src/main/res/layout/math_entity.xml b/app/src/main/res/layout/fragment_entities_item.xml similarity index 83% rename from app/src/main/res/layout/math_entity.xml rename to app/src/main/res/layout/fragment_entities_item.xml index 6c5cc287..97024dda 100644 --- a/app/src/main/res/layout/math_entity.xml +++ b/app/src/main/res/layout/fragment_entities_item.xml @@ -24,20 +24,18 @@ diff --git a/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java b/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java index 1f38c3df..376b3abb 100644 --- a/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java +++ b/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java @@ -93,7 +93,7 @@ public class CalculatorTestUtils { final JsclMathEngine jsclEngine = JsclMathEngine.getInstance(); final CalculatorVarsRegistry varsRegistry = new CalculatorVarsRegistry(jsclEngine.getConstantsRegistry(), mathEntityDao); - final CalculatorFunctionsMathRegistry functionsRegistry = new CalculatorFunctionsMathRegistry(jsclEngine.getFunctionsRegistry(), mathEntityDao); + final FunctionsRegistry functionsRegistry = new FunctionsRegistry(jsclEngine.getFunctionsRegistry(), mathEntityDao); final CalculatorOperatorsMathRegistry operatorsRegistry = new CalculatorOperatorsMathRegistry(jsclEngine.getOperatorsRegistry(), mathEntityDao); final CalculatorPostfixFunctionsRegistry postfixFunctionsRegistry = new CalculatorPostfixFunctionsRegistry(jsclEngine.getPostfixFunctionsRegistry(), mathEntityDao);