From 3edaa64d2db189d2d79f7054c0ac028fd0f3250e Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 28 Feb 2016 13:54:02 +0100 Subject: [PATCH] DI for ToJsclTextProcessor --- .../solovyev/android/calculator/Calculator.java | 4 ++-- .../android/calculator/ToJsclTextProcessor.java | 14 +++++--------- .../calculator/variables/EditVariableFragment.java | 14 +++++++++++++- .../calculator/variables/VariablesFragment.java | 9 --------- 4 files changed, 20 insertions(+), 21 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 74cb06eb..64ad57ac 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Calculator.java +++ b/app/src/main/java/org/solovyev/android/calculator/Calculator.java @@ -70,8 +70,6 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis @Nonnull private static final AtomicLong SEQUENCER = new AtomicLong(NO_SEQUENCE); @Nonnull - private final ToJsclTextProcessor preprocessor = ToJsclTextProcessor.getInstance(); - @Nonnull private final SharedPreferences preferences; @Nonnull private final Bus bus; @@ -88,6 +86,8 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis Editor editor; @Inject JsclMathEngine mathEngine; + @Inject + ToJsclTextProcessor preprocessor; @Inject public Calculator(@Nonnull SharedPreferences preferences, @Nonnull Bus bus, @Named(AppModule.THREAD_UI) @Nonnull Executor ui, @Named(AppModule.THREAD_BACKGROUND) @Nonnull Executor background) { diff --git a/app/src/main/java/org/solovyev/android/calculator/ToJsclTextProcessor.java b/app/src/main/java/org/solovyev/android/calculator/ToJsclTextProcessor.java index bd1a2551..71fe0c23 100644 --- a/app/src/main/java/org/solovyev/android/calculator/ToJsclTextProcessor.java +++ b/app/src/main/java/org/solovyev/android/calculator/ToJsclTextProcessor.java @@ -30,23 +30,19 @@ import org.solovyev.common.msg.MessageType; import org.solovyev.common.search.StartsWithFinder; import javax.annotation.Nonnull; +import javax.inject.Inject; +import javax.inject.Singleton; import java.util.ArrayList; import java.util.List; +@Singleton public class ToJsclTextProcessor implements TextProcessor { @Nonnull private static final Integer MAX_DEPTH = 20; - @Nonnull - private static final ToJsclTextProcessor instance = new ToJsclTextProcessor(); - - private ToJsclTextProcessor() { - } - - @Nonnull - public static ToJsclTextProcessor getInstance() { - return instance; + @Inject + public ToJsclTextProcessor() { } private static PreparedExpression processWithDepth(@Nonnull String s, int depth, @Nonnull List undefinedVars) throws ParseException { diff --git a/app/src/main/java/org/solovyev/android/calculator/variables/EditVariableFragment.java b/app/src/main/java/org/solovyev/android/calculator/variables/EditVariableFragment.java index 770fa233..eac031cf 100644 --- a/app/src/main/java/org/solovyev/android/calculator/variables/EditVariableFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/variables/EditVariableFragment.java @@ -43,6 +43,7 @@ import android.widget.EditText; import android.widget.PopupWindow; import butterknife.Bind; import butterknife.ButterKnife; +import dagger.Lazy; import jscl.math.function.IConstant; import org.solovyev.android.Activities; import org.solovyev.android.Check; @@ -96,6 +97,8 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF FunctionsRegistry functionsRegistry; @Inject VariablesRegistry variablesRegistry; + @Inject + Lazy toJsclTextProcessor; @Nullable private CppVariable variable; @@ -133,6 +136,15 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF App.showDialog(create(variable), "variable-editor", fm); } + public boolean isValidValue(@Nonnull String value) { + try { + final PreparedExpression pe = toJsclTextProcessor.get().process(value); + return !pe.hasUndefinedVariables(); + } catch (RuntimeException e) { + return false; + } + } + @Override public void onCreate(@android.support.annotation.Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -220,7 +232,7 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF final String value = valueView.getText().toString(); if (!Strings.isEmpty(value)) { // value is not empty => must be a number - if (!VariablesFragment.isValidValue(value)) { + if (!isValidValue(value)) { setError(valueLabel, R.string.c_value_is_not_a_number); return false; } diff --git a/app/src/main/java/org/solovyev/android/calculator/variables/VariablesFragment.java b/app/src/main/java/org/solovyev/android/calculator/variables/VariablesFragment.java index 4ea56b45..6465ddbd 100644 --- a/app/src/main/java/org/solovyev/android/calculator/variables/VariablesFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/variables/VariablesFragment.java @@ -55,15 +55,6 @@ public class VariablesFragment extends BaseEntitiesFragment { @Inject Bus bus; - public static boolean isValidValue(@Nonnull String value) { - try { - final PreparedExpression pe = ToJsclTextProcessor.getInstance().process(value); - return !pe.hasUndefinedVariables(); - } catch (RuntimeException e) { - return false; - } - } - @Override protected void inject(@Nonnull AppComponent component) { super.inject(component);