From 8cb117b5ead58654bfac291cca378492d053e8cb Mon Sep 17 00:00:00 2001 From: serso Date: Tue, 1 Mar 2016 13:10:22 +0100 Subject: [PATCH] Restore editor state from recent history --- .../solovyev/android/calculator/Editor.java | 14 ++++++++++++++ .../android/calculator/EditorState.java | 4 ++++ .../android/calculator/history/History.java | 16 +++++++++++----- .../calculator/history/RecentHistory.java | 19 +++++++++++-------- 4 files changed, 40 insertions(+), 13 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 4b388eb6..6015d6e7 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Editor.java +++ b/app/src/main/java/org/solovyev/android/calculator/Editor.java @@ -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; diff --git a/app/src/main/java/org/solovyev/android/calculator/EditorState.java b/app/src/main/java/org/solovyev/android/calculator/EditorState.java index 14784dea..39533d77 100644 --- a/app/src/main/java/org/solovyev/android/calculator/EditorState.java +++ b/app/src/main/java/org/solovyev/android/calculator/EditorState.java @@ -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{" + diff --git a/app/src/main/java/org/solovyev/android/calculator/history/History.java b/app/src/main/java/org/solovyev/android/calculator/history/History.java index 1f64332d..95375dff 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/History.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/History.java @@ -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 getRecent() { + return getRecent(true); + } + + @Nonnull + private List getRecent(boolean forUi) { Check.isMainThread(); final List 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 states = recent ? getRecent() : getSaved(); + final List states = recent ? getRecent(false) : getSaved(); backgroundThread.execute(new Runnable() { @Override public void run() { diff --git a/app/src/main/java/org/solovyev/android/calculator/history/RecentHistory.java b/app/src/main/java/org/solovyev/android/calculator/history/RecentHistory.java index 84c8c717..45022de8 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/RecentHistory.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/RecentHistory.java @@ -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 states) { + public void addInitial(@NonNull List states) { + Check.isMainThread(); for (HistoryState state : states) { - add(state); + list.add(0, state); } + current += states.size(); + trim(); } public void remove(@NonNull HistoryState state) {