Store grouping separator in integer preference and remove boolean from Engine

This commit is contained in:
serso
2016-05-02 14:20:27 +02:00
parent 1c6651bd4b
commit 7e26b35199
10 changed files with 119 additions and 77 deletions

View File

@@ -33,6 +33,7 @@ import org.solovyev.android.calculator.functions.FunctionsRegistry;
import org.solovyev.android.calculator.operators.OperatorsRegistry;
import org.solovyev.android.calculator.operators.PostfixFunctionsRegistry;
import org.solovyev.android.prefs.BooleanPreference;
import org.solovyev.android.prefs.CharacterPreference;
import org.solovyev.android.prefs.IntegerPreference;
import org.solovyev.android.prefs.Preference;
import org.solovyev.android.prefs.StringPreference;
@@ -93,7 +94,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
this.mathEngine = mathEngine;
this.mathEngine.setRoundResult(true);
this.mathEngine.setUseGroupingSeparator(true);
this.mathEngine.setGroupingSeparator(JsclMathEngine.GROUPING_SEPARATOR_DEFAULT);
}
private static void migratePreference(@Nonnull SharedPreferences preferences, @Nonnull BooleanPreference preference, @Nonnull String oldKey, @Nonnull SharedPreferences.Editor editor) {
@@ -110,6 +111,15 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
editor.putString(preference.getKey(), preferences.getString(oldKey, null));
}
private static void migratePreference(@Nonnull SharedPreferences preferences, @Nonnull CharacterPreference preference, @Nonnull String oldKey, @Nonnull SharedPreferences.Editor editor) {
if (!preferences.contains(oldKey)) {
return;
}
final String s = preferences.getString(oldKey, null);
editor.putInt(preference.getKey(), TextUtils.isEmpty(s) ? 0 : s.charAt(0));
}
public static boolean isValidName(@Nullable String name) {
if (!TextUtils.isEmpty(name)) {
try {
@@ -212,14 +222,8 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
mathEngine.setPrecision(Preferences.Output.precision.getPreference(preferences));
mathEngine.setScienceNotation(Preferences.Output.scientificNotation.getPreference(preferences));
mathEngine.setRoundResult(Preferences.Output.round.getPreference(preferences));
mathEngine.setGroupingSeparator(Preferences.Output.separator.getPreference(preferences));
final String groupingSeparator = Preferences.Output.separator.getPreference(preferences);
if (TextUtils.isEmpty(groupingSeparator)) {
mathEngine.setUseGroupingSeparator(false);
} else {
mathEngine.setUseGroupingSeparator(true);
mathEngine.setGroupingSeparator(groupingSeparator.charAt(0));
}
bus.post(ChangedEvent.INSTANCE);
}
@@ -290,7 +294,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
public static final BooleanPreference scientificNotation = BooleanPreference.of("engine.output.scientificNotation", false);
public static final BooleanPreference round = BooleanPreference.of("engine.output.round", true);
public static final StringPreference<Notation> notation = StringPreference.ofEnum("engine.output.notation", Notation.dec, Notation.class);
public static final StringPreference<String> separator = StringPreference.of("engine.output.separator", String.valueOf(JsclMathEngine.GROUPING_SEPARATOR_DEFAULT));
public static final CharacterPreference separator = CharacterPreference.of("engine.output.separator", JsclMathEngine.GROUPING_SEPARATOR_DEFAULT);
}
}

View File

@@ -114,15 +114,15 @@ public final class Preferences {
final Locale locale = Locale.getDefault();
if (locale != null) {
final DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(locale);
int index = MathType.grouping_separator.getTokens().indexOf(String.valueOf(decimalFormatSymbols.getGroupingSeparator()));
final String groupingSeparator;
final int index = MathType.grouping_separator.getTokens().indexOf(String.valueOf(decimalFormatSymbols.getGroupingSeparator()));
final char separator;
if (index >= 0) {
groupingSeparator = MathType.grouping_separator.getTokens().get(index);
separator = MathType.grouping_separator.getTokens().get(index).charAt(0);
} else {
groupingSeparator = String.valueOf(JsclMathEngine.GROUPING_SEPARATOR_DEFAULT);
separator = JsclMathEngine.GROUPING_SEPARATOR_DEFAULT;
}
Engine.Preferences.Output.separator.putPreference(editor, groupingSeparator);
Engine.Preferences.Output.separator.putPreference(editor, separator);
}
}

View File

