From b40b82574eced06b3babc08ec7500b9dc670c60f Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 28 Feb 2016 13:47:21 +0100 Subject: [PATCH] Use engine in TextHighlighter --- .../android/calculator/BaseNumberBuilder.java | 9 +------ .../solovyev/android/calculator/Display.java | 3 +++ .../android/calculator/DisplayView.java | 26 +++++++++++++++---- .../solovyev/android/calculator/Editor.java | 10 +++---- .../android/calculator/LiteNumberBuilder.java | 11 +------- .../android/calculator/NumberBuilder.java | 5 ---- .../functions/FunctionsRegistry.java | 4 --- .../calculator/view/EditorTextProcessor.java | 12 ++++----- .../calculator/view/TextHighlighter.java | 6 +++-- .../calculator/TextHighlighterTest.java | 10 +++---- 10 files changed, 45 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/BaseNumberBuilder.java b/app/src/main/java/org/solovyev/android/calculator/BaseNumberBuilder.java index 0e22f144..1386c370 100644 --- a/app/src/main/java/org/solovyev/android/calculator/BaseNumberBuilder.java +++ b/app/src/main/java/org/solovyev/android/calculator/BaseNumberBuilder.java @@ -23,20 +23,13 @@ package org.solovyev.android.calculator; import android.text.SpannableStringBuilder; - +import jscl.NumeralBase; import org.solovyev.android.calculator.math.MathType; import org.solovyev.common.text.Strings; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import jscl.NumeralBase; - -/** - * User: serso - * Date: 12/15/11 - * Time: 9:01 PM - */ public abstract class BaseNumberBuilder { @Nonnull diff --git a/app/src/main/java/org/solovyev/android/calculator/Display.java b/app/src/main/java/org/solovyev/android/calculator/Display.java index 0898b7ce..966b93ba 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Display.java +++ b/app/src/main/java/org/solovyev/android/calculator/Display.java @@ -52,6 +52,8 @@ public class Display { @Inject Application application; @Inject + Engine engine; + @Inject Lazy clipboard; @Inject Lazy notifier; @@ -139,6 +141,7 @@ public class Display { Check.isMainThread(); this.view = view; this.view.setState(state); + this.view.setEngine(engine); } @Nonnull diff --git a/app/src/main/java/org/solovyev/android/calculator/DisplayView.java b/app/src/main/java/org/solovyev/android/calculator/DisplayView.java index 82f42da7..df87b0b9 100644 --- a/app/src/main/java/org/solovyev/android/calculator/DisplayView.java +++ b/app/src/main/java/org/solovyev/android/calculator/DisplayView.java @@ -22,16 +22,14 @@ package org.solovyev.android.calculator; -import android.app.Service; import android.content.Context; import android.content.res.Resources; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.text.TextUtils; import android.text.method.ScrollingMovementMethod; import android.util.AttributeSet; import org.solovyev.android.Check; -import org.solovyev.android.calculator.text.TextProcessor; -import org.solovyev.android.calculator.text.TextProcessorEditorResult; import org.solovyev.android.calculator.view.TextHighlighter; import org.solovyev.android.views.AutoResizeTextView; @@ -42,8 +40,10 @@ import static android.util.TypedValue.applyDimension; public class DisplayView extends AutoResizeTextView { - @Nonnull - private final TextProcessor textHighlighter = new TextHighlighter(getTextColors().getDefaultColor(), false); + @Nullable + private Engine engine; + @Nullable + private TextHighlighter textHighlighter; @Nonnull private DisplayState state = DisplayState.empty(); @@ -62,6 +62,14 @@ public class DisplayView extends AutoResizeTextView { init(context); } + @Nullable + private TextHighlighter getTextHighlighter() { + if (textHighlighter == null && engine != null) { + textHighlighter = new TextHighlighter(getTextColors().getDefaultColor(), false, engine); + } + return textHighlighter; + } + private void init(@Nonnull Context context) { final Resources resources = getResources(); setAddEllipsis(false); @@ -95,12 +103,20 @@ public class DisplayView extends AutoResizeTextView { } } + public void setEngine(@Nullable Engine engine) { + this.engine = engine; + } + @NonNull private CharSequence highlightText(@Nonnull DisplayState state) { final String text = state.text; if (TextUtils.isEmpty(text)) { return ""; } + final TextHighlighter textHighlighter = getTextHighlighter(); + if (textHighlighter == null) { + return text; + } try { return textHighlighter.process(text).getCharSequence(); } catch (ParseException e) { 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 b8c984c4..4dd26105 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Editor.java +++ b/app/src/main/java/org/solovyev/android/calculator/Editor.java @@ -22,13 +22,9 @@ package org.solovyev.android.calculator; -import static java.lang.Math.min; - import android.content.SharedPreferences; - import com.squareup.otto.Bus; import com.squareup.otto.Subscribe; - import org.solovyev.android.Check; import org.solovyev.android.calculator.text.TextProcessorEditorResult; import org.solovyev.android.calculator.view.EditorTextProcessor; @@ -38,6 +34,8 @@ import javax.annotation.Nullable; import javax.inject.Inject; import javax.inject.Singleton; +import static java.lang.Math.min; + @Singleton public class Editor { @@ -51,8 +49,8 @@ public class Editor { Bus bus; @Inject - public Editor(@Nonnull SharedPreferences preferences) { - textProcessor = new EditorTextProcessor(preferences); + public Editor(@Nonnull SharedPreferences preferences, @Nonnull Engine engine) { + textProcessor = new EditorTextProcessor(preferences, engine); } public void init() { diff --git a/app/src/main/java/org/solovyev/android/calculator/LiteNumberBuilder.java b/app/src/main/java/org/solovyev/android/calculator/LiteNumberBuilder.java index db96956d..d1ade32d 100644 --- a/app/src/main/java/org/solovyev/android/calculator/LiteNumberBuilder.java +++ b/app/src/main/java/org/solovyev/android/calculator/LiteNumberBuilder.java @@ -23,24 +23,15 @@ package org.solovyev.android.calculator; import android.text.SpannableStringBuilder; - +import jscl.NumeralBase; import org.solovyev.android.calculator.math.MathType; import javax.annotation.Nonnull; -import jscl.NumeralBase; - -/** - * User: serso - * Date: 12/15/11 - * Time: 8:33 PM - */ - public class LiteNumberBuilder extends BaseNumberBuilder { public LiteNumberBuilder(@Nonnull Engine engine) { super(engine); - this.nb = engine.getMathEngine().getNumeralBase(); } @Override diff --git a/app/src/main/java/org/solovyev/android/calculator/NumberBuilder.java b/app/src/main/java/org/solovyev/android/calculator/NumberBuilder.java index 8b068954..49081844 100644 --- a/app/src/main/java/org/solovyev/android/calculator/NumberBuilder.java +++ b/app/src/main/java/org/solovyev/android/calculator/NumberBuilder.java @@ -41,11 +41,6 @@ import jscl.text.JsclIntegerParser; import jscl.text.ParseException; import jscl.text.Parser; -/** - * User: serso - * Date: 10/23/11 - * Time: 2:57 PM - */ public class NumberBuilder extends BaseNumberBuilder { public NumberBuilder(@Nonnull Engine engine) { diff --git a/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsRegistry.java b/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsRegistry.java index aad286e8..c7aa19b7 100644 --- a/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsRegistry.java @@ -30,7 +30,6 @@ import jscl.math.function.IFunction; import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; import org.solovyev.android.Check; -import org.solovyev.android.calculator.Calculator; import org.solovyev.android.calculator.entities.BaseEntitiesRegistry; import org.solovyev.android.calculator.entities.Category; import org.solovyev.android.calculator.entities.Entities; @@ -59,9 +58,6 @@ public class FunctionsRegistry extends BaseEntitiesRegistry { substitutes.put("√", "sqrt"); } - @Inject - Calculator calculator; - @Inject public FunctionsRegistry(@Nonnull JsclMathEngine mathEngine) { super(mathEngine.getFunctionsRegistry(), "c_fun_description_"); diff --git a/app/src/main/java/org/solovyev/android/calculator/view/EditorTextProcessor.java b/app/src/main/java/org/solovyev/android/calculator/view/EditorTextProcessor.java index b233dd1c..d81f06c0 100644 --- a/app/src/main/java/org/solovyev/android/calculator/view/EditorTextProcessor.java +++ b/app/src/main/java/org/solovyev/android/calculator/view/EditorTextProcessor.java @@ -4,6 +4,7 @@ import android.app.Application; import android.content.SharedPreferences; import org.solovyev.android.calculator.App; +import org.solovyev.android.calculator.Engine; import org.solovyev.android.calculator.Preferences; import org.solovyev.android.calculator.text.TextProcessor; import org.solovyev.android.calculator.text.TextProcessorEditorResult; @@ -20,8 +21,11 @@ public final class EditorTextProcessor implements TextProcessor textHighlighter = new TextHighlighter(Color.TRANSPARENT, false); + TextProcessor textHighlighter = new TextHighlighter(Color.TRANSPARENT, false, engine); final Random random = new Random(new Date().getTime()); for (int i = 0; i < 1000; i++) { @@ -69,7 +69,7 @@ public class TextHighlighterTest { assertEquals(")", textHighlighter.process(")").toString()); assertEquals(")()(", textHighlighter.process(")()(").toString()); - textHighlighter = new TextHighlighter(0, true); + textHighlighter = new TextHighlighter(0, true, engine); assertEquals("1 000 000", textHighlighter.process("1000000").toString()); assertEquals("1 000 000", textHighlighter.process("1000000").toString()); assertEquals("0.1E3", textHighlighter.process("0.1E3").toString()); @@ -88,7 +88,7 @@ public class TextHighlighterTest { assertEquals("-1 000 000E3", textHighlighter.process("-1000000E3").toString()); assertEquals("-1 000 000E-3", textHighlighter.process("-1000000E-3").toString()); assertEquals("-1 000 000E-30000", textHighlighter.process("-1000000E-30000").toString()); - textHighlighter = new TextHighlighter(0, false); + textHighlighter = new TextHighlighter(0, false, engine); textHighlighter.process("cannot calculate 3^10^10 !!!\n" + " unable to enter 0. FIXED\n" + @@ -159,7 +159,7 @@ public class TextHighlighterTest { @Test public void testTime() throws Exception { - final TextProcessor textHighlighter = new TextHighlighter(Color.WHITE, false); + final TextProcessor textHighlighter = new TextHighlighter(Color.WHITE, false, engine); final int count = 1000; final String subExpression = "cos(acos(t8ln(t5t85tln(8ln(5t55tln(5))))))+tln(88cos(tln(t)))+t√(ln(t))"; @@ -178,7 +178,7 @@ public class TextHighlighterTest { @Test public void testDarkColor() throws Exception { - final TextProcessor textHighlighter = new TextHighlighter(Color.BLACK, false); + final TextProcessor textHighlighter = new TextHighlighter(Color.BLACK, false, engine); assertEquals("", textHighlighter.process("sin(2cos(3))")); }