From 70abeeeea5d4673491a15fa377c45017e37c57b7 Mon Sep 17 00:00:00 2001 From: serso Date: Sat, 30 Apr 2016 09:32:47 +0200 Subject: [PATCH] Save history even when calculation doesn't happen on-the-fly --- .../android/calculator/Calculator.java | 10 ++------- .../android/calculator/history/History.java | 5 +++-- .../calculator/history/RecentHistory.java | 22 ++++++++++++++++--- .../calculator/BaseCalculatorTest.java | 4 ++-- 4 files changed, 26 insertions(+), 15 deletions(-) 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 f6a3e50c..4ca7f32a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Calculator.java +++ b/app/src/main/java/org/solovyev/android/calculator/Calculator.java @@ -102,17 +102,12 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis public void evaluate() { final EditorState state = editor.getState(); - evaluate(JsclOperation.numeric, state.getTextString()); + evaluate(JsclOperation.numeric, state.getTextString(), state.sequence); } public void simplify() { final EditorState state = editor.getState(); - evaluate(JsclOperation.simplify, state.getTextString()); - } - - public long evaluate(@Nonnull final JsclOperation operation, - @Nonnull final String expression) { - return evaluate(operation, expression, nextSequence()); + evaluate(JsclOperation.simplify, state.getTextString(), state.sequence); } public long evaluate(@Nonnull final JsclOperation operation, @Nonnull final String expression, @@ -343,5 +338,4 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis public static long nextSequence() { return SEQUENCER.incrementAndGet(); } - } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/History.java b/app/src/main/java/org/solovyev/android/calculator/history/History.java index ca6408c8..e4b90a9f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/History.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/History.java @@ -254,8 +254,9 @@ public class History { // don't add empty states to empty history return; } - recent.add(state); - onRecentChanged(new AddedEvent(state, true)); + if (recent.add(state)) { + onRecentChanged(new AddedEvent(state, true)); + } } public void updateSaved(@Nonnull HistoryState state) { diff --git a/app/src/main/java/org/solovyev/android/calculator/history/RecentHistory.java b/app/src/main/java/org/solovyev/android/calculator/history/RecentHistory.java index 45022de8..5cd58044 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/RecentHistory.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/RecentHistory.java @@ -2,7 +2,6 @@ package org.solovyev.android.calculator.history; import android.support.annotation.NonNull; import android.support.annotation.Nullable; - import org.solovyev.android.Check; import java.util.Collections; @@ -17,10 +16,13 @@ public class RecentHistory { private final List list = new LinkedList<>(); private int current = -1; - public void add(@NonNull HistoryState state) { + public boolean add(@NonNull HistoryState state) { Check.isMainThread(); if (isCurrent(state)) { - return; + return false; + } + if (updateState(state)) { + return true; } while (current != list.size() - 1) { list.remove(list.size() - 1); @@ -28,6 +30,20 @@ public class RecentHistory { list.add(state); current++; trim(); + return true; + } + + private boolean updateState(@NonNull HistoryState state) { + if (current == -1) { + return false; + } + final HistoryState old = list.get(current); + if (old.display.sequence == state.display.sequence && old.editor.sequence == state.editor.sequence) { + // if recalculation is taking place we need to update current history item + list.set(current, state); + return true; + } + return false; } private void trim() { diff --git a/app/src/test/java/org/solovyev/android/calculator/BaseCalculatorTest.java b/app/src/test/java/org/solovyev/android/calculator/BaseCalculatorTest.java index d5617970..93cdbdbb 100644 --- a/app/src/test/java/org/solovyev/android/calculator/BaseCalculatorTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/BaseCalculatorTest.java @@ -32,7 +32,7 @@ public abstract class BaseCalculatorTest { } protected final void assertError(@NonNull String expression) { - calculator.evaluate(numeric, expression); + calculator.evaluate(numeric, expression, 0); verify(calculator.bus, atLeastOnce()).post(argThat(failed())); } @@ -55,7 +55,7 @@ public abstract class BaseCalculatorTest { } protected final void assertEval(@NonNull final String expected, @NonNull final String expression, final JsclOperation operation) { - calculator.evaluate(operation, expression); + calculator.evaluate(operation, expression, 0); verify(calculator.bus, atLeastOnce()).post(finishedEvent(expected, expression, operation)); }