History refactor

This commit is contained in:
serso 2016-01-10 23:27:14 +01:00
parent 0badc913c0
commit b21d8084f0
25 changed files with 328 additions and 651 deletions

View File

@ -22,6 +22,8 @@
package org.solovyev.android; package org.solovyev.android;
import android.os.Looper;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Collection; import java.util.Collection;
@ -48,9 +50,15 @@ public final class Check {
} }
public static void isMainThread() { public static void isMainThread() {
/*if (!junit && Looper.getMainLooper() != Looper.myLooper()) { if (!junit && Looper.getMainLooper() != Looper.myLooper()) {
throw new AssertionException("Should be called on the main thread"); throw new AssertionException("Should be called on the main thread");
}*/ }
}
public static void isNotMainThread() {
if (!junit && Looper.getMainLooper() == Looper.myLooper()) {
throw new AssertionException("Should not be called on the main thread");
}
} }
public static void isNotNull(@Nullable Object o) { public static void isNotNull(@Nullable Object o) {

View File

@ -27,7 +27,7 @@ import android.content.SharedPreferences;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import jscl.NumeralBase; import jscl.NumeralBase;
import jscl.math.Generic; import jscl.math.Generic;
import org.solovyev.android.calculator.history.HistoryState; import org.solovyev.android.calculator.history.OldHistoryState;
import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.common.history.HistoryAction; import org.solovyev.common.history.HistoryAction;
import org.solovyev.common.msg.Message; import org.solovyev.common.msg.Message;
@ -156,12 +156,12 @@ public class AndroidCalculator implements Calculator, CalculatorEventListener, S
@Override @Override
@Nonnull @Nonnull
public HistoryState getCurrentHistoryState() { public OldHistoryState getCurrentHistoryState() {
return calculator.getCurrentHistoryState(); return calculator.getCurrentHistoryState();
} }
@Override @Override
public void setCurrentHistoryState(@Nonnull HistoryState editorHistoryState) { public void setCurrentHistoryState(@Nonnull OldHistoryState editorHistoryState) {
calculator.setCurrentHistoryState(editorHistoryState); calculator.setCurrentHistoryState(editorHistoryState);
} }

View File

@ -40,7 +40,7 @@ import org.solovyev.android.views.dragbutton.DragButton;
import org.solovyev.android.views.dragbutton.DragDirection; import org.solovyev.android.views.dragbutton.DragDirection;
import org.solovyev.android.views.dragbutton.DragListener; import org.solovyev.android.views.dragbutton.DragListener;
import org.solovyev.android.views.dragbutton.SimpleDragListener; import org.solovyev.android.views.dragbutton.SimpleDragListener;
import org.solovyev.android.calculator.history.HistoryState; import org.solovyev.android.calculator.history.OldHistoryState;
import org.solovyev.android.calculator.history.HistoryDragProcessor; import org.solovyev.android.calculator.history.HistoryDragProcessor;
import org.solovyev.android.calculator.view.AngleUnitsButton; import org.solovyev.android.calculator.view.AngleUnitsButton;
import org.solovyev.android.calculator.view.LongClickEraser; import org.solovyev.android.calculator.view.LongClickEraser;
@ -165,7 +165,7 @@ public abstract class BaseUi implements SharedPreferences.OnSharedPreferenceChan
final ViewsCache views = ViewsCache.forView(root); final ViewsCache views = ViewsCache.forView(root);
setOnDragListeners(views, activity); setOnDragListeners(views, activity);
HistoryDragProcessor<HistoryState> historyDragProcessor = new HistoryDragProcessor<>(getCalculator()); HistoryDragProcessor<OldHistoryState> historyDragProcessor = new HistoryDragProcessor<>(getCalculator());
final DragListener historyDragListener = newDragListener(historyDragProcessor, activity); final DragListener historyDragListener = newDragListener(historyDragProcessor, activity);
final DragButton historyButton = getButton(views, R.id.cpp_button_history); final DragButton historyButton = getButton(views, R.id.cpp_button_history);
if (historyButton != null) { if (historyButton != null) {

View File

@ -24,7 +24,7 @@ package org.solovyev.android.calculator;
import jscl.NumeralBase; import jscl.NumeralBase;
import jscl.math.Generic; import jscl.math.Generic;
import org.solovyev.android.calculator.history.HistoryState; import org.solovyev.android.calculator.history.OldHistoryState;
import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.common.history.HistoryControl; import org.solovyev.common.history.HistoryControl;
@ -36,7 +36,7 @@ import javax.annotation.Nullable;
* Date: 20.09.12 * Date: 20.09.12
* Time: 16:38 * Time: 16:38
*/ */
public interface Calculator extends CalculatorEventContainer, HistoryControl<HistoryState> { public interface Calculator extends CalculatorEventContainer, HistoryControl<OldHistoryState> {
void init(); void init();

View File

@ -30,7 +30,7 @@ import org.acra.ACRA;
import org.acra.ACRAConfiguration; import org.acra.ACRAConfiguration;
import org.acra.sender.HttpSender; import org.acra.sender.HttpSender;
import org.solovyev.android.Android; import org.solovyev.android.Android;
import org.solovyev.android.calculator.history.AndroidCalculatorHistory; import org.solovyev.android.calculator.history.CalculatorHistory;
import org.solovyev.android.calculator.language.Language; import org.solovyev.android.calculator.language.Language;
import org.solovyev.android.calculator.language.Languages; import org.solovyev.android.calculator.language.Languages;
import org.solovyev.android.calculator.model.AndroidCalculatorEngine; import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
@ -74,7 +74,7 @@ public class CalculatorApplication extends android.app.Application implements Sh
new AndroidCalculatorEngine(this), new AndroidCalculatorEngine(this),
new AndroidCalculatorClipboard(this), new AndroidCalculatorClipboard(this),
new AndroidCalculatorNotifier(this), new AndroidCalculatorNotifier(this),
new AndroidCalculatorHistory(this, calculator), new CalculatorHistory(),
new AndroidCalculatorLogger(), new AndroidCalculatorLogger(),
new AndroidCalculatorPreferenceService(this), new AndroidCalculatorPreferenceService(this),
new CalculatorKeyboard(), new CalculatorKeyboard(),
@ -93,14 +93,7 @@ public class CalculatorApplication extends android.app.Application implements Sh
App.getInitializer().execute(new Runnable() { App.getInitializer().execute(new Runnable() {
@Override @Override
public void run() { public void run() {
try { warmUpEngine();
// warm-up engine
CalculatorMathEngine mathEngine = Locator.getInstance().getEngine().getMathEngine();
mathEngine.evaluate("1+1");
mathEngine.evaluate("1*1");
} catch (Throwable e) {
Log.e(App.TAG, e.getMessage(), e);
}
} }
}); });
@ -113,6 +106,17 @@ public class CalculatorApplication extends android.app.Application implements Sh
}, 100); }, 100);
} }
private void warmUpEngine() {
try {
// warm-up engine
CalculatorMathEngine mathEngine = Locator.getInstance().getEngine().getMathEngine();
mathEngine.evaluate("1+1");
mathEngine.evaluate("1*1");
} catch (Throwable e) {
Log.e(App.TAG, e.getMessage(), e);
}
}
private void onPreCreate(@Nonnull SharedPreferences preferences, @Nonnull Languages languages) { private void onPreCreate(@Nonnull SharedPreferences preferences, @Nonnull Languages languages) {
// first we need to setup crash handler and memory leak analyzer // first we need to setup crash handler and memory leak analyzer
if (!BuildConfig.DEBUG) { if (!BuildConfig.DEBUG) {

View File

@ -33,7 +33,7 @@ import jscl.math.function.IConstant;
import jscl.math.operator.Operator; import jscl.math.operator.Operator;
import jscl.text.ParseInterruptedException; import jscl.text.ParseInterruptedException;
import org.solovyev.android.calculator.history.CalculatorHistory; import org.solovyev.android.calculator.history.CalculatorHistory;
import org.solovyev.android.calculator.history.HistoryState; import org.solovyev.android.calculator.history.OldHistoryState;
import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.android.calculator.model.Var; import org.solovyev.android.calculator.model.Var;
import org.solovyev.android.calculator.text.TextProcessor; import org.solovyev.android.calculator.text.TextProcessor;
@ -560,7 +560,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
public void doHistoryAction(@Nonnull HistoryAction historyAction) { public void doHistoryAction(@Nonnull HistoryAction historyAction) {
final CalculatorHistory history = Locator.getInstance().getHistory(); final CalculatorHistory history = Locator.getInstance().getHistory();
if (history.isActionAvailable(historyAction)) { if (history.isActionAvailable(historyAction)) {
final HistoryState newState = history.doAction(historyAction, getCurrentHistoryState()); final OldHistoryState newState = history.doAction(historyAction, getCurrentHistoryState());
if (newState != null) { if (newState != null) {
setCurrentHistoryState(newState); setCurrentHistoryState(newState);
} }
@ -569,12 +569,12 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
@Nonnull @Nonnull
@Override @Override
public HistoryState getCurrentHistoryState() { public OldHistoryState getCurrentHistoryState() {
return HistoryState.create(getEditor(), getDisplay()); return OldHistoryState.create(getEditor(), getDisplay());
} }
@Override @Override
public void setCurrentHistoryState(@Nonnull HistoryState editorHistoryState) { public void setCurrentHistoryState(@Nonnull OldHistoryState editorHistoryState) {
editorHistoryState.setValuesFromHistory(getEditor(), getDisplay()); editorHistoryState.setValuesFromHistory(getEditor(), getDisplay());
} }

View File

@ -23,7 +23,7 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import org.solovyev.android.Check; import org.solovyev.android.Check;
import org.solovyev.android.calculator.history.EditorHistoryState; import org.solovyev.android.calculator.history.OldEditorHistoryState;
import org.solovyev.android.calculator.text.TextProcessor; import org.solovyev.android.calculator.text.TextProcessor;
import org.solovyev.android.calculator.text.TextProcessorEditorResult; import org.solovyev.android.calculator.text.TextProcessorEditorResult;
import org.solovyev.common.text.Strings; import org.solovyev.common.text.Strings;
@ -104,7 +104,7 @@ public class Editor {
} }
@Nonnull @Nonnull
public EditorState setState(@Nonnull EditorHistoryState state) { public EditorState setState(@Nonnull OldEditorHistoryState state) {
Check.isMainThread(); Check.isMainThread();
return setText(Strings.getNotEmpty(state.getText(), ""), state.getCursorPosition()); return setText(Strings.getNotEmpty(state.getText(), ""), state.getCursorPosition());
} }

View File

@ -108,8 +108,6 @@ public final class Preferences {
applyDefaultPreference(preferences, Gui.language); applyDefaultPreference(preferences, Gui.language);
applyDefaultPreference(preferences, Graph.plotImag); applyDefaultPreference(preferences, Graph.plotImag);
applyDefaultPreference(preferences, History.showIntermediateCalculations);
applyDefaultPreference(preferences, History.showDatetime);
applyDefaultPreference(preferences, Calculations.calculateOnFly); applyDefaultPreference(preferences, Calculations.calculateOnFly);
applyDefaultPreference(preferences, Calculations.preferredAngleUnits); applyDefaultPreference(preferences, Calculations.preferredAngleUnits);
applyDefaultPreference(preferences, Calculations.preferredNumeralBase); applyDefaultPreference(preferences, Calculations.preferredNumeralBase);

View File

@ -1,175 +0,0 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.calculator.history;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import org.solovyev.android.calculator.Calculator;
import org.solovyev.android.calculator.CalculatorEventData;
import org.solovyev.android.calculator.CalculatorEventType;
import org.solovyev.common.history.HistoryAction;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* User: serso
* Date: 10/9/11
* Time: 6:35 PM
*/
public class AndroidCalculatorHistory implements CalculatorHistory {
@Nonnull
private final CalculatorHistoryImpl calculatorHistory;
@Nonnull
private final Context context;
public AndroidCalculatorHistory(@Nonnull Application application, @Nonnull Calculator calculator) {
this.context = application;
calculatorHistory = new CalculatorHistoryImpl(calculator);
}
@Override
public void load() {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences != null) {
final String value = preferences.getString("org.solovyev.android.calculator.CalculatorModel_history", null);
if (value != null) {
calculatorHistory.fromXml(value);
}
}
}
public void save() {
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
final SharedPreferences.Editor editor = settings.edit();
editor.putString("org.solovyev.android.calculator.CalculatorModel_history", calculatorHistory.toXml());
editor.apply();
}
public void clearSavedHistory() {
calculatorHistory.clearSavedHistory();
save();
}
public void removeSavedHistory(@Nonnull HistoryState historyState) {
historyState.setSaved(false);
calculatorHistory.removeSavedHistory(historyState);
save();
}
@Override
public boolean isEmpty() {
return calculatorHistory.isEmpty();
}
@Override
public HistoryState getLastHistoryState() {
return calculatorHistory.getLastHistoryState();
}
@Override
public boolean isUndoAvailable() {
return calculatorHistory.isUndoAvailable();
}
@Override
public HistoryState undo(@Nullable HistoryState currentState) {
return calculatorHistory.undo(currentState);
}
@Override
public boolean isRedoAvailable() {
return calculatorHistory.isRedoAvailable();
}
@Override
public HistoryState redo(@Nullable HistoryState currentState) {
return calculatorHistory.redo(currentState);
}
@Override
public boolean isActionAvailable(@Nonnull HistoryAction historyAction) {
return calculatorHistory.isActionAvailable(historyAction);
}
@Override
public HistoryState doAction(@Nonnull HistoryAction historyAction, @Nullable HistoryState currentState) {
return calculatorHistory.doAction(historyAction, currentState);
}
@Override
public void addState(@Nullable HistoryState currentState) {
calculatorHistory.addState(currentState);
}
@Nonnull
@Override
public List<HistoryState> getStates() {
return calculatorHistory.getStates();
}
@Nonnull
@Override
public List<HistoryState> getStates(boolean includeIntermediateStates) {
return calculatorHistory.getStates(includeIntermediateStates);
}
@Override
public void clear() {
calculatorHistory.clear();
}
@Nonnull
public List<HistoryState> getSavedHistory() {
return calculatorHistory.getSavedHistory();
}
@Nonnull
public HistoryState addSavedState(@Nonnull HistoryState historyState) {
return calculatorHistory.addSavedState(historyState);
}
@Override
public void fromXml(@Nonnull String xml) {
calculatorHistory.fromXml(xml);
}
@Override
public String toXml() {
return calculatorHistory.toXml();
}
@Override
public void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, @Nonnull CalculatorEventType calculatorEventType, @Nullable Object data) {
calculatorHistory.onCalculatorEvent(calculatorEventData, calculatorEventType, data);
}
}

View File

@ -61,9 +61,9 @@ import static org.solovyev.android.calculator.CalculatorEventType.clear_history_
public abstract class BaseHistoryFragment extends ListFragment implements CalculatorEventListener { public abstract class BaseHistoryFragment extends ListFragment implements CalculatorEventListener {
public static final Comparator<HistoryState> COMPARATOR = new Comparator<HistoryState>() { public static final Comparator<OldHistoryState> COMPARATOR = new Comparator<OldHistoryState>() {
@Override @Override
public int compare(HistoryState state1, HistoryState state2) { public int compare(OldHistoryState state1, OldHistoryState state2) {
if (state1.isSaved() == state2.isSaved()) { if (state1.isSaved() == state2.isSaved()) {
long l = state2.getTime() - state1.getTime(); long l = state2.getTime() - state1.getTime();
return l > 0l ? 1 : (l < 0l ? -1 : 0); return l > 0l ? 1 : (l < 0l ? -1 : 0);
@ -103,15 +103,15 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
ui = new FragmentUi(fragmentType.getDefaultLayoutId(), fragmentType.getDefaultTitleResId(), false); ui = new FragmentUi(fragmentType.getDefaultLayoutId(), fragmentType.getDefaultTitleResId(), false);
} }
public static boolean isAlreadySaved(@Nonnull HistoryState historyState) { public static boolean isAlreadySaved(@Nonnull OldHistoryState historyState) {
Check.isTrue(!historyState.isSaved()); Check.isTrue(!historyState.isSaved());
boolean result = false; boolean result = false;
try { try {
historyState.setSaved(true); historyState.setSaved(true);
if (Collections.contains(historyState, Locator.getInstance().getHistory().getSavedHistory(), new Equalizer<HistoryState>() { if (Collections.contains(historyState, Locator.getInstance().getHistory().getSavedOldHistory(), new Equalizer<OldHistoryState>() {
@Override @Override
public boolean areEqual(@Nullable HistoryState first, @Nullable HistoryState second) { public boolean areEqual(@Nullable OldHistoryState first, @Nullable OldHistoryState second) {
return first != null && second != null && return first != null && second != null &&
first.getTime() == second.getTime() && first.getTime() == second.getTime() &&
first.getDisplayState().equals(second.getDisplayState()) && first.getDisplayState().equals(second.getDisplayState()) &&
@ -127,7 +127,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
} }
@Nonnull @Nonnull
public static String getHistoryText(@Nonnull HistoryState state) { public static String getHistoryText(@Nonnull OldHistoryState state) {
final StringBuilder result = new StringBuilder(); final StringBuilder result = new StringBuilder();
result.append(state.getEditorState().getText()); result.append(state.getEditorState().getText());
result.append(getIdentitySign(state.getDisplayState().getJsclOperation())); result.append(getIdentitySign(state.getDisplayState().getJsclOperation()));
@ -143,7 +143,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
return jsclOperation == JsclOperation.simplify ? "" : "="; return jsclOperation == JsclOperation.simplify ? "" : "=";
} }
public void useState(@Nonnull final HistoryState state) { public void useState(@Nonnull final OldHistoryState state) {
App.getEditor().setState(state.getEditorState()); App.getEditor().setState(state.getEditorState());
final FragmentActivity activity = getActivity(); final FragmentActivity activity = getActivity();
if (!(activity instanceof CalculatorActivity)) { if (!(activity instanceof CalculatorActivity)) {
@ -180,7 +180,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
ui.onViewCreated(this, root); ui.onViewCreated(this, root);
adapter = new HistoryArrayAdapter(this.getActivity(), getItemLayoutId(), org.solovyev.android.calculator.R.id.history_item, new ArrayList<HistoryState>(), showDatetime); adapter = new HistoryArrayAdapter(this.getActivity(), getItemLayoutId(), org.solovyev.android.calculator.R.id.history_item, new ArrayList<OldHistoryState>(), showDatetime);
setListAdapter(adapter); setListAdapter(adapter);
final ListView lv = getListView(); final ListView lv = getListView();
@ -200,7 +200,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
final View view, final View view,
final int position, final int position,
final long id) { final long id) {
useState((HistoryState) parent.getItemAtPosition(position)); useState((OldHistoryState) parent.getItemAtPosition(position));
} }
}); });
@ -221,7 +221,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo); super.onCreateContextMenu(menu, v, menuInfo);
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
final HistoryState state = (HistoryState) getListView().getItemAtPosition(info.position); final OldHistoryState state = (OldHistoryState) getListView().getItemAtPosition(info.position);
if (state.isSaved()) { if (state.isSaved()) {
menu.add(NONE, R.string.c_use, NONE, R.string.c_use); menu.add(NONE, R.string.c_use, NONE, R.string.c_use);
@ -247,7 +247,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
public boolean onContextItemSelected(MenuItem item) { public boolean onContextItemSelected(MenuItem item) {
final Context context = getActivity(); final Context context = getActivity();
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
final HistoryState state = (HistoryState) getListView().getItemAtPosition(info.position); final OldHistoryState state = (OldHistoryState) getListView().getItemAtPosition(info.position);
switch (item.getItemId()) { switch (item.getItemId()) {
case R.string.c_use: case R.string.c_use:
@ -296,7 +296,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
return super.onContextItemSelected(item); return super.onContextItemSelected(item);
} }
private void createEditHistoryDialog(@Nonnull final HistoryState state, @Nonnull final Context context, final boolean save) { private void createEditHistoryDialog(@Nonnull final OldHistoryState state, @Nonnull final Context context, final boolean save) {
final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View editView = layoutInflater.inflate(R.layout.history_edit, null); final View editView = layoutInflater.inflate(R.layout.history_edit, null);
final TextView historyExpression = (TextView) editView.findViewById(R.id.history_edit_expression); final TextView historyExpression = (TextView) editView.findViewById(R.id.history_edit_expression);
@ -313,7 +313,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
if (save) { if (save) {
final HistoryState savedHistoryItem = Locator.getInstance().getHistory().addSavedState(state); final OldHistoryState savedHistoryItem = Locator.getInstance().getHistory().addSavedState(state);
savedHistoryItem.setComment(comment.getText().toString()); savedHistoryItem.setComment(comment.getText().toString());
Locator.getInstance().getHistory().save(); Locator.getInstance().getHistory().save();
// we don't need to add element to the adapter as adapter of another activity must be updated and not this // we don't need to add element to the adapter as adapter of another activity must be updated and not this
@ -331,7 +331,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
builder.create().show(); builder.create().show();
} }
private boolean shouldHaveCopyResult(@Nonnull HistoryState state) { private boolean shouldHaveCopyResult(@Nonnull OldHistoryState state) {
return !state.getDisplayState().isValid() || !Strings.isEmpty(state.getDisplayState().getEditorState().getText()); return !state.getDisplayState().isValid() || !Strings.isEmpty(state.getDisplayState().getEditorState().getText());
} }
@ -365,13 +365,13 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
protected abstract int getItemLayoutId(); protected abstract int getItemLayoutId();
private void updateAdapter() { private void updateAdapter() {
final List<HistoryState> historyList = getHistoryList(); final List<OldHistoryState> historyList = getHistoryList();
final ArrayAdapter<HistoryState> adapter = getAdapter(); final ArrayAdapter<OldHistoryState> adapter = getAdapter();
try { try {
adapter.setNotifyOnChange(false); adapter.setNotifyOnChange(false);
adapter.clear(); adapter.clear();
for (HistoryState historyState : historyList) { for (OldHistoryState historyState : historyList) {
adapter.add(historyState); adapter.add(historyState);
} }
} finally { } finally {
@ -382,15 +382,15 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
} }
@Nonnull @Nonnull
private List<HistoryState> getHistoryList() { private List<OldHistoryState> getHistoryList() {
final List<HistoryState> historyStates = getHistoryItems(); final List<OldHistoryState> historyStates = getHistoryItems();
java.util.Collections.sort(historyStates, COMPARATOR); java.util.Collections.sort(historyStates, COMPARATOR);
final FilterRulesChain<HistoryState> filterRulesChain = new FilterRulesChain<>(); final FilterRulesChain<OldHistoryState> filterRulesChain = new FilterRulesChain<>();
filterRulesChain.addFilterRule(new JPredicate<HistoryState>() { filterRulesChain.addFilterRule(new JPredicate<OldHistoryState>() {
@Override @Override
public boolean apply(HistoryState object) { public boolean apply(OldHistoryState object) {
return object == null || Strings.isEmpty(object.getEditorState().getText()); return object == null || Strings.isEmpty(object.getEditorState().getText());
} }
}); });
@ -401,7 +401,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
} }
@Nonnull @Nonnull
protected abstract List<HistoryState> getHistoryItems(); protected abstract List<OldHistoryState> getHistoryItems();
protected abstract void clearHistory(); protected abstract void clearHistory();

View File

@ -22,42 +22,204 @@
package org.solovyev.android.calculator.history; package org.solovyev.android.calculator.history;
import org.solovyev.android.calculator.CalculatorEventListener; import android.content.SharedPreferences;
import android.text.TextUtils;
import com.squareup.otto.Subscribe;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.*;
import org.solovyev.common.history.HistoryAction;
import org.solovyev.common.history.HistoryHelper; import org.solovyev.common.history.HistoryHelper;
import org.solovyev.common.history.SimpleHistoryHelper;
import java.util.List;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/** public class CalculatorHistory {
* User: Solovyev_S
* Date: 20.09.12
* Time: 16:11
*/
public interface CalculatorHistory extends HistoryHelper<HistoryState>, CalculatorEventListener {
void load(); private final AtomicInteger counter = new AtomicInteger(0);
void save();
void fromXml(@Nonnull String xml);
String toXml();
void clearSavedHistory();
void removeSavedHistory(@Nonnull HistoryState historyState);
@Nonnull @Nonnull
List<HistoryState> getSavedHistory(); private final HistoryHelper<OldHistoryState> history = SimpleHistoryHelper.newInstance();
@Nonnull @Nonnull
HistoryState addSavedState(@Nonnull HistoryState historyState); private final OldHistory savedOldHistory = new OldHistory();
@Nullable
private EditorState lastEditorState;
public CalculatorHistory() {
App.getBus().register(this);
App.getInitializer().execute(new Runnable() {
@Override
public void run() {
init();
}
});
}
private void init() {
Check.isNotMainThread();
migrateOldHistory();
}
private void migrateOldHistory() {
final SharedPreferences preferences = App.getPreferences();
final String xml = preferences.getString("org.solovyev.android.calculator.CalculatorModel_history", null);
if (TextUtils.isEmpty(xml)) {
return;
}
final OldHistory history = OldHistory.fromXml(xml);
if (history == null) {
// strange, history seems to be broken. Avoid clearing the preference
return;
}
for (OldHistoryState state : history.getItems()) {
state.setSaved(true);
state.setId(counter.incrementAndGet());
savedOldHistory.add(state);
}
}
public boolean isEmpty() {
Check.isMainThread();
return history.isEmpty();
}
public boolean isActionAvailable(@Nonnull HistoryAction action) {
Check.isMainThread();
return history.isActionAvailable(action);
}
public OldHistoryState doAction(@Nonnull HistoryAction action, @Nullable OldHistoryState state) {
Check.isMainThread();
return history.doAction(action, state);
}
public void addState(@Nullable OldHistoryState currentState) {
Check.isMainThread();
history.addState(currentState);
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.history_state_added, currentState);
}
@Nonnull @Nonnull
List<HistoryState> getStates(); public List<OldHistoryState> getStates() {
Check.isMainThread();
return history.getStates();
}
@Nonnull @Nonnull
List<HistoryState> getStates(boolean includeIntermediateStates); public List<OldHistoryState> getStates(boolean includeIntermediateStates) {
Check.isMainThread();
if (includeIntermediateStates) {
return getStates();
} else {
final List<OldHistoryState> states = getStates();
final List<OldHistoryState> result = new LinkedList<>();
OldHistoryState laterState = null;
for (OldHistoryState state : org.solovyev.common.collections.Collections.reversed(states)) {
if (laterState != null) {
final String laterEditorText = laterState.getEditorState().getText();
final String editorText = state.getEditorState().getText();
if (laterEditorText != null && editorText != null && isIntermediate(laterEditorText, editorText)) {
// intermediate result => skip from add
} else {
result.add(0, state);
}
} else {
result.add(0, state);
}
laterState = state;
}
return result;
}
}
private boolean isIntermediate(@Nonnull String laterEditorText,
@Nonnull String editorText) {
if (Math.abs(laterEditorText.length() - editorText.length()) <= 1) {
if (laterEditorText.length() > editorText.length()) {
return laterEditorText.startsWith(editorText);
} else {
return editorText.startsWith(laterEditorText);
}
}
return false;
}
public void clear() {
Check.isMainThread();
history.clear();
}
@Nonnull
public List<OldHistoryState> getSavedOldHistory() {
return Collections.unmodifiableList(savedOldHistory.getItems());
}
@Nonnull
public OldHistoryState addSavedState(@Nonnull OldHistoryState state) {
if (state.isSaved()) {
return state;
} else {
final OldHistoryState savedState = state.clone();
savedState.setId(counter.incrementAndGet());
savedState.setSaved(true);
savedOldHistory.add(savedState);
return savedState;
}
}
public void load() {
}
public void save() {
final SharedPreferences settings = App.getPreferences();
final SharedPreferences.Editor editor = settings.edit();
editor.putString("org.solovyev.android.calculator.CalculatorModel_history", toXml());
editor.apply();
}
public String toXml() {
return savedOldHistory.toXml();
}
public void clearSavedHistory() {
savedOldHistory.clear();
save();
}
public void removeSavedHistory(@Nonnull OldHistoryState historyState) {
historyState.setSaved(false);
this.savedOldHistory.remove(historyState);
save();
}
@Subscribe
public void onEditorChanged(@Nonnull Editor.ChangedEvent e) {
lastEditorState = e.newState;
}
@Subscribe
public void onDisplayChanged(@Nonnull Display.ChangedEvent e) {
if (lastEditorState == null) {
return;
}
if (lastEditorState.sequence != e.newState.getSequence()) {
return;
}
addState(OldHistoryState.create(lastEditorState, e.newState));
lastEditorState = null;
}
} }

View File

@ -1,279 +0,0 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.calculator.history;
import com.squareup.otto.Subscribe;
import org.solovyev.android.calculator.*;
import org.solovyev.common.history.HistoryAction;
import org.solovyev.common.history.HistoryHelper;
import org.solovyev.common.history.SimpleHistoryHelper;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import static org.solovyev.android.calculator.CalculatorEventType.manual_calculation_requested;
public class CalculatorHistoryImpl implements CalculatorHistory {
private final AtomicInteger counter = new AtomicInteger(0);
@Nonnull
private final HistoryHelper<HistoryState> history = SimpleHistoryHelper.newInstance();
@Nonnull
private final History savedHistory = new History();
@Nonnull
private final CalculatorEventHolder lastEventData = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId());
@Nullable
private EditorState lastEditorState;
public CalculatorHistoryImpl(@Nonnull Calculator calculator) {
calculator.addCalculatorEventListener(this);
App.getBus().register(this);
}
@Override
public boolean isEmpty() {
synchronized (history) {
return this.history.isEmpty();
}
}
@Override
public HistoryState getLastHistoryState() {
synchronized (history) {
return this.history.getLastHistoryState();
}
}
@Override
public boolean isUndoAvailable() {
synchronized (history) {
return history.isUndoAvailable();
}
}
@Override
public HistoryState undo(@Nullable HistoryState currentState) {
synchronized (history) {
return history.undo(currentState);
}
}
@Override
public boolean isRedoAvailable() {
return history.isRedoAvailable();
}
@Override
public HistoryState redo(@Nullable HistoryState currentState) {
synchronized (history) {
return history.redo(currentState);
}
}
@Override
public boolean isActionAvailable(@Nonnull HistoryAction historyAction) {
synchronized (history) {
return history.isActionAvailable(historyAction);
}
}
@Override
public HistoryState doAction(@Nonnull HistoryAction historyAction, @Nullable HistoryState currentState) {
synchronized (history) {
return history.doAction(historyAction, currentState);
}
}
@Override
public void addState(@Nullable HistoryState currentState) {
synchronized (history) {
history.addState(currentState);
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.history_state_added, currentState);
}
}
@Nonnull
@Override
public List<HistoryState> getStates() {
synchronized (history) {
return history.getStates();
}
}
@Nonnull
@Override
public List<HistoryState> getStates(boolean includeIntermediateStates) {
synchronized (history) {
if (includeIntermediateStates) {
return getStates();
} else {
final List<HistoryState> states = getStates();
final List<HistoryState> result = new LinkedList<HistoryState>();
HistoryState laterState = null;
for (HistoryState state : org.solovyev.common.collections.Collections.reversed(states)) {
if (laterState != null) {
final String laterEditorText = laterState.getEditorState().getText();
final String editorText = state.getEditorState().getText();
if (laterEditorText != null && editorText != null && isIntermediate(laterEditorText, editorText)) {
// intermediate result => skip from add
} else {
result.add(0, state);
}
} else {
result.add(0, state);
}
laterState = state;
}
return result;
}
}
}
private boolean isIntermediate(@Nonnull String laterEditorText,
@Nonnull String editorText) {
if (Math.abs(laterEditorText.length() - editorText.length()) <= 1) {
if (laterEditorText.length() > editorText.length()) {
return laterEditorText.startsWith(editorText);
} else {
return editorText.startsWith(laterEditorText);
}
}
return false;
}
@Override
public void clear() {
synchronized (history) {
this.history.clear();
}
}
@Override
@Nonnull
public List<HistoryState> getSavedHistory() {
return Collections.unmodifiableList(savedHistory.getItems());
}
@Override
@Nonnull
public HistoryState addSavedState(@Nonnull HistoryState historyState) {
if (historyState.isSaved()) {
return historyState;
} else {
final HistoryState savedState = historyState.clone();
savedState.setId(counter.incrementAndGet());
savedState.setSaved(true);
savedHistory.add(savedState);
return savedState;
}
}
@Override
public void load() {
// todo serso: create saved/loader class
}
@Override
public void save() {
// todo serso: create saved/loader class
}
@Override
public void fromXml(@Nonnull String xml) {
clearSavedHistory();
final History history = History.fromXml(xml);
if (history == null) {
return;
}
for (HistoryState historyState : history.getItems()) {
historyState.setSaved(true);
historyState.setId(counter.incrementAndGet());
savedHistory.add(historyState);
}
}
@Override
public String toXml() {
return savedHistory.toXml();
}
@Override
public void clearSavedHistory() {
savedHistory.clear();
}
@Override
public void removeSavedHistory(@Nonnull HistoryState historyState) {
this.savedHistory.remove(historyState);
}
@Subscribe
public void onEditorChanged(@Nonnull Editor.ChangedEvent e) {
lastEditorState = e.newState;
}
@Subscribe
public void onDisplayChanged(@Nonnull Display.ChangedEvent e) {
if (lastEditorState == null) {
return;
}
if (lastEditorState.sequence != e.newState.getSequence()) {
return;
}
addState(HistoryState.create(lastEditorState, e.newState));
lastEditorState = null;
}
@Override
public void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData,
@Nonnull CalculatorEventType calculatorEventType,
@Nullable Object data) {
if (calculatorEventType.isOfType(manual_calculation_requested)) {
final CalculatorEventHolder.Result result = lastEventData.apply(calculatorEventData);
if (result.isNewAfter() && result.isNewSameOrAfterSequence()) {
switch (calculatorEventType) {
case manual_calculation_requested:
lastEditorState = (EditorState) data;
break;
}
}
}
}
}

View File

@ -47,12 +47,12 @@ import static org.solovyev.android.calculator.history.BaseHistoryFragment.isAlre
* Date: 12/18/11 * Date: 12/18/11
* Time: 7:39 PM * Time: 7:39 PM
*/ */
public class HistoryArrayAdapter extends ArrayAdapter<HistoryState> { public class HistoryArrayAdapter extends ArrayAdapter<OldHistoryState> {
private static final int DATETIME_FORMAT = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_MONTH | DateUtils.FORMAT_ABBREV_TIME; private static final int DATETIME_FORMAT = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_MONTH | DateUtils.FORMAT_ABBREV_TIME;
private boolean showDatetime; private boolean showDatetime;
HistoryArrayAdapter(Context context, int resource, int textViewResourceId, @Nonnull List<HistoryState> historyList, boolean showDatetime) { HistoryArrayAdapter(Context context, int resource, int textViewResourceId, @Nonnull List<OldHistoryState> historyList, boolean showDatetime) {
super(context, resource, textViewResourceId, historyList); super(context, resource, textViewResourceId, historyList);
this.showDatetime = showDatetime; this.showDatetime = showDatetime;
} }
@ -61,7 +61,7 @@ public class HistoryArrayAdapter extends ArrayAdapter<HistoryState> {
public View getView(int position, View convertView, ViewGroup parent) { public View getView(int position, View convertView, ViewGroup parent) {
final ViewGroup result = (ViewGroup) super.getView(position, convertView, parent); final ViewGroup result = (ViewGroup) super.getView(position, convertView, parent);
final HistoryState state = getItem(position); final OldHistoryState state = getItem(position);
final TextView time = (TextView) result.findViewById(R.id.history_time); final TextView time = (TextView) result.findViewById(R.id.history_time);
if (showDatetime) { if (showDatetime) {

View File

@ -22,19 +22,14 @@
package org.solovyev.android.calculator.history; package org.solovyev.android.calculator.history;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import org.solovyev.android.calculator.CalculatorFragmentType; import org.solovyev.android.calculator.CalculatorFragmentType;
import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.Locator;
import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import javax.annotation.Nonnull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.annotation.Nonnull;
public class HistoryFragment extends BaseHistoryFragment { public class HistoryFragment extends BaseHistoryFragment {
public HistoryFragment() { public HistoryFragment() {
@ -48,11 +43,8 @@ public class HistoryFragment extends BaseHistoryFragment {
@Nonnull @Nonnull
@Override @Override
protected List<HistoryState> getHistoryItems() { protected List<OldHistoryState> getHistoryItems() {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); return new ArrayList<>(Locator.getInstance().getHistory().getStates(false));
final boolean showIntermediateCalculations = Preferences.History.showIntermediateCalculations.getPreference(preferences);
final List<HistoryState> historyStates = Locator.getInstance().getHistory().getStates(showIntermediateCalculations);
return new ArrayList<HistoryState>(historyStates);
} }
@Override @Override

View File

@ -34,23 +34,23 @@ import javax.annotation.Nonnull;
public class HistoryItemMenuData { public class HistoryItemMenuData {
@Nonnull @Nonnull
private final ArrayAdapter<HistoryState> adapter; private final ArrayAdapter<OldHistoryState> adapter;
@Nonnull @Nonnull
private final HistoryState historyState; private final OldHistoryState historyState;
public HistoryItemMenuData(@Nonnull HistoryState historyState, ArrayAdapter<HistoryState> adapter) { public HistoryItemMenuData(@Nonnull OldHistoryState historyState, ArrayAdapter<OldHistoryState> adapter) {
this.historyState = historyState; this.historyState = historyState;
this.adapter = adapter; this.adapter = adapter;
} }
@Nonnull @Nonnull
public HistoryState getHistoryState() { public OldHistoryState getHistoryState() {
return historyState; return historyState;
} }
@Nonnull @Nonnull
public ArrayAdapter<HistoryState> getAdapter() { public ArrayAdapter<OldHistoryState> getAdapter() {
return adapter; return adapter;
} }
} }

View File

@ -1,32 +0,0 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.calculator.history;
final class HistoryUtils {
// not intended for instantiation
private HistoryUtils() {
throw new AssertionError();
}
}

View File

@ -35,8 +35,8 @@ import org.solovyev.common.text.Strings;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@Root @Root(name = "DisplayHistoryState")
public class DisplayHistoryState implements Cloneable { public class OldDisplayHistoryState implements Cloneable {
@Transient @Transient
private boolean valid = true; private boolean valid = true;
@ -47,7 +47,7 @@ public class DisplayHistoryState implements Cloneable {
@Element @Element
@Nonnull @Nonnull
private EditorHistoryState editorState; private OldEditorHistoryState editorState;
@Element @Element
@Nonnull @Nonnull
@ -57,15 +57,15 @@ public class DisplayHistoryState implements Cloneable {
@Nullable @Nullable
private Generic genericResult; private Generic genericResult;
private DisplayHistoryState() { private OldDisplayHistoryState() {
// for xml // for xml
} }
@Nonnull @Nonnull
public static DisplayHistoryState newInstance(@Nonnull DisplayState viewState) { public static OldDisplayHistoryState newInstance(@Nonnull DisplayState viewState) {
final DisplayHistoryState result = new DisplayHistoryState(); final OldDisplayHistoryState result = new OldDisplayHistoryState();
result.editorState = EditorHistoryState.create(viewState); result.editorState = OldEditorHistoryState.create(viewState);
result.valid = viewState.isValid(); result.valid = viewState.isValid();
result.jsclOperation = viewState.getOperation(); result.jsclOperation = viewState.getOperation();
@ -89,7 +89,7 @@ public class DisplayHistoryState implements Cloneable {
} }
@Nonnull @Nonnull
public EditorHistoryState getEditorState() { public OldEditorHistoryState getEditorState() {
return editorState; return editorState;
} }
@ -114,7 +114,7 @@ public class DisplayHistoryState implements Cloneable {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
DisplayHistoryState that = (DisplayHistoryState) o; OldDisplayHistoryState that = (OldDisplayHistoryState) o;
if (!editorState.equals(that.editorState)) return false; if (!editorState.equals(that.editorState)) return false;
if (jsclOperation != that.jsclOperation) return false; if (jsclOperation != that.jsclOperation) return false;
@ -140,9 +140,9 @@ public class DisplayHistoryState implements Cloneable {
} }
@Override @Override
protected DisplayHistoryState clone() { protected OldDisplayHistoryState clone() {
try { try {
final DisplayHistoryState clone = (DisplayHistoryState) super.clone(); final OldDisplayHistoryState clone = (OldDisplayHistoryState) super.clone();
clone.editorState = this.editorState.clone(); clone.editorState = this.editorState.clone();

View File

@ -32,8 +32,8 @@ import org.solovyev.common.text.Strings;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@Root @Root(name = "EditorHistoryState")
public class EditorHistoryState implements Cloneable { public class OldEditorHistoryState implements Cloneable {
@Element @Element
private int cursorPosition; private int cursorPosition;
@ -42,13 +42,13 @@ public class EditorHistoryState implements Cloneable {
@Nullable @Nullable
private String text = ""; private String text = "";
private EditorHistoryState() { private OldEditorHistoryState() {
// for xml // for xml
} }
@Nonnull @Nonnull
public static EditorHistoryState create(@Nonnull EditorState state) { public static OldEditorHistoryState create(@Nonnull EditorState state) {
final EditorHistoryState result = new EditorHistoryState(); final OldEditorHistoryState result = new OldEditorHistoryState();
result.text = state.getTextString(); result.text = state.getTextString();
result.cursorPosition = state.selection; result.cursorPosition = state.selection;
@ -57,8 +57,8 @@ public class EditorHistoryState implements Cloneable {
} }
@Nonnull @Nonnull
public static EditorHistoryState create(@Nonnull DisplayState viewState) { public static OldEditorHistoryState create(@Nonnull DisplayState viewState) {
final EditorHistoryState result = new EditorHistoryState(); final OldEditorHistoryState result = new OldEditorHistoryState();
result.text = viewState.getText(); result.text = viewState.getText();
result.cursorPosition = viewState.getSelection(); result.cursorPosition = viewState.getSelection();
@ -83,9 +83,9 @@ public class EditorHistoryState implements Cloneable {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof EditorHistoryState)) return false; if (!(o instanceof OldEditorHistoryState)) return false;
EditorHistoryState that = (EditorHistoryState) o; OldEditorHistoryState that = (OldEditorHistoryState) o;
if (cursorPosition != that.cursorPosition) return false; if (cursorPosition != that.cursorPosition) return false;
if (text != null ? !text.equals(that.text) : that.text != null) return false; if (text != null ? !text.equals(that.text) : that.text != null) return false;
@ -109,9 +109,9 @@ public class EditorHistoryState implements Cloneable {
} }
@Override @Override
protected EditorHistoryState clone() { protected OldEditorHistoryState clone() {
try { try {
return (EditorHistoryState) super.clone(); return (OldEditorHistoryState) super.clone();
} catch (CloneNotSupportedException e) { } catch (CloneNotSupportedException e) {
throw new UnsupportedOperationException(e); throw new UnsupportedOperationException(e);
} }

View File

@ -34,24 +34,24 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@Root @Root(name = "History")
public class History { public class OldHistory {
@Nonnull @Nonnull
@ElementList(type = HistoryState.class, name = "historyItems") @ElementList(type = OldHistoryState.class, name = "historyItems")
private List<HistoryState> items = new ArrayList<HistoryState>(); private List<OldHistoryState> items = new ArrayList<OldHistoryState>();
public History() { public OldHistory() {
} }
@Nullable @Nullable
public static History fromXml(@Nullable String xml) { public static OldHistory fromXml(@Nullable String xml) {
if (xml == null) { if (xml == null) {
return null; return null;
} }
final Serializer serializer = new Persister(); final Serializer serializer = new Persister();
try { try {
return serializer.read(History.class, xml); return serializer.read(OldHistory.class, xml);
} catch (Exception e) { } catch (Exception e) {
return null; return null;
} }
@ -70,15 +70,15 @@ public class History {
} }
@Nonnull @Nonnull
public List<HistoryState> getItems() { public List<OldHistoryState> getItems() {
return items; return items;
} }
public void add(@Nonnull HistoryState state) { public void add(@Nonnull OldHistoryState state) {
items.add(state); items.add(state);
} }
public void addAll(@Nonnull Collection<HistoryState> states) { public void addAll(@Nonnull Collection<OldHistoryState> states) {
items.addAll(states); items.addAll(states);
} }
@ -86,7 +86,7 @@ public class History {
items.clear(); items.clear();
} }
public void remove(@Nonnull HistoryState state) { public void remove(@Nonnull OldHistoryState state) {
items.remove(state); items.remove(state);
} }
} }

View File

@ -31,46 +31,46 @@ import org.solovyev.android.calculator.EditorState;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@Root @Root(name = "HistoryState")
public class HistoryState extends BaseHistoryState { public class OldHistoryState extends BaseHistoryState {
@Element @Element
@Nonnull @Nonnull
private EditorHistoryState editorState; private OldEditorHistoryState editorState;
@Element @Element
@Nonnull @Nonnull
private DisplayHistoryState displayState; private OldDisplayHistoryState displayState;
private HistoryState() { private OldHistoryState() {
// for xml // for xml
} }
private HistoryState(@Nonnull EditorHistoryState editorState, private OldHistoryState(@Nonnull OldEditorHistoryState editorState,
@Nonnull DisplayHistoryState displayState) { @Nonnull OldDisplayHistoryState displayState) {
this.editorState = editorState; this.editorState = editorState;
this.displayState = displayState; this.displayState = displayState;
} }
@Nonnull @Nonnull
public static HistoryState create(@Nonnull Editor editor, public static OldHistoryState create(@Nonnull Editor editor,
@Nonnull Display display) { @Nonnull Display display) {
return create(editor.getState(), display.getState()); return create(editor.getState(), display.getState());
} }
@Nonnull @Nonnull
public static HistoryState create(@Nonnull EditorState editorState, public static OldHistoryState create(@Nonnull EditorState editorState,
@Nonnull DisplayState displayState) { @Nonnull DisplayState displayState) {
return new HistoryState(EditorHistoryState.create(editorState), DisplayHistoryState.newInstance(displayState)); return new OldHistoryState(OldEditorHistoryState.create(editorState), OldDisplayHistoryState.newInstance(displayState));
} }
@Nonnull @Nonnull
public EditorHistoryState getEditorState() { public OldEditorHistoryState getEditorState() {
return editorState; return editorState;
} }
@Nonnull @Nonnull
public DisplayHistoryState getDisplayState() { public OldDisplayHistoryState getDisplayState() {
return displayState; return displayState;
} }
@ -87,7 +87,7 @@ public class HistoryState extends BaseHistoryState {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
HistoryState that = (HistoryState) o; OldHistoryState that = (OldHistoryState) o;
if (this.isSaved() != that.isSaved()) return false; if (this.isSaved() != that.isSaved()) return false;
if (this.getId() != that.getId()) return false; if (this.getId() != that.getId()) return false;
@ -112,8 +112,8 @@ public class HistoryState extends BaseHistoryState {
} }
@Override @Override
protected HistoryState clone() { protected OldHistoryState clone() {
final HistoryState that = (HistoryState) super.clone(); final OldHistoryState that = (OldHistoryState) super.clone();
that.editorState = this.editorState.clone(); that.editorState = this.editorState.clone();
that.displayState = this.displayState.clone(); that.displayState = this.displayState.clone();

View File

@ -44,8 +44,8 @@ public class SavedHistoryFragment extends BaseHistoryFragment {
@Nonnull @Nonnull
@Override @Override
protected List<HistoryState> getHistoryItems() { protected List<OldHistoryState> getHistoryItems() {
return new ArrayList<HistoryState>(Locator.getInstance().getHistory().getSavedHistory()); return new ArrayList<OldHistoryState>(Locator.getInstance().getHistory().getSavedOldHistory());
} }
@Override @Override

Binary file not shown.

After

Width:  |  Height:  |  Size: 97 B

View File

@ -294,6 +294,7 @@
<style name="CppListView"> <style name="CppListView">
<item name="android:id">@android:id/list</item> <item name="android:id">@android:id/list</item>
<item name="android:dividerHeight">1px</item> <item name="android:dividerHeight">1px</item>
<item name="android:divider">@drawable/divider</item>
<item name="android:cacheColorHint">@android:color/transparent</item> <item name="android:cacheColorHint">@android:color/transparent</item>
<item name="android:layout_height">match_parent</item> <item name="android:layout_height">match_parent</item>
<item name="android:layout_width">match_parent</item> <item name="android:layout_width">match_parent</item>

View File

@ -25,21 +25,19 @@ package org.solovyev.android.calculator.history;
import org.junit.Assert; import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.solovyev.android.calculator.CalculatorTestUtils;
import org.solovyev.android.calculator.DisplayState; import org.solovyev.android.calculator.DisplayState;
import org.solovyev.android.calculator.EditorState; import org.solovyev.android.calculator.EditorState;
import org.solovyev.android.calculator.CalculatorTestUtils;
import org.solovyev.android.calculator.Locator;
import java.util.List;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.List;
/** /**
* User: Solovyev_S * User: Solovyev_S
* Date: 10.10.12 * Date: 10.10.12
* Time: 15:07 * Time: 15:07
*/ */
public class CalculatorHistoryImplTest { public class CalculatorHistoryTest {
@BeforeClass @BeforeClass
public static void setUp() throws Exception { public static void setUp() throws Exception {
@ -48,7 +46,7 @@ public class CalculatorHistoryImplTest {
@Test @Test
public void testGetStates() throws Exception { public void testGetStates() throws Exception {
CalculatorHistory calculatorHistory = new CalculatorHistoryImpl(Locator.getInstance().getCalculator()); CalculatorHistory calculatorHistory = new CalculatorHistory();
addState(calculatorHistory, "1"); addState(calculatorHistory, "1");
addState(calculatorHistory, "12"); addState(calculatorHistory, "12");
@ -64,13 +62,13 @@ public class CalculatorHistoryImplTest {
addState(calculatorHistory, "2354"); addState(calculatorHistory, "2354");
addState(calculatorHistory, "23547"); addState(calculatorHistory, "23547");
final List<HistoryState> states = calculatorHistory.getStates(false); final List<OldHistoryState> states = calculatorHistory.getStates(false);
Assert.assertEquals(2, states.size()); Assert.assertEquals(2, states.size());
Assert.assertEquals("23547", states.get(1).getEditorState().getText()); Assert.assertEquals("23547", states.get(1).getEditorState().getText());
Assert.assertEquals("123+3", states.get(0).getEditorState().getText()); Assert.assertEquals("123+3", states.get(0).getEditorState().getText());
} }
private void addState(@Nonnull CalculatorHistory calculatorHistory, @Nonnull String text) { private void addState(@Nonnull CalculatorHistory calculatorHistory, @Nonnull String text) {
calculatorHistory.addState(HistoryState.create(EditorState.create(text, 3), DisplayState.empty())); calculatorHistory.addState(OldHistoryState.create(EditorState.create(text, 3), DisplayState.empty()));
} }
} }

View File

@ -135,13 +135,13 @@ public class HistoryUtilsTest {
public void testToXml() throws Exception { public void testToXml() throws Exception {
final Date date = new Date(100000000); final Date date = new Date(100000000);
HistoryHelper<HistoryState> history = SimpleHistoryHelper.newInstance(); HistoryHelper<OldHistoryState> history = SimpleHistoryHelper.newInstance();
DisplayState calculatorDisplay = DisplayState.createError(JsclOperation.simplify, "Error", EditorState.NO_SEQUENCE); DisplayState calculatorDisplay = DisplayState.createError(JsclOperation.simplify, "Error", EditorState.NO_SEQUENCE);
EditorState calculatorEditor = EditorState.create("1+1", 3); EditorState calculatorEditor = EditorState.create("1+1", 3);
HistoryState state = HistoryState.create(calculatorEditor, calculatorDisplay); OldHistoryState state = OldHistoryState.create(calculatorEditor, calculatorDisplay);
state.setTime(date.getTime()); state.setTime(date.getTime());
history.addState(state); history.addState(state);
@ -156,7 +156,7 @@ public class HistoryUtilsTest {
calculatorEditor = EditorState.create("5/6", 2); calculatorEditor = EditorState.create("5/6", 2);
state = HistoryState.create(calculatorEditor, calculatorDisplay); state = OldHistoryState.create(calculatorEditor, calculatorDisplay);
state.setSaved(true); state.setSaved(true);
state.setTime(date.getTime()); state.setTime(date.getTime());
history.addState(state); history.addState(state);
@ -165,7 +165,7 @@ public class HistoryUtilsTest {
calculatorEditor = EditorState.create("", 1); calculatorEditor = EditorState.create("", 1);
state = HistoryState.create(calculatorEditor, calculatorDisplay); state = OldHistoryState.create(calculatorEditor, calculatorDisplay);
state.setSaved(true); state.setSaved(true);
state.setTime(date.getTime()); state.setTime(date.getTime());
history.addState(state); history.addState(state);
@ -174,7 +174,7 @@ public class HistoryUtilsTest {
calculatorEditor = EditorState.create("4+5/35sin(41)+dfdsfsdfs", 0); calculatorEditor = EditorState.create("4+5/35sin(41)+dfdsfsdfs", 0);
state = HistoryState.create(calculatorEditor, calculatorDisplay); state = OldHistoryState.create(calculatorEditor, calculatorDisplay);
state.setSaved(true); state.setSaved(true);
state.setTime(date.getTime()); state.setTime(date.getTime());
history.addState(state); history.addState(state);
@ -182,28 +182,28 @@ public class HistoryUtilsTest {
String xml = createHistory(history).toXml(); String xml = createHistory(history).toXml();
assertEquals(toXml2, xml); assertEquals(toXml2, xml);
final HistoryHelper<HistoryState> historyFromXml = SimpleHistoryHelper.newInstance(); final HistoryHelper<OldHistoryState> historyFromXml = SimpleHistoryHelper.newInstance();
final History actual = History.fromXml(xml); final OldHistory actual = OldHistory.fromXml(xml);
for (HistoryState historyState : actual.getItems()) { for (OldHistoryState historyState : actual.getItems()) {
historyFromXml.addState(historyState); historyFromXml.addState(historyState);
} }
assertEquals(history.getStates().size(), historyFromXml.getStates().size()); assertEquals(history.getStates().size(), historyFromXml.getStates().size());
for (HistoryState historyState : history.getStates()) { for (OldHistoryState historyState : history.getStates()) {
historyState.setId(0); historyState.setId(0);
historyState.setSaved(true); historyState.setSaved(true);
} }
for (HistoryState historyState : historyFromXml.getStates()) { for (OldHistoryState historyState : historyFromXml.getStates()) {
historyState.setId(0); historyState.setId(0);
historyState.setSaved(true); historyState.setSaved(true);
} }
Assert.assertTrue(Objects.areEqual(history.getStates(), historyFromXml.getStates(), new CollectionEqualizer<HistoryState>(null))); Assert.assertTrue(Objects.areEqual(history.getStates(), historyFromXml.getStates(), new CollectionEqualizer<OldHistoryState>(null)));
} }
@Nonnull @Nonnull
private History createHistory(HistoryHelper<HistoryState> history) { private OldHistory createHistory(HistoryHelper<OldHistoryState> history) {
final History result = new History(); final OldHistory result = new OldHistory();
result.addAll(history.getStates()); result.addAll(history.getStates());
return result; return result;
} }