From 8b36232e07ba8826b502c57bd3bf3dd73096750f Mon Sep 17 00:00:00 2001 From: serso Date: Mon, 29 Feb 2016 17:45:37 +0100 Subject: [PATCH] Calculator should re-evaluate expression if it was caused by preference change --- .../android/calculator/Calculator.java | 35 ++++++++++++------- .../solovyev/android/calculator/Editor.java | 19 ++++++++-- 2 files changed, 39 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 89e73f17..766f26a2 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Calculator.java +++ b/app/src/main/java/org/solovyev/android/calculator/Calculator.java @@ -26,15 +26,16 @@ import android.content.SharedPreferences; import android.support.annotation.NonNull; import android.text.TextUtils; import android.util.Log; + import com.squareup.otto.Bus; 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.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.jscl.JsclOperation; 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.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.util.ArrayList; import java.util.Collections; @@ -57,6 +53,21 @@ import java.util.List; import java.util.concurrent.Executor; 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 public class Calculator implements SharedPreferences.OnSharedPreferenceChangeListener { @@ -274,7 +285,7 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis if (!calculateOnFly) { return; } - if (TextUtils.equals(e.newState.text, e.oldState.text)) { + if (!e.shouldEvaluate()) { return; } evaluate(JsclOperation.numeric, e.newState.getTextString(), e.newState.sequence); diff --git a/app/src/main/java/org/solovyev/android/calculator/Editor.java b/app/src/main/java/org/solovyev/android/calculator/Editor.java index 4dd26105..4b388eb6 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Editor.java +++ b/app/src/main/java/org/solovyev/android/calculator/Editor.java @@ -23,6 +23,8 @@ package org.solovyev.android.calculator; import android.content.SharedPreferences; +import android.text.TextUtils; + import com.squareup.otto.Bus; import com.squareup.otto.Subscribe; import org.solovyev.android.Check; @@ -87,6 +89,11 @@ public class Editor { @Nonnull public EditorState onTextChanged(@Nonnull EditorState newState) { + return onTextChanged(newState, false); + } + + @Nonnull + public EditorState onTextChanged(@Nonnull EditorState newState, boolean force) { Check.isMainThread(); if (textProcessor != null) { final TextProcessorEditorResult result = textProcessor.process(newState.getTextString()); @@ -97,7 +104,7 @@ public class Editor { if (view != null) { view.setState(newState); } - bus.post(new ChangedEvent(oldState, newState)); + bus.post(new ChangedEvent(oldState, newState, force)); return state; } @@ -224,7 +231,7 @@ public class Editor { public void onEngineChanged(@Nonnull Engine.ChangedEvent e) { // this will effectively apply new formatting (if f.e. grouping separator has changed) and // will start new evaluation - onTextChanged(getState()); + onTextChanged(getState(), true); } public static class ChangedEvent { @@ -232,10 +239,16 @@ public class Editor { public final EditorState oldState; @Nonnull 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.newState = newState; + this.force = force; + } + + boolean shouldEvaluate() { + return force || !TextUtils.equals(newState.text, oldState.text); } }