Save history even when calculation doesn't happen on-the-fly

This commit is contained in:
serso 2016-04-30 09:32:47 +02:00
parent b2e04975b7
commit 95071736b4
4 changed files with 26 additions and 15 deletions

View File

@ -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();
}
}

View File

@ -254,9 +254,10 @@ public class History {
// don't add empty states to empty history
return;
}
recent.add(state);
if (recent.add(state)) {
onRecentChanged(new AddedEvent(state, true));
}
}
public void updateSaved(@Nonnull HistoryState state) {
Check.isMainThread();

View File

@ -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<HistoryState> 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() {

View File

@ -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));
}