Move grouping separator preference to Output section

This commit is contained in:
serso 2016-05-02 13:57:29 +02:00
parent 0f7e5ee3ee
commit 1c6651bd4b
5 changed files with 57 additions and 27 deletions

View File

@ -174,13 +174,15 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
} }
final SharedPreferences.Editor editor = preferences.edit(); final SharedPreferences.Editor editor = preferences.edit();
if (oldVersion == 0) { if (oldVersion == 0) {
migratePreference(preferences, Preferences.groupingSeparator, "org.solovyev.android.calculator.CalculatorActivity_calc_grouping_separator", editor); migratePreference(preferences, Preferences.Output.separator, "org.solovyev.android.calculator.CalculatorActivity_calc_grouping_separator", editor);
migratePreference(preferences, Preferences.multiplicationSign, "org.solovyev.android.calculator.CalculatorActivity_calc_multiplication_sign", editor); migratePreference(preferences, Preferences.multiplicationSign, "org.solovyev.android.calculator.CalculatorActivity_calc_multiplication_sign", editor);
migratePreference(preferences, Preferences.numeralBase, "org.solovyev.android.calculator.CalculatorActivity_numeral_bases", editor); migratePreference(preferences, Preferences.numeralBase, "org.solovyev.android.calculator.CalculatorActivity_numeral_bases", editor);
migratePreference(preferences, Preferences.angleUnit, "org.solovyev.android.calculator.CalculatorActivity_angle_units", editor); migratePreference(preferences, Preferences.angleUnit, "org.solovyev.android.calculator.CalculatorActivity_angle_units", editor);
migratePreference(preferences, Preferences.Output.precision, "org.solovyev.android.calculator.CalculatorModel_result_precision", editor); migratePreference(preferences, Preferences.Output.precision, "org.solovyev.android.calculator.CalculatorModel_result_precision", editor);
migratePreference(preferences, Preferences.Output.scientificNotation, "calculation.output.science_notation", editor); migratePreference(preferences, Preferences.Output.scientificNotation, "calculation.output.science_notation", editor);
migratePreference(preferences, Preferences.Output.round, "org.solovyev.android.calculator.CalculatorModel_round_result", editor); migratePreference(preferences, Preferences.Output.round, "org.solovyev.android.calculator.CalculatorModel_round_result", editor);
} else if (oldVersion == 1) {
migratePreference(preferences, Preferences.Output.separator, "engine.groupingSeparator", editor);
} }
Preferences.version.putDefault(editor); Preferences.version.putDefault(editor);
editor.apply(); editor.apply();
@ -211,7 +213,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
mathEngine.setScienceNotation(Preferences.Output.scientificNotation.getPreference(preferences)); mathEngine.setScienceNotation(Preferences.Output.scientificNotation.getPreference(preferences));
mathEngine.setRoundResult(Preferences.Output.round.getPreference(preferences)); mathEngine.setRoundResult(Preferences.Output.round.getPreference(preferences));
final String groupingSeparator = Preferences.groupingSeparator.getPreference(preferences); final String groupingSeparator = Preferences.Output.separator.getPreference(preferences);
if (TextUtils.isEmpty(groupingSeparator)) { if (TextUtils.isEmpty(groupingSeparator)) {
mathEngine.setUseGroupingSeparator(false); mathEngine.setUseGroupingSeparator(false);
} else { } else {
@ -260,16 +262,13 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
} }
public static class Preferences { public static class Preferences {
// todo serso: move to Output
public static final StringPreference<String> groupingSeparator = StringPreference.of("engine.groupingSeparator", String.valueOf(JsclMathEngine.GROUPING_SEPARATOR_DEFAULT));
public static final StringPreference<String> multiplicationSign = StringPreference.of("engine.multiplicationSign", "×"); public static final StringPreference<String> multiplicationSign = StringPreference.of("engine.multiplicationSign", "×");
public static final StringPreference<NumeralBase> numeralBase = StringPreference.ofTypedValue("engine.numeralBase", "dec", EnumMapper.of(NumeralBase.class)); public static final StringPreference<NumeralBase> numeralBase = StringPreference.ofTypedValue("engine.numeralBase", "dec", EnumMapper.of(NumeralBase.class));
public static final StringPreference<AngleUnit> angleUnit = StringPreference.ofTypedValue("engine.angleUnit", "deg", EnumMapper.of(AngleUnit.class)); public static final StringPreference<AngleUnit> angleUnit = StringPreference.ofTypedValue("engine.angleUnit", "deg", EnumMapper.of(AngleUnit.class));
public static final Preference<Integer> version = IntegerPreference.of("engine.version", 1); public static final Preference<Integer> version = IntegerPreference.of("engine.version", 2);
private static final List<String> preferenceKeys = new ArrayList<>(); private static final List<String> preferenceKeys = new ArrayList<>();
static { static {
preferenceKeys.add(groupingSeparator.getKey());
preferenceKeys.add(multiplicationSign.getKey()); preferenceKeys.add(multiplicationSign.getKey());
preferenceKeys.add(numeralBase.getKey()); preferenceKeys.add(numeralBase.getKey());
preferenceKeys.add(angleUnit.getKey()); preferenceKeys.add(angleUnit.getKey());
@ -277,6 +276,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
preferenceKeys.add(Output.scientificNotation.getKey()); preferenceKeys.add(Output.scientificNotation.getKey());
preferenceKeys.add(Output.round.getKey()); preferenceKeys.add(Output.round.getKey());
preferenceKeys.add(Output.notation.getKey()); preferenceKeys.add(Output.notation.getKey());
preferenceKeys.add(Output.separator.getKey());
} }
@Nonnull @Nonnull
@ -290,6 +290,8 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
public static final BooleanPreference scientificNotation = BooleanPreference.of("engine.output.scientificNotation", false); 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 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<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));
} }
} }
} }

