vars activity
This commit is contained in:
parent
76bc626778
commit
6359e921d5
@ -16,11 +16,13 @@
|
|||||||
a:layout_width="match_parent"
|
a:layout_width="match_parent"
|
||||||
a:layout_height="match_parent">
|
a:layout_height="match_parent">
|
||||||
|
|
||||||
<TextView xmlns:a="http://schemas.android.com/apk/res/android"
|
<TextView a:id="@+id/var_text"
|
||||||
a:id="@+id/var_text"
|
a:layout_width="match_parent"
|
||||||
a:layout_width="fill_parent"
|
a:layout_height="match_parent"
|
||||||
a:layout_height="fill_parent">
|
a:textSize="20dp"
|
||||||
|
a:padding="5dp">
|
||||||
</TextView>
|
</TextView>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
@ -16,11 +16,13 @@
|
|||||||
a:layout_width="match_parent"
|
a:layout_width="match_parent"
|
||||||
a:layout_height="match_parent">
|
a:layout_height="match_parent">
|
||||||
|
|
||||||
<TextView xmlns:a="http://schemas.android.com/apk/res/android"
|
<TextView a:id="@+id/var_text"
|
||||||
a:id="@+id/var_text"
|
a:layout_width="match_parent"
|
||||||
a:layout_width="fill_parent"
|
a:layout_height="match_parent"
|
||||||
a:layout_height="fill_parent">
|
a:textSize="20dp"
|
||||||
|
a:padding="5dp">
|
||||||
</TextView>
|
</TextView>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
@ -21,7 +21,6 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import org.solovyev.android.view.FontSizeAdjuster;
|
import org.solovyev.android.view.FontSizeAdjuster;
|
||||||
import org.solovyev.android.view.widgets.*;
|
import org.solovyev.android.view.widgets.*;
|
||||||
import org.solovyev.common.BooleanMapper;
|
import org.solovyev.common.BooleanMapper;
|
||||||
import org.solovyev.common.NumberMapper;
|
|
||||||
import org.solovyev.common.utils.Announcer;
|
import org.solovyev.common.utils.Announcer;
|
||||||
import org.solovyev.common.utils.StringUtils;
|
import org.solovyev.common.utils.StringUtils;
|
||||||
import org.solovyev.common.utils.history.HistoryAction;
|
import org.solovyev.common.utils.history.HistoryAction;
|
||||||
@ -57,7 +56,9 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
|
|||||||
setContentView(R.layout.main);
|
setContentView(R.layout.main);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
CalculatorModel.init(this);
|
if (!CalculatorModel.isLoaded()) {
|
||||||
|
CalculatorModel.init(this);
|
||||||
|
}
|
||||||
this.calculatorModel = CalculatorModel.getInstance();
|
this.calculatorModel = CalculatorModel.getInstance();
|
||||||
} catch (EvalError evalError) {
|
} catch (EvalError evalError) {
|
||||||
// todo serso: create serso runtime exception
|
// todo serso: create serso runtime exception
|
||||||
|
@ -39,9 +39,7 @@ public class CalculatorModel {
|
|||||||
private static CalculatorModel instance;
|
private static CalculatorModel instance;
|
||||||
|
|
||||||
private CalculatorModel(@Nullable Context context) throws EvalError {
|
private CalculatorModel(@Nullable Context context) throws EvalError {
|
||||||
if (context != null) {
|
load(context);
|
||||||
load(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
interpreter = new Interpreter();
|
interpreter = new Interpreter();
|
||||||
interpreter.eval(ToJsclPreprocessor.wrap(JsclOperation.importCommands, "/jscl/editorengine/commands"));
|
interpreter.eval(ToJsclPreprocessor.wrap(JsclOperation.importCommands, "/jscl/editorengine/commands"));
|
||||||
@ -49,11 +47,21 @@ public class CalculatorModel {
|
|||||||
|
|
||||||
public String evaluate(@NotNull JsclOperation operation, @NotNull String expression) throws EvalError, ParseException {
|
public String evaluate(@NotNull JsclOperation operation, @NotNull String expression) throws EvalError, ParseException {
|
||||||
|
|
||||||
final String preprocessedExpression = preprocessor.process(expression);
|
final StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
/*
|
||||||
|
for (Var var : varsRegister.getVars()) {
|
||||||
|
if (!var.isSystem()) {
|
||||||
|
sb.append(var.getName()).append("=").append(var.getValue()).append(";");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
sb.append(preprocessor.process(expression));
|
||||||
|
|
||||||
//Log.d(CalculatorModel.class.getName(), "Preprocessed expression: " + preprocessedExpression);
|
//Log.d(CalculatorModel.class.getName(), "Preprocessed expression: " + preprocessedExpression);
|
||||||
|
|
||||||
Object evaluationObject = interpreter.eval(ToJsclPreprocessor.wrap(operation, preprocessedExpression));
|
Object evaluationObject = interpreter.eval(ToJsclPreprocessor.wrap(operation, sb.toString()));
|
||||||
String result = String.valueOf(evaluationObject).trim();
|
String result = String.valueOf(evaluationObject).trim();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -112,11 +120,13 @@ public class CalculatorModel {
|
|||||||
return MathUtils.round(dResult, numberOfFractionDigits);
|
return MathUtils.round(dResult, numberOfFractionDigits);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void load(@NotNull Context context) {
|
public synchronized void load(@Nullable Context context) {
|
||||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
if (context != null) {
|
||||||
|
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||||
|
|
||||||
final NumberMapper<Integer> integerNumberMapper = new NumberMapper<Integer>(Integer.class);
|
final NumberMapper<Integer> integerNumberMapper = new NumberMapper<Integer>(Integer.class);
|
||||||
this.setNumberOfFractionDigits(integerNumberMapper.parseValue(preferences.getString(context.getString(R.string.p_calc_result_precision_key), context.getString(R.string.p_calc_result_precision))));
|
this.setNumberOfFractionDigits(integerNumberMapper.parseValue(preferences.getString(context.getString(R.string.p_calc_result_precision_key), context.getString(R.string.p_calc_result_precision))));
|
||||||
|
}
|
||||||
|
|
||||||
varsRegister.load(context);
|
varsRegister.load(context);
|
||||||
}
|
}
|
||||||
@ -136,7 +146,7 @@ public class CalculatorModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized void init(@Nullable Context context) throws EvalError {
|
public static synchronized void init(@Nullable Context context) throws EvalError {
|
||||||
if ( instance == null ) {
|
if (!isLoaded()) {
|
||||||
instance = new CalculatorModel(context);
|
instance = new CalculatorModel(context);
|
||||||
} else {
|
} else {
|
||||||
throw new RuntimeException("Calculator model already instantiated!");
|
throw new RuntimeException("Calculator model already instantiated!");
|
||||||
@ -144,13 +154,17 @@ public class CalculatorModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static CalculatorModel getInstance() {
|
public static CalculatorModel getInstance() {
|
||||||
if ( instance == null ) {
|
if (!isLoaded()) {
|
||||||
throw new RuntimeException("CalculatorModel must be instantiated!");
|
throw new RuntimeException("CalculatorModel must be instantiated!");
|
||||||
}
|
}
|
||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean isLoaded() {
|
||||||
|
return instance != null;
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public VarsRegister getVarsRegister() {
|
public VarsRegister getVarsRegister() {
|
||||||
return varsRegister;
|
return varsRegister;
|
||||||
|
@ -7,12 +7,13 @@
|
|||||||
package org.solovyev.android.calculator;
|
package org.solovyev.android.calculator;
|
||||||
|
|
||||||
import android.app.ListActivity;
|
import android.app.ListActivity;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.AdapterView;
|
import android.view.ViewGroup;
|
||||||
import android.widget.ArrayAdapter;
|
import android.widget.*;
|
||||||
import android.widget.ListView;
|
import org.solovyev.common.utils.StringUtils;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -31,7 +32,7 @@ public class CalculatorVarsActivity extends ListActivity {
|
|||||||
setTheme(android.R.style.Theme_Dialog);
|
setTheme(android.R.style.Theme_Dialog);
|
||||||
|
|
||||||
final List<Var> vars = new ArrayList<Var>(CalculatorModel.getInstance().getVarsRegister().getVars());
|
final List<Var> vars = new ArrayList<Var>(CalculatorModel.getInstance().getVarsRegister().getVars());
|
||||||
setListAdapter(new ArrayAdapter<Var>(this, R.layout.var, R.id.var_text, vars));
|
setListAdapter(new VarsArrayAdapter(this, R.layout.var, R.id.var_text, vars));
|
||||||
|
|
||||||
final ListView lv = getListView();
|
final ListView lv = getListView();
|
||||||
lv.setTextFilterEnabled(true);
|
lv.setTextFilterEnabled(true);
|
||||||
@ -48,4 +49,30 @@ public class CalculatorVarsActivity extends ListActivity {
|
|||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class VarsArrayAdapter extends ArrayAdapter<Var> {
|
||||||
|
|
||||||
|
private VarsArrayAdapter(Context context, int resource, int textViewResourceId, List<Var> objects) {
|
||||||
|
super(context, resource, textViewResourceId, objects);
|
||||||
|
}
|
||||||
|
|
||||||
|
public VarsArrayAdapter(Context context, int resource, List<Var> objects) {
|
||||||
|
super(context, resource, objects);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public View getView(int position, View convertView, ViewGroup parent) {
|
||||||
|
final ViewGroup result = (ViewGroup)super.getView(position, convertView, parent);
|
||||||
|
|
||||||
|
final Var var = getItem(position);
|
||||||
|
|
||||||
|
if (!StringUtils.isEmpty(var.getDescription())) {
|
||||||
|
final TextView description = new TextView(getContext());
|
||||||
|
description.setText(var.getDescription());
|
||||||
|
result.addView(description, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -86,4 +86,21 @@ public class Var {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return getName() + " = " + value;
|
return getName() + " = " + value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
|
Var var = (Var) o;
|
||||||
|
|
||||||
|
if (!name.equals(var.name)) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return name.hashCode();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,9 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||||
|
* For more information, please, contact se.solovyev@gmail.com
|
||||||
|
* or visit http://se.solovyev.org
|
||||||
|
*/
|
||||||
|
|
||||||
package org.solovyev.android.calculator;
|
package org.solovyev.android.calculator;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
@ -8,15 +14,11 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import org.simpleframework.xml.Serializer;
|
import org.simpleframework.xml.Serializer;
|
||||||
import org.simpleframework.xml.core.Persister;
|
import org.simpleframework.xml.core.Persister;
|
||||||
import org.solovyev.android.calculator.math.MathEntityType;
|
import org.solovyev.android.calculator.math.MathEntityType;
|
||||||
import org.solovyev.android.view.widgets.SimpleOnDragListener;
|
|
||||||
import org.solovyev.common.utils.CollectionsUtils;
|
import org.solovyev.common.utils.CollectionsUtils;
|
||||||
import org.solovyev.common.utils.Finder;
|
import org.solovyev.common.utils.Finder;
|
||||||
|
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.util.Collections;
|
import java.util.*;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User: serso
|
* User: serso
|
||||||
@ -26,19 +28,19 @@ import java.util.Set;
|
|||||||
public class VarsRegister {
|
public class VarsRegister {
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final Set<Var> vars = new HashSet<Var>();
|
private final List<Var> vars = new ArrayList<Var>();
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final Set<Var> systemVars = new HashSet<Var>();
|
private final List<Var> systemVars = new ArrayList<Var>();
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public Set<Var> getVars() {
|
public List<Var> getVars() {
|
||||||
return Collections.unmodifiableSet(vars);
|
return Collections.unmodifiableList(vars);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public Set<Var> getSystemVars() {
|
public List<Var> getSystemVars() {
|
||||||
return Collections.unmodifiableSet(systemVars);
|
return Collections.unmodifiableList(systemVars);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@ -64,20 +66,34 @@ public class VarsRegister {
|
|||||||
vars.addAll(result);
|
vars.addAll(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void load(@NotNull Context context) {
|
public synchronized void load(@Nullable Context context) {
|
||||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
|
||||||
|
|
||||||
this.vars.clear();
|
this.vars.clear();
|
||||||
this.systemVars.clear();
|
this.systemVars.clear();
|
||||||
|
|
||||||
final String value = preferences.getString(context.getString(R.string.p_calc_vars), null);
|
Var t = new Var("T", 0d, false);
|
||||||
if (value != null) {
|
t.setDescription("Some text description!");
|
||||||
final Serializer serializer = new Persister();
|
this.vars.add(t);
|
||||||
try {
|
|
||||||
final Vars vars = serializer.read(Vars.class, value);
|
Var t2 = new Var("T2", 2d, false);
|
||||||
this.vars.addAll(vars.getVars());
|
this.vars.add(t2);
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException(e);
|
Var t1 = new Var("T1", 1d, false);
|
||||||
|
t1.setDescription("Не так давно в рунете появились упоминания англоязычного проекта DearPhotograph.com. Сайт нас растрогал, зарядил идеей и вдохновением.");
|
||||||
|
this.vars.add(t1);
|
||||||
|
|
||||||
|
if (context != null) {
|
||||||
|
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||||
|
|
||||||
|
final String value = preferences.getString(context.getString(R.string.p_calc_vars), null);
|
||||||
|
if (value != null) {
|
||||||
|
final Serializer serializer = new Persister();
|
||||||
|
try {
|
||||||
|
final Vars vars = serializer.read(Vars.class, value);
|
||||||
|
this.vars.addAll(vars.getVars());
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,6 +105,11 @@ public class VarsRegister {
|
|||||||
vars.add(systemVar);
|
vars.add(systemVar);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*Log.d(VarsRegister.class.getName(), vars.size() + " variables registered!");
|
||||||
|
for (Var var : vars) {
|
||||||
|
Log.d(VarsRegister.class.getName(), var.toString());
|
||||||
|
}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void save(@NotNull Context context) {
|
public synchronized void save(@NotNull Context context) {
|
||||||
|
@ -7,11 +7,11 @@ package org.solovyev.android.calculator.math;
|
|||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.solovyev.android.calculator.CalculatorModel;
|
||||||
import org.solovyev.android.calculator.Var;
|
import org.solovyev.android.calculator.Var;
|
||||||
import org.solovyev.common.utils.CollectionsUtils;
|
import org.solovyev.common.utils.CollectionsUtils;
|
||||||
import org.solovyev.common.utils.Finder;
|
import org.solovyev.common.utils.Finder;
|
||||||
|
|
||||||
import java.lang.reflect.Array;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@ -71,7 +71,9 @@ public enum MathEntityType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ( result == null ) {
|
if ( result == null ) {
|
||||||
if ( prefixFunctions.contains(s) ) {
|
if ( isConstant(s) ) {
|
||||||
|
result = MathEntityType.constant;
|
||||||
|
} else if ( prefixFunctions.contains(s) ) {
|
||||||
result = MathEntityType.function;
|
result = MathEntityType.function;
|
||||||
} else if ( groupSymbols.contains(s) ) {
|
} else if ( groupSymbols.contains(s) ) {
|
||||||
result = MathEntityType.group_symbols;
|
result = MathEntityType.group_symbols;
|
||||||
@ -107,7 +109,11 @@ public enum MathEntityType {
|
|||||||
private static boolean isConstant(final char ch) {
|
private static boolean isConstant(final char ch) {
|
||||||
final String name = String.valueOf(ch);
|
final String name = String.valueOf(ch);
|
||||||
|
|
||||||
return CollectionsUtils.get(constants, new Finder<Var>() {
|
return isConstant(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isConstant(final String name) {
|
||||||
|
return CollectionsUtils.get(CalculatorModel.getInstance().getVarsRegister().getVars(), new Finder<Var>() {
|
||||||
@Override
|
@Override
|
||||||
public boolean isFound(@Nullable Var var) {
|
public boolean isFound(@Nullable Var var) {
|
||||||
return var != null && var.getName().equals(name);
|
return var != null && var.getName().equals(name);
|
||||||
|
Loading…
Reference in New Issue
Block a user