From 22cec09802c90aa6cf72f4b7b0887a899ac7bd12 Mon Sep 17 00:00:00 2001 From: serso Date: Tue, 13 Sep 2011 18:56:58 +0400 Subject: [PATCH] preferences + asyn result calculation --- .../android/calculator/CalculatorView.java | 43 +++++++++++++------ .../org/solovyev/util/date/MutableObject.java | 26 +++++++++++ 2 files changed, 56 insertions(+), 13 deletions(-) create mode 100644 src/main/java/org/solovyev/util/date/MutableObject.java diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorView.java b/src/main/java/org/solovyev/android/calculator/CalculatorView.java index 314016bb..362c918c 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorView.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorView.java @@ -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 currentRunner = new MutableObject(); + 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(); } - }); + } } } diff --git a/src/main/java/org/solovyev/util/date/MutableObject.java b/src/main/java/org/solovyev/util/date/MutableObject.java new file mode 100644 index 00000000..733f1e67 --- /dev/null +++ b/src/main/java/org/solovyev/util/date/MutableObject.java @@ -0,0 +1,26 @@ +package org.solovyev.util.date; + +/** + * User: serso + * Date: 9/13/11 + * Time: 6:46 PM + */ +public class MutableObject { + + 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; + } +}