EntitiesFragment

This commit is contained in:
serso 2016-01-17 18:45:20 +01:00
parent c0710801cd
commit b5600114ed
31 changed files with 729 additions and 1029 deletions

View File

@ -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<T extends MathEntity, P extends MathPersistenceEntity> implements CalculatorMathRegistry<T> {
public abstract class BaseEntitiesRegistry<T extends MathEntity, P extends MathPersistenceEntity> implements EntitiesRegistry<T> {
@Nonnull
private final MathRegistry<T> mathRegistry;
@ -49,7 +43,7 @@ public abstract class AbstractCalculatorMathRegistry<T extends MathEntity, P ext
@Nonnull
private final MathEntityDao<P> mathEntityDao;
protected AbstractCalculatorMathRegistry(@Nonnull MathRegistry<T> mathRegistry,
protected BaseEntitiesRegistry(@Nonnull MathRegistry<T> mathRegistry,
@Nonnull String prefix,
@Nonnull MathEntityDao<P> mathEntityDao) {
this.mathRegistry = mathRegistry;
@ -63,13 +57,13 @@ public abstract class AbstractCalculatorMathRegistry<T extends MathEntity, P ext
@Nullable
@Override
public String getDescription(@Nonnull String mathEntityName) {
public String getDescription(@Nonnull String name) {
final String stringName;
final Map<String, String> 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;
}

View File

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

View File

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

View File

@ -39,13 +39,13 @@ public interface CalculatorEngine {
void softReset();
@Nonnull
CalculatorMathRegistry<IConstant> getVarsRegistry();
EntitiesRegistry<IConstant> getVarsRegistry();
@Nonnull
CalculatorMathRegistry<Function> getFunctionsRegistry();
EntitiesRegistry<Function> getFunctionsRegistry();
@Nonnull
CalculatorMathRegistry<Operator> getOperatorsRegistry();
EntitiesRegistry<Operator> getOperatorsRegistry();
@Nonnull
CalculatorMathRegistry<Operator> getPostfixFunctionsRegistry();
EntitiesRegistry<Operator> getPostfixFunctionsRegistry();
@Nonnull
CalculatorMathEngine getMathEngine();
@Deprecated

View File

@ -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<IConstant> varsRegistry;
private final EntitiesRegistry<IConstant> varsRegistry;
@Nonnull
private final CalculatorMathRegistry<Function> functionsRegistry;
private final EntitiesRegistry<Function> functionsRegistry;
@Nonnull
private final CalculatorMathRegistry<Operator> operatorsRegistry;
private final EntitiesRegistry<Operator> operatorsRegistry;
@Nonnull
private final CalculatorMathRegistry<Operator> postfixFunctionsRegistry;
private final EntitiesRegistry<Operator> 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<IConstant> varsRegistry,
@Nonnull CalculatorMathRegistry<Function> functionsRegistry,
@Nonnull CalculatorMathRegistry<Operator> operatorsRegistry,
@Nonnull CalculatorMathRegistry<Operator> postfixFunctionsRegistry,
@Nonnull EntitiesRegistry<IConstant> varsRegistry,
@Nonnull EntitiesRegistry<Function> functionsRegistry,
@Nonnull EntitiesRegistry<Operator> operatorsRegistry,
@Nonnull EntitiesRegistry<Operator> postfixFunctionsRegistry,
@Nullable Object lock) {
this.engine = engine;
@ -118,25 +117,25 @@ public class CalculatorEngineImpl implements CalculatorEngine {
*/
@Nonnull
@Override
public CalculatorMathRegistry<IConstant> getVarsRegistry() {
public EntitiesRegistry<IConstant> getVarsRegistry() {
return this.varsRegistry;
}
@Nonnull
@Override
public CalculatorMathRegistry<Function> getFunctionsRegistry() {
public EntitiesRegistry<Function> getFunctionsRegistry() {
return this.functionsRegistry;
}
@Nonnull
@Override
public CalculatorMathRegistry<Operator> getOperatorsRegistry() {
public EntitiesRegistry<Operator> getOperatorsRegistry() {
return this.operatorsRegistry;
}
@Nonnull
@Override
public CalculatorMathRegistry<Operator> getPostfixFunctionsRegistry() {
public EntitiesRegistry<Operator> 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) {

View File

@ -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),

View File

@ -498,7 +498,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
if (TextUtils.isEmpty(text)) {
return;
}
final CalculatorMathRegistry<IConstant> varsRegistry = Locator.getInstance().getEngine().getVarsRegistry();
final EntitiesRegistry<IConstant> 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();

View File

@ -37,7 +37,7 @@ import jscl.math.operator.Operator;
* Date: 11/17/11
* Time: 11:29 PM
*/
public class CalculatorOperatorsMathRegistry extends AbstractCalculatorMathRegistry<Operator, MathPersistenceEntity> {
public class CalculatorOperatorsMathRegistry extends BaseEntitiesRegistry<Operator, MathPersistenceEntity> {
@Nonnull
private static final Map<String, String> substitutes = new HashMap<String, String>();

View File

@ -37,7 +37,7 @@ import jscl.math.operator.Operator;
* Date: 11/19/11
* Time: 1:48 PM
*/
public class CalculatorPostfixFunctionsRegistry extends AbstractCalculatorMathRegistry<Operator, MathPersistenceEntity> {
public class CalculatorPostfixFunctionsRegistry extends BaseEntitiesRegistry<Operator, MathPersistenceEntity> {
@Nonnull
private static final Map<String, String> substitutes = new HashMap<String, String>();

View File

@ -42,7 +42,7 @@ import jscl.math.function.IConstant;
* Date: 9/29/11
* Time: 4:57 PM
*/
public class CalculatorVarsRegistry extends AbstractCalculatorMathRegistry<IConstant, Var> {
public class CalculatorVarsRegistry extends BaseEntitiesRegistry<IConstant, Var> {
@Nonnull
public static final String ANS = "ans";
@ -63,7 +63,7 @@ public class CalculatorVarsRegistry extends AbstractCalculatorMathRegistry<ICons
super(mathRegistry, "c_var_description_", mathEntityDao);
}
public static <T extends MathEntity> void saveVariable(@Nonnull CalculatorMathRegistry<T> registry,
public static <T extends MathEntity> void saveVariable(@Nonnull EntitiesRegistry<T> registry,
@Nonnull MathEntityBuilder<? extends T> builder,
@Nullable T editedInstance,
@Nonnull Object source, boolean save) {
@ -131,12 +131,12 @@ public class CalculatorVarsRegistry extends AbstractCalculatorMathRegistry<ICons
}
@Override
public String getDescription(@Nonnull String mathEntityName) {
final IConstant var = get(mathEntityName);
public String getDescription(@Nonnull String name) {
final IConstant var = get(name);
if (var != null && !var.isSystem()) {
return var.getDescription();
} else {
return super.getDescription(mathEntityName);
return super.getDescription(name);
}
}

View File

@ -28,18 +28,13 @@ import org.solovyev.common.math.MathRegistry;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* User: serso
* Date: 10/30/11
* Time: 1:02 AM
*/
public interface CalculatorMathRegistry<T extends MathEntity> extends MathRegistry<T> {
public interface EntitiesRegistry<E extends MathEntity> extends MathRegistry<E> {
@Nullable
String getDescription(@Nonnull String mathEntityName);
String getDescription(@Nonnull String name);
@Nullable
String getCategory(@Nonnull T mathEntity);
String getCategory(@Nonnull E entity);
void load();

View File

@ -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<Function, AFunction> {
public class FunctionsRegistry extends BaseEntitiesRegistry<Function, AFunction> {
@Nonnull
private static final Map<String, String> substitutes = new HashMap<String, String>();
@ -58,12 +51,12 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis
substitutes.put("", "sqrt");
}
public CalculatorFunctionsMathRegistry(@Nonnull MathRegistry<Function> functionsRegistry,
public FunctionsRegistry(@Nonnull MathRegistry<Function> functionsRegistry,
@Nonnull MathEntityDao<AFunction> mathEntityDao) {
super(functionsRegistry, FUNCTION_DESCRIPTION_PREFIX, mathEntityDao);
}
public static void saveFunction(@Nonnull CalculatorMathRegistry<Function> registry,
public static void saveFunction(@Nonnull EntitiesRegistry<Function> registry,
@Nonnull MathEntityBuilder<? extends Function> 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;

View File

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

View File

@ -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<Function> mathRegistry;
private final EntitiesRegistry<Function> mathRegistry;
public FunctionEditorSaver(@Nonnull AFunction.Builder builder,
@Nullable IFunction editedInstance,
@Nonnull View view,
@Nonnull CalculatorMathRegistry<Function> registry,
@Nonnull EntitiesRegistry<Function> 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) {

View File

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

View File

@ -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<T extends MathEntity> 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<T> 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<T> 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<LabeledMenuItem<T>> menuItems = getMenuItemsOnLongClick(item);
if (!menuItems.isEmpty()) {
final ContextMenuBuilder<LabeledMenuItem<T>, T> menuBuilder = ContextMenuBuilder.newInstance(AbstractMathEntityListFragment.this.getActivity(), "math-entity-menu", ListContextMenu.newInstance(menuItems));
menuBuilder.build(item).show();
}
return true;
}
});
}
@Nullable
protected abstract AMenuItem<T> getOnClickAction();
@Override
public void onDestroyView() {
ui.onDestroyView(this);
super.onDestroyView();
}
@Override
public void onDestroy() {
ui.onDestroy(this);
super.onDestroy();
}
@Nonnull
protected abstract List<LabeledMenuItem<T>> 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<T>(getDescriptionGetter(), this.getActivity(), getMathEntitiesByCategory());
setListAdapter(adapter);
sort();
}
@Nonnull
private List<T> getMathEntitiesByCategory() {
final List<T> result = getMathEntities();
new Filter<T>(new JPredicate<T>() {
@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<T> getMathEntities();
@Nullable
abstract String getMathEntityCategory(@Nonnull T t);
protected void sort() {
final MathEntityArrayAdapter<T> localAdapter = adapter;
if (localAdapter != null) {
localAdapter.sort(new Comparator<T>() {
@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<T> 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<T extends MathEntity> extends ArrayAdapter<T> {
@Nonnull
private final MathEntityDescriptionGetter descriptionGetter;
private MathEntityArrayAdapter(@Nonnull MathEntityDescriptionGetter descriptionGetter,
@Nonnull Context context,
@Nonnull List<T> 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);
}
}
}

View File

@ -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<E extends MathEntity> 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<E> getEntities(@NonNull String category) {
Check.isNotEmpty(category);
final List<E> entities = getEntities();
final Iterator<E> 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<E> getEntities();
@Nullable
abstract String getCategory(@Nonnull E e);
protected void sort() {
/*
final EntitiesAdapter localAdapter = adapter;
if (localAdapter != null) {
localAdapter.sort(new Comparator<E>() {
@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<EntityViewHolder> {
@Nonnull
private final LayoutInflater inflater;
@Nonnull
private final List<E> list;
private EntitiesAdapter(@Nonnull Context context,
@Nonnull List<E> 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);
}
}
}

View File

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

View File

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

View File

@ -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<Operator> {
public CalculatorOperatorsFragment() {
super(CalculatorFragmentType.operators);
}
@Override
protected AMenuItem<Operator> getOnClickAction() {
return LongClickMenuItem.use;
}
@Nonnull
@Override
protected List<LabeledMenuItem<Operator>> getMenuItemsOnLongClick(@Nonnull Operator item) {
final List<LabeledMenuItem<Operator>> result = new ArrayList<LabeledMenuItem<Operator>>(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<Operator> getMathEntities() {
final List<Operator> result = new ArrayList<Operator>();
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<Operator> {
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);
}
}
}

View File

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

View File

@ -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<Function> {
/**
* User: serso
* Date: 10/29/11
* Time: 4:55 PM
*/
public class CalculatorFunctionsFragment extends AbstractMathEntityListFragment<Function> {
public static final String EXTRA_FUNCTION = "function";
@NonNull
private final EntitiesRegistry<Function> 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<Function> getOnClickAction() {
return LongClickMenuItem.use;
protected void onClick(@NonNull @Nonnull Function function) {
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_function, function);
}
@Override
protected void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull Function function, @NonNull MenuItem.OnMenuItemClickListener listener) {
addMenu(menu, R.string.c_use, listener);
final EntitiesRegistry<Function> functionsRegistry = registry;
if (!Strings.isEmpty(functionsRegistry.getDescription(function.getName()))) {
addMenu(menu, R.string.c_copy_description, listener);
}
if (!function.isSystem()) {
addMenu(menu, R.string.c_edit, listener);
addMenu(menu, R.string.c_remove, listener);
}
}
@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 List<LabeledMenuItem<Function>> getMenuItemsOnLongClick(@Nonnull Function item) {
List<LabeledMenuItem<Function>> result = new ArrayList<LabeledMenuItem<Function>>(Arrays.asList(LongClickMenuItem.values()));
final CalculatorMathRegistry<Function> functionsRegistry = Locator.getInstance().getEngine().getFunctionsRegistry();
if (Strings.isEmpty(functionsRegistry.getDescription(item.getName()))) {
result.remove(LongClickMenuItem.copy_description);
}
final Function function = functionsRegistry.get(item.getName());
if (function == null || function.isSystem()) {
result.remove(LongClickMenuItem.edit);
result.remove(LongClickMenuItem.remove);
}
return result;
}
@Nonnull
@Override
protected MathEntityDescriptionGetter getDescriptionGetter() {
return new MathEntityDescriptionGetterImpl(Locator.getInstance().getEngine().getFunctionsRegistry());
}
@Nonnull
@Override
protected List<Function> getMathEntities() {
return new ArrayList<Function>(Locator.getInstance().getEngine().getFunctionsRegistry().getEntities());
protected List<Function> 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<Function> 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<Function> {
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);
}
}
}

View File

@ -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<T extends MathEntity> implements View.OnClickList
private final boolean confirmed;
@Nonnull
private final CalculatorMathRegistry<? super T> varsRegistry;
private final EntitiesRegistry<? super T> varsRegistry;
@Nonnull
private final Object source;
@Nonnull
@ -75,7 +75,7 @@ public class MathEntityRemover<T extends MathEntity> implements View.OnClickList
private MathEntityRemover(@Nonnull T mathEntity,
@Nullable DialogInterface.OnClickListener callbackOnCancel,
boolean confirmed,
@Nonnull CalculatorMathRegistry<? super T> varsRegistry,
@Nonnull EntitiesRegistry<? super T> varsRegistry,
@Nonnull Context context,
@Nonnull Object source,
@Nonnull Params params) {

View File

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

View File

@ -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<T extends MathEntity> implements View.OnClickListene
private final T editedInstance;
@Nonnull
private final CalculatorMathRegistry<T> mathRegistry;
private final EntitiesRegistry<T> mathRegistry;
@Nonnull
private final Object source;
@ -68,7 +68,7 @@ public class VarEditorSaver<T extends MathEntity> implements View.OnClickListene
public VarEditorSaver(@Nonnull MathEntityBuilder<? extends T> varBuilder,
@Nullable T editedInstance,
@Nonnull View editView,
@Nonnull CalculatorMathRegistry<T> mathRegistry,
@Nonnull EntitiesRegistry<T> mathRegistry,
@Nonnull Object source) {
this.varBuilder = varBuilder;
this.editedInstance = editedInstance;
@ -130,7 +130,7 @@ public class VarEditorSaver<T extends MathEntity> 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);

View File

@ -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<IConstant> {
public class VarsFragment extends BaseEntitiesFragment<IConstant> {
public static final String CREATE_VAR_EXTRA_STRING = "create_var";
@NonNull
private final EntitiesRegistry<IConstant> registry = Locator.getInstance().getEngine().getVarsRegistry();
public CalculatorVarsFragment() {
public VarsFragment() {
super(CalculatorFragmentType.variables);
}
@ -102,10 +85,9 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
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) {
@ -115,35 +97,8 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
}
@Override
protected AMenuItem<IConstant> getOnClickAction() {
return LongClickMenuItem.use;
}
@Nonnull
@Override
protected List<LabeledMenuItem<IConstant>> getMenuItemsOnLongClick(@Nonnull IConstant item) {
final List<LabeledMenuItem<IConstant>> result = new ArrayList<LabeledMenuItem<IConstant>>(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<ICons
@Nonnull
@Override
protected List<IConstant> getMathEntities() {
final List<IConstant> result = new ArrayList<IConstant>(Locator.getInstance().getEngine().getVarsRegistry().getEntities());
protected List<IConstant> getEntities() {
final List<IConstant> result = new ArrayList<IConstant>(registry.getEntities());
Collections.removeAll(result, new JPredicate<IConstant>() {
@Override
@ -167,8 +122,8 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
}
@Override
protected String getMathEntityCategory(@Nonnull IConstant var) {
return Locator.getInstance().getEngine().getVarsRegistry().getCategory(var);
protected String getCategory(@Nonnull IConstant var) {
return registry.getCategory(var);
}
@Override
@ -190,6 +145,52 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
}
}
@Override
protected void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull IConstant constant, @Nonnull MenuItem.OnMenuItemClickListener listener) {
addMenu(menu, R.string.c_use, listener);
if (!constant.isSystem()) {
addMenu(menu, R.string.c_edit, listener);
addMenu(menu, R.string.c_remove, listener);
}
if (!Strings.isEmpty(constant.getValue())) {
addMenu(menu, R.string.c_copy_value, listener);
}
if (!Strings.isEmpty(registry.getDescription(constant.getName()))) {
addMenu(menu, R.string.c_copy_description, listener);
}
}
@Override
protected boolean onMenuItemClicked(@Nonnull MenuItem item, @Nonnull IConstant constant) {
FragmentActivity activity = getActivity();
switch (item.getItemId()) {
case R.string.c_use:
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_constant, constant);
return true;
case R.string.c_edit:
VarEditDialogFragment.showDialog(VarEditDialogFragment.Input.newFromConstant(constant), activity.getSupportFragmentManager());
return true;
case R.string.c_remove:
MathEntityRemover.newConstantRemover(constant, null, activity, activity).showConfirmationDialog();
return true;
case R.string.c_copy_value:
final String value = constant.getValue();
if (!Strings.isEmpty(value)) {
Locator.getInstance().getClipboard().setText(value);
}
return true;
case R.string.c_copy_description:
final String description = registry.getDescription(constant.getName());
if (!Strings.isEmpty(description)) {
Locator.getInstance().getClipboard().setText(description);
}
return true;
}
return false;
}
private void processConstantRemoved(@Nonnull final IConstant constant) {
if (this.isInCategory(constant)) {
getUiHandler().post(new Runnable() {
@ -228,67 +229,10 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
}
}
/*
**********************************************************************
*
* STATIC
*
**********************************************************************
*/
private static enum LongClickMenuItem implements LabeledMenuItem<IConstant> {
use(R.string.c_use) {
@Nullable
@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;
protected String getDescription(@NonNull IConstant constant) {
return registry.getDescription(constant.getName());
}
@Nonnull
@Override
public String getCaption(@Nonnull Context context) {
return context.getString(captionId);
}
}
}

View File

@ -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,14 +68,6 @@ public class AFunction implements IFunction, MathPersistenceEntity, Serializable
@Nonnull
private String description = "";
/*
**********************************************************************
*
* CONSTRUCTORS
*
**********************************************************************
*/
public AFunction() {
}
@ -106,14 +83,6 @@ public class AFunction implements IFunction, MathPersistenceEntity, Serializable
return result;
}
/*
**********************************************************************
*
* METHODS
*
**********************************************************************
*/
private static void copy(@Nonnull AFunction target,
@Nonnull IFunction source) {
target.name = source.getName();
@ -123,7 +92,7 @@ public class AFunction implements IFunction, MathPersistenceEntity, Serializable
if (source.isIdDefined()) {
target.id = source.getId();
}
target.parameterNames = new ArrayList<String>(source.getParameterNames());
target.parameterNames = new ArrayList<>(source.getParameterNames());
}
@Override
@ -149,14 +118,6 @@ public class AFunction implements IFunction, MathPersistenceEntity, Serializable
'}';
}
/*
**********************************************************************
*
* GETTERS/SETTERS
*
**********************************************************************
*/
@Nonnull
public String getName() {
return name;
@ -211,14 +172,6 @@ public class AFunction implements IFunction, MathPersistenceEntity, Serializable
this.parameterNames = parameterNames;
}
/*
**********************************************************************
*
* STATIC
*
**********************************************************************
*/
public static class Builder implements MathEntityBuilder<AFunction> {
@Nonnull

View File

@ -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<Var>("org.solovyev.android.calculator.CalculatorModel_vars", application, Vars.class)),
new CalculatorFunctionsMathRegistry(engine.getFunctionsRegistry(), new AndroidMathEntityDao<AFunction>("org.solovyev.android.calculator.CalculatorModel_functions", application, Functions.class)),
new FunctionsRegistry(engine.getFunctionsRegistry(), new AndroidMathEntityDao<AFunction>("org.solovyev.android.calculator.CalculatorModel_functions", application, Functions.class)),
new CalculatorOperatorsMathRegistry(engine.getOperatorsRegistry(), new AndroidMathEntityDao<MathPersistenceEntity>(null, application, null)),
new CalculatorPostfixFunctionsRegistry(engine.getPostfixFunctionsRegistry(), new AndroidMathEntityDao<MathPersistenceEntity>(null, application, null)),
this.lock);
@ -109,25 +109,25 @@ public class AndroidCalculatorEngine implements CalculatorEngine, SharedPreferen
@Override
@Nonnull
public CalculatorMathRegistry<IConstant> getVarsRegistry() {
public EntitiesRegistry<IConstant> getVarsRegistry() {
return calculatorEngine.getVarsRegistry();
}
@Override
@Nonnull
public CalculatorMathRegistry<Function> getFunctionsRegistry() {
public EntitiesRegistry<Function> getFunctionsRegistry() {
return calculatorEngine.getFunctionsRegistry();
}
@Override
@Nonnull
public CalculatorMathRegistry<Operator> getOperatorsRegistry() {
public EntitiesRegistry<Operator> getOperatorsRegistry() {
return calculatorEngine.getOperatorsRegistry();
}
@Override
@Nonnull
public CalculatorMathRegistry<Operator> getPostfixFunctionsRegistry() {
public EntitiesRegistry<Operator> getPostfixFunctionsRegistry() {
return calculatorEngine.getPostfixFunctionsRegistry();
}

View File

@ -41,10 +41,13 @@
a:layout_width="match_parent"
a:layout_height="match_parent">
<ListView style="@style/CppListView" />
<android.support.v7.widget.RecyclerView
a:id="@+id/entities_recyclerview"
a:layout_width="match_parent"
a:layout_height="match_parent" />
<com.melnykov.fab.FloatingActionButton
a:id="@+id/fab"
a:id="@+id/entities_fab"
style="@style/CppFab"
a:src="@drawable/ic_add_white_36dp"
a:visibility="gone" />

View File

@ -24,20 +24,18 @@
<LinearLayout
xmlns:a="http://schemas.android.com/apk/res/android"
style="@style/CppListItem"
a:layout_width="match_parent"
a:layout_height="wrap_content"
style="@style/CppListItem.TwoLines"
a:orientation="vertical">
<TextView
a:id="@+id/math_entity_text"
a:id="@+id/entity_text"
style="@style/CppListItemText.Primary"
a:layout_width="match_parent"
a:layout_height="wrap_content" />
<TextView
a:id="@+id/math_entity_short_description"
style="@style/CppListViewItemTextSecondary"
a:id="@+id/entity_description"
style="@style/CppListItemText.Secondary"
a:layout_width="match_parent"
a:layout_height="wrap_content" />

View File

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