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 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);
}

View File

@ -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();

View File

@ -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<Operator> operatorsRegistry;
@Nonnull
private final EntitiesRegistry<Operator> 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<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.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) {

View File

@ -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<E extends MathEntity> 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<E extends MathEntity> 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<E extends MathEntity> 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<E> getEntities(@NonNull String category) {

View File

@ -50,8 +50,6 @@ public class FunctionsFragment extends BaseEntitiesFragment<Function> {
@Inject
Calculator calculator;
@Inject
Keyboard keyboard;
@Inject
Bus bus;
public FunctionsFragment() {
@ -79,15 +77,6 @@ public class FunctionsFragment extends BaseEntitiesFragment<Function> {
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);

View File

@ -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;
}
}
}

View File

@ -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<Operator> {
@NonNull
private final EntitiesRegistry<Operator> operatorsRegistry = Locator.getInstance().getEngine().getOperatorsRegistry();
@NonNull
private final EntitiesRegistry<Operator> 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<Operator> {
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;
}

View File

@ -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<Operator> {
@Nonnull
@ -54,8 +53,9 @@ public class OperatorsRegistry extends BaseEntitiesRegistry<Operator> {
substitutes.put("Σ", "sum");
}
public OperatorsRegistry(@Nonnull MathRegistry<Operator> functionsRegistry) {
super(functionsRegistry, "c_op_description_");
@Inject
public OperatorsRegistry(@Nonnull JsclMathEngine mathEngine) {
super(mathEngine.getOperatorsRegistry(), "c_op_description_");
}
@Nonnull

View File

@ -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<Operator> {
@Nonnull
@ -52,8 +51,9 @@ public class PostfixFunctionsRegistry extends BaseEntitiesRegistry<Operator> {
substitutes.put("°", "degree");
}
public PostfixFunctionsRegistry(@Nonnull MathRegistry<Operator> functionsRegistry) {
super(functionsRegistry, "c_pf_description_");
@Inject
public PostfixFunctionsRegistry(@Nonnull JsclMathEngine mathEngine) {
super(mathEngine.getPostfixFunctionsRegistry(), "c_pf_description_");
}
@Nonnull

View File

@ -53,8 +53,6 @@ public class VariablesFragment extends BaseEntitiesFragment<IConstant> {
@Inject
Calculator calculator;
@Inject
Keyboard keyboard;
@Inject
Bus bus;
public VariablesFragment() {
@ -98,19 +96,10 @@ public class VariablesFragment extends BaseEntitiesFragment<IConstant> {
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<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>() {
@Override

View File

@ -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;