Restore editor state from recent history

This commit is contained in:
serso 2016-03-01 13:10:22 +01:00
parent 7585aaa2fb
commit 8cb117b5ea
4 changed files with 40 additions and 13 deletions

View File

@ -27,7 +27,10 @@ import android.text.TextUtils;
import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.history.HistoryState;
import org.solovyev.android.calculator.history.RecentHistory;
import org.solovyev.android.calculator.text.TextProcessorEditorResult;
import org.solovyev.android.calculator.view.EditorTextProcessor;
@ -234,6 +237,17 @@ public class Editor {
onTextChanged(getState(), true);
}
public void onHistoryLoaded(@Nonnull RecentHistory history) {
if (!state.isEmpty()) {
return;
}
final HistoryState state = history.getCurrent();
if (state == null) {
return;
}
setState(state.editor);
}
public static class ChangedEvent {
@Nonnull
public final EditorState oldState;

View File

@ -106,6 +106,10 @@ public class EditorState implements Parcelable {
return TextUtils.equals(text, that.text) && selection == that.selection;
}
public boolean isEmpty() {
return TextUtils.isEmpty(text);
}
@Override
public String toString() {
return "EditorState{" +

View File

@ -22,8 +22,6 @@
package org.solovyev.android.calculator.history;
import static android.text.TextUtils.isEmpty;
import android.app.Application;
import android.content.SharedPreferences;
import android.os.Handler;
@ -62,6 +60,8 @@ import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import static android.text.TextUtils.isEmpty;
@Singleton
public class History {
@ -219,8 +219,9 @@ public class History {
public void run() {
Check.isTrue(recent.isEmpty());
Check.isTrue(saved.isEmpty());
recent.addAll(recentStates);
recent.addInitial(recentStates);
saved.addAll(savedStates);
editor.onHistoryLoaded(recent);
}
});
}
@ -267,6 +268,11 @@ public class History {
@Nonnull
public List<HistoryState> getRecent() {
return getRecent(true);
}
@Nonnull
private List<HistoryState> getRecent(boolean forUi) {
Check.isMainThread();
final List<HistoryState> result = new LinkedList<>();
@ -291,7 +297,7 @@ public class History {
if (statesCount > 0) {
// try add last state if not empty
final HistoryState state = states.get(statesCount - 1);
if (!isEmpty(state.editor.getTextString())) {
if (!state.editor.isEmpty() || !forUi) {
result.add(0, state);
}
}
@ -401,7 +407,7 @@ public class History {
public void run() {
Check.isMainThread();
// don't need to save intermediate states, thus {@link History#getRecent}
final List<HistoryState> states = recent ? getRecent() : getSaved();
final List<HistoryState> states = recent ? getRecent(false) : getSaved();
backgroundThread.execute(new Runnable() {
@Override
public void run() {

View File

@ -2,13 +2,9 @@ package org.solovyev.android.calculator.history;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.solovyev.android.Check;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
@ -31,16 +27,23 @@ public class RecentHistory {
}
list.add(state);
current++;
if (list.size() > MAX_HISTORY) {
trim();
}
private void trim() {
while (list.size() > MAX_HISTORY) {
current--;
list.remove(0);
}
}
public void addAll(@NonNull List<HistoryState> states) {
public void addInitial(@NonNull List<HistoryState> states) {
Check.isMainThread();
for (HistoryState state : states) {
add(state);
list.add(0, state);
}
current += states.size();
trim();
}
public void remove(@NonNull HistoryState state) {