@@ -125,15 +125,15 @@ public class History {
private static boolean isIntermediate(@Nonnull String olderText,
@Nonnull String newerText,
@NonNull String groupingSeparator) {
char separator) {
if (TextUtils.isEmpty(olderText)) {
return true;
}
if (TextUtils.isEmpty(newerText)) {
return false;
}
olderText = trimGroupingSeparators(olderText, groupingSeparator);
newerText = trimGroupingSeparators(newerText, groupingSeparator);
olderText = trimGroupingSeparators(olderText, separator);
newerText = trimGroupingSeparators(newerText, separator);
final int diff = newerText.length() - olderText.length();
if (diff >= 1) {
@@ -148,11 +148,10 @@ public class History {
}
@NonNull
static String trimGroupingSeparators(@NonNull String text, @NonNull String groupingSeparator) {
if (TextUtils.isEmpty(groupingSeparator)) {
static String trimGroupingSeparators(@NonNull String text, char separator) {
if (separator == 0) {
return text;
}
Check.isTrue(groupingSeparator.length() == 1);
final StringBuilder sb = new StringBuilder(text.length());
for (int i = 0; i < text.length(); i++) {
if (i == 0 || i == text.length() - 1) {
@@ -160,7 +159,7 @@ public class History {
sb.append(text.charAt(i));
continue;
}
if (Character.isDigit(text.charAt(i - 1)) && text.charAt(i) == groupingSeparator.charAt(0) && Character.isDigit(text.charAt(i + 1))) {
if (Character.isDigit(text.charAt(i - 1)) && text.charAt(i) == separator && Character.isDigit(text.charAt(i + 1))) {
// grouping separator => skip
continue;
}
@@ -312,7 +311,7 @@ public class History {
final List<HistoryState> result = new LinkedList<>();
final String separator = Preferences.Output.separator.getPreference(preferences);
final char separator = Preferences.Output.separator.getPreference(preferences);
final List<HistoryState> states = recent.asList();
final int statesCount = states.size();

View File

@@ -20,6 +20,7 @@ import org.solovyev.android.views.DiscreteSeekBar;
import butterknife.Bind;
import butterknife.ButterKnife;
import jscl.JsclMathEngine;
import static org.solovyev.android.calculator.Engine.Preferences.Output;
@@ -31,7 +32,7 @@ public class NumberFormatPreference extends DialogPreference {
DiscreteSeekBar precisionSeekBar;
@Bind(R.id.nf_separator_spinner)
Spinner separatorSpinner;
ArrayAdapter<Named<String>> separatorAdapter;
ArrayAdapter<Named<Character>> separatorAdapter;
{
setPersistent(false);
@@ -108,12 +109,12 @@ public class NumberFormatPreference extends DialogPreference {
}
@NonNull
private ArrayAdapter<Named<String>> makeSeparatorAdapter() {
private ArrayAdapter<Named<Character>> makeSeparatorAdapter() {
final Context context = getContext();
final ArrayAdapter<Named<String>> adapter = App.makeSimpleSpinnerAdapter(context);
adapter.add(Named.create("", R.string.p_grouping_separator_no, context));
adapter.add(Named.create("'", R.string.p_grouping_separator_apostrophe, context));
adapter.add(Named.create(" ", R.string.p_grouping_separator_space, context));
final ArrayAdapter<Named<Character>> adapter = App.makeSimpleSpinnerAdapter(context);
adapter.add(Named.create(JsclMathEngine.GROUPING_SEPARATOR_NO, R.string.p_grouping_separator_no, context));
adapter.add(Named.create('\'', R.string.p_grouping_separator_apostrophe, context));
adapter.add(Named.create(' ', R.string.p_grouping_separator_space, context));
return adapter;
}
}

View File

@@ -0,0 +1,27 @@
package org.solovyev.android.prefs;
import android.content.SharedPreferences;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class CharacterPreference extends AbstractPreference<Character> {
private CharacterPreference(@Nonnull String key, @Nullable Character defaultValue) {
super(key, defaultValue);
}
public static CharacterPreference of(@Nonnull String key, @Nullable Character defaultValue) {
return new CharacterPreference(key, defaultValue);
}
@Nullable
@Override
protected Character getPersistedValue(@Nonnull SharedPreferences preferences) {
return (char) preferences.getInt(getKey(), 0);
}
@Override
protected void putPersistedValue(@Nonnull SharedPreferences.Editor editor, @Nonnull Character value) {
editor.putInt(getKey(), value);
}
}

View File

@@ -23,7 +23,6 @@ public class Tests {
@NonNull
public static Engine makeEngine() {
final JsclMathEngine mathEngine = JsclMathEngine.getInstance();
mathEngine.setUseGroupingSeparator(true);
mathEngine.setGroupingSeparator(' ');
final Engine engine = new Engine(mathEngine);
engine.postfixFunctionsRegistry = new PostfixFunctionsRegistry(mathEngine);