preferences + asyn result calculation

This commit is contained in:
serso 2011-09-13 18:56:58 +04:00
parent 5f4da0a65f
commit be9060823e
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.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.util.StringUtils; import org.solovyev.util.StringUtils;
import org.solovyev.util.date.MutableObject;
import org.solovyev.util.math.MathEntityType; import org.solovyev.util.math.MathEntityType;
import java.util.Date; 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) { public void doTextOperation(@NotNull TextOperation operation) {
final String editorStateBefore = this.editor.getText().toString(); final String editorStateBefore = this.editor.getText().toString();
@ -99,8 +103,26 @@ public class CalculatorView implements CursorControl{
final String editorStateAfter = this.editor.getText().toString(); final String editorStateAfter = this.editor.getText().toString();
if (!editorStateBefore.equals(editorStateAfter)) { 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(); saveHistoryState();
} }
@ -112,18 +134,13 @@ public class CalculatorView implements CursorControl{
final TextView localDisplay = display; final TextView localDisplay = display;
final Activity localActivity = activity; final Activity localActivity = activity;
new Handler().post(new Runnable() { try {
@Override localDisplay.setText(calculator.evaluate(JsclOperation.numeric, expression));
public void run() { } catch (EvalError evalError) {
try { if (showError) {
localDisplay.setText(calculator.evaluate(JsclOperation.numeric, expression)); Toast.makeText(localActivity, R.string.syntax_error, Toast.LENGTH_SHORT).show();
} 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;
}
}