From c09d7d6bf59ceb1a211b53083185a346c0e71c2f Mon Sep 17 00:00:00 2001 From: serso Date: Mon, 14 Mar 2016 21:32:12 +0100 Subject: [PATCH] All public fields of memory should wait for Memory to be loaded --- .../solovyev/android/calculator/Editor.java | 9 +++ .../solovyev/android/calculator/Keyboard.java | 2 +- .../calculator/keyboard/KeyboardUi.java | 2 +- .../android/calculator/memory/Memory.java | 69 +++++++++++++++++-- 4 files changed, 73 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/Editor.java b/app/src/main/java/org/solovyev/android/calculator/Editor.java index 90f02f70..a693a578 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Editor.java +++ b/app/src/main/java/org/solovyev/android/calculator/Editor.java @@ -31,6 +31,7 @@ import org.solovyev.android.Check; import org.solovyev.android.calculator.history.HistoryState; import org.solovyev.android.calculator.history.RecentHistory; import org.solovyev.android.calculator.math.MathType; +import org.solovyev.android.calculator.memory.Memory; import org.solovyev.android.calculator.text.TextProcessorEditorResult; import org.solovyev.android.calculator.view.EditorTextProcessor; @@ -215,6 +216,9 @@ public class Editor { @Nonnull public EditorState insert(@Nonnull String text, int selectionOffset) { Check.isMainThread(); + if (TextUtils.isEmpty(text) && selectionOffset == 0) { + return state; + } final String oldText = state.getTextString(); final int selection = clamp(state.selection, oldText); final int newTextLength = text.length() + oldText.length(); @@ -245,6 +249,11 @@ public class Editor { onTextChanged(getState(), true); } + @Subscribe + public void onMemoryValueReady(@Nonnull Memory.ValueReadyEvent e) { + insert(e.value); + } + public void onHistoryLoaded(@Nonnull RecentHistory history) { if (!state.isEmpty()) { return; diff --git a/app/src/main/java/org/solovyev/android/calculator/Keyboard.java b/app/src/main/java/org/solovyev/android/calculator/Keyboard.java index 11d8713c..7d610c20 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Keyboard.java +++ b/app/src/main/java/org/solovyev/android/calculator/Keyboard.java @@ -152,7 +152,7 @@ public class Keyboard implements SharedPreferences.OnSharedPreferenceChangeListe launcher.openFacebook(); break; case memory: - editor.insert(memory.get().getValue()); + memory.get().requestValue(); break; case erase: editor.erase(); diff --git a/app/src/main/java/org/solovyev/android/calculator/keyboard/KeyboardUi.java b/app/src/main/java/org/solovyev/android/calculator/keyboard/KeyboardUi.java index 18de0617..f919f982 100644 --- a/app/src/main/java/org/solovyev/android/calculator/keyboard/KeyboardUi.java +++ b/app/src/main/java/org/solovyev/android/calculator/keyboard/KeyboardUi.java @@ -292,7 +292,7 @@ public class KeyboardUi extends BaseKeyboardUi { } } if (value == null) { - memory.get().show(); + memory.get().requestShow(); return false; } switch (direction) { diff --git a/app/src/main/java/org/solovyev/android/calculator/memory/Memory.java b/app/src/main/java/org/solovyev/android/calculator/memory/Memory.java index 9887d157..b38c14d1 100644 --- a/app/src/main/java/org/solovyev/android/calculator/memory/Memory.java +++ b/app/src/main/java/org/solovyev/android/calculator/memory/Memory.java @@ -4,15 +4,13 @@ import android.os.Handler; import android.support.annotation.NonNull; import android.text.TextUtils; import android.util.Log; +import com.squareup.otto.Bus; import jscl.math.Expression; import jscl.math.Generic; import jscl.math.JsclInteger; import jscl.text.ParseException; import org.solovyev.android.Check; -import org.solovyev.android.calculator.App; -import org.solovyev.android.calculator.AppModule; -import org.solovyev.android.calculator.Notifier; -import org.solovyev.android.calculator.Runnables; +import org.solovyev.android.calculator.*; import org.solovyev.android.io.FileSystem; import javax.annotation.Nonnull; @@ -40,9 +38,13 @@ public class Memory { private final Handler handler; @Inject Notifier notifier; + @Inject + ToJsclTextProcessor jsclProcessor; @Named(AppModule.THREAD_BACKGROUND) @Inject Executor backgroundThread; + @Inject + Bus bus; @NonNull private Generic value = EMPTY; private boolean loaded; @@ -143,7 +145,8 @@ public class Memory { } @NonNull - public String getValue() { + private String getValue() { + Check.isTrue(loaded); try { return value.toString(); } catch (RuntimeException e) { @@ -160,7 +163,7 @@ public class Memory { show(); } - public void show() { + private void show() { notifier.showMessage(getValue()); } @@ -187,6 +190,49 @@ public class Memory { return new File(filesDir, "memory.txt"); } + public void requestValue() { + if (!loaded) { + postValue(); + return; + } + bus.post(new ValueReadyEvent(getValue())); + } + + private void postValue() { + whenLoadedRunnables.add(new Runnable() { + @Override + public void run() { + requestValue(); + } + }); + } + + public void requestShow() { + if (!loaded) { + postShow(); + return; + } + show(); + } + + private void postShow() { + whenLoadedRunnables.add(new Runnable() { + @Override + public void run() { + requestShow(); + } + }); + } + + public static final class ValueReadyEvent { + @NonNull + public final String value; + + public ValueReadyEvent(@NonNull String value) { + this.value = value; + } + } + private class WriteTask implements Runnable { @Override public void run() { @@ -198,9 +244,18 @@ public class Memory { backgroundThread.execute(new Runnable() { @Override public void run() { - fileSystem.writeSilently(getFile(), value); + fileSystem.writeSilently(getFile(), prepareExpression(value)); } }); } + + @NonNull + private String prepareExpression(@NonNull String value) { + try { + return jsclProcessor.process(value).getValue(); + } catch (org.solovyev.android.calculator.ParseException ignored) { + return value; + } + } } }