CurrentHistory -> RecentHistory
This commit is contained in:
parent
269df2f727
commit
96788ccc1f
@ -26,7 +26,7 @@ import android.support.v4.app.Fragment;
|
|||||||
|
|
||||||
import org.solovyev.android.calculator.about.CalculatorAboutFragment;
|
import org.solovyev.android.calculator.about.CalculatorAboutFragment;
|
||||||
import org.solovyev.android.calculator.about.CalculatorReleaseNotesFragment;
|
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.history.SavedHistoryFragment;
|
||||||
import org.solovyev.android.calculator.math.edit.CalculatorFunctionsFragment;
|
import org.solovyev.android.calculator.math.edit.CalculatorFunctionsFragment;
|
||||||
import org.solovyev.android.calculator.math.edit.CalculatorOperatorsFragment;
|
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),
|
editor(CalculatorEditorFragment.class, R.layout.cpp_app_editor, R.string.editor),
|
||||||
//display(CalculatorHistoryFragment.class, "history", R.layout.history_fragment, R.string.c_history),
|
//display(CalculatorHistoryFragment.class, "history", R.layout.history_fragment, R.string.c_history),
|
||||||
//keyboard(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),
|
saved_history(SavedHistoryFragment.class, R.layout.history_fragment, R.string.c_saved_history),
|
||||||
variables(CalculatorVarsFragment.class, R.layout.vars_fragment, R.string.c_vars),
|
variables(CalculatorVarsFragment.class, R.layout.vars_fragment, R.string.c_vars),
|
||||||
functions(CalculatorFunctionsFragment.class, R.layout.math_entities_fragment, R.string.c_functions),
|
functions(CalculatorFunctionsFragment.class, R.layout.math_entities_fragment, R.string.c_functions),
|
||||||
|
@ -59,11 +59,11 @@ public class History {
|
|||||||
|
|
||||||
public static final String TAG = App.subTag("History");
|
public static final String TAG = App.subTag("History");
|
||||||
@NonNull
|
@NonNull
|
||||||
private final Runnable writeCurrent = new WriteTask(true);
|
private final Runnable writeRecent = new WriteTask(true);
|
||||||
@NonNull
|
@NonNull
|
||||||
private final Runnable writeSaved = new WriteTask(false);
|
private final Runnable writeSaved = new WriteTask(false);
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private final HistoryList current = new HistoryList();
|
private final RecentHistory recent = new RecentHistory();
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private final List<HistoryState> saved = new ArrayList<>();
|
private final List<HistoryState> saved = new ArrayList<>();
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@ -89,37 +89,46 @@ public class History {
|
|||||||
if (TextUtils.isEmpty(xml)) {
|
if (TextUtils.isEmpty(xml)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final OldHistory history = OldHistory.fromXml(xml);
|
final List<HistoryState> states = convertOldHistory(xml);
|
||||||
if (history == null) {
|
if (states == null) {
|
||||||
// strange, history seems to be broken. Avoid clearing the preference
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final List<HistoryState> states = new ArrayList<>();
|
final JSONArray json = RecentHistory.toJson(states);
|
||||||
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);
|
|
||||||
FileSaver.save(getSavedHistoryFile(), json.toString());
|
FileSaver.save(getSavedHistoryFile(), json.toString());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Locator.getInstance().getLogger().error(TAG, e.getMessage(), e);
|
Locator.getInstance().getLogger().error(TAG, e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private static List<HistoryState> 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<HistoryState> 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
|
@NonNull
|
||||||
private static File getSavedHistoryFile() {
|
private static File getSavedHistoryFile() {
|
||||||
return new File(App.getApplication().getFilesDir(), "history-saved.json");
|
return new File(App.getApplication().getFilesDir(), "history-saved.json");
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private static File getCurrentHistoryFile() {
|
private static File getRecentHistoryFile() {
|
||||||
return new File(App.getApplication().getFilesDir(), "history-current.json");
|
return new File(App.getApplication().getFilesDir(), "history-recent.json");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@ -132,7 +141,7 @@ public class History {
|
|||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return HistoryList.fromJson(new JSONArray(json.toString()));
|
return RecentHistory.fromJson(new JSONArray(json.toString()));
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
Locator.getInstance().getLogger().error(TAG, e.getMessage(), e);
|
Locator.getInstance().getLogger().error(TAG, e.getMessage(), e);
|
||||||
}
|
}
|
||||||
@ -142,25 +151,25 @@ public class History {
|
|||||||
private void init() {
|
private void init() {
|
||||||
Check.isNotMainThread();
|
Check.isNotMainThread();
|
||||||
migrateOldHistory();
|
migrateOldHistory();
|
||||||
final List<HistoryState> currentStates = loadStates(getCurrentHistoryFile());
|
final List<HistoryState> recentStates = loadStates(getRecentHistoryFile());
|
||||||
final List<HistoryState> savedStates = loadStates(getSavedHistoryFile());
|
final List<HistoryState> savedStates = loadStates(getSavedHistoryFile());
|
||||||
handler.post(new Runnable() {
|
handler.post(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Check.isTrue(current.isEmpty());
|
Check.isTrue(recent.isEmpty());
|
||||||
Check.isTrue(saved.isEmpty());
|
Check.isTrue(saved.isEmpty());
|
||||||
current.addAll(currentStates);
|
recent.addAll(recentStates);
|
||||||
saved.addAll(savedStates);
|
saved.addAll(savedStates);
|
||||||
initialized = true;
|
initialized = true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addCurrent(@Nonnull HistoryState state) {
|
public void addRecent(@Nonnull HistoryState state) {
|
||||||
Check.isMainThread();
|
Check.isMainThread();
|
||||||
current.add(state);
|
recent.add(state);
|
||||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.history_state_added, state);
|
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.history_state_added, state);
|
||||||
onCurrentChanged();
|
onRecentChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addSaved(@Nonnull HistoryState state) {
|
public void addSaved(@Nonnull HistoryState state) {
|
||||||
@ -169,9 +178,9 @@ public class History {
|
|||||||
onSavedChanged();
|
onSavedChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onCurrentChanged() {
|
private void onRecentChanged() {
|
||||||
handler.removeCallbacks(writeCurrent);
|
handler.removeCallbacks(writeRecent);
|
||||||
handler.postDelayed(writeCurrent, 500);
|
handler.postDelayed(writeRecent, 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onSavedChanged() {
|
private void onSavedChanged() {
|
||||||
@ -180,14 +189,14 @@ public class History {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public List<HistoryState> getCurrent() {
|
public List<HistoryState> getRecent() {
|
||||||
Check.isMainThread();
|
Check.isMainThread();
|
||||||
|
|
||||||
final List<HistoryState> result = new LinkedList<>();
|
final List<HistoryState> result = new LinkedList<>();
|
||||||
|
|
||||||
final String groupingSeparator = AndroidCalculatorEngine.Preferences.groupingSeparator.getPreference(App.getPreferences());
|
final String groupingSeparator = AndroidCalculatorEngine.Preferences.groupingSeparator.getPreference(App.getPreferences());
|
||||||
|
|
||||||
final List<HistoryState> states = current.asList();
|
final List<HistoryState> states = recent.asList();
|
||||||
final int statesCount = states.size();
|
final int statesCount = states.size();
|
||||||
for (int i = 1; i < statesCount; i++) {
|
for (int i = 1; i < statesCount; i++) {
|
||||||
final HistoryState olderState = states.get(i - 1);
|
final HistoryState olderState = states.get(i - 1);
|
||||||
@ -260,10 +269,10 @@ public class History {
|
|||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clearCurrent() {
|
public void clearRecent() {
|
||||||
Check.isMainThread();
|
Check.isMainThread();
|
||||||
current.clear();
|
recent.clear();
|
||||||
onCurrentChanged();
|
onRecentChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clearSaved() {
|
public void clearSaved() {
|
||||||
@ -273,7 +282,7 @@ public class History {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void undo() {
|
public void undo() {
|
||||||
final HistoryState state = current.undo();
|
final HistoryState state = recent.undo();
|
||||||
if (state == null) {
|
if (state == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -281,7 +290,7 @@ public class History {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void redo() {
|
public void redo() {
|
||||||
final HistoryState state = current.redo();
|
final HistoryState state = recent.redo();
|
||||||
if (state == null) {
|
if (state == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -299,10 +308,10 @@ public class History {
|
|||||||
onSavedChanged();
|
onSavedChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeCurrent(@Nonnull HistoryState state) {
|
public void removeRecent(@Nonnull HistoryState state) {
|
||||||
Check.isMainThread();
|
Check.isMainThread();
|
||||||
current.remove(state);
|
recent.remove(state);
|
||||||
onCurrentChanged();
|
onRecentChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
@ -324,27 +333,27 @@ public class History {
|
|||||||
if (lastEditorState.sequence != e.newState.sequence) {
|
if (lastEditorState.sequence != e.newState.sequence) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
addCurrent(HistoryState.newBuilder(lastEditorState, e.newState).build());
|
addRecent(HistoryState.newBuilder(lastEditorState, e.newState).build());
|
||||||
lastEditorState = null;
|
lastEditorState = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class WriteTask implements Runnable {
|
private class WriteTask implements Runnable {
|
||||||
private final boolean current;
|
private final boolean recent;
|
||||||
|
|
||||||
public WriteTask(boolean current) {
|
public WriteTask(boolean recent) {
|
||||||
this.current = current;
|
this.recent = recent;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Check.isMainThread();
|
Check.isMainThread();
|
||||||
// don't need to save intermediate states, thus {@link History#getCurrent}
|
// don't need to save intermediate states, thus {@link History#getRecent}
|
||||||
final List<HistoryState> states = current ? getCurrent() : getSaved();
|
final List<HistoryState> states = recent ? getRecent() : getSaved();
|
||||||
App.getBackground().execute(new Runnable() {
|
App.getBackground().execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
final File file = current ? getCurrentHistoryFile() : getSavedHistoryFile();
|
final File file = recent ? getRecentHistoryFile() : getSavedHistoryFile();
|
||||||
final JSONArray array = HistoryList.toJson(states);
|
final JSONArray array = RecentHistory.toJson(states);
|
||||||
FileSaver.save(file, array.toString());
|
FileSaver.save(file, array.toString());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -14,7 +14,7 @@ import java.util.Collections;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class HistoryList {
|
public class RecentHistory {
|
||||||
|
|
||||||
private static final int MAX_HISTORY = 20;
|
private static final int MAX_HISTORY = 20;
|
||||||
|
|
@ -26,13 +26,13 @@ import org.solovyev.android.calculator.CalculatorFragmentType;
|
|||||||
import org.solovyev.android.calculator.Locator;
|
import org.solovyev.android.calculator.Locator;
|
||||||
import org.solovyev.android.calculator.R;
|
import org.solovyev.android.calculator.R;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
|
|
||||||
import java.util.List;
|
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);
|
super(CalculatorFragmentType.history);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,12 +44,12 @@ public class HistoryFragment extends BaseHistoryFragment {
|
|||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
protected List<HistoryState> getHistoryItems() {
|
protected List<HistoryState> getHistoryItems() {
|
||||||
return Locator.getInstance().getHistory().getCurrent();
|
return Locator.getInstance().getHistory().getRecent();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void clearHistory() {
|
protected void clearHistory() {
|
||||||
Locator.getInstance().getHistory().clearCurrent();
|
Locator.getInstance().getHistory().clearRecent();
|
||||||
getAdapter().clear();
|
getAdapter().clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -63,13 +63,13 @@ public class HistoryTest {
|
|||||||
addState(history, "2354");
|
addState(history, "2354");
|
||||||
addState(history, "23547");
|
addState(history, "23547");
|
||||||
|
|
||||||
final List<HistoryState> states = history.getCurrent();
|
final List<HistoryState> states = history.getRecent();
|
||||||
Assert.assertEquals(2, states.size());
|
Assert.assertEquals(2, states.size());
|
||||||
Assert.assertEquals("23547", states.get(1).editor.getTextString());
|
Assert.assertEquals("23547", states.get(1).editor.getTextString());
|
||||||
Assert.assertEquals("123+3", states.get(0).editor.getTextString());
|
Assert.assertEquals("123+3", states.get(0).editor.getTextString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addState(@Nonnull History history, @Nonnull String text) {
|
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()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user