preferences + asyn result calculation

This commit is contained in:
Sergey Solovyev 2011-09-13 18:56:58 +04:00
parent d00d1ef52a
commit 4341a5a0f4
2 changed files with 56 additions and 13 deletions

View File

@ -14,6 +14,7 @@ import bsh.EvalError;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.util.StringUtils;
import org.solovyev.util.date.MutableObject;
import org.solovyev.util.math.MathEntityType;
import java.util.Date;
@ -92,6 +93,9 @@ public class CalculatorView implements CursorControl{
}
}
@NotNull
private final MutableObject<Runnable> currentRunner = new MutableObject<Runnable>();
public void doTextOperation(@NotNull TextOperation operation) {
final String editorStateBefore = this.editor.getText().toString();
@ -99,8 +103,26 @@ public class CalculatorView implements CursorControl{
final String editorStateAfter = this.editor.getText().toString();
if (!editorStateBefore.equals(editorStateAfter)) {
// actually nothing shall be logged while text operations are done
evaluate(editorStateAfter, false);
currentRunner.setObject(new Runnable() {
@Override
public void run() {
synchronized (currentRunner) {
// do only if nothing was post delayed before current instance was posted
if (currentRunner.getObject() == this) {
// actually nothing shall be logged while text operations are done
evaluate(editorStateAfter, false);
if (history.isRedoAvailable()) {
history.redo(getCurrentHistoryState());
}
saveHistoryState();
}
}
}
});
new Handler().postDelayed(currentRunner.getObject(), 500);
saveHistoryState();
}
@ -112,18 +134,13 @@ public class CalculatorView implements CursorControl{
final TextView localDisplay = display;
final Activity localActivity = activity;
new Handler().post(new Runnable() {
@Override
public void run() {
try {
localDisplay.setText(calculator.evaluate(JsclOperation.numeric, expression));
} catch (EvalError evalError) {
if (showError) {
Toast.makeText(localActivity, R.string.syntax_error, Toast.LENGTH_SHORT).show();
}
}
try {
localDisplay.setText(calculator.evaluate(JsclOperation.numeric, expression));
} catch (EvalError evalError) {
if (showError) {
Toast.makeText(localActivity, R.string.syntax_error, Toast.LENGTH_SHORT).show();
}
});
}
}
}

View File

@ -0,0 +1,26 @@
package org.solovyev.util.date;
/**
* User: serso
* Date: 9/13/11
* Time: 6:46 PM
*/
public class MutableObject<T> {
private T object;
public MutableObject() {
}
public MutableObject(T object) {
this.object = object;
}
public T getObject() {
return object;
}
public void setObject(T object) {
this.object = object;
}
}