Functions loading fails sometimes
This commit is contained in:
parent
26ac11ce89
commit
1120864668
@ -22,6 +22,7 @@
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.util.Log;
|
||||
import org.acra.ACRA;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
@ -33,6 +34,7 @@ public class AcraErrorReporter implements ErrorReporter {
|
||||
@Override
|
||||
public void onException(@Nonnull Throwable e) {
|
||||
if (!ENABLED) {
|
||||
Log.e("Acra", e.getMessage(), e);
|
||||
return;
|
||||
}
|
||||
ACRA.getErrorReporter().reportBuilder().forceSilent().exception(e).send();
|
||||
@ -41,6 +43,7 @@ public class AcraErrorReporter implements ErrorReporter {
|
||||
@Override
|
||||
public void onError(@Nonnull String message) {
|
||||
if (!ENABLED) {
|
||||
Log.e("Acra", message);
|
||||
return;
|
||||
}
|
||||
ACRA.getErrorReporter().reportBuilder().forceSilent().exception(new Throwable(message)).send();
|
||||
|
@ -47,6 +47,12 @@ public abstract class BaseEntitiesRegistry<T extends MathEntity, P extends Persi
|
||||
@Nullable
|
||||
protected final EntityDao<P> entityDao;
|
||||
|
||||
@Nonnull
|
||||
protected final Object lock = this;
|
||||
|
||||
// synchronized on lock
|
||||
private boolean initialized;
|
||||
|
||||
protected BaseEntitiesRegistry(@Nonnull MathRegistry<T> mathRegistry,
|
||||
@Nonnull String prefix,
|
||||
@Nullable EntityDao<P> entityDao) {
|
||||
@ -125,6 +131,20 @@ public abstract class BaseEntitiesRegistry<T extends MathEntity, P extends Persi
|
||||
// just in case
|
||||
Locator.getInstance().getErrorReporter().onException(e);
|
||||
}
|
||||
setInitialized();
|
||||
}
|
||||
|
||||
protected final void setInitialized() {
|
||||
synchronized (lock) {
|
||||
Check.isTrue(!initialized);
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isInitialized() {
|
||||
synchronized (lock) {
|
||||
return initialized;
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
|
@ -104,16 +104,18 @@ public class FunctionsRegistry extends BaseEntitiesRegistry<Function, OldFunctio
|
||||
Check.isNotMainThread();
|
||||
migrateOldFunctions();
|
||||
|
||||
add(new CustomFunction.Builder(true, "log", Arrays.asList("base", "x"), "ln(x)/ln(base)"));
|
||||
add(new CustomFunction.Builder(true, "√3", Collections.singletonList("x"), "x^(1/3)"));
|
||||
add(new CustomFunction.Builder(true, "√4", Collections.singletonList("x"), "x^(1/4)"));
|
||||
add(new CustomFunction.Builder(true, "√n", Arrays.asList("x", "n"), "x^(1/n)"));
|
||||
add(new CustomFunction.Builder(true, "re", Collections.singletonList("x"), "(x+conjugate(x))/2"));
|
||||
add(new CustomFunction.Builder(true, "im", Collections.singletonList("x"), "(x-conjugate(x))/(2*i)"));
|
||||
addSafely(new CustomFunction.Builder(true, "log", Arrays.asList("base", "x"), "ln(x)/ln(base)"));
|
||||
addSafely(new CustomFunction.Builder(true, "√3", Collections.singletonList("x"), "x^(1/3)"));
|
||||
addSafely(new CustomFunction.Builder(true, "√4", Collections.singletonList("x"), "x^(1/4)"));
|
||||
addSafely(new CustomFunction.Builder(true, "√n", Arrays.asList("x", "n"), "x^(1/n)"));
|
||||
addSafely(new CustomFunction.Builder(true, "re", Collections.singletonList("x"), "(x+conjugate(x))/2"));
|
||||
addSafely(new CustomFunction.Builder(true, "im", Collections.singletonList("x"), "(x-conjugate(x))/(2*i)"));
|
||||
|
||||
for (CppFunction function : loadFunctions()) {
|
||||
add(function.toCustomFunctionBuilder());
|
||||
addSafely(function.toCustomFunctionBuilder());
|
||||
}
|
||||
|
||||
setInitialized();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -124,13 +126,26 @@ public class FunctionsRegistry extends BaseEntitiesRegistry<Function, OldFunctio
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function add(@Nonnull JBuilder<? extends Function> result) {
|
||||
final Function function = super.add(result);
|
||||
// todo serso: don't save while we're initializing
|
||||
save();
|
||||
public Function add(@Nonnull JBuilder<? extends Function> builder) {
|
||||
final Function function = super.add(builder);
|
||||
if (isInitialized()) {
|
||||
save();
|
||||
}
|
||||
return function;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Function addSafely(@Nonnull JBuilder<? extends Function> builder) {
|
||||
try {
|
||||
// todo serso: currently JSCL might produce function's body which can't be loaded afterwards. F.e. f(x) = 3 * 6
|
||||
// might be simpliffied to f(x) = 18 × 10 ^ 0 and × is not supported by JSCL
|
||||
return add(builder);
|
||||
} catch (Exception e) {
|
||||
errorReporter.onException(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private List<CppFunction> loadFunctions() {
|
||||
try {
|
||||
@ -151,6 +166,7 @@ public class FunctionsRegistry extends BaseEntitiesRegistry<Function, OldFunctio
|
||||
final OldFunctions oldFunctions = serializer.read(OldFunctions.class, xml);
|
||||
if (oldFunctions != null) {
|
||||
List<CppFunction> functions = OldFunctions.toCppFunctions(oldFunctions);
|
||||
// todo serso: fix multiplication sign issue
|
||||
FileSaver.save(getFunctionsFile(), Json.toJson(functions).toString());
|
||||
}
|
||||
preferences.edit().remove(OldFunctions.PREFS_KEY).apply();
|
||||
|
@ -49,6 +49,8 @@ public class KeyboardUi {
|
||||
private final int sidePadding;
|
||||
@DrawableRes
|
||||
private final int buttonBackground;
|
||||
@NonNull
|
||||
private final String multiplicationSign = Locator.getInstance().getEngine().getMultiplicationSign();
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public KeyboardUi(@NonNull User user, @NonNull List<String> parameterNames) {
|
||||
@ -110,7 +112,7 @@ public class KeyboardUi {
|
||||
addButton(row, 0, "7");
|
||||
addButton(row, 0, "8");
|
||||
addButton(row, 0, "9").setText("π", up).setText("e", down);
|
||||
addOperationButton(row, R.id.cpp_kb_button_multiply, Locator.getInstance().getEngine().getMultiplicationSign()).setText("^n", up).setText("^2", down);
|
||||
addOperationButton(row, R.id.cpp_kb_button_multiply, multiplicationSign).setText("^n", up).setText("^2", down);
|
||||
addButton(row, R.id.cpp_kb_button_clear, "C");
|
||||
|
||||
row = makeRow();
|
||||
@ -264,7 +266,7 @@ public class KeyboardUi {
|
||||
user.insertOperator('-');
|
||||
break;
|
||||
case R.id.cpp_kb_button_multiply:
|
||||
user.insertOperator('*');
|
||||
user.insertOperator(multiplicationSign);
|
||||
break;
|
||||
case R.id.cpp_kb_button_functions_constants:
|
||||
user.showFunctionsConstants(v);
|
||||
|
@ -235,6 +235,13 @@ public class CppFunction implements Jsonable, Parcelable {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public Builder withBody(@NonNull String body) {
|
||||
Check.isTrue(!built);
|
||||
function.body = body;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public CppFunction build() {
|
||||
built = true;
|
||||
|
Loading…
Reference in New Issue
Block a user