Operators

This commit is contained in:
serso 2016-01-31 10:15:21 +01:00
parent a67a647add
commit 6d7e75d94b
11 changed files with 61 additions and 103 deletions

View File

@ -2,10 +2,11 @@ package org.solovyev.android.calculator;
import dagger.Component; import dagger.Component;
import org.solovyev.android.calculator.functions.EditFunctionFragment; 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.BaseHistoryFragment;
import org.solovyev.android.calculator.history.EditHistoryFragment; 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.onscreen.CalculatorOnscreenService;
import org.solovyev.android.calculator.operators.OperatorsFragment;
import org.solovyev.android.calculator.variables.EditVariableFragment; import org.solovyev.android.calculator.variables.EditVariableFragment;
import org.solovyev.android.calculator.variables.VariablesFragment; import org.solovyev.android.calculator.variables.VariablesFragment;
@ -25,4 +26,5 @@ public interface AppComponent {
void inject(EditHistoryFragment fragment); void inject(EditHistoryFragment fragment);
void inject(FunctionsFragment fragment); void inject(FunctionsFragment fragment);
void inject(VariablesFragment fragment); void inject(VariablesFragment fragment);
void inject(OperatorsFragment fragment);
} }

View File

@ -33,7 +33,6 @@ import jscl.NumeralBase;
import jscl.NumeralBaseException; import jscl.NumeralBaseException;
import jscl.math.Generic; import jscl.math.Generic;
import jscl.math.function.IConstant; import jscl.math.function.IConstant;
import jscl.math.operator.Operator;
import jscl.text.ParseInterruptedException; import jscl.text.ParseInterruptedException;
import org.solovyev.android.calculator.functions.FunctionsRegistry; import org.solovyev.android.calculator.functions.FunctionsRegistry;
import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.jscl.JsclOperation;
@ -55,7 +54,7 @@ import java.util.concurrent.Executor;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
public class CalculatorImpl implements Calculator, CalculatorEventListener { public class CalculatorImpl implements Calculator {
// one minute // one minute
private static final long PREFERENCE_CHECK_INTERVAL = 1000L * 60L; 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) { public CalculatorImpl(@Nonnull Bus bus, @Nonnull Executor eventExecutor) {
this.eventExecutor = eventExecutor; this.eventExecutor = eventExecutor;
bus.register(this); 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 @Nonnull
private Editor getEditor() { private Editor getEditor() {
return App.getEditor(); return App.getEditor();

View File

@ -32,6 +32,8 @@ import jscl.math.operator.Operator;
import jscl.text.*; import jscl.text.*;
import org.solovyev.android.Check; import org.solovyev.android.Check;
import org.solovyev.android.calculator.functions.FunctionsRegistry; 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.BooleanPreference;
import org.solovyev.android.prefs.IntegerPreference; import org.solovyev.android.prefs.IntegerPreference;
import org.solovyev.android.prefs.Preference; import org.solovyev.android.prefs.Preference;
@ -54,10 +56,6 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
@Nonnull @Nonnull
private final MathEngine mathEngine; private final MathEngine mathEngine;
@Nonnull
private final EntitiesRegistry<Operator> operatorsRegistry;
@Nonnull
private final EntitiesRegistry<Operator> postfixFunctionsRegistry;
@Inject @Inject
SharedPreferences preferences; SharedPreferences preferences;
@Inject @Inject
@ -68,10 +66,14 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
FunctionsRegistry functionsRegistry; FunctionsRegistry functionsRegistry;
@Inject @Inject
VariablesRegistry variablesRegistry; VariablesRegistry variablesRegistry;
@Inject
OperatorsRegistry operatorsRegistry;
@Inject
PostfixFunctionsRegistry postfixFunctionsRegistry;
@Nonnull @Nonnull
private String multiplicationSign = Preferences.multiplicationSign.getDefaultValue(); private String multiplicationSign = Preferences.multiplicationSign.getDefaultValue();
public Engine(@Nonnull MathEngine mathEngine, @Nonnull VariablesRegistry variablesRegistry, @Nonnull FunctionsRegistry functionsRegistry, @Nonnull EntitiesRegistry<Operator> operatorsRegistry, @Nonnull EntitiesRegistry<Operator> postfixFunctionsRegistry) { public Engine(@Nonnull MathEngine mathEngine, @Nonnull VariablesRegistry variablesRegistry, @Nonnull FunctionsRegistry functionsRegistry, @Nonnull OperatorsRegistry operatorsRegistry, @Nonnull PostfixFunctionsRegistry postfixFunctionsRegistry) {
this.mathEngine = mathEngine; this.mathEngine = mathEngine;
this.variablesRegistry = variablesRegistry; this.variablesRegistry = variablesRegistry;
this.functionsRegistry = functionsRegistry; this.functionsRegistry = functionsRegistry;
@ -85,9 +87,6 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
this.mathEngine.setRoundResult(true); this.mathEngine.setRoundResult(true);
this.mathEngine.setUseGroupingSeparator(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) { private static void migratePreference(@Nonnull SharedPreferences preferences, @Nonnull BooleanPreference preference, @Nonnull String oldKey, @Nonnull SharedPreferences.Editor editor) {

View File

@ -26,6 +26,7 @@ import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.text.ClipboardManager; import android.text.ClipboardManager;
@ -36,15 +37,14 @@ import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import com.melnykov.fab.FloatingActionButton; import com.melnykov.fab.FloatingActionButton;
import org.solovyev.android.Check; import org.solovyev.android.Check;
import org.solovyev.android.calculator.BaseFragment; import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.CalculatorFragmentType;
import org.solovyev.android.calculator.R;
import org.solovyev.android.views.llm.DividerItemDecoration; import org.solovyev.android.views.llm.DividerItemDecoration;
import org.solovyev.common.math.MathEntity; import org.solovyev.common.math.MathEntity;
import org.solovyev.common.text.Strings; import org.solovyev.common.text.Strings;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.inject.Inject;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Iterator; import java.util.Iterator;
@ -65,6 +65,8 @@ public abstract class BaseEntitiesFragment<E extends MathEntity> extends BaseFra
public FloatingActionButton fab; public FloatingActionButton fab;
@Bind(R.id.entities_recyclerview) @Bind(R.id.entities_recyclerview)
public RecyclerView recyclerView; public RecyclerView recyclerView;
@Inject
Keyboard keyboard;
private EntitiesAdapter adapter; private EntitiesAdapter adapter;
@Nullable @Nullable
private String category; private String category;
@ -73,17 +75,6 @@ public abstract class BaseEntitiesFragment<E extends MathEntity> extends BaseFra
super(type); 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 @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -106,7 +97,13 @@ public abstract class BaseEntitiesFragment<E extends MathEntity> extends BaseFra
return view; 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 @Nonnull
private List<E> getEntities(@NonNull String category) { private List<E> getEntities(@NonNull String category) {

View File

@ -50,8 +50,6 @@ public class FunctionsFragment extends BaseEntitiesFragment<Function> {
@Inject @Inject
Calculator calculator; Calculator calculator;
@Inject @Inject
Keyboard keyboard;
@Inject
Bus bus; Bus bus;
public FunctionsFragment() { public FunctionsFragment() {
@ -79,15 +77,6 @@ public class FunctionsFragment extends BaseEntitiesFragment<Function> {
return view; return view;
} }
@Override
protected void onClick(@Nonnull Function function) {
keyboard.buttonPressed(function.getName());
final FragmentActivity activity = getActivity();
if (activity instanceof FunctionsActivity) {
activity.finish();
}
}
@Override @Override
protected void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull Function function, @NonNull MenuItem.OnMenuItemClickListener listener) { protected void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull Function function, @NonNull MenuItem.OnMenuItemClickListener listener) {
addMenu(menu, R.string.c_use, listener); addMenu(menu, R.string.c_use, listener);

View File

@ -23,12 +23,13 @@
package org.solovyev.android.calculator.operators; package org.solovyev.android.calculator.operators;
import android.os.Bundle; 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; import javax.annotation.Nullable;
public class OperatorsActivity extends BaseActivity implements CalculatorEventListener { public class OperatorsActivity extends BaseActivity {
public OperatorsActivity() { public OperatorsActivity() {
super(R.layout.main_empty, OperatorsActivity.class.getSimpleName()); super(R.layout.main_empty, OperatorsActivity.class.getSimpleName());
@ -42,13 +43,4 @@ public class OperatorsActivity extends BaseActivity implements CalculatorEventLi
addTab(category, CalculatorFragmentType.operators); 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;
}
}
} }

View File

@ -33,23 +33,25 @@ import org.solovyev.common.text.Strings;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.inject.Inject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class OperatorsFragment extends BaseEntitiesFragment<Operator> { public class OperatorsFragment extends BaseEntitiesFragment<Operator> {
@NonNull @Inject
private final EntitiesRegistry<Operator> operatorsRegistry = Locator.getInstance().getEngine().getOperatorsRegistry(); OperatorsRegistry operatorsRegistry;
@NonNull @Inject
private final EntitiesRegistry<Operator> postfixFunctionsRegistry = Locator.getInstance().getEngine().getPostfixFunctionsRegistry(); PostfixFunctionsRegistry postfixFunctionsRegistry;
public OperatorsFragment() { public OperatorsFragment() {
super(CalculatorFragmentType.operators); super(CalculatorFragmentType.operators);
} }
@Override @Override
protected void onClick(@NonNull Operator operator) { protected void inject(@Nonnull AppComponent component) {
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_operator, operator); super.inject(component);
component.inject(this);
} }
@Nonnull @Nonnull
@ -81,7 +83,7 @@ public class OperatorsFragment extends BaseEntitiesFragment<Operator> {
protected boolean onMenuItemClicked(@Nonnull MenuItem item, @Nonnull Operator operator) { protected boolean onMenuItemClicked(@Nonnull MenuItem item, @Nonnull Operator operator) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.string.c_use: case R.string.c_use:
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_operator, operator); onClick(operator);
return true; return true;
} }

View File

@ -20,26 +20,25 @@
* Site: http://se.solovyev.org * Site: http://se.solovyev.org
*/ */
package org.solovyev.android.calculator; package org.solovyev.android.calculator.operators;
import android.support.annotation.NonNull; 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.BaseEntitiesRegistry;
import org.solovyev.android.calculator.entities.Category; import org.solovyev.android.calculator.entities.Category;
import org.solovyev.android.calculator.entities.Entities; import org.solovyev.android.calculator.entities.Entities;
import org.solovyev.android.calculator.json.Jsonable; 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.io.File;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.annotation.Nonnull; @Singleton
import javax.annotation.Nullable;
import jscl.math.operator.Operator;
public class OperatorsRegistry extends BaseEntitiesRegistry<Operator> { public class OperatorsRegistry extends BaseEntitiesRegistry<Operator> {
@Nonnull @Nonnull
@ -54,8 +53,9 @@ public class OperatorsRegistry extends BaseEntitiesRegistry<Operator> {
substitutes.put("Σ", "sum"); substitutes.put("Σ", "sum");
} }
public OperatorsRegistry(@Nonnull MathRegistry<Operator> functionsRegistry) { @Inject
super(functionsRegistry, "c_op_description_"); public OperatorsRegistry(@Nonnull JsclMathEngine mathEngine) {
super(mathEngine.getOperatorsRegistry(), "c_op_description_");
} }
@Nonnull @Nonnull

View File

@ -20,26 +20,25 @@
* Site: http://se.solovyev.org * Site: http://se.solovyev.org
*/ */
package org.solovyev.android.calculator; package org.solovyev.android.calculator.operators;
import android.support.annotation.NonNull; 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.BaseEntitiesRegistry;
import org.solovyev.android.calculator.entities.Category; import org.solovyev.android.calculator.entities.Category;
import org.solovyev.android.calculator.entities.Entities; import org.solovyev.android.calculator.entities.Entities;
import org.solovyev.android.calculator.json.Jsonable; 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.io.File;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.annotation.Nonnull; @Singleton
import javax.annotation.Nullable;
import jscl.math.operator.Operator;
public class PostfixFunctionsRegistry extends BaseEntitiesRegistry<Operator> { public class PostfixFunctionsRegistry extends BaseEntitiesRegistry<Operator> {
@Nonnull @Nonnull
@ -52,8 +51,9 @@ public class PostfixFunctionsRegistry extends BaseEntitiesRegistry<Operator> {
substitutes.put("°", "degree"); substitutes.put("°", "degree");
} }
public PostfixFunctionsRegistry(@Nonnull MathRegistry<Operator> functionsRegistry) { @Inject
super(functionsRegistry, "c_pf_description_"); public PostfixFunctionsRegistry(@Nonnull JsclMathEngine mathEngine) {
super(mathEngine.getPostfixFunctionsRegistry(), "c_pf_description_");
} }
@Nonnull @Nonnull

View File

@ -53,8 +53,6 @@ public class VariablesFragment extends BaseEntitiesFragment<IConstant> {
@Inject @Inject
Calculator calculator; Calculator calculator;
@Inject @Inject
Keyboard keyboard;
@Inject
Bus bus; Bus bus;
public VariablesFragment() { public VariablesFragment() {
@ -98,19 +96,10 @@ public class VariablesFragment extends BaseEntitiesFragment<IConstant> {
super.onDestroyView(); super.onDestroyView();
} }
@Override
protected void onClick(@NonNull IConstant constant) {
keyboard.buttonPressed(constant.getName());
final FragmentActivity activity = getActivity();
if (activity instanceof VariablesActivity) {
activity.finish();
}
}
@Nonnull @Nonnull
@Override @Override
protected List<IConstant> getEntities() { protected List<IConstant> getEntities() {
final List<IConstant> result = new ArrayList<IConstant>(registry.getEntities()); final List<IConstant> result = new ArrayList<>(registry.getEntities());
Collections.removeAll(result, new JPredicate<IConstant>() { Collections.removeAll(result, new JPredicate<IConstant>() {
@Override @Override

View File

@ -32,6 +32,8 @@ import org.robolectric.fakes.RoboSharedPreferences;
import org.solovyev.android.calculator.functions.FunctionsRegistry; import org.solovyev.android.calculator.functions.FunctionsRegistry;
import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.android.calculator.language.Languages; 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 org.solovyev.android.calculator.plot.CalculatorPlotter;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;