Calculations on fly preference

This commit is contained in:
Sergey Solovyev 2012-10-26 22:13:17 +04:00
parent 401ff87e91
commit ef141ed054
6 changed files with 801 additions and 759 deletions

View File

@ -51,6 +51,8 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
@NotNull @NotNull
private final Executor eventExecutor = Executors.newFixedThreadPool(1); private final Executor eventExecutor = Executors.newFixedThreadPool(1);
private volatile boolean calculateOnFly = true;
public CalculatorImpl() { public CalculatorImpl() {
this.addCalculatorEventListener(this); this.addCalculatorEventListener(this);
} }
@ -84,8 +86,8 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
@Override @Override
public void evaluate() { public void evaluate() {
final CalculatorEditorViewState viewState = getEditor().getViewState(); final CalculatorEditorViewState viewState = getEditor().getViewState();
fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState); final CalculatorEventData eventData = fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState);
this.evaluate(JsclOperation.numeric, viewState.getText()); this.evaluate(JsclOperation.numeric, viewState.getText(), eventData.getSequenceId());
} }
@Override @Override
@ -98,8 +100,8 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
@Override @Override
public void simplify() { public void simplify() {
final CalculatorEditorViewState viewState = getEditor().getViewState(); final CalculatorEditorViewState viewState = getEditor().getViewState();
fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState); final CalculatorEventData eventData = fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState);
this.evaluate(JsclOperation.simplify, viewState.getText()); this.evaluate(JsclOperation.simplify, viewState.getText(), eventData.getSequenceId());
} }
@NotNull @NotNull
@ -140,6 +142,10 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
CalculatorLocatorImpl.getInstance().getHistory().load(); CalculatorLocatorImpl.getInstance().getHistory().load();
} }
public void setCalculateOnFly(boolean calculateOnFly) {
this.calculateOnFly = calculateOnFly;
}
@NotNull @NotNull
private CalculatorConversionEventData newConversionEventData(@NotNull Long sequenceId, private CalculatorConversionEventData newConversionEventData(@NotNull Long sequenceId,
@NotNull Generic value, @NotNull Generic value,
@ -389,6 +395,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
switch (calculatorEventType) { switch (calculatorEventType) {
case editor_state_changed: case editor_state_changed:
if (calculateOnFly) {
final CalculatorEditorChangeEventData editorChangeEventData = (CalculatorEditorChangeEventData) data; final CalculatorEditorChangeEventData editorChangeEventData = (CalculatorEditorChangeEventData) data;
final String newText = editorChangeEventData.getNewValue().getText(); final String newText = editorChangeEventData.getNewValue().getText();
@ -397,6 +404,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
if (!newText.equals(oldText)) { if (!newText.equals(oldText)) {
evaluate(JsclOperation.numeric, editorChangeEventData.getNewValue().getText(), calculatorEventData.getSequenceId()); evaluate(JsclOperation.numeric, editorChangeEventData.getNewValue().getText(), calculatorEventData.getSequenceId());
} }
}
break; break;
case display_state_changed: case display_state_changed:

View File

@ -10,4 +10,7 @@
<string name="prefs_history_show_intermediate_calculations_title">Show intermediate calculations in history</string> <string name="prefs_history_show_intermediate_calculations_title">Show intermediate calculations in history</string>
<string name="prefs_history_show_intermediate_calculations_summary">If turned on all calculations will be shown on history screen</string> <string name="prefs_history_show_intermediate_calculations_summary">If turned on all calculations will be shown on history screen</string>
<string name="p_calculations_calculate_on_fly_title">Result is calculated while typing expression</string>
<string name="p_calculations_calculate_on_fly_summary">If turned on calculations are done automatically while typing expression</string>
</resources> </resources>

View File

@ -51,6 +51,11 @@
a:defaultValue="5" a:defaultValue="5"
range:boundaries="3;1000"/> range:boundaries="3;1000"/>
<android.preference.CheckBoxPreference
a:key="calculations_calculate_on_fly"
a:title="@string/p_calculations_calculate_on_fly_title"
a:summary="@string/p_calculations_calculate_on_fly_summary"/>
</PreferenceScreen> </PreferenceScreen>
</PreferenceScreen> </PreferenceScreen>

View File

@ -2,7 +2,10 @@ package org.solovyev.android.calculator;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Application;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
@ -21,13 +24,19 @@ import java.util.List;
* Date: 9/22/12 * Date: 9/22/12
* Time: 5:42 PM * Time: 5:42 PM
*/ */
public class AndroidCalculator implements Calculator, CalculatorEventListener { public class AndroidCalculator implements Calculator, CalculatorEventListener, SharedPreferences.OnSharedPreferenceChangeListener {
@NotNull @NotNull
private final Calculator calculator = new CalculatorImpl(); private final CalculatorImpl calculator = new CalculatorImpl();
public AndroidCalculator() { @NotNull
private final Application context;
public AndroidCalculator(@NotNull Application application) {
this.context = application;
this.calculator.addCalculatorEventListener(this); this.calculator.addCalculatorEventListener(this);
PreferenceManager.getDefaultSharedPreferences(application).registerOnSharedPreferenceChangeListener(this);
} }
public static void showEvaluationError(@NotNull Context context, @NotNull final String errorMessage) { public static void showEvaluationError(@NotNull Context context, @NotNull final String errorMessage) {
@ -121,6 +130,9 @@ public class AndroidCalculator implements Calculator, CalculatorEventListener {
@Override @Override
public void init() { public void init() {
this.calculator.init(); this.calculator.init();
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
this.calculator.setCalculateOnFly(CalculatorPreferences.Calculations.calculateOnFly.getPreference(prefs));
} }
@Override @Override
@ -215,4 +227,11 @@ public class AndroidCalculator implements Calculator, CalculatorEventListener {
break; break;
} }
} }
@Override
public void onSharedPreferenceChanged(@NotNull SharedPreferences prefs, @NotNull String key) {
if ( CalculatorPreferences.Calculations.calculateOnFly.getKey().equals(key) ) {
this.calculator.setCalculateOnFly(CalculatorPreferences.Calculations.calculateOnFly.getPreference(prefs));
}
}
} }

View File

@ -83,7 +83,7 @@ public class CalculatorApplication extends android.app.Application {
super.onCreate(); super.onCreate();
final AndroidCalculator calculator = new AndroidCalculator(); final AndroidCalculator calculator = new AndroidCalculator(this);
CalculatorLocatorImpl.getInstance().init(calculator, CalculatorLocatorImpl.getInstance().init(calculator,
new AndroidCalculatorEngine(this), new AndroidCalculatorEngine(this),

View File

@ -29,6 +29,12 @@ public final class CalculatorPreferences {
public static final Preference<Integer> appVersion = new IntegerPreference("application.version", -1); public static final Preference<Integer> appVersion = new IntegerPreference("application.version", -1);
public static final Preference<Integer> appOpenedCounter = new IntegerPreference("app_opened_counter", 0); public static final Preference<Integer> appOpenedCounter = new IntegerPreference("app_opened_counter", 0);
public static class Calculations {
public static final Preference<Boolean> calculateOnFly = new BooleanPreference("calculations_calculate_on_fly", true);
}
public static class Gui { public static class Gui {
public static final Preference<Theme> theme = StringPreference.newInstance("org.solovyev.android.calculator.CalculatorActivity_calc_theme", Theme.metro_blue_theme, Theme.class); public static final Preference<Theme> theme = StringPreference.newInstance("org.solovyev.android.calculator.CalculatorActivity_calc_theme", Theme.metro_blue_theme, Theme.class);
@ -169,6 +175,7 @@ public final class CalculatorPreferences {
applyDefaultPreference(preferences, Graph.lineColorImag); applyDefaultPreference(preferences, Graph.lineColorImag);
applyDefaultPreference(preferences, Graph.lineColorReal); applyDefaultPreference(preferences, Graph.lineColorReal);
applyDefaultPreference(preferences, History.showIntermediateCalculations); applyDefaultPreference(preferences, History.showIntermediateCalculations);
applyDefaultPreference(preferences, Calculations.calculateOnFly);
if ( !VibratorContainer.Preferences.hapticFeedbackEnabled.isSet(preferences) ) { if ( !VibratorContainer.Preferences.hapticFeedbackEnabled.isSet(preferences) ) {
VibratorContainer.Preferences.hapticFeedbackEnabled.putPreference(preferences, true); VibratorContainer.Preferences.hapticFeedbackEnabled.putPreference(preferences, true);