From 09158df194cb0d14b47deb0657de011463c847a9 Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Sun, 18 Dec 2011 17:24:00 +0400 Subject: [PATCH] history changes --- res/values/strings.xml | 1 + .../calculator/CalculatorHistoryActivity.java | 20 ++++++++++++++++--- .../history/AbstractHistoryState.java | 11 ++++++++++ .../calculator/history/CalculatorHistory.java | 8 ++++++-- .../history/CalculatorHistoryState.java | 2 ++ .../history/HistoryItemMenuItem.java | 4 ++-- 6 files changed, 39 insertions(+), 7 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 96c92235..8d895559 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -309,6 +309,7 @@ Check the \'Round result\' preference in application settings - it should be tur Copy expression Copy result Value + Already saved (see entries above) Comment Save history Modify history diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorHistoryActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorHistoryActivity.java index 73c64a93..6088f991 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorHistoryActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorHistoryActivity.java @@ -14,6 +14,7 @@ import android.os.Bundle; import android.view.*; import android.widget.*; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.history.*; import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.view.AMenu; @@ -99,6 +100,7 @@ public class CalculatorHistoryActivity extends ListActivity { if (isAlreadySaved(historyState)) { menuItems.remove(HistoryItemMenuItem.save); } + menuItems.remove(HistoryItemMenuItem.remove); menuItems.remove(HistoryItemMenuItem.edit); } @@ -128,7 +130,15 @@ public class CalculatorHistoryActivity extends ListActivity { boolean result = false; try { historyState.setSaved(true); - if ( CalculatorHistory.instance.getSavedHistory().contains(historyState) ) { + if ( CollectionsUtils.contains(historyState, CalculatorHistory.instance.getSavedHistory(), new Equalizer() { + @Override + public boolean equals(@Nullable CalculatorHistoryState first, @Nullable CalculatorHistoryState second) { + return first != null && second != null && + first.getTime().getTime() == second.getTime().getTime() && + first.getDisplayState().equals(second.getDisplayState()) && + first.getEditorState().equals(second.getEditorState()); + } + }) ) { result = true; } } finally { @@ -199,10 +209,14 @@ public class CalculatorHistoryActivity extends ListActivity { } final TextView status = (TextView) result.findViewById(R.id.history_item_status); - if (state.isSaved() || isAlreadySaved(state)) { + if (state.isSaved()) { status.setText(ResourceCache.instance.getCaption("c_history_item_saved")); } else { - status.setText(ResourceCache.instance.getCaption("c_history_item_not_saved")); + if ( isAlreadySaved(state) ) { + status.setText(ResourceCache.instance.getCaption("c_history_item_already_saved")); + } else { + status.setText(ResourceCache.instance.getCaption("c_history_item_not_saved")); + } } return result; diff --git a/src/main/java/org/solovyev/android/calculator/history/AbstractHistoryState.java b/src/main/java/org/solovyev/android/calculator/history/AbstractHistoryState.java index b94f6786..553ecb5e 100644 --- a/src/main/java/org/solovyev/android/calculator/history/AbstractHistoryState.java +++ b/src/main/java/org/solovyev/android/calculator/history/AbstractHistoryState.java @@ -31,6 +31,17 @@ public class AbstractHistoryState implements Cloneable{ @Transient private boolean saved; + @Transient + private int id = 0; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + @NotNull public Date getTime() { return time; diff --git a/src/main/java/org/solovyev/android/calculator/history/CalculatorHistory.java b/src/main/java/org/solovyev/android/calculator/history/CalculatorHistory.java index 29d51476..b28aa9ae 100644 --- a/src/main/java/org/solovyev/android/calculator/history/CalculatorHistory.java +++ b/src/main/java/org/solovyev/android/calculator/history/CalculatorHistory.java @@ -29,6 +29,9 @@ public enum CalculatorHistory implements HistoryHelper { instance; + // todo serso: not synchronized + private int counter = 0; + @NotNull private final HistoryHelper history = new SimpleHistoryHelper(); @@ -97,6 +100,7 @@ public enum CalculatorHistory implements HistoryHelper { HistoryUtils.fromXml(value, this.savedHistory); for (CalculatorHistoryState historyState : savedHistory) { historyState.setSaved(true); + historyState.setId(counter++); } } } @@ -122,6 +126,7 @@ public enum CalculatorHistory implements HistoryHelper { } else { final CalculatorHistoryState savedState = historyState.clone(); + savedState.setId(counter++); savedState.setSaved(true); savedHistory.add(savedState); @@ -133,8 +138,7 @@ public enum CalculatorHistory implements HistoryHelper { public void removeSavedHistory(@NotNull CalculatorHistoryState historyState, @NotNull Context context, @NotNull SharedPreferences preferences) { historyState.setSaved(false); + this.savedHistory.remove(historyState); save(context); - this.savedHistory.clear(); - load(context, preferences); } } diff --git a/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryState.java b/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryState.java index 6d4bde09..e9cb74e2 100644 --- a/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryState.java +++ b/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryState.java @@ -77,6 +77,7 @@ public class CalculatorHistoryState extends AbstractHistoryState { CalculatorHistoryState that = (CalculatorHistoryState) o; if (this.isSaved() != that.isSaved()) return false; + if (this.getId() != that.getId()) return false; if (!displayState.equals(that.displayState)) return false; if (!editorState.equals(that.editorState)) return false; @@ -86,6 +87,7 @@ public class CalculatorHistoryState extends AbstractHistoryState { @Override public int hashCode() { int result = Boolean.valueOf(isSaved()).hashCode(); + result = 31 * result + getId(); result = 31 * result + editorState.hashCode(); result = 31 * result + displayState.hashCode(); return result; diff --git a/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuItem.java b/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuItem.java index 126572af..75718c86 100644 --- a/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuItem.java +++ b/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuItem.java @@ -91,12 +91,12 @@ public enum HistoryItemMenuItem implements AMenuItem { @Override public void doAction(@NotNull HistoryItemMenuData data, @NotNull Context context) { final CalculatorHistoryState historyState = data.getHistoryState(); - data.getAdapter().remove(historyState); if (historyState.isSaved()) { + data.getAdapter().remove(historyState); CalculatorHistory.instance.removeSavedHistory(historyState, context, PreferenceManager.getDefaultSharedPreferences(context)); Toast.makeText(context, "History item was removed!", Toast.LENGTH_LONG).show(); + data.getAdapter().notifyDataSetChanged(); } - data.getAdapter().notifyDataSetChanged(); } };