Save history even when calculation doesn't happen on-the-fly
This commit is contained in:
parent
f618f2cd44
commit
70abeeeea5
@ -102,17 +102,12 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis
|
|||||||
|
|
||||||
public void evaluate() {
|
public void evaluate() {
|
||||||
final EditorState state = editor.getState();
|
final EditorState state = editor.getState();
|
||||||
evaluate(JsclOperation.numeric, state.getTextString());
|
evaluate(JsclOperation.numeric, state.getTextString(), state.sequence);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void simplify() {
|
public void simplify() {
|
||||||
final EditorState state = editor.getState();
|
final EditorState state = editor.getState();
|
||||||
evaluate(JsclOperation.simplify, state.getTextString());
|
evaluate(JsclOperation.simplify, state.getTextString(), state.sequence);
|
||||||
}
|
|
||||||
|
|
||||||
public long evaluate(@Nonnull final JsclOperation operation,
|
|
||||||
@Nonnull final String expression) {
|
|
||||||
return evaluate(operation, expression, nextSequence());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public long evaluate(@Nonnull final JsclOperation operation, @Nonnull final String expression,
|
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() {
|
public static long nextSequence() {
|
||||||
return SEQUENCER.incrementAndGet();
|
return SEQUENCER.incrementAndGet();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -254,8 +254,9 @@ public class History {
|
|||||||
// don't add empty states to empty history
|
// don't add empty states to empty history
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
recent.add(state);
|
if (recent.add(state)) {
|
||||||
onRecentChanged(new AddedEvent(state, true));
|
onRecentChanged(new AddedEvent(state, true));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateSaved(@Nonnull HistoryState state) {
|
public void updateSaved(@Nonnull HistoryState state) {
|
||||||
|
@ -2,7 +2,6 @@ package org.solovyev.android.calculator.history;
|
|||||||
|
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
import org.solovyev.android.Check;
|
import org.solovyev.android.Check;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@ -17,10 +16,13 @@ public class RecentHistory {
|
|||||||
private final List<HistoryState> list = new LinkedList<>();
|
private final List<HistoryState> list = new LinkedList<>();
|
||||||
private int current = -1;
|
private int current = -1;
|
||||||
|
|
||||||
public void add(@NonNull HistoryState state) {
|
public boolean add(@NonNull HistoryState state) {
|
||||||
Check.isMainThread();
|
Check.isMainThread();
|
||||||
if (isCurrent(state)) {
|
if (isCurrent(state)) {
|
||||||
return;
|
return false;
|
||||||
|
}
|
||||||
|
if (updateState(state)) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
while (current != list.size() - 1) {
|
while (current != list.size() - 1) {
|
||||||
list.remove(list.size() - 1);
|
list.remove(list.size() - 1);
|
||||||
@ -28,6 +30,20 @@ public class RecentHistory {
|
|||||||
list.add(state);
|
list.add(state);
|
||||||
current++;
|
current++;
|
||||||
trim();
|
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() {
|
private void trim() {
|
||||||
|
@ -32,7 +32,7 @@ public abstract class BaseCalculatorTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected final void assertError(@NonNull String expression) {
|
protected final void assertError(@NonNull String expression) {
|
||||||
calculator.evaluate(numeric, expression);
|
calculator.evaluate(numeric, expression, 0);
|
||||||
verify(calculator.bus, atLeastOnce()).post(argThat(failed()));
|
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) {
|
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));
|
verify(calculator.bus, atLeastOnce()).post(finishedEvent(expected, expression, operation));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user