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.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;

View File

@ -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();

View File

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

View File

@ -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;
}
}
}
}