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.Bus;
import com.squareup.otto.Subscribe; import com.squareup.otto.Subscribe;
import org.solovyev.android.Check; 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.text.TextProcessorEditorResult;
import org.solovyev.android.calculator.view.EditorTextProcessor; import org.solovyev.android.calculator.view.EditorTextProcessor;
@ -234,6 +237,17 @@ public class Editor {
onTextChanged(getState(), true); 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 { public static class ChangedEvent {
@Nonnull @Nonnull
public final EditorState oldState; public final EditorState oldState;

View File

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

View File

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

View File

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