diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorHistoryActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorHistoryActivity.java index 6088f991..1bd2b2ac 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorHistoryActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorHistoryActivity.java @@ -25,10 +25,7 @@ import org.solovyev.common.utils.*; import org.solovyev.common.utils.Filter; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; +import java.util.*; /** * User: serso @@ -41,7 +38,8 @@ public class CalculatorHistoryActivity extends ListActivity { @Override public int compare(CalculatorHistoryState state1, CalculatorHistoryState state2) { if (state1.isSaved() == state2.isSaved()) { - return state2.getTime().compareTo(state1.getTime()); + long l = state2.getTime() - state1.getTime(); + return l > 0l ? 1 : (l < 0l ? -1 : 0); } else if (state1.isSaved()) { return -1; } else if (state2.isSaved()) { @@ -134,7 +132,7 @@ public class CalculatorHistoryActivity extends ListActivity { @Override public boolean equals(@Nullable CalculatorHistoryState first, @Nullable CalculatorHistoryState second) { return first != null && second != null && - first.getTime().getTime() == second.getTime().getTime() && + first.getTime() == second.getTime() && first.getDisplayState().equals(second.getDisplayState()) && first.getEditorState().equals(second.getEditorState()); } @@ -195,7 +193,7 @@ public class CalculatorHistoryActivity extends ListActivity { final CalculatorHistoryState state = getItem(position); final TextView time = (TextView) result.findViewById(R.id.history_time); - time.setText(new SimpleDateFormat().format(state.getTime())); + time.setText(new SimpleDateFormat().format(new Date(state.getTime()))); final TextView editor = (TextView) result.findViewById(R.id.history_item); editor.setText(getHistoryText(state)); 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 553ecb5e..39d942b9 100644 --- a/src/main/java/org/solovyev/android/calculator/history/AbstractHistoryState.java +++ b/src/main/java/org/solovyev/android/calculator/history/AbstractHistoryState.java @@ -21,8 +21,7 @@ import java.util.Date; public class AbstractHistoryState implements Cloneable{ @Element - @NotNull - private Date time = new Date(); + private long time = new Date().getTime(); @Element(required = false) @Nullable @@ -42,12 +41,11 @@ public class AbstractHistoryState implements Cloneable{ this.id = id; } - @NotNull - public Date getTime() { + public long getTime() { return time; } - public void setTime(@NotNull Date time) { + public void setTime(long time) { this.time = time; } @@ -74,7 +72,6 @@ public class AbstractHistoryState implements Cloneable{ try { clone = (AbstractHistoryState)super.clone(); - clone.time = new Date(this.time.getTime()); } catch (CloneNotSupportedException e) { throw new UnsupportedOperationException(e); } 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 b28aa9ae..947d1775 100644 --- a/src/main/java/org/solovyev/android/calculator/history/CalculatorHistory.java +++ b/src/main/java/org/solovyev/android/calculator/history/CalculatorHistory.java @@ -97,6 +97,7 @@ public enum CalculatorHistory implements HistoryHelper { public void load(@Nullable Context context, @Nullable SharedPreferences preferences) { if (context != null && preferences != null) { final String value = preferences.getString(context.getString(R.string.p_calc_history), null); + this.savedHistory.clear(); HistoryUtils.fromXml(value, this.savedHistory); for (CalculatorHistoryState historyState : savedHistory) { historyState.setSaved(true); diff --git a/src/test/java/org/solovyev/android/calculator/history/HistoryUtilsTest.java b/src/test/java/org/solovyev/android/calculator/history/HistoryUtilsTest.java index 35c6687f..7f73d43e 100644 --- a/src/test/java/org/solovyev/android/calculator/history/HistoryUtilsTest.java +++ b/src/test/java/org/solovyev/android/calculator/history/HistoryUtilsTest.java @@ -41,7 +41,7 @@ public class HistoryUtilsTest { private static final String toXml1 = "\n" + " \n" + " \n" + - " \n" + + " \n" + " \n" + " 3\n" + " 1+1\n" + @@ -60,7 +60,7 @@ public class HistoryUtilsTest { private static final String toXml2 = "\n" + " \n" + " \n" + - " \n" + + " \n" + " \n" + " 3\n" + " 1+1\n" + @@ -74,7 +74,7 @@ public class HistoryUtilsTest { " \n" + " \n" + " \n" + - " \n" + + " \n" + " \n" + " 2\n" + " 5/6\n" + @@ -88,7 +88,7 @@ public class HistoryUtilsTest { " \n" + " \n" + " \n" + - " \n" + + " \n" + " \n" + " 1\n" + " null\n" + @@ -102,7 +102,7 @@ public class HistoryUtilsTest { " \n" + " \n" + " \n" + - " \n" + + " \n" + " \n" + " 0\n" + " 4+5/35sin(41)+dfdsfsdfs\n" + @@ -135,7 +135,7 @@ public class HistoryUtilsTest { calculatorEditor.setText("1+1"); CalculatorHistoryState state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay); - state.setTime(date); + state.setTime(date.getTime()); history.addState(state); Assert.assertEquals(emptyHistory, HistoryUtils.toXml(history.getStates())); @@ -157,7 +157,7 @@ public class HistoryUtilsTest { state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay); state.setSaved(true); - state.setTime(date); + state.setTime(date.getTime()); history.addState(state); calculatorDisplay = new TestCalculatorDisplay(); @@ -172,7 +172,7 @@ public class HistoryUtilsTest { state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay); state.setSaved(true); - state.setTime(date); + state.setTime(date.getTime()); history.addState(state); calculatorDisplay = new TestCalculatorDisplay(); @@ -187,7 +187,7 @@ public class HistoryUtilsTest { state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay); state.setSaved(true); - state.setTime(date); + state.setTime(date.getTime()); history.addState(state); String xml = HistoryUtils.toXml(history.getStates());