All public fields of memory should wait for Memory to be loaded

This commit is contained in:
serso 2016-03-14 21:32:12 +01:00
parent e07a0d6d13
commit c09d7d6bf5
4 changed files with 73 additions and 9 deletions

View File

@ -31,6 +31,7 @@ import org.solovyev.android.Check;
import org.solovyev.android.calculator.history.HistoryState; import org.solovyev.android.calculator.history.HistoryState;
import org.solovyev.android.calculator.history.RecentHistory; import org.solovyev.android.calculator.history.RecentHistory;
import org.solovyev.android.calculator.math.MathType; 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.text.TextProcessorEditorResult;
import org.solovyev.android.calculator.view.EditorTextProcessor; import org.solovyev.android.calculator.view.EditorTextProcessor;
@ -215,6 +216,9 @@ public class Editor {
@Nonnull @Nonnull
public EditorState insert(@Nonnull String text, int selectionOffset) { public EditorState insert(@Nonnull String text, int selectionOffset) {
Check.isMainThread(); Check.isMainThread();
if (TextUtils.isEmpty(text) && selectionOffset == 0) {
return state;
}
final String oldText = state.getTextString(); final String oldText = state.getTextString();
final int selection = clamp(state.selection, oldText); final int selection = clamp(state.selection, oldText);
final int newTextLength = text.length() + oldText.length(); final int newTextLength = text.length() + oldText.length();
@ -245,6 +249,11 @@ public class Editor {
onTextChanged(getState(), true); onTextChanged(getState(), true);
} }
@Subscribe
public void onMemoryValueReady(@Nonnull Memory.ValueReadyEvent e) {
insert(e.value);
}
public void onHistoryLoaded(@Nonnull RecentHistory history) { public void onHistoryLoaded(@Nonnull RecentHistory history) {
if (!state.isEmpty()) { if (!state.isEmpty()) {
return; return;

View File

@ -152,7 +152,7 @@ public class Keyboard implements SharedPreferences.OnSharedPreferenceChangeListe
launcher.openFacebook(); launcher.openFacebook();
break; break;
case memory: case memory:
editor.insert(memory.get().getValue()); memory.get().requestValue();
break; break;
case erase: case erase:
editor.erase(); editor.erase();

View File

@ -292,7 +292,7 @@ public class KeyboardUi extends BaseKeyboardUi {
} }
} }
if (value == null) { if (value == null) {
memory.get().show(); memory.get().requestShow();
return false; return false;
} }
switch (direction) { switch (direction) {

View File

@ -4,15 +4,13 @@ import android.os.Handler;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.squareup.otto.Bus;
import jscl.math.Expression; import jscl.math.Expression;
import jscl.math.Generic; import jscl.math.Generic;
import jscl.math.JsclInteger; import jscl.math.JsclInteger;
import jscl.text.ParseException; import jscl.text.ParseException;
import org.solovyev.android.Check; import org.solovyev.android.Check;
import org.solovyev.android.calculator.App; import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.AppModule;
import org.solovyev.android.calculator.Notifier;
import org.solovyev.android.calculator.Runnables;
import org.solovyev.android.io.FileSystem; import org.solovyev.android.io.FileSystem;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -40,9 +38,13 @@ public class Memory {
private final Handler handler; private final Handler handler;
@Inject @Inject
Notifier notifier; Notifier notifier;
@Inject
ToJsclTextProcessor jsclProcessor;
@Named(AppModule.THREAD_BACKGROUND) @Named(AppModule.THREAD_BACKGROUND)
@Inject @Inject
Executor backgroundThread; Executor backgroundThread;
@Inject
Bus bus;
@NonNull @NonNull
private Generic value = EMPTY; private Generic value = EMPTY;
private boolean loaded; private boolean loaded;
@ -143,7 +145,8 @@ public class Memory {
} }
@NonNull @NonNull
public String getValue() { private String getValue() {
Check.isTrue(loaded);
try { try {
return value.toString(); return value.toString();
} catch (RuntimeException e) { } catch (RuntimeException e) {
@ -160,7 +163,7 @@ public class Memory {
show(); show();
} }
public void show() { private void show() {
notifier.showMessage(getValue()); notifier.showMessage(getValue());
} }
@ -187,6 +190,49 @@ public class Memory {
return new File(filesDir, "memory.txt"); 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 { private class WriteTask implements Runnable {
@Override @Override
public void run() { public void run() {
@ -198,9 +244,18 @@ public class Memory {
backgroundThread.execute(new Runnable() { backgroundThread.execute(new Runnable() {
@Override @Override
public void run() { 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;
}
}
} }
} }