From 6d7e75d94bb13db6c4f4f10d6d008daaf3a46fe6 Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 31 Jan 2016 10:15:21 +0100 Subject: [PATCH] Operators --- .../android/calculator/AppComponent.java | 4 ++- .../android/calculator/CalculatorImpl.java | 16 +---------- .../solovyev/android/calculator/Engine.java | 15 +++++------ .../entities/BaseEntitiesFragment.java | 27 +++++++++---------- .../functions/FunctionsFragment.java | 11 -------- .../operators/OperatorsActivity.java | 16 +++-------- .../operators/OperatorsFragment.java | 16 ++++++----- .../{ => operators}/OperatorsRegistry.java | 22 +++++++-------- .../PostfixFunctionsRegistry.java | 22 +++++++-------- .../variables/VariablesFragment.java | 13 +-------- .../calculator/CalculatorTestUtils.java | 2 ++ 11 files changed, 61 insertions(+), 103 deletions(-) rename app/src/main/java/org/solovyev/android/calculator/{ => operators}/OperatorsRegistry.java (87%) rename app/src/main/java/org/solovyev/android/calculator/{ => operators}/PostfixFunctionsRegistry.java (87%) diff --git a/app/src/main/java/org/solovyev/android/calculator/AppComponent.java b/app/src/main/java/org/solovyev/android/calculator/AppComponent.java index 594a70de..48811740 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AppComponent.java +++ b/app/src/main/java/org/solovyev/android/calculator/AppComponent.java @@ -2,10 +2,11 @@ package org.solovyev.android.calculator; import dagger.Component; import org.solovyev.android.calculator.functions.EditFunctionFragment; +import org.solovyev.android.calculator.functions.FunctionsFragment; import org.solovyev.android.calculator.history.BaseHistoryFragment; import org.solovyev.android.calculator.history.EditHistoryFragment; -import org.solovyev.android.calculator.functions.FunctionsFragment; import org.solovyev.android.calculator.onscreen.CalculatorOnscreenService; +import org.solovyev.android.calculator.operators.OperatorsFragment; import org.solovyev.android.calculator.variables.EditVariableFragment; import org.solovyev.android.calculator.variables.VariablesFragment; @@ -25,4 +26,5 @@ public interface AppComponent { void inject(EditHistoryFragment fragment); void inject(FunctionsFragment fragment); void inject(VariablesFragment fragment); + void inject(OperatorsFragment fragment); } 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 2e716f99..db30c858 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java @@ -33,7 +33,6 @@ import jscl.NumeralBase; import jscl.NumeralBaseException; import jscl.math.Generic; import jscl.math.function.IConstant; -import jscl.math.operator.Operator; import jscl.text.ParseInterruptedException; import org.solovyev.android.calculator.functions.FunctionsRegistry; import org.solovyev.android.calculator.jscl.JsclOperation; @@ -55,7 +54,7 @@ import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicLong; -public class CalculatorImpl implements Calculator, CalculatorEventListener { +public class CalculatorImpl implements Calculator { // one minute private static final long PREFERENCE_CHECK_INTERVAL = 1000L * 60L; @@ -83,7 +82,6 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { public CalculatorImpl(@Nonnull Bus bus, @Nonnull Executor eventExecutor) { this.eventExecutor = eventExecutor; bus.register(this); - this.addCalculatorEventListener(this); } @@ -521,18 +519,6 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { } } - @Override - public void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, @Nonnull CalculatorEventType calculatorEventType, @Nullable Object data) { - - switch (calculatorEventType) { - case use_operator: - final Operator operator = (Operator) data; - Locator.getInstance().getKeyboard().buttonPressed(operator.getName()); - break; - - } - } - @Nonnull private Editor getEditor() { return App.getEditor(); diff --git a/app/src/main/java/org/solovyev/android/calculator/Engine.java b/app/src/main/java/org/solovyev/android/calculator/Engine.java index 6e0de85b..22c26cc7 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Engine.java +++ b/app/src/main/java/org/solovyev/android/calculator/Engine.java @@ -32,6 +32,8 @@ import jscl.math.operator.Operator; import jscl.text.*; import org.solovyev.android.Check; import org.solovyev.android.calculator.functions.FunctionsRegistry; +import org.solovyev.android.calculator.operators.OperatorsRegistry; +import org.solovyev.android.calculator.operators.PostfixFunctionsRegistry; import org.solovyev.android.prefs.BooleanPreference; import org.solovyev.android.prefs.IntegerPreference; import org.solovyev.android.prefs.Preference; @@ -54,10 +56,6 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene @Nonnull private final MathEngine mathEngine; - @Nonnull - private final EntitiesRegistry operatorsRegistry; - @Nonnull - private final EntitiesRegistry postfixFunctionsRegistry; @Inject SharedPreferences preferences; @Inject @@ -68,10 +66,14 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene FunctionsRegistry functionsRegistry; @Inject VariablesRegistry variablesRegistry; + @Inject + OperatorsRegistry operatorsRegistry; + @Inject + PostfixFunctionsRegistry postfixFunctionsRegistry; @Nonnull private String multiplicationSign = Preferences.multiplicationSign.getDefaultValue(); - public Engine(@Nonnull MathEngine mathEngine, @Nonnull VariablesRegistry variablesRegistry, @Nonnull FunctionsRegistry functionsRegistry, @Nonnull EntitiesRegistry operatorsRegistry, @Nonnull EntitiesRegistry postfixFunctionsRegistry) { + public Engine(@Nonnull MathEngine mathEngine, @Nonnull VariablesRegistry variablesRegistry, @Nonnull FunctionsRegistry functionsRegistry, @Nonnull OperatorsRegistry operatorsRegistry, @Nonnull PostfixFunctionsRegistry postfixFunctionsRegistry) { this.mathEngine = mathEngine; this.variablesRegistry = variablesRegistry; this.functionsRegistry = functionsRegistry; @@ -85,9 +87,6 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene this.mathEngine.setRoundResult(true); this.mathEngine.setUseGroupingSeparator(true); - - this.operatorsRegistry = new OperatorsRegistry(mathEngine.getOperatorsRegistry()); - this.postfixFunctionsRegistry = new PostfixFunctionsRegistry(mathEngine.getPostfixFunctionsRegistry()); } private static void migratePreference(@Nonnull SharedPreferences preferences, @Nonnull BooleanPreference preference, @Nonnull String oldKey, @Nonnull SharedPreferences.Editor editor) { diff --git a/app/src/main/java/org/solovyev/android/calculator/entities/BaseEntitiesFragment.java b/app/src/main/java/org/solovyev/android/calculator/entities/BaseEntitiesFragment.java index 4b1a018b..6257436b 100644 --- a/app/src/main/java/org/solovyev/android/calculator/entities/BaseEntitiesFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/entities/BaseEntitiesFragment.java @@ -26,6 +26,7 @@ import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.v4.app.FragmentActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.text.ClipboardManager; @@ -36,15 +37,14 @@ import butterknife.Bind; import butterknife.ButterKnife; import com.melnykov.fab.FloatingActionButton; import org.solovyev.android.Check; -import org.solovyev.android.calculator.BaseFragment; -import org.solovyev.android.calculator.CalculatorFragmentType; -import org.solovyev.android.calculator.R; +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 javax.inject.Inject; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; @@ -65,6 +65,8 @@ public abstract class BaseEntitiesFragment extends BaseFra public FloatingActionButton fab; @Bind(R.id.entities_recyclerview) public RecyclerView recyclerView; + @Inject + Keyboard keyboard; private EntitiesAdapter adapter; @Nullable private String category; @@ -73,17 +75,6 @@ public abstract class BaseEntitiesFragment extends BaseFra super(type); } - @Nonnull - public static Bundle createBundleFor(@Nonnull String categoryId) { - final Bundle result = new Bundle(1); - putCategory(result, categoryId); - return result; - } - - public static void putCategory(@Nonnull Bundle bundle, @Nonnull String categoryId) { - bundle.putString(ARG_CATEGORY, categoryId); - } - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -106,7 +97,13 @@ public abstract class BaseEntitiesFragment extends BaseFra return view; } - protected abstract void onClick(@Nonnull E entity); + protected final void onClick(@Nonnull E entity) { + keyboard.buttonPressed(entity.getName()); + final FragmentActivity activity = getActivity(); + if (!(activity instanceof CalculatorActivity)) { + activity.finish(); + } + } @Nonnull private List getEntities(@NonNull String category) { diff --git a/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsFragment.java b/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsFragment.java index b1425b77..14391b9e 100644 --- a/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsFragment.java @@ -50,8 +50,6 @@ public class FunctionsFragment extends BaseEntitiesFragment { @Inject Calculator calculator; @Inject - Keyboard keyboard; - @Inject Bus bus; public FunctionsFragment() { @@ -79,15 +77,6 @@ public class FunctionsFragment extends BaseEntitiesFragment { return view; } - @Override - protected void onClick(@Nonnull Function function) { - keyboard.buttonPressed(function.getName()); - final FragmentActivity activity = getActivity(); - if (activity instanceof FunctionsActivity) { - activity.finish(); - } - } - @Override protected void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull Function function, @NonNull MenuItem.OnMenuItemClickListener listener) { addMenu(menu, R.string.c_use, listener); diff --git a/app/src/main/java/org/solovyev/android/calculator/operators/OperatorsActivity.java b/app/src/main/java/org/solovyev/android/calculator/operators/OperatorsActivity.java index 497e7155..39c3ab50 100644 --- a/app/src/main/java/org/solovyev/android/calculator/operators/OperatorsActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/operators/OperatorsActivity.java @@ -23,12 +23,13 @@ package org.solovyev.android.calculator.operators; import android.os.Bundle; -import org.solovyev.android.calculator.*; +import org.solovyev.android.calculator.BaseActivity; +import org.solovyev.android.calculator.CalculatorFragmentType; +import org.solovyev.android.calculator.R; -import javax.annotation.Nonnull; import javax.annotation.Nullable; -public class OperatorsActivity extends BaseActivity implements CalculatorEventListener { +public class OperatorsActivity extends BaseActivity { public OperatorsActivity() { super(R.layout.main_empty, OperatorsActivity.class.getSimpleName()); @@ -42,13 +43,4 @@ public class OperatorsActivity extends BaseActivity implements CalculatorEventLi addTab(category, CalculatorFragmentType.operators); } } - - @Override - public void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, @Nonnull CalculatorEventType calculatorEventType, @Nullable Object data) { - switch (calculatorEventType) { - case use_operator: - this.finish(); - break; - } - } } diff --git a/app/src/main/java/org/solovyev/android/calculator/operators/OperatorsFragment.java b/app/src/main/java/org/solovyev/android/calculator/operators/OperatorsFragment.java index 14c003fd..e5da7c52 100644 --- a/app/src/main/java/org/solovyev/android/calculator/operators/OperatorsFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/operators/OperatorsFragment.java @@ -33,23 +33,25 @@ import org.solovyev.common.text.Strings; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import javax.inject.Inject; 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(); + @Inject + OperatorsRegistry operatorsRegistry; + @Inject + PostfixFunctionsRegistry postfixFunctionsRegistry; public OperatorsFragment() { super(CalculatorFragmentType.operators); } @Override - protected void onClick(@NonNull Operator operator) { - Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_operator, operator); + protected void inject(@Nonnull AppComponent component) { + super.inject(component); + component.inject(this); } @Nonnull @@ -81,7 +83,7 @@ public class OperatorsFragment extends BaseEntitiesFragment { 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); + onClick(operator); return true; } diff --git a/app/src/main/java/org/solovyev/android/calculator/OperatorsRegistry.java b/app/src/main/java/org/solovyev/android/calculator/operators/OperatorsRegistry.java similarity index 87% rename from app/src/main/java/org/solovyev/android/calculator/OperatorsRegistry.java rename to app/src/main/java/org/solovyev/android/calculator/operators/OperatorsRegistry.java index 35d85204..28980aeb 100644 --- a/app/src/main/java/org/solovyev/android/calculator/OperatorsRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/operators/OperatorsRegistry.java @@ -20,26 +20,25 @@ * Site: http://se.solovyev.org */ -package org.solovyev.android.calculator; +package org.solovyev.android.calculator.operators; import android.support.annotation.NonNull; - +import jscl.JsclMathEngine; +import jscl.math.operator.Operator; import org.solovyev.android.calculator.entities.BaseEntitiesRegistry; import org.solovyev.android.calculator.entities.Category; import org.solovyev.android.calculator.entities.Entities; import org.solovyev.android.calculator.json.Jsonable; -import org.solovyev.android.calculator.operators.OperatorCategory; -import org.solovyev.common.math.MathRegistry; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.inject.Inject; +import javax.inject.Singleton; import java.io.File; import java.util.HashMap; import java.util.Map; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import jscl.math.operator.Operator; - +@Singleton public class OperatorsRegistry extends BaseEntitiesRegistry { @Nonnull @@ -54,8 +53,9 @@ public class OperatorsRegistry extends BaseEntitiesRegistry { substitutes.put("Σ", "sum"); } - public OperatorsRegistry(@Nonnull MathRegistry functionsRegistry) { - super(functionsRegistry, "c_op_description_"); + @Inject + public OperatorsRegistry(@Nonnull JsclMathEngine mathEngine) { + super(mathEngine.getOperatorsRegistry(), "c_op_description_"); } @Nonnull diff --git a/app/src/main/java/org/solovyev/android/calculator/PostfixFunctionsRegistry.java b/app/src/main/java/org/solovyev/android/calculator/operators/PostfixFunctionsRegistry.java similarity index 87% rename from app/src/main/java/org/solovyev/android/calculator/PostfixFunctionsRegistry.java rename to app/src/main/java/org/solovyev/android/calculator/operators/PostfixFunctionsRegistry.java index 845aae47..ba45972b 100644 --- a/app/src/main/java/org/solovyev/android/calculator/PostfixFunctionsRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/operators/PostfixFunctionsRegistry.java @@ -20,26 +20,25 @@ * Site: http://se.solovyev.org */ -package org.solovyev.android.calculator; +package org.solovyev.android.calculator.operators; import android.support.annotation.NonNull; - +import jscl.JsclMathEngine; +import jscl.math.operator.Operator; import org.solovyev.android.calculator.entities.BaseEntitiesRegistry; import org.solovyev.android.calculator.entities.Category; import org.solovyev.android.calculator.entities.Entities; import org.solovyev.android.calculator.json.Jsonable; -import org.solovyev.android.calculator.operators.OperatorCategory; -import org.solovyev.common.math.MathRegistry; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.inject.Inject; +import javax.inject.Singleton; import java.io.File; import java.util.HashMap; import java.util.Map; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import jscl.math.operator.Operator; - +@Singleton public class PostfixFunctionsRegistry extends BaseEntitiesRegistry { @Nonnull @@ -52,8 +51,9 @@ public class PostfixFunctionsRegistry extends BaseEntitiesRegistry { substitutes.put("°", "degree"); } - public PostfixFunctionsRegistry(@Nonnull MathRegistry functionsRegistry) { - super(functionsRegistry, "c_pf_description_"); + @Inject + public PostfixFunctionsRegistry(@Nonnull JsclMathEngine mathEngine) { + super(mathEngine.getPostfixFunctionsRegistry(), "c_pf_description_"); } @Nonnull diff --git a/app/src/main/java/org/solovyev/android/calculator/variables/VariablesFragment.java b/app/src/main/java/org/solovyev/android/calculator/variables/VariablesFragment.java index 69f63100..2b6f358b 100644 --- a/app/src/main/java/org/solovyev/android/calculator/variables/VariablesFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/variables/VariablesFragment.java @@ -53,8 +53,6 @@ public class VariablesFragment extends BaseEntitiesFragment { @Inject Calculator calculator; @Inject - Keyboard keyboard; - @Inject Bus bus; public VariablesFragment() { @@ -98,19 +96,10 @@ public class VariablesFragment extends BaseEntitiesFragment { super.onDestroyView(); } - @Override - protected void onClick(@NonNull IConstant constant) { - keyboard.buttonPressed(constant.getName()); - final FragmentActivity activity = getActivity(); - if (activity instanceof VariablesActivity) { - activity.finish(); - } - } - @Nonnull @Override protected List getEntities() { - final List result = new ArrayList(registry.getEntities()); + final List result = new ArrayList<>(registry.getEntities()); Collections.removeAll(result, new JPredicate() { @Override 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 4b1dccd1..f3fb486f 100644 --- a/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java +++ b/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java @@ -32,6 +32,8 @@ import org.robolectric.fakes.RoboSharedPreferences; import org.solovyev.android.calculator.functions.FunctionsRegistry; import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.language.Languages; +import org.solovyev.android.calculator.operators.OperatorsRegistry; +import org.solovyev.android.calculator.operators.PostfixFunctionsRegistry; import org.solovyev.android.calculator.plot.CalculatorPlotter; import java.io.ByteArrayInputStream;