From b21d8084f04eb21a262b8b39169706a3d3dd40c1 Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 10 Jan 2016 23:27:14 +0100 Subject: [PATCH] History refactor --- .../main/java/org/solovyev/android/Check.java | 12 +- .../android/calculator/AndroidCalculator.java | 6 +- .../solovyev/android/calculator/BaseUi.java | 4 +- .../android/calculator/Calculator.java | 4 +- .../calculator/CalculatorApplication.java | 24 +- .../android/calculator/CalculatorImpl.java | 10 +- .../solovyev/android/calculator/Editor.java | 4 +- .../android/calculator/Preferences.java | 2 - .../history/AndroidCalculatorHistory.java | 175 ----------- .../history/BaseHistoryFragment.java | 46 +-- .../calculator/history/CalculatorHistory.java | 210 +++++++++++-- .../history/CalculatorHistoryImpl.java | 279 ------------------ .../history/HistoryArrayAdapter.java | 6 +- .../calculator/history/HistoryFragment.java | 14 +- .../history/HistoryItemMenuData.java | 10 +- .../calculator/history/HistoryUtils.java | 32 -- ...State.java => OldDisplayHistoryState.java} | 22 +- ...yState.java => OldEditorHistoryState.java} | 22 +- .../history/{History.java => OldHistory.java} | 22 +- ...HistoryState.java => OldHistoryState.java} | 30 +- .../history/SavedHistoryFragment.java | 4 +- app/src/main/res/drawable-nodpi/divider.9.png | Bin 0 -> 97 bytes app/src/main/res/values/styles.xml | 1 + ...plTest.java => CalculatorHistoryTest.java} | 14 +- .../calculator/history/HistoryUtilsTest.java | 26 +- 25 files changed, 328 insertions(+), 651 deletions(-) delete mode 100644 app/src/main/java/org/solovyev/android/calculator/history/AndroidCalculatorHistory.java delete mode 100644 app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryImpl.java delete mode 100644 app/src/main/java/org/solovyev/android/calculator/history/HistoryUtils.java rename app/src/main/java/org/solovyev/android/calculator/history/{DisplayHistoryState.java => OldDisplayHistoryState.java} (85%) rename app/src/main/java/org/solovyev/android/calculator/history/{EditorHistoryState.java => OldEditorHistoryState.java} (80%) rename app/src/main/java/org/solovyev/android/calculator/history/{History.java => OldHistory.java} (77%) rename app/src/main/java/org/solovyev/android/calculator/history/{HistoryState.java => OldHistoryState.java} (76%) create mode 100644 app/src/main/res/drawable-nodpi/divider.9.png rename app/src/test/java/org/solovyev/android/calculator/history/{CalculatorHistoryImplTest.java => CalculatorHistoryTest.java} (87%) diff --git a/app/src/main/java/org/solovyev/android/Check.java b/app/src/main/java/org/solovyev/android/Check.java index 16a9e013..363639c3 100644 --- a/app/src/main/java/org/solovyev/android/Check.java +++ b/app/src/main/java/org/solovyev/android/Check.java @@ -22,6 +22,8 @@ package org.solovyev.android; +import android.os.Looper; + import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.Collection; @@ -48,9 +50,15 @@ public final class Check { } 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"); - }*/ + } + } + + 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) { diff --git a/app/src/main/java/org/solovyev/android/calculator/AndroidCalculator.java b/app/src/main/java/org/solovyev/android/calculator/AndroidCalculator.java index a2fa8520..dd03e5ad 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AndroidCalculator.java +++ b/app/src/main/java/org/solovyev/android/calculator/AndroidCalculator.java @@ -27,7 +27,7 @@ import android.content.SharedPreferences; import android.preference.PreferenceManager; import jscl.NumeralBase; 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.common.history.HistoryAction; import org.solovyev.common.msg.Message; @@ -156,12 +156,12 @@ public class AndroidCalculator implements Calculator, CalculatorEventListener, S @Override @Nonnull - public HistoryState getCurrentHistoryState() { + public OldHistoryState getCurrentHistoryState() { return calculator.getCurrentHistoryState(); } @Override - public void setCurrentHistoryState(@Nonnull HistoryState editorHistoryState) { + public void setCurrentHistoryState(@Nonnull OldHistoryState editorHistoryState) { calculator.setCurrentHistoryState(editorHistoryState); } diff --git a/app/src/main/java/org/solovyev/android/calculator/BaseUi.java b/app/src/main/java/org/solovyev/android/calculator/BaseUi.java index 597d6843..98dce673 100644 --- a/app/src/main/java/org/solovyev/android/calculator/BaseUi.java +++ b/app/src/main/java/org/solovyev/android/calculator/BaseUi.java @@ -40,7 +40,7 @@ import org.solovyev.android.views.dragbutton.DragButton; import org.solovyev.android.views.dragbutton.DragDirection; import org.solovyev.android.views.dragbutton.DragListener; 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.view.AngleUnitsButton; import org.solovyev.android.calculator.view.LongClickEraser; @@ -165,7 +165,7 @@ public abstract class BaseUi implements SharedPreferences.OnSharedPreferenceChan final ViewsCache views = ViewsCache.forView(root); setOnDragListeners(views, activity); - HistoryDragProcessor historyDragProcessor = new HistoryDragProcessor<>(getCalculator()); + HistoryDragProcessor historyDragProcessor = new HistoryDragProcessor<>(getCalculator()); final DragListener historyDragListener = newDragListener(historyDragProcessor, activity); final DragButton historyButton = getButton(views, R.id.cpp_button_history); if (historyButton != null) { diff --git a/app/src/main/java/org/solovyev/android/calculator/Calculator.java b/app/src/main/java/org/solovyev/android/calculator/Calculator.java index 08738315..2031e7c0 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Calculator.java +++ b/app/src/main/java/org/solovyev/android/calculator/Calculator.java @@ -24,7 +24,7 @@ package org.solovyev.android.calculator; import jscl.NumeralBase; 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.common.history.HistoryControl; @@ -36,7 +36,7 @@ import javax.annotation.Nullable; * Date: 20.09.12 * Time: 16:38 */ -public interface Calculator extends CalculatorEventContainer, HistoryControl { +public interface Calculator extends CalculatorEventContainer, HistoryControl { void init(); diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java index ec66d109..146ab181 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java @@ -30,7 +30,7 @@ import org.acra.ACRA; import org.acra.ACRAConfiguration; import org.acra.sender.HttpSender; 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.Languages; import org.solovyev.android.calculator.model.AndroidCalculatorEngine; @@ -74,7 +74,7 @@ public class CalculatorApplication extends android.app.Application implements Sh new AndroidCalculatorEngine(this), new AndroidCalculatorClipboard(this), new AndroidCalculatorNotifier(this), - new AndroidCalculatorHistory(this, calculator), + new CalculatorHistory(), new AndroidCalculatorLogger(), new AndroidCalculatorPreferenceService(this), new CalculatorKeyboard(), @@ -93,14 +93,7 @@ public class CalculatorApplication extends android.app.Application implements Sh App.getInitializer().execute(new Runnable() { @Override public void run() { - 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); - } + warmUpEngine(); } }); @@ -113,6 +106,17 @@ public class CalculatorApplication extends android.app.Application implements Sh }, 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) { // first we need to setup crash handler and memory leak analyzer if (!BuildConfig.DEBUG) { diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java index 90daec54..0c0b7ec4 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java @@ -33,7 +33,7 @@ import jscl.math.function.IConstant; import jscl.math.operator.Operator; import jscl.text.ParseInterruptedException; 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.model.Var; import org.solovyev.android.calculator.text.TextProcessor; @@ -560,7 +560,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { public void doHistoryAction(@Nonnull HistoryAction historyAction) { final CalculatorHistory history = Locator.getInstance().getHistory(); if (history.isActionAvailable(historyAction)) { - final HistoryState newState = history.doAction(historyAction, getCurrentHistoryState()); + final OldHistoryState newState = history.doAction(historyAction, getCurrentHistoryState()); if (newState != null) { setCurrentHistoryState(newState); } @@ -569,12 +569,12 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { @Nonnull @Override - public HistoryState getCurrentHistoryState() { - return HistoryState.create(getEditor(), getDisplay()); + public OldHistoryState getCurrentHistoryState() { + return OldHistoryState.create(getEditor(), getDisplay()); } @Override - public void setCurrentHistoryState(@Nonnull HistoryState editorHistoryState) { + public void setCurrentHistoryState(@Nonnull OldHistoryState editorHistoryState) { editorHistoryState.setValuesFromHistory(getEditor(), getDisplay()); } diff --git a/app/src/main/java/org/solovyev/android/calculator/Editor.java b/app/src/main/java/org/solovyev/android/calculator/Editor.java index 343e749a..83ce958f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Editor.java +++ b/app/src/main/java/org/solovyev/android/calculator/Editor.java @@ -23,7 +23,7 @@ package org.solovyev.android.calculator; 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.TextProcessorEditorResult; import org.solovyev.common.text.Strings; @@ -104,7 +104,7 @@ public class Editor { } @Nonnull - public EditorState setState(@Nonnull EditorHistoryState state) { + public EditorState setState(@Nonnull OldEditorHistoryState state) { Check.isMainThread(); return setText(Strings.getNotEmpty(state.getText(), ""), state.getCursorPosition()); } diff --git a/app/src/main/java/org/solovyev/android/calculator/Preferences.java b/app/src/main/java/org/solovyev/android/calculator/Preferences.java index c56350ac..55344cf8 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Preferences.java +++ b/app/src/main/java/org/solovyev/android/calculator/Preferences.java @@ -108,8 +108,6 @@ public final class Preferences { applyDefaultPreference(preferences, Gui.language); applyDefaultPreference(preferences, Graph.plotImag); - applyDefaultPreference(preferences, History.showIntermediateCalculations); - applyDefaultPreference(preferences, History.showDatetime); applyDefaultPreference(preferences, Calculations.calculateOnFly); applyDefaultPreference(preferences, Calculations.preferredAngleUnits); applyDefaultPreference(preferences, Calculations.preferredNumeralBase); diff --git a/app/src/main/java/org/solovyev/android/calculator/history/AndroidCalculatorHistory.java b/app/src/main/java/org/solovyev/android/calculator/history/AndroidCalculatorHistory.java deleted file mode 100644 index 6df3882f..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/history/AndroidCalculatorHistory.java +++ /dev/null @@ -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 getStates() { - return calculatorHistory.getStates(); - } - - @Nonnull - @Override - public List getStates(boolean includeIntermediateStates) { - return calculatorHistory.getStates(includeIntermediateStates); - } - - @Override - public void clear() { - calculatorHistory.clear(); - } - - @Nonnull - public List 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); - } -} diff --git a/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryFragment.java b/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryFragment.java index d9b890b1..9d4ab363 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryFragment.java @@ -61,9 +61,9 @@ import static org.solovyev.android.calculator.CalculatorEventType.clear_history_ public abstract class BaseHistoryFragment extends ListFragment implements CalculatorEventListener { - public static final Comparator COMPARATOR = new Comparator() { + public static final Comparator COMPARATOR = new Comparator() { @Override - public int compare(HistoryState state1, HistoryState state2) { + public int compare(OldHistoryState state1, OldHistoryState state2) { if (state1.isSaved() == state2.isSaved()) { long l = state2.getTime() - state1.getTime(); 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); } - public static boolean isAlreadySaved(@Nonnull HistoryState historyState) { + public static boolean isAlreadySaved(@Nonnull OldHistoryState historyState) { Check.isTrue(!historyState.isSaved()); boolean result = false; try { historyState.setSaved(true); - if (Collections.contains(historyState, Locator.getInstance().getHistory().getSavedHistory(), new Equalizer() { + if (Collections.contains(historyState, Locator.getInstance().getHistory().getSavedOldHistory(), new Equalizer() { @Override - public boolean areEqual(@Nullable HistoryState first, @Nullable HistoryState second) { + public boolean areEqual(@Nullable OldHistoryState first, @Nullable OldHistoryState second) { return first != null && second != null && first.getTime() == second.getTime() && first.getDisplayState().equals(second.getDisplayState()) && @@ -127,7 +127,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul } @Nonnull - public static String getHistoryText(@Nonnull HistoryState state) { + public static String getHistoryText(@Nonnull OldHistoryState state) { final StringBuilder result = new StringBuilder(); result.append(state.getEditorState().getText()); result.append(getIdentitySign(state.getDisplayState().getJsclOperation())); @@ -143,7 +143,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul return jsclOperation == JsclOperation.simplify ? "≡" : "="; } - public void useState(@Nonnull final HistoryState state) { + public void useState(@Nonnull final OldHistoryState state) { App.getEditor().setState(state.getEditorState()); final FragmentActivity activity = getActivity(); if (!(activity instanceof CalculatorActivity)) { @@ -180,7 +180,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul ui.onViewCreated(this, root); - adapter = new HistoryArrayAdapter(this.getActivity(), getItemLayoutId(), org.solovyev.android.calculator.R.id.history_item, new ArrayList(), showDatetime); + adapter = new HistoryArrayAdapter(this.getActivity(), getItemLayoutId(), org.solovyev.android.calculator.R.id.history_item, new ArrayList(), showDatetime); setListAdapter(adapter); final ListView lv = getListView(); @@ -200,7 +200,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul final View view, final int position, 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) { super.onCreateContextMenu(menu, v, 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()) { 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) { final Context context = getActivity(); 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()) { case R.string.c_use: @@ -296,7 +296,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul 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 View editView = layoutInflater.inflate(R.layout.history_edit, null); final TextView historyExpression = (TextView) editView.findViewById(R.id.history_edit_expression); @@ -313,7 +313,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul @Override public void onClick(DialogInterface dialog, int which) { if (save) { - final HistoryState savedHistoryItem = Locator.getInstance().getHistory().addSavedState(state); + final OldHistoryState savedHistoryItem = Locator.getInstance().getHistory().addSavedState(state); savedHistoryItem.setComment(comment.getText().toString()); 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 @@ -331,7 +331,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul 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()); } @@ -365,13 +365,13 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul protected abstract int getItemLayoutId(); private void updateAdapter() { - final List historyList = getHistoryList(); + final List historyList = getHistoryList(); - final ArrayAdapter adapter = getAdapter(); + final ArrayAdapter adapter = getAdapter(); try { adapter.setNotifyOnChange(false); adapter.clear(); - for (HistoryState historyState : historyList) { + for (OldHistoryState historyState : historyList) { adapter.add(historyState); } } finally { @@ -382,15 +382,15 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul } @Nonnull - private List getHistoryList() { - final List historyStates = getHistoryItems(); + private List getHistoryList() { + final List historyStates = getHistoryItems(); java.util.Collections.sort(historyStates, COMPARATOR); - final FilterRulesChain filterRulesChain = new FilterRulesChain<>(); - filterRulesChain.addFilterRule(new JPredicate() { + final FilterRulesChain filterRulesChain = new FilterRulesChain<>(); + filterRulesChain.addFilterRule(new JPredicate() { @Override - public boolean apply(HistoryState object) { + public boolean apply(OldHistoryState object) { return object == null || Strings.isEmpty(object.getEditorState().getText()); } }); @@ -401,7 +401,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul } @Nonnull - protected abstract List getHistoryItems(); + protected abstract List getHistoryItems(); protected abstract void clearHistory(); diff --git a/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistory.java b/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistory.java index 48d9d1c1..100f1b9b 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistory.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistory.java @@ -22,42 +22,204 @@ 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 java.util.List; +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; -/** - * User: Solovyev_S - * Date: 20.09.12 - * Time: 16:11 - */ -public interface CalculatorHistory extends HistoryHelper, CalculatorEventListener { +public class CalculatorHistory { - void load(); - - void save(); - - void fromXml(@Nonnull String xml); - - String toXml(); - - void clearSavedHistory(); - - void removeSavedHistory(@Nonnull HistoryState historyState); + private final AtomicInteger counter = new AtomicInteger(0); @Nonnull - List getSavedHistory(); + private final HistoryHelper history = SimpleHistoryHelper.newInstance(); @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 - List getStates(); + public List getStates() { + Check.isMainThread(); + return history.getStates(); + } @Nonnull - List getStates(boolean includeIntermediateStates); + public List getStates(boolean includeIntermediateStates) { + Check.isMainThread(); + if (includeIntermediateStates) { + return getStates(); + } else { + final List states = getStates(); + final List 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 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; + } } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryImpl.java b/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryImpl.java deleted file mode 100644 index 0ea2121c..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryImpl.java +++ /dev/null @@ -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 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 getStates() { - synchronized (history) { - return history.getStates(); - } - } - - @Nonnull - @Override - public List getStates(boolean includeIntermediateStates) { - synchronized (history) { - if (includeIntermediateStates) { - return getStates(); - } else { - final List states = getStates(); - - final List result = new LinkedList(); - - 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 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; - } - } - } - } -} diff --git a/app/src/main/java/org/solovyev/android/calculator/history/HistoryArrayAdapter.java b/app/src/main/java/org/solovyev/android/calculator/history/HistoryArrayAdapter.java index 9f633347..1f080e3d 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/HistoryArrayAdapter.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/HistoryArrayAdapter.java @@ -47,12 +47,12 @@ import static org.solovyev.android.calculator.history.BaseHistoryFragment.isAlre * Date: 12/18/11 * Time: 7:39 PM */ -public class HistoryArrayAdapter extends ArrayAdapter { +public class HistoryArrayAdapter extends ArrayAdapter { 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; - HistoryArrayAdapter(Context context, int resource, int textViewResourceId, @Nonnull List historyList, boolean showDatetime) { + HistoryArrayAdapter(Context context, int resource, int textViewResourceId, @Nonnull List historyList, boolean showDatetime) { super(context, resource, textViewResourceId, historyList); this.showDatetime = showDatetime; } @@ -61,7 +61,7 @@ public class HistoryArrayAdapter extends ArrayAdapter { public View getView(int position, View convertView, ViewGroup 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); if (showDatetime) { diff --git a/app/src/main/java/org/solovyev/android/calculator/history/HistoryFragment.java b/app/src/main/java/org/solovyev/android/calculator/history/HistoryFragment.java index be8b4ee9..8726810a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/HistoryFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/HistoryFragment.java @@ -22,19 +22,14 @@ 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.Locator; -import org.solovyev.android.calculator.Preferences; import org.solovyev.android.calculator.R; +import javax.annotation.Nonnull; import java.util.ArrayList; import java.util.List; -import javax.annotation.Nonnull; - public class HistoryFragment extends BaseHistoryFragment { public HistoryFragment() { @@ -48,11 +43,8 @@ public class HistoryFragment extends BaseHistoryFragment { @Nonnull @Override - protected List getHistoryItems() { - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); - final boolean showIntermediateCalculations = Preferences.History.showIntermediateCalculations.getPreference(preferences); - final List historyStates = Locator.getInstance().getHistory().getStates(showIntermediateCalculations); - return new ArrayList(historyStates); + protected List getHistoryItems() { + return new ArrayList<>(Locator.getInstance().getHistory().getStates(false)); } @Override diff --git a/app/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuData.java b/app/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuData.java index 9f07e202..91edbf0e 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuData.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuData.java @@ -34,23 +34,23 @@ import javax.annotation.Nonnull; public class HistoryItemMenuData { @Nonnull - private final ArrayAdapter adapter; + private final ArrayAdapter adapter; @Nonnull - private final HistoryState historyState; + private final OldHistoryState historyState; - public HistoryItemMenuData(@Nonnull HistoryState historyState, ArrayAdapter adapter) { + public HistoryItemMenuData(@Nonnull OldHistoryState historyState, ArrayAdapter adapter) { this.historyState = historyState; this.adapter = adapter; } @Nonnull - public HistoryState getHistoryState() { + public OldHistoryState getHistoryState() { return historyState; } @Nonnull - public ArrayAdapter getAdapter() { + public ArrayAdapter getAdapter() { return adapter; } } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/HistoryUtils.java b/app/src/main/java/org/solovyev/android/calculator/history/HistoryUtils.java deleted file mode 100644 index fcb21b08..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/history/HistoryUtils.java +++ /dev/null @@ -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(); - } - -} diff --git a/app/src/main/java/org/solovyev/android/calculator/history/DisplayHistoryState.java b/app/src/main/java/org/solovyev/android/calculator/history/OldDisplayHistoryState.java similarity index 85% rename from app/src/main/java/org/solovyev/android/calculator/history/DisplayHistoryState.java rename to app/src/main/java/org/solovyev/android/calculator/history/OldDisplayHistoryState.java index 66cd0733..5068e5fa 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/DisplayHistoryState.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/OldDisplayHistoryState.java @@ -35,8 +35,8 @@ import org.solovyev.common.text.Strings; import javax.annotation.Nonnull; import javax.annotation.Nullable; -@Root -public class DisplayHistoryState implements Cloneable { +@Root(name = "DisplayHistoryState") +public class OldDisplayHistoryState implements Cloneable { @Transient private boolean valid = true; @@ -47,7 +47,7 @@ public class DisplayHistoryState implements Cloneable { @Element @Nonnull - private EditorHistoryState editorState; + private OldEditorHistoryState editorState; @Element @Nonnull @@ -57,15 +57,15 @@ public class DisplayHistoryState implements Cloneable { @Nullable private Generic genericResult; - private DisplayHistoryState() { + private OldDisplayHistoryState() { // for xml } @Nonnull - public static DisplayHistoryState newInstance(@Nonnull DisplayState viewState) { - final DisplayHistoryState result = new DisplayHistoryState(); + public static OldDisplayHistoryState newInstance(@Nonnull DisplayState viewState) { + final OldDisplayHistoryState result = new OldDisplayHistoryState(); - result.editorState = EditorHistoryState.create(viewState); + result.editorState = OldEditorHistoryState.create(viewState); result.valid = viewState.isValid(); result.jsclOperation = viewState.getOperation(); @@ -89,7 +89,7 @@ public class DisplayHistoryState implements Cloneable { } @Nonnull - public EditorHistoryState getEditorState() { + public OldEditorHistoryState getEditorState() { return editorState; } @@ -114,7 +114,7 @@ public class DisplayHistoryState implements Cloneable { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - DisplayHistoryState that = (DisplayHistoryState) o; + OldDisplayHistoryState that = (OldDisplayHistoryState) o; if (!editorState.equals(that.editorState)) return false; if (jsclOperation != that.jsclOperation) return false; @@ -140,9 +140,9 @@ public class DisplayHistoryState implements Cloneable { } @Override - protected DisplayHistoryState clone() { + protected OldDisplayHistoryState clone() { try { - final DisplayHistoryState clone = (DisplayHistoryState) super.clone(); + final OldDisplayHistoryState clone = (OldDisplayHistoryState) super.clone(); clone.editorState = this.editorState.clone(); diff --git a/app/src/main/java/org/solovyev/android/calculator/history/EditorHistoryState.java b/app/src/main/java/org/solovyev/android/calculator/history/OldEditorHistoryState.java similarity index 80% rename from app/src/main/java/org/solovyev/android/calculator/history/EditorHistoryState.java rename to app/src/main/java/org/solovyev/android/calculator/history/OldEditorHistoryState.java index 8284d7ca..9ddbfcf6 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/EditorHistoryState.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/OldEditorHistoryState.java @@ -32,8 +32,8 @@ import org.solovyev.common.text.Strings; import javax.annotation.Nonnull; import javax.annotation.Nullable; -@Root -public class EditorHistoryState implements Cloneable { +@Root(name = "EditorHistoryState") +public class OldEditorHistoryState implements Cloneable { @Element private int cursorPosition; @@ -42,13 +42,13 @@ public class EditorHistoryState implements Cloneable { @Nullable private String text = ""; - private EditorHistoryState() { + private OldEditorHistoryState() { // for xml } @Nonnull - public static EditorHistoryState create(@Nonnull EditorState state) { - final EditorHistoryState result = new EditorHistoryState(); + public static OldEditorHistoryState create(@Nonnull EditorState state) { + final OldEditorHistoryState result = new OldEditorHistoryState(); result.text = state.getTextString(); result.cursorPosition = state.selection; @@ -57,8 +57,8 @@ public class EditorHistoryState implements Cloneable { } @Nonnull - public static EditorHistoryState create(@Nonnull DisplayState viewState) { - final EditorHistoryState result = new EditorHistoryState(); + public static OldEditorHistoryState create(@Nonnull DisplayState viewState) { + final OldEditorHistoryState result = new OldEditorHistoryState(); result.text = viewState.getText(); result.cursorPosition = viewState.getSelection(); @@ -83,9 +83,9 @@ public class EditorHistoryState implements Cloneable { @Override public boolean equals(Object o) { 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 (text != null ? !text.equals(that.text) : that.text != null) return false; @@ -109,9 +109,9 @@ public class EditorHistoryState implements Cloneable { } @Override - protected EditorHistoryState clone() { + protected OldEditorHistoryState clone() { try { - return (EditorHistoryState) super.clone(); + return (OldEditorHistoryState) super.clone(); } catch (CloneNotSupportedException e) { throw new UnsupportedOperationException(e); } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/History.java b/app/src/main/java/org/solovyev/android/calculator/history/OldHistory.java similarity index 77% rename from app/src/main/java/org/solovyev/android/calculator/history/History.java rename to app/src/main/java/org/solovyev/android/calculator/history/OldHistory.java index eefa6f37..802d2da6 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/History.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/OldHistory.java @@ -34,24 +34,24 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; -@Root -public class History { +@Root(name = "History") +public class OldHistory { @Nonnull - @ElementList(type = HistoryState.class, name = "historyItems") - private List items = new ArrayList(); + @ElementList(type = OldHistoryState.class, name = "historyItems") + private List items = new ArrayList(); - public History() { + public OldHistory() { } @Nullable - public static History fromXml(@Nullable String xml) { + public static OldHistory fromXml(@Nullable String xml) { if (xml == null) { return null; } final Serializer serializer = new Persister(); try { - return serializer.read(History.class, xml); + return serializer.read(OldHistory.class, xml); } catch (Exception e) { return null; } @@ -70,15 +70,15 @@ public class History { } @Nonnull - public List getItems() { + public List getItems() { return items; } - public void add(@Nonnull HistoryState state) { + public void add(@Nonnull OldHistoryState state) { items.add(state); } - public void addAll(@Nonnull Collection states) { + public void addAll(@Nonnull Collection states) { items.addAll(states); } @@ -86,7 +86,7 @@ public class History { items.clear(); } - public void remove(@Nonnull HistoryState state) { + public void remove(@Nonnull OldHistoryState state) { items.remove(state); } } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/HistoryState.java b/app/src/main/java/org/solovyev/android/calculator/history/OldHistoryState.java similarity index 76% rename from app/src/main/java/org/solovyev/android/calculator/history/HistoryState.java rename to app/src/main/java/org/solovyev/android/calculator/history/OldHistoryState.java index e1e4dd68..31b61434 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/HistoryState.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/OldHistoryState.java @@ -31,46 +31,46 @@ import org.solovyev.android.calculator.EditorState; import javax.annotation.Nonnull; -@Root -public class HistoryState extends BaseHistoryState { +@Root(name = "HistoryState") +public class OldHistoryState extends BaseHistoryState { @Element @Nonnull - private EditorHistoryState editorState; + private OldEditorHistoryState editorState; @Element @Nonnull - private DisplayHistoryState displayState; + private OldDisplayHistoryState displayState; - private HistoryState() { + private OldHistoryState() { // for xml } - private HistoryState(@Nonnull EditorHistoryState editorState, - @Nonnull DisplayHistoryState displayState) { + private OldHistoryState(@Nonnull OldEditorHistoryState editorState, + @Nonnull OldDisplayHistoryState displayState) { this.editorState = editorState; this.displayState = displayState; } @Nonnull - public static HistoryState create(@Nonnull Editor editor, + public static OldHistoryState create(@Nonnull Editor editor, @Nonnull Display display) { return create(editor.getState(), display.getState()); } @Nonnull - public static HistoryState create(@Nonnull EditorState editorState, + public static OldHistoryState create(@Nonnull EditorState editorState, @Nonnull DisplayState displayState) { - return new HistoryState(EditorHistoryState.create(editorState), DisplayHistoryState.newInstance(displayState)); + return new OldHistoryState(OldEditorHistoryState.create(editorState), OldDisplayHistoryState.newInstance(displayState)); } @Nonnull - public EditorHistoryState getEditorState() { + public OldEditorHistoryState getEditorState() { return editorState; } @Nonnull - public DisplayHistoryState getDisplayState() { + public OldDisplayHistoryState getDisplayState() { return displayState; } @@ -87,7 +87,7 @@ public class HistoryState extends BaseHistoryState { if (this == o) return true; 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.getId() != that.getId()) return false; @@ -112,8 +112,8 @@ public class HistoryState extends BaseHistoryState { } @Override - protected HistoryState clone() { - final HistoryState that = (HistoryState) super.clone(); + protected OldHistoryState clone() { + final OldHistoryState that = (OldHistoryState) super.clone(); that.editorState = this.editorState.clone(); that.displayState = this.displayState.clone(); diff --git a/app/src/main/java/org/solovyev/android/calculator/history/SavedHistoryFragment.java b/app/src/main/java/org/solovyev/android/calculator/history/SavedHistoryFragment.java index 7d5f56ec..7e2119b9 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/SavedHistoryFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/SavedHistoryFragment.java @@ -44,8 +44,8 @@ public class SavedHistoryFragment extends BaseHistoryFragment { @Nonnull @Override - protected List getHistoryItems() { - return new ArrayList(Locator.getInstance().getHistory().getSavedHistory()); + protected List getHistoryItems() { + return new ArrayList(Locator.getInstance().getHistory().getSavedOldHistory()); } @Override diff --git a/app/src/main/res/drawable-nodpi/divider.9.png b/app/src/main/res/drawable-nodpi/divider.9.png new file mode 100644 index 0000000000000000000000000000000000000000..41b776bb48f40b085747f8567b762706cb219745 GIT binary patch literal 97 zcmeAS@N?(olHy`uVBq!ia0vp^%plCc1|-8Yw(bW~oCO|{#S9GGogmC;e)9BMprEj) qi(?4K%;bcIhW|VsJc%WYYz&^JECT1{zh(iY89ZJ6T-G@yGywp8lNGlB literal 0 HcmV?d00001 diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 06e0e99a..83528343 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -294,6 +294,7 @@