CurrentHistory -> RecentHistory

This commit is contained in:
serso 2016-01-13 10:38:52 +01:00
parent 269df2f727
commit 96788ccc1f
5 changed files with 67 additions and 58 deletions

View File

@ -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),

View File

@ -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<HistoryState> saved = new ArrayList<>();
@Nonnull
@ -89,10 +89,23 @@ public class History {
if (TextUtils.isEmpty(xml)) {
return;
}
final List<HistoryState> states = convertOldHistory(xml);
if (states == null) {
return;
}
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<HistoryState> convertOldHistory(String xml) {
final OldHistory history = OldHistory.fromXml(xml);
if (history == null) {
// strange, history seems to be broken. Avoid clearing the preference
return;
return null;
}
final List<HistoryState> states = new ArrayList<>();
for (OldHistoryState state : history.getItems()) {
@ -105,11 +118,7 @@ public class History {
: 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());
} catch (Exception e) {
Locator.getInstance().getLogger().error(TAG, e.getMessage(), e);
}
return states;
}
@NonNull
@ -118,8 +127,8 @@ public class History {
}
@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<HistoryState> currentStates = loadStates(getCurrentHistoryFile());
final List<HistoryState> recentStates = loadStates(getRecentHistoryFile());
final List<HistoryState> 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<HistoryState> getCurrent() {
public List<HistoryState> getRecent() {
Check.isMainThread();
final List<HistoryState> result = new LinkedList<>();
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();
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<HistoryState> states = current ? getCurrent() : getSaved();
// don't need to save intermediate states, thus {@link History#getRecent}
final List<HistoryState> 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());
}
});

View File

@ -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;

View File

@ -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<HistoryState> 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();
}
}

View File

@ -63,13 +63,13 @@ public class HistoryTest {
addState(history, "2354");
addState(history, "23547");
final List<HistoryState> states = history.getCurrent();
final List<HistoryState> 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()));
}
}