From 96788ccc1f12fa935ed14a003a56af5b2c0d9d9d Mon Sep 17 00:00:00 2001 From: serso Date: Wed, 13 Jan 2016 10:38:52 +0100 Subject: [PATCH] CurrentHistory -> RecentHistory --- .../calculator/CalculatorFragmentType.java | 4 +- .../android/calculator/history/History.java | 103 ++++++++++-------- .../{HistoryList.java => RecentHistory.java} | 2 +- ...agment.java => RecentHistoryFragment.java} | 12 +- .../calculator/history/HistoryTest.java | 4 +- 5 files changed, 67 insertions(+), 58 deletions(-) rename app/src/main/java/org/solovyev/android/calculator/history/{HistoryList.java => RecentHistory.java} (99%) rename app/src/main/java/org/solovyev/android/calculator/history/{HistoryFragment.java => RecentHistoryFragment.java} (85%) diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorFragmentType.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorFragmentType.java index 906c33df..ff7e5015 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorFragmentType.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorFragmentType.java @@ -26,7 +26,7 @@ import android.support.v4.app.Fragment; import org.solovyev.android.calculator.about.CalculatorAboutFragment; import org.solovyev.android.calculator.about.CalculatorReleaseNotesFragment; -import org.solovyev.android.calculator.history.HistoryFragment; +import org.solovyev.android.calculator.history.RecentHistoryFragment; import org.solovyev.android.calculator.history.SavedHistoryFragment; import org.solovyev.android.calculator.math.edit.CalculatorFunctionsFragment; import org.solovyev.android.calculator.math.edit.CalculatorOperatorsFragment; @@ -50,7 +50,7 @@ public enum CalculatorFragmentType { editor(CalculatorEditorFragment.class, R.layout.cpp_app_editor, R.string.editor), //display(CalculatorHistoryFragment.class, "history", R.layout.history_fragment, R.string.c_history), //keyboard(CalculatorHistoryFragment.class, "history", R.layout.history_fragment, R.string.c_history), - history(HistoryFragment.class, R.layout.history_fragment, R.string.c_history), + history(RecentHistoryFragment.class, R.layout.history_fragment, R.string.c_history), saved_history(SavedHistoryFragment.class, R.layout.history_fragment, R.string.c_saved_history), variables(CalculatorVarsFragment.class, R.layout.vars_fragment, R.string.c_vars), functions(CalculatorFunctionsFragment.class, R.layout.math_entities_fragment, R.string.c_functions), 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 08bc80fe..89c3a485 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 @@ -59,11 +59,11 @@ public class History { public static final String TAG = App.subTag("History"); @NonNull - private final Runnable writeCurrent = new WriteTask(true); + private final Runnable writeRecent = new WriteTask(true); @NonNull private final Runnable writeSaved = new WriteTask(false); @Nonnull - private final HistoryList current = new HistoryList(); + private final RecentHistory recent = new RecentHistory(); @Nonnull private final List saved = new ArrayList<>(); @Nonnull @@ -89,37 +89,46 @@ public class History { if (TextUtils.isEmpty(xml)) { return; } - final OldHistory history = OldHistory.fromXml(xml); - if (history == null) { - // strange, history seems to be broken. Avoid clearing the preference + final List states = convertOldHistory(xml); + if (states == null) { return; } - final List states = new ArrayList<>(); - for (OldHistoryState state : history.getItems()) { - final OldEditorHistoryState oldEditorState = state.getEditorState(); - final OldDisplayHistoryState oldDisplayState = state.getDisplayState(); - final String editorText = oldEditorState.getText(); - final EditorState editor = EditorState.create(Strings.nullToEmpty(editorText), oldEditorState.getCursorPosition()); - final DisplayState display = oldDisplayState.isValid() - ? DisplayState.createValid(oldDisplayState.getJsclOperation(), null, Strings.nullToEmpty(oldDisplayState.getEditorState().getText()), EditorState.NO_SEQUENCE) - : DisplayState.createError(oldDisplayState.getJsclOperation(), "", EditorState.NO_SEQUENCE); - states.add(HistoryState.newBuilder(editor, display).build()); - } - final JSONArray json = HistoryList.toJson(states); + final JSONArray json = RecentHistory.toJson(states); FileSaver.save(getSavedHistoryFile(), json.toString()); } catch (Exception e) { Locator.getInstance().getLogger().error(TAG, e.getMessage(), e); } } + @Nullable + private static List convertOldHistory(String xml) { + final OldHistory history = OldHistory.fromXml(xml); + if (history == null) { + // strange, history seems to be broken. Avoid clearing the preference + return null; + } + final List states = new ArrayList<>(); + for (OldHistoryState state : history.getItems()) { + final OldEditorHistoryState oldEditorState = state.getEditorState(); + final OldDisplayHistoryState oldDisplayState = state.getDisplayState(); + final String editorText = oldEditorState.getText(); + final EditorState editor = EditorState.create(Strings.nullToEmpty(editorText), oldEditorState.getCursorPosition()); + final DisplayState display = oldDisplayState.isValid() + ? DisplayState.createValid(oldDisplayState.getJsclOperation(), null, Strings.nullToEmpty(oldDisplayState.getEditorState().getText()), EditorState.NO_SEQUENCE) + : DisplayState.createError(oldDisplayState.getJsclOperation(), "", EditorState.NO_SEQUENCE); + states.add(HistoryState.newBuilder(editor, display).build()); + } + return states; + } + @NonNull private static File getSavedHistoryFile() { return new File(App.getApplication().getFilesDir(), "history-saved.json"); } @NonNull - private static File getCurrentHistoryFile() { - return new File(App.getApplication().getFilesDir(), "history-current.json"); + private static File getRecentHistoryFile() { + return new File(App.getApplication().getFilesDir(), "history-recent.json"); } @Nonnull @@ -132,7 +141,7 @@ public class History { return Collections.emptyList(); } try { - return HistoryList.fromJson(new JSONArray(json.toString())); + return RecentHistory.fromJson(new JSONArray(json.toString())); } catch (JSONException e) { Locator.getInstance().getLogger().error(TAG, e.getMessage(), e); } @@ -142,25 +151,25 @@ public class History { private void init() { Check.isNotMainThread(); migrateOldHistory(); - final List currentStates = loadStates(getCurrentHistoryFile()); + final List recentStates = loadStates(getRecentHistoryFile()); final List savedStates = loadStates(getSavedHistoryFile()); handler.post(new Runnable() { @Override public void run() { - Check.isTrue(current.isEmpty()); + Check.isTrue(recent.isEmpty()); Check.isTrue(saved.isEmpty()); - current.addAll(currentStates); + recent.addAll(recentStates); saved.addAll(savedStates); initialized = true; } }); } - public void addCurrent(@Nonnull HistoryState state) { + public void addRecent(@Nonnull HistoryState state) { Check.isMainThread(); - current.add(state); + recent.add(state); Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.history_state_added, state); - onCurrentChanged(); + onRecentChanged(); } public void addSaved(@Nonnull HistoryState state) { @@ -169,9 +178,9 @@ public class History { onSavedChanged(); } - private void onCurrentChanged() { - handler.removeCallbacks(writeCurrent); - handler.postDelayed(writeCurrent, 500); + private void onRecentChanged() { + handler.removeCallbacks(writeRecent); + handler.postDelayed(writeRecent, 500); } private void onSavedChanged() { @@ -180,14 +189,14 @@ public class History { } @Nonnull - public List getCurrent() { + public List getRecent() { Check.isMainThread(); final List result = new LinkedList<>(); final String groupingSeparator = AndroidCalculatorEngine.Preferences.groupingSeparator.getPreference(App.getPreferences()); - final List states = current.asList(); + final List states = recent.asList(); final int statesCount = states.size(); for (int i = 1; i < statesCount; i++) { final HistoryState olderState = states.get(i - 1); @@ -260,10 +269,10 @@ public class History { return sb.toString(); } - public void clearCurrent() { + public void clearRecent() { Check.isMainThread(); - current.clear(); - onCurrentChanged(); + recent.clear(); + onRecentChanged(); } public void clearSaved() { @@ -273,7 +282,7 @@ public class History { } public void undo() { - final HistoryState state = current.undo(); + final HistoryState state = recent.undo(); if (state == null) { return; } @@ -281,7 +290,7 @@ public class History { } public void redo() { - final HistoryState state = current.redo(); + final HistoryState state = recent.redo(); if (state == null) { return; } @@ -299,10 +308,10 @@ public class History { onSavedChanged(); } - public void removeCurrent(@Nonnull HistoryState state) { + public void removeRecent(@Nonnull HistoryState state) { Check.isMainThread(); - current.remove(state); - onCurrentChanged(); + recent.remove(state); + onRecentChanged(); } @Subscribe @@ -324,27 +333,27 @@ public class History { if (lastEditorState.sequence != e.newState.sequence) { return; } - addCurrent(HistoryState.newBuilder(lastEditorState, e.newState).build()); + addRecent(HistoryState.newBuilder(lastEditorState, e.newState).build()); lastEditorState = null; } private class WriteTask implements Runnable { - private final boolean current; + private final boolean recent; - public WriteTask(boolean current) { - this.current = current; + public WriteTask(boolean recent) { + this.recent = recent; } @Override public void run() { Check.isMainThread(); - // don't need to save intermediate states, thus {@link History#getCurrent} - final List states = current ? getCurrent() : getSaved(); + // don't need to save intermediate states, thus {@link History#getRecent} + final List states = recent ? getRecent() : getSaved(); App.getBackground().execute(new Runnable() { @Override public void run() { - final File file = current ? getCurrentHistoryFile() : getSavedHistoryFile(); - final JSONArray array = HistoryList.toJson(states); + final File file = recent ? getRecentHistoryFile() : getSavedHistoryFile(); + final JSONArray array = RecentHistory.toJson(states); FileSaver.save(file, array.toString()); } }); diff --git a/app/src/main/java/org/solovyev/android/calculator/history/HistoryList.java b/app/src/main/java/org/solovyev/android/calculator/history/RecentHistory.java similarity index 99% rename from app/src/main/java/org/solovyev/android/calculator/history/HistoryList.java rename to app/src/main/java/org/solovyev/android/calculator/history/RecentHistory.java index 53b9c4ec..0c78d8da 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/HistoryList.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/RecentHistory.java @@ -14,7 +14,7 @@ import java.util.Collections; import java.util.LinkedList; import java.util.List; -public class HistoryList { +public class RecentHistory { private static final int MAX_HISTORY = 20; diff --git a/app/src/main/java/org/solovyev/android/calculator/history/HistoryFragment.java b/app/src/main/java/org/solovyev/android/calculator/history/RecentHistoryFragment.java similarity index 85% rename from app/src/main/java/org/solovyev/android/calculator/history/HistoryFragment.java rename to app/src/main/java/org/solovyev/android/calculator/history/RecentHistoryFragment.java index 7c45044a..e191879a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/HistoryFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/RecentHistoryFragment.java @@ -26,13 +26,13 @@ import org.solovyev.android.calculator.CalculatorFragmentType; import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.R; -import javax.annotation.Nonnull; - import java.util.List; -public class HistoryFragment extends BaseHistoryFragment { +import javax.annotation.Nonnull; - public HistoryFragment() { +public class RecentHistoryFragment extends BaseHistoryFragment { + + public RecentHistoryFragment() { super(CalculatorFragmentType.history); } @@ -44,12 +44,12 @@ public class HistoryFragment extends BaseHistoryFragment { @Nonnull @Override protected List getHistoryItems() { - return Locator.getInstance().getHistory().getCurrent(); + return Locator.getInstance().getHistory().getRecent(); } @Override protected void clearHistory() { - Locator.getInstance().getHistory().clearCurrent(); + Locator.getInstance().getHistory().clearRecent(); getAdapter().clear(); } } diff --git a/app/src/test/java/org/solovyev/android/calculator/history/HistoryTest.java b/app/src/test/java/org/solovyev/android/calculator/history/HistoryTest.java index 8bd78c4d..c43a5455 100644 --- a/app/src/test/java/org/solovyev/android/calculator/history/HistoryTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/history/HistoryTest.java @@ -63,13 +63,13 @@ public class HistoryTest { addState(history, "2354"); addState(history, "23547"); - final List states = history.getCurrent(); + final List states = history.getRecent(); Assert.assertEquals(2, states.size()); Assert.assertEquals("23547", states.get(1).editor.getTextString()); Assert.assertEquals("123+3", states.get(0).editor.getTextString()); } private void addState(@Nonnull History history, @Nonnull String text) { - history.addCurrent(HistoryState.newBuilder(EditorState.create(text, 3), DisplayState.empty())); + history.addRecent(HistoryState.newBuilder(EditorState.create(text, 3), DisplayState.empty())); } }