Calculator should re-evaluate expression if it was caused by preference change

This commit is contained in:
serso 2016-02-29 17:45:37 +01:00
parent dd74c3a231
commit 8b36232e07
2 changed files with 39 additions and 15 deletions

View File

@ -26,15 +26,16 @@ import android.content.SharedPreferences;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe; import com.squareup.otto.Subscribe;
import jscl.*;
import jscl.math.Generic;
import jscl.math.function.Constants;
import jscl.math.function.IConstant;
import jscl.text.ParseInterruptedException;
import org.solovyev.android.Check; import org.solovyev.android.Check;
import org.solovyev.android.calculator.calculations.*; import org.solovyev.android.calculator.calculations.CalculationCancelledEvent;
import org.solovyev.android.calculator.calculations.CalculationFailedEvent;
import org.solovyev.android.calculator.calculations.CalculationFinishedEvent;
import org.solovyev.android.calculator.calculations.ConversionFailedEvent;
import org.solovyev.android.calculator.calculations.ConversionFinishedEvent;
import org.solovyev.android.calculator.functions.FunctionsRegistry; import org.solovyev.android.calculator.functions.FunctionsRegistry;
import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.android.calculator.variables.CppVariable; import org.solovyev.android.calculator.variables.CppVariable;
@ -45,11 +46,6 @@ import org.solovyev.common.msg.MessageType;
import org.solovyev.common.text.Strings; import org.solovyev.common.text.Strings;
import org.solovyev.common.units.ConversionException; import org.solovyev.common.units.ConversionException;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -57,6 +53,21 @@ import java.util.List;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import jscl.JsclArithmeticException;
import jscl.MathEngine;
import jscl.NumeralBase;
import jscl.NumeralBaseException;
import jscl.math.Generic;
import jscl.math.function.Constants;
import jscl.math.function.IConstant;
import jscl.text.ParseInterruptedException;
@Singleton @Singleton
public class Calculator implements SharedPreferences.OnSharedPreferenceChangeListener { public class Calculator implements SharedPreferences.OnSharedPreferenceChangeListener {
@ -274,7 +285,7 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis
if (!calculateOnFly) { if (!calculateOnFly) {
return; return;
} }
if (TextUtils.equals(e.newState.text, e.oldState.text)) { if (!e.shouldEvaluate()) {
return; return;
} }
evaluate(JsclOperation.numeric, e.newState.getTextString(), e.newState.sequence); evaluate(JsclOperation.numeric, e.newState.getTextString(), e.newState.sequence);

View File

@ -23,6 +23,8 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.text.TextUtils;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe; import com.squareup.otto.Subscribe;
import org.solovyev.android.Check; import org.solovyev.android.Check;
@ -87,6 +89,11 @@ public class Editor {
@Nonnull @Nonnull
public EditorState onTextChanged(@Nonnull EditorState newState) { public EditorState onTextChanged(@Nonnull EditorState newState) {
return onTextChanged(newState, false);
}
@Nonnull
public EditorState onTextChanged(@Nonnull EditorState newState, boolean force) {
Check.isMainThread(); Check.isMainThread();
if (textProcessor != null) { if (textProcessor != null) {
final TextProcessorEditorResult result = textProcessor.process(newState.getTextString()); final TextProcessorEditorResult result = textProcessor.process(newState.getTextString());
@ -97,7 +104,7 @@ public class Editor {
if (view != null) { if (view != null) {
view.setState(newState); view.setState(newState);
} }
bus.post(new ChangedEvent(oldState, newState)); bus.post(new ChangedEvent(oldState, newState, force));
return state; return state;
} }
@ -224,7 +231,7 @@ public class Editor {
public void onEngineChanged(@Nonnull Engine.ChangedEvent e) { public void onEngineChanged(@Nonnull Engine.ChangedEvent e) {
// this will effectively apply new formatting (if f.e. grouping separator has changed) and // this will effectively apply new formatting (if f.e. grouping separator has changed) and
// will start new evaluation // will start new evaluation
onTextChanged(getState()); onTextChanged(getState(), true);
} }
public static class ChangedEvent { public static class ChangedEvent {
@ -232,10 +239,16 @@ public class Editor {
public final EditorState oldState; public final EditorState oldState;
@Nonnull @Nonnull
public final EditorState newState; public final EditorState newState;
public final boolean force;
private ChangedEvent(@Nonnull EditorState oldState, @Nonnull EditorState newState) { private ChangedEvent(@Nonnull EditorState oldState, @Nonnull EditorState newState, boolean force) {
this.oldState = oldState; this.oldState = oldState;
this.newState = newState; this.newState = newState;
this.force = force;
}
boolean shouldEvaluate() {
return force || !TextUtils.equals(newState.text, oldState.text);
} }
} }