View File

@ -53,6 +53,8 @@ import java.util.EnumMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import jscl.JsclMathEngine;
import static org.solovyev.android.prefs.IntegerPreference.DEF_VALUE; import static org.solovyev.android.prefs.IntegerPreference.DEF_VALUE;
public final class Preferences { public final class Preferences {
@ -108,7 +110,7 @@ public final class Preferences {
} }
private static void setInitialDefaultValues(@Nonnull Application application, @Nonnull SharedPreferences preferences, @Nonnull SharedPreferences.Editor editor) { private static void setInitialDefaultValues(@Nonnull Application application, @Nonnull SharedPreferences preferences, @Nonnull SharedPreferences.Editor editor) {
if (!Engine.Preferences.groupingSeparator.isSet(preferences)) { if (!Engine.Preferences.Output.separator.isSet(preferences)) {
final Locale locale = Locale.getDefault(); final Locale locale = Locale.getDefault();
if (locale != null) { if (locale != null) {
final DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(locale); final DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(locale);
@ -117,10 +119,10 @@ public final class Preferences {
if (index >= 0) { if (index >= 0) {
groupingSeparator = MathType.grouping_separator.getTokens().get(index); groupingSeparator = MathType.grouping_separator.getTokens().get(index);
} else { } else {
groupingSeparator = " "; groupingSeparator = String.valueOf(JsclMathEngine.GROUPING_SEPARATOR_DEFAULT);
} }
Engine.Preferences.groupingSeparator.putPreference(editor, groupingSeparator); Engine.Preferences.Output.separator.putPreference(editor, groupingSeparator);
} }
} }

View File

@ -27,22 +27,26 @@ import android.content.SharedPreferences;
import android.os.Handler; import android.os.Handler;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.text.TextUtils; import android.text.TextUtils;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe; import com.squareup.otto.Subscribe;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.solovyev.android.Check; import org.solovyev.android.Check;
import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.AppModule;
import org.solovyev.android.calculator.Calculator;
import org.solovyev.android.calculator.Display;
import org.solovyev.android.calculator.DisplayState;
import org.solovyev.android.calculator.Editor;
import org.solovyev.android.calculator.EditorState;
import org.solovyev.android.calculator.Engine.Preferences; import org.solovyev.android.calculator.Engine.Preferences;
import org.solovyev.android.calculator.ErrorReporter;
import org.solovyev.android.calculator.Runnables;
import org.solovyev.android.calculator.json.Json; import org.solovyev.android.calculator.json.Json;
import org.solovyev.android.io.FileSystem; import org.solovyev.android.io.FileSystem;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -51,6 +55,12 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import static android.text.TextUtils.isEmpty; import static android.text.TextUtils.isEmpty;
@Singleton @Singleton
@ -302,7 +312,7 @@ public class History {
final List<HistoryState> result = new LinkedList<>(); final List<HistoryState> result = new LinkedList<>();
final String groupingSeparator = Preferences.groupingSeparator.getPreference(preferences); final String separator = Preferences.Output.separator.getPreference(preferences);
final List<HistoryState> states = recent.asList(); final List<HistoryState> states = recent.asList();
final int statesCount = states.size(); final int statesCount = states.size();
@ -312,7 +322,7 @@ public class History {
final HistoryState newerState = states.get(i); final HistoryState newerState = states.get(i);
final String olderText = olderState.editor.getTextString(); final String olderText = olderState.editor.getTextString();
final String newerText = newerState.editor.getTextString(); final String newerText = newerState.editor.getTextString();
if (streak >= MAX_INTERMEDIATE_STREAK || !isIntermediate(olderText, newerText, groupingSeparator)) { if (streak >= MAX_INTERMEDIATE_STREAK || !isIntermediate(olderText, newerText, separator)) {
result.add(0, olderState); result.add(0, olderState);
streak = 0; streak = 0;
} else { } else {

View File

@ -71,7 +71,7 @@ public class NumberFormatPreference extends DialogPreference {
separatorAdapter = makeSeparatorAdapter(); separatorAdapter = makeSeparatorAdapter();
separatorSpinner.setAdapter(separatorAdapter); separatorSpinner.setAdapter(separatorAdapter);
separatorSpinner.setSelection(indexOf(separatorAdapter, Engine.Preferences.groupingSeparator.getPreference(preferences))); separatorSpinner.setSelection(indexOf(separatorAdapter, Output.separator.getPreference(preferences)));
} }
@Override @Override
@ -83,7 +83,7 @@ public class NumberFormatPreference extends DialogPreference {
final SharedPreferences.Editor editor = getSharedPreferences().edit(); final SharedPreferences.Editor editor = getSharedPreferences().edit();
Output.precision.putPreference(editor, precisionSeekBar.getCurrentTick()); Output.precision.putPreference(editor, precisionSeekBar.getCurrentTick());
Output.notation.putPreference(editor, notationAdapter.getItem(notationSpinner.getSelectedItemPosition()).item); Output.notation.putPreference(editor, notationAdapter.getItem(notationSpinner.getSelectedItemPosition()).item);
Engine.Preferences.groupingSeparator.putPreference(editor, separatorAdapter.getItem(separatorSpinner.getSelectedItemPosition()).item); Output.separator.putPreference(editor, separatorAdapter.getItem(separatorSpinner.getSelectedItemPosition()).item);
editor.apply(); editor.apply();
} }

View File

@ -25,7 +25,9 @@ package org.solovyev.android.calculator.history;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -35,19 +37,33 @@ import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.solovyev.android.CalculatorTestRunner; import org.solovyev.android.CalculatorTestRunner;
import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.BuildConfig;
import org.solovyev.android.calculator.Display;
import org.solovyev.android.calculator.DisplayState;
import org.solovyev.android.calculator.Editor;
import org.solovyev.android.calculator.EditorState;
import org.solovyev.android.calculator.Engine;
import org.solovyev.android.calculator.ErrorReporter;
import org.solovyev.android.calculator.json.Json; import org.solovyev.android.calculator.json.Json;
import org.solovyev.android.io.FileSystem; import org.solovyev.android.io.FileSystem;
import javax.annotation.Nonnull;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
import static org.junit.Assert.*; import javax.annotation.Nonnull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.anyString;
import static org.solovyev.android.calculator.Engine.Preferences.groupingSeparator; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.solovyev.android.calculator.Tests.sameThreadExecutor; import static org.solovyev.android.calculator.Tests.sameThreadExecutor;
import static org.solovyev.android.calculator.jscl.JsclOperation.numeric; import static org.solovyev.android.calculator.jscl.JsclOperation.numeric;
@ -106,8 +122,8 @@ public class HistoryTest {
@Test @Test
public void testRecentHistoryShouldTakeIntoAccountGroupingSeparator() throws Exception { public void testRecentHistoryShouldTakeIntoAccountGroupingSeparator() throws Exception {
when(history.preferences.contains(eq(groupingSeparator.getKey()))).thenReturn(true); when(history.preferences.contains(eq(Engine.Preferences.Output.separator.getKey()))).thenReturn(true);
when(history.preferences.getString(eq(groupingSeparator.getKey()), anyString())).thenReturn(" "); when(history.preferences.getString(eq(Engine.Preferences.Output.separator.getKey()), anyString())).thenReturn(" ");
addState("1"); addState("1");
addState("12"); addState("12");
addState("123"); addState("123");
@ -119,7 +135,7 @@ public class HistoryTest {
assertEquals("12 345", states.get(0).editor.getTextString()); assertEquals("12 345", states.get(0).editor.getTextString());
history.clearRecent(); history.clearRecent();
when(history.preferences.getString(eq(groupingSeparator.getKey()), anyString())).thenReturn("'"); when(history.preferences.getString(eq(Engine.Preferences.Output.separator.getKey()), anyString())).thenReturn("'");
addState("1"); addState("1");
addState("12"); addState("12");
addState("123"); addState("123");