Use engine in TextHighlighter

This commit is contained in:
serso 2016-02-28 13:47:21 +01:00
parent c324c1408e
commit b40b82574e
10 changed files with 45 additions and 51 deletions

View File

@ -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

View File

@ -52,6 +52,8 @@ public class Display {
@Inject
Application application;
@Inject
Engine engine;
@Inject
Lazy<Clipboard> clipboard;
@Inject
Lazy<Notifier> notifier;
@ -139,6 +141,7 @@ public class Display {
Check.isMainThread();
this.view = view;
this.view.setState(state);
this.view.setEngine(engine);
}
@Nonnull

View File

@ -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<TextProcessorEditorResult, String> 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) {

View File

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

View File

@ -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

View File

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

View File

@ -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<Function> {
substitutes.put("", "sqrt");
}
@Inject
Calculator calculator;
@Inject
public FunctionsRegistry(@Nonnull JsclMathEngine mathEngine) {
super(mathEngine.getFunctionsRegistry(), "c_fun_description_");

View File

@ -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<TextProcessorEdi
@Nullable
private TextHighlighter textHighlighter;
@Nonnull
private final Engine engine;
public EditorTextProcessor(@Nonnull SharedPreferences preferences) {
public EditorTextProcessor(@Nonnull SharedPreferences preferences, @Nonnull Engine engine) {
this.engine = engine;
preferences.registerOnSharedPreferenceChangeListener(this);
onSharedPreferenceChanged(preferences, colorDisplay.getKey());
}
@ -44,10 +48,6 @@ public final class EditorTextProcessor implements TextProcessor<TextProcessorEdi
return textHighlighter;
}
public boolean isHighlightText() {
return highlightText;
}
public void setHighlightText(boolean highlightText) {
this.highlightText = highlightText;
}
@ -58,7 +58,7 @@ public final class EditorTextProcessor implements TextProcessor<TextProcessorEdi
setHighlightText(colorDisplay.getPreference(preferences));
} else if (theme.isSameKey(key)) {
final int color = getTextColor(preferences);
textHighlighter = new TextHighlighter(color, true);
textHighlighter = new TextHighlighter(color, true, engine);
}
}

View File

@ -44,9 +44,12 @@ public class TextHighlighter implements TextProcessor<TextProcessorEditorResult,
private final int blue;
private final boolean formatNumber;
private final int dark;
@Nonnull
private final Engine engine;
public TextHighlighter(int color, boolean formatNumber) {
public TextHighlighter(int color, boolean formatNumber, @Nonnull Engine engine) {
this.formatNumber = formatNumber;
this.engine = engine;
red = red(color);
green = green(color);
blue = blue(color);
@ -77,7 +80,6 @@ public class TextHighlighter implements TextProcessor<TextProcessorEditorResult,
@Nonnull
@Override
public TextProcessorEditorResult process(@Nonnull String text) {
final Engine engine = Locator.getInstance().getEngine();
final SpannableStringBuilder sb = new SpannableStringBuilder();
final BaseNumberBuilder nb = !formatNumber ? new LiteNumberBuilder(engine) : new NumberBuilder(engine);
final MathType.Result result = new MathType.Result();

View File

@ -49,7 +49,7 @@ public class TextHighlighterTest {
@Test
public void testProcess() throws Exception {
TextProcessor<?, String> textHighlighter = new TextHighlighter(Color.TRANSPARENT, false);
TextProcessor<?, String> 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<?, String> textHighlighter = new TextHighlighter(Color.WHITE, false);
final TextProcessor<?, String> 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<?, String> textHighlighter = new TextHighlighter(Color.BLACK, false);
final TextProcessor<?, String> textHighlighter = new TextHighlighter(Color.BLACK, false, engine);
assertEquals("", textHighlighter.process("sin(2cos(3))"));
}