From 10daedc21ba3c6ee0554b536fe2aa26934eb5b0d Mon Sep 17 00:00:00 2001 From: serso Date: Thu, 28 Apr 2016 18:40:11 +0200 Subject: [PATCH] fixup! Number format preference --- .../solovyev/android/calculator/Engine.java | 7 +- .../calculator/{converter => }/Named.java | 8 +- .../converter/ConverterFragment.java | 1 + .../calculator/converter/Convertible.java | 2 + .../converter/ConvertibleDimension.java | 2 + .../converter/NumeralBaseConvertible.java | 3 + .../converter/NumeralBaseDimension.java | 2 + .../calculator/converter/UnitConvertible.java | 3 + .../calculator/converter/UnitDimension.java | 2 + .../preferences/NumberFormatPreference.java | 75 ++++++++++++------- .../android/views/DiscreteSeekBar.java | 14 ++-- .../res/layout/preference_number_format.xml | 11 +++ app/src/main/res/values/text_strings.xml | 2 +- 13 files changed, 92 insertions(+), 40 deletions(-) rename app/src/main/java/org/solovyev/android/calculator/{converter => }/Named.java (79%) diff --git a/app/src/main/java/org/solovyev/android/calculator/Engine.java b/app/src/main/java/org/solovyev/android/calculator/Engine.java index 36ceb743..dae12ad4 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Engine.java +++ b/app/src/main/java/org/solovyev/android/calculator/Engine.java @@ -238,7 +238,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene } public enum Notation { - simple(Real.NumberFormat.FSE_NONE, R.string.cpp_number_format_simple), + dec(Real.NumberFormat.FSE_NONE, R.string.cpp_number_format_dec), eng(Real.NumberFormat.FSE_ENG, R.string.cpp_number_format_eng), sci(Real.NumberFormat.FSE_SCI, R.string.cpp_number_format_sci); @@ -260,6 +260,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene } public static class Preferences { + // todo serso: move to Output public static final StringPreference groupingSeparator = StringPreference.of("engine.groupingSeparator", String.valueOf(JsclMathEngine.GROUPING_SEPARATOR_DEFAULT)); public static final StringPreference multiplicationSign = StringPreference.of("engine.multiplicationSign", "×"); public static final StringPreference numeralBase = StringPreference.ofTypedValue("engine.numeralBase", "dec", EnumMapper.of(NumeralBase.class)); @@ -275,6 +276,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene preferenceKeys.add(Output.precision.getKey()); preferenceKeys.add(Output.scientificNotation.getKey()); preferenceKeys.add(Output.round.getKey()); + preferenceKeys.add(Output.notation.getKey()); } @Nonnull @@ -284,9 +286,10 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene public static class Output { public static final StringPreference precision = StringPreference.ofTypedValue("engine.output.precision", "5", NumberMapper.of(Integer.class)); + // todo serso: remove 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 = StringPreference.ofEnum("engine.output.notation", Notation.simple, Notation.class); + public static final StringPreference notation = StringPreference.ofEnum("engine.output.notation", Notation.dec, Notation.class); } } } diff --git a/app/src/main/java/org/solovyev/android/calculator/converter/Named.java b/app/src/main/java/org/solovyev/android/calculator/Named.java similarity index 79% rename from app/src/main/java/org/solovyev/android/calculator/converter/Named.java rename to app/src/main/java/org/solovyev/android/calculator/Named.java index c22059ad..bea042f6 100644 --- a/app/src/main/java/org/solovyev/android/calculator/converter/Named.java +++ b/app/src/main/java/org/solovyev/android/calculator/Named.java @@ -1,4 +1,4 @@ -package org.solovyev.android.calculator.converter; +package org.solovyev.android.calculator; import android.content.Context; import android.support.annotation.NonNull; @@ -6,7 +6,7 @@ import android.support.annotation.StringRes; import javax.annotation.Nonnull; -class Named { +public class Named { @NonNull public final T item; @NonNull @@ -18,12 +18,12 @@ class Named { } @NonNull - static Named create(@NonNull T item, @Nonnull String name) { + public static Named create(@NonNull T item, @Nonnull String name) { return new Named(item, name); } @NonNull - static Named create(@NonNull T item, @StringRes int name, @NonNull Context context) { + public static Named create(@NonNull T item, @StringRes int name, @NonNull Context context) { return create(item, name == 0 ? item.toString() : context.getString(name)); } diff --git a/app/src/main/java/org/solovyev/android/calculator/converter/ConverterFragment.java b/app/src/main/java/org/solovyev/android/calculator/converter/ConverterFragment.java index 448a6b8d..275f5173 100644 --- a/app/src/main/java/org/solovyev/android/calculator/converter/ConverterFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/converter/ConverterFragment.java @@ -36,6 +36,7 @@ import org.solovyev.android.calculator.BaseDialogFragment; import org.solovyev.android.calculator.Clipboard; import org.solovyev.android.calculator.Editor; import org.solovyev.android.calculator.Keyboard; +import org.solovyev.android.calculator.Named; import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.keyboard.FloatingKeyboard; import org.solovyev.android.calculator.keyboard.FloatingKeyboardWindow; diff --git a/app/src/main/java/org/solovyev/android/calculator/converter/Convertible.java b/app/src/main/java/org/solovyev/android/calculator/converter/Convertible.java index d02017dc..701fe7b0 100644 --- a/app/src/main/java/org/solovyev/android/calculator/converter/Convertible.java +++ b/app/src/main/java/org/solovyev/android/calculator/converter/Convertible.java @@ -3,6 +3,8 @@ package org.solovyev.android.calculator.converter; import android.content.Context; import android.support.annotation.NonNull; +import org.solovyev.android.calculator.Named; + interface Convertible { @NonNull String convert(@NonNull Convertible to, @NonNull String value) throws NumberFormatException; diff --git a/app/src/main/java/org/solovyev/android/calculator/converter/ConvertibleDimension.java b/app/src/main/java/org/solovyev/android/calculator/converter/ConvertibleDimension.java index f33b6f1b..f945b4dc 100644 --- a/app/src/main/java/org/solovyev/android/calculator/converter/ConvertibleDimension.java +++ b/app/src/main/java/org/solovyev/android/calculator/converter/ConvertibleDimension.java @@ -3,6 +3,8 @@ package org.solovyev.android.calculator.converter; import android.content.Context; import android.support.annotation.NonNull; +import org.solovyev.android.calculator.Named; + import java.util.List; public interface ConvertibleDimension { diff --git a/app/src/main/java/org/solovyev/android/calculator/converter/NumeralBaseConvertible.java b/app/src/main/java/org/solovyev/android/calculator/converter/NumeralBaseConvertible.java index f211340b..5c5dc63c 100644 --- a/app/src/main/java/org/solovyev/android/calculator/converter/NumeralBaseConvertible.java +++ b/app/src/main/java/org/solovyev/android/calculator/converter/NumeralBaseConvertible.java @@ -2,6 +2,9 @@ package org.solovyev.android.calculator.converter; import android.content.Context; import android.support.annotation.NonNull; + +import org.solovyev.android.calculator.Named; + import jscl.JsclMathEngine; import jscl.NumeralBase; import midpcalc.Real; diff --git a/app/src/main/java/org/solovyev/android/calculator/converter/NumeralBaseDimension.java b/app/src/main/java/org/solovyev/android/calculator/converter/NumeralBaseDimension.java index 1834e257..8368101e 100644 --- a/app/src/main/java/org/solovyev/android/calculator/converter/NumeralBaseDimension.java +++ b/app/src/main/java/org/solovyev/android/calculator/converter/NumeralBaseDimension.java @@ -3,6 +3,8 @@ package org.solovyev.android.calculator.converter; import android.content.Context; import android.support.annotation.NonNull; import jscl.NumeralBase; + +import org.solovyev.android.calculator.Named; import org.solovyev.android.calculator.R; import java.util.ArrayList; diff --git a/app/src/main/java/org/solovyev/android/calculator/converter/UnitConvertible.java b/app/src/main/java/org/solovyev/android/calculator/converter/UnitConvertible.java index e8b3dded..91aadf3f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/converter/UnitConvertible.java +++ b/app/src/main/java/org/solovyev/android/calculator/converter/UnitConvertible.java @@ -2,6 +2,9 @@ package org.solovyev.android.calculator.converter; import android.content.Context; import android.support.annotation.NonNull; + +import org.solovyev.android.calculator.Named; + import jscl.JsclMathEngine; import jscl.NumeralBase; diff --git a/app/src/main/java/org/solovyev/android/calculator/converter/UnitDimension.java b/app/src/main/java/org/solovyev/android/calculator/converter/UnitDimension.java index 6dc91974..9fbd4b35 100644 --- a/app/src/main/java/org/solovyev/android/calculator/converter/UnitDimension.java +++ b/app/src/main/java/org/solovyev/android/calculator/converter/UnitDimension.java @@ -4,6 +4,8 @@ import android.content.Context; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.StringRes; + +import org.solovyev.android.calculator.Named; import org.solovyev.android.calculator.R; import javax.measure.unit.Dimension; diff --git a/app/src/main/java/org/solovyev/android/calculator/preferences/NumberFormatPreference.java b/app/src/main/java/org/solovyev/android/calculator/preferences/NumberFormatPreference.java index 2edb24db..d5ecf45f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/preferences/NumberFormatPreference.java +++ b/app/src/main/java/org/solovyev/android/calculator/preferences/NumberFormatPreference.java @@ -9,22 +9,29 @@ import android.support.annotation.NonNull; import android.util.AttributeSet; import android.view.View; import android.widget.ArrayAdapter; -import android.widget.SeekBar; import android.widget.Spinner; import org.solovyev.android.calculator.App; import org.solovyev.android.calculator.Engine; +import org.solovyev.android.calculator.Named; import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.text.NaturalComparator; +import org.solovyev.android.views.DiscreteSeekBar; import butterknife.Bind; import butterknife.ButterKnife; +import static org.solovyev.android.calculator.Engine.Preferences.Output; + public class NumberFormatPreference extends DialogPreference { @Bind(R.id.nf_notation_spinner) Spinner notationSpinner; + ArrayAdapter> notationAdapter; @Bind(R.id.nf_precision_seekbar) - SeekBar precisionSeekBar; + DiscreteSeekBar precisionSeekBar; + @Bind(R.id.nf_separator_spinner) + Spinner separatorSpinner; + ArrayAdapter> separatorAdapter; { setPersistent(false); @@ -55,16 +62,34 @@ public class NumberFormatPreference extends DialogPreference { ButterKnife.bind(this, view); final SharedPreferences preferences = getSharedPreferences(); - precisionSeekBar.setMax(15); - precisionSeekBar.setProgress(Math.max(0, Math.min(15, Engine.Preferences.Output.precision.getPreference(preferences)))); - final ArrayAdapter adapter = makeNumberFormatAdapter(); - notationSpinner.setAdapter(adapter); - notationSpinner.setSelection(indexOf(adapter, Engine.Preferences.Output.notation.getPreference(preferences))); + final int maxPrecision = precisionSeekBar.getMaxTick(); + precisionSeekBar.setMax(maxPrecision); + precisionSeekBar.setCurrentTick(Math.max(0, Math.min(maxPrecision, Output.precision.getPreference(preferences)))); + notationAdapter = makeNotationAdapter(); + notationSpinner.setAdapter(notationAdapter); + notationSpinner.setSelection(indexOf(notationAdapter, Output.notation.getPreference(preferences))); + + separatorAdapter = makeSeparatorAdapter(); + separatorSpinner.setAdapter(separatorAdapter); + separatorSpinner.setSelection(indexOf(separatorAdapter, Engine.Preferences.groupingSeparator.getPreference(preferences))); } - private int indexOf(ArrayAdapter adapter, Engine.Notation notation) { + @Override + protected void onDialogClosed(boolean save) { + super.onDialogClosed(save); + if (!save) { + return; + } + 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); + editor.apply(); + } + + private int indexOf(ArrayAdapter> adapter, T item) { for (int i = 0; i < adapter.getCount(); i++) { - if (adapter.getItem(i).notation == notation) { + if (adapter.getItem(i).item.equals(item)) { return i; } } @@ -72,29 +97,23 @@ public class NumberFormatPreference extends DialogPreference { } @NonNull - private ArrayAdapter makeNumberFormatAdapter() { - final ArrayAdapter adapter = App.makeSimpleSpinnerAdapter(getContext()); - for (Engine.Notation format : Engine.Notation.values()) { - adapter.add(new NotationItem(format)); + private ArrayAdapter> makeNotationAdapter() { + final Context context = getContext(); + final ArrayAdapter> adapter = App.makeSimpleSpinnerAdapter(context); + for (Engine.Notation notation : Engine.Notation.values()) { + adapter.add(Named.create(notation, notation.name, context)); } adapter.sort(NaturalComparator.INSTANCE); return adapter; } - private final class NotationItem { - @NonNull - final Engine.Notation notation; - @NonNull - final String name; - - private NotationItem(@NonNull Engine.Notation notation) { - this.notation = notation; - this.name = getContext().getString(notation.name); - } - - @Override - public String toString() { - return name; - } + @NonNull + private ArrayAdapter> makeSeparatorAdapter() { + final Context context = getContext(); + final ArrayAdapter> 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)); + return adapter; } } diff --git a/app/src/main/java/org/solovyev/android/views/DiscreteSeekBar.java b/app/src/main/java/org/solovyev/android/views/DiscreteSeekBar.java index eed1180c..088f309e 100644 --- a/app/src/main/java/org/solovyev/android/views/DiscreteSeekBar.java +++ b/app/src/main/java/org/solovyev/android/views/DiscreteSeekBar.java @@ -152,15 +152,19 @@ public class DiscreteSeekBar extends SeekBar { /** * Set tick value * - * @param tickValue tick value in range [0, maxTick] + * @param tick tick value in range [0, maxTick] */ - public void setTickValue(int tickValue) { - Check.isTrue(tickValue >= 0); - Check.isTrue(tickValue <= getMaxTick()); - mCurrentTick = tickValue; + public void setCurrentTick(int tick) { + Check.isTrue(tick >= 0); + Check.isTrue(tick <= getMaxTick()); + mCurrentTick = tick; setProgress(getProgressForTick(mCurrentTick)); } + public int getCurrentTick() { + return mCurrentTick; + } + public void setLabelColor(int color) { mLabelColor = ColorStateList.valueOf(color); updateLabelColor(); diff --git a/app/src/main/res/layout/preference_number_format.xml b/app/src/main/res/layout/preference_number_format.xml index fb1478ea..14921c8e 100644 --- a/app/src/main/res/layout/preference_number_format.xml +++ b/app/src/main/res/layout/preference_number_format.xml @@ -31,4 +31,15 @@ app:labelsSize="12sp" app:values="@array/cpp_prefs_precisions"/> + + + + \ No newline at end of file diff --git a/app/src/main/res/values/text_strings.xml b/app/src/main/res/values/text_strings.xml index becee1e2..446c5885 100644 --- a/app/src/main/res/values/text_strings.xml +++ b/app/src/main/res/values/text_strings.xml @@ -124,7 +124,7 @@ Angles Radix Numeral system - Simple + Decimal Engineering Scientific