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();
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.numeralBase, "org.solovyev.android.calculator.CalculatorActivity_numeral_bases", 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.scientificNotation, "calculation.output.science_notation", 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);
editor.apply();
@ -211,7 +213,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
mathEngine.setScienceNotation(Preferences.Output.scientificNotation.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)) {
mathEngine.setUseGroupingSeparator(false);
} else {
@ -260,16 +262,13 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
}
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<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 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<>();
static {
preferenceKeys.add(groupingSeparator.getKey());
preferenceKeys.add(multiplicationSign.getKey());
preferenceKeys.add(numeralBase.getKey());
preferenceKeys.add(angleUnit.getKey());
@ -277,6 +276,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
preferenceKeys.add(Output.scientificNotation.getKey());
preferenceKeys.add(Output.round.getKey());
preferenceKeys.add(Output.notation.getKey());
preferenceKeys.add(Output.separator.getKey());
}
@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 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));
}
}
}

View File

@ -53,6 +53,8 @@ import java.util.EnumMap;
import java.util.Locale;
import java.util.Map;
import jscl.JsclMathEngine;
import static org.solovyev.android.prefs.IntegerPreference.DEF_VALUE;
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) {
if (!Engine.Preferences.groupingSeparator.isSet(preferences)) {
if (!Engine.Preferences.Output.separator.isSet(preferences)) {
final Locale locale = Locale.getDefault();
if (locale != null) {
final DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(locale);
@ -117,10 +119,10 @@ public final class Preferences {
if (index >= 0) {
groupingSeparator = MathType.grouping_separator.getTokens().get(index);
} 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.support.annotation.NonNull;
import android.text.TextUtils;
import com.google.common.base.Strings;
import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;
import org.json.JSONArray;
import org.json.JSONException;
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.ErrorReporter;
import org.solovyev.android.calculator.Runnables;
import org.solovyev.android.calculator.json.Json;
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.IOException;
import java.util.ArrayList;
@ -51,6 +55,12 @@ import java.util.LinkedList;
import java.util.List;
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;
@Singleton
@ -302,7 +312,7 @@ public class History {
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 int statesCount = states.size();
@ -312,7 +322,7 @@ public class History {
final HistoryState newerState = states.get(i);
final String olderText = olderState.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);
streak = 0;
} else {

View File

@ -71,7 +71,7 @@ public class NumberFormatPreference extends DialogPreference {
separatorAdapter = makeSeparatorAdapter();
separatorSpinner.setAdapter(separatorAdapter);
separatorSpinner.setSelection(indexOf(separatorAdapter, Engine.Preferences.groupingSeparator.getPreference(preferences)));
separatorSpinner.setSelection(indexOf(separatorAdapter, Output.separator.getPreference(preferences)));
}
@Override
@ -83,7 +83,7 @@ public class NumberFormatPreference extends DialogPreference {
final SharedPreferences.Editor editor = getSharedPreferences().edit();
Output.precision.putPreference(editor, precisionSeekBar.getCurrentTick());
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();
}

View File

@ -25,7 +25,9 @@ package org.solovyev.android.calculator.history;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import com.squareup.otto.Bus;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -35,19 +37,33 @@ import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
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.io.FileSystem;
import javax.annotation.Nonnull;
import java.io.File;
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.eq;
import static org.mockito.Mockito.*;
import static org.solovyev.android.calculator.Engine.Preferences.groupingSeparator;
import static org.mockito.Mockito.anyString;
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.jscl.JsclOperation.numeric;
@ -106,8 +122,8 @@ public class HistoryTest {
@Test
public void testRecentHistoryShouldTakeIntoAccountGroupingSeparator() throws Exception {
when(history.preferences.contains(eq(groupingSeparator.getKey()))).thenReturn(true);
when(history.preferences.getString(eq(groupingSeparator.getKey()), anyString())).thenReturn(" ");
when(history.preferences.contains(eq(Engine.Preferences.Output.separator.getKey()))).thenReturn(true);
when(history.preferences.getString(eq(Engine.Preferences.Output.separator.getKey()), anyString())).thenReturn(" ");
addState("1");
addState("12");
addState("123");
@ -119,7 +135,7 @@ public class HistoryTest {
assertEquals("12 345", states.get(0).editor.getTextString());
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("12");
addState("123");