fixup! Number format preference

This commit is contained in:
serso 2016-04-28 18:40:11 +02:00
parent 5a9bcdede7
commit 10daedc21b
13 changed files with 92 additions and 40 deletions

View File

@ -238,7 +238,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
} }
public enum Notation { 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), eng(Real.NumberFormat.FSE_ENG, R.string.cpp_number_format_eng),
sci(Real.NumberFormat.FSE_SCI, R.string.cpp_number_format_sci); 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 { 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> 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));
@ -275,6 +276,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
preferenceKeys.add(Output.precision.getKey()); preferenceKeys.add(Output.precision.getKey());
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());
} }
@Nonnull @Nonnull
@ -284,9 +286,10 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
public static class Output { public static class Output {
public static final StringPreference<Integer> precision = StringPreference.ofTypedValue("engine.output.precision", "5", NumberMapper.of(Integer.class)); public static final StringPreference<Integer> 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 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.simple, Notation.class); public static final StringPreference<Notation> notation = StringPreference.ofEnum("engine.output.notation", Notation.dec, Notation.class);
} }
} }
} }

View File

@ -1,4 +1,4 @@
package org.solovyev.android.calculator.converter; package org.solovyev.android.calculator;
import android.content.Context; import android.content.Context;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
@ -6,7 +6,7 @@ import android.support.annotation.StringRes;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
class Named<T> { public class Named<T> {
@NonNull @NonNull
public final T item; public final T item;
@NonNull @NonNull
@ -18,12 +18,12 @@ class Named<T> {
} }
@NonNull @NonNull
static <T> Named<T> create(@NonNull T item, @Nonnull String name) { public static <T> Named<T> create(@NonNull T item, @Nonnull String name) {
return new Named<T>(item, name); return new Named<T>(item, name);
} }
@NonNull @NonNull
static <T> Named<T> create(@NonNull T item, @StringRes int name, @NonNull Context context) { public static <T> Named<T> create(@NonNull T item, @StringRes int name, @NonNull Context context) {
return create(item, name == 0 ? item.toString() : context.getString(name)); return create(item, name == 0 ? item.toString() : context.getString(name));
} }

View File

@ -36,6 +36,7 @@ import org.solovyev.android.calculator.BaseDialogFragment;
import org.solovyev.android.calculator.Clipboard; import org.solovyev.android.calculator.Clipboard;
import org.solovyev.android.calculator.Editor; import org.solovyev.android.calculator.Editor;
import org.solovyev.android.calculator.Keyboard; import org.solovyev.android.calculator.Keyboard;
import org.solovyev.android.calculator.Named;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.keyboard.FloatingKeyboard; import org.solovyev.android.calculator.keyboard.FloatingKeyboard;
import org.solovyev.android.calculator.keyboard.FloatingKeyboardWindow; import org.solovyev.android.calculator.keyboard.FloatingKeyboardWindow;

View File

@ -3,6 +3,8 @@ package org.solovyev.android.calculator.converter;
import android.content.Context; import android.content.Context;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import org.solovyev.android.calculator.Named;
interface Convertible { interface Convertible {
@NonNull @NonNull
String convert(@NonNull Convertible to, @NonNull String value) throws NumberFormatException; String convert(@NonNull Convertible to, @NonNull String value) throws NumberFormatException;

View File

@ -3,6 +3,8 @@ package org.solovyev.android.calculator.converter;
import android.content.Context; import android.content.Context;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import org.solovyev.android.calculator.Named;
import java.util.List; import java.util.List;
public interface ConvertibleDimension { public interface ConvertibleDimension {

View File

@ -2,6 +2,9 @@ package org.solovyev.android.calculator.converter;
import android.content.Context; import android.content.Context;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import org.solovyev.android.calculator.Named;
import jscl.JsclMathEngine; import jscl.JsclMathEngine;
import jscl.NumeralBase; import jscl.NumeralBase;
import midpcalc.Real; import midpcalc.Real;

View File

@ -3,6 +3,8 @@ package org.solovyev.android.calculator.converter;
import android.content.Context; import android.content.Context;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import jscl.NumeralBase; import jscl.NumeralBase;
import org.solovyev.android.calculator.Named;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import java.util.ArrayList; import java.util.ArrayList;

View File

@ -2,6 +2,9 @@ package org.solovyev.android.calculator.converter;
import android.content.Context; import android.content.Context;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import org.solovyev.android.calculator.Named;
import jscl.JsclMathEngine; import jscl.JsclMathEngine;
import jscl.NumeralBase; import jscl.NumeralBase;

View File

@ -4,6 +4,8 @@ import android.content.Context;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.annotation.StringRes; import android.support.annotation.StringRes;
import org.solovyev.android.calculator.Named;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import javax.measure.unit.Dimension; import javax.measure.unit.Dimension;

View File

@ -9,22 +9,29 @@ import android.support.annotation.NonNull;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View; import android.view.View;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.SeekBar;
import android.widget.Spinner; import android.widget.Spinner;
import org.solovyev.android.calculator.App; import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.Engine; import org.solovyev.android.calculator.Engine;
import org.solovyev.android.calculator.Named;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.text.NaturalComparator; import org.solovyev.android.calculator.text.NaturalComparator;
import org.solovyev.android.views.DiscreteSeekBar;
import butterknife.Bind; import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import static org.solovyev.android.calculator.Engine.Preferences.Output;
public class NumberFormatPreference extends DialogPreference { public class NumberFormatPreference extends DialogPreference {
@Bind(R.id.nf_notation_spinner) @Bind(R.id.nf_notation_spinner)
Spinner notationSpinner; Spinner notationSpinner;
ArrayAdapter<Named<Engine.Notation>> notationAdapter;
@Bind(R.id.nf_precision_seekbar) @Bind(R.id.nf_precision_seekbar)
SeekBar precisionSeekBar; DiscreteSeekBar precisionSeekBar;
@Bind(R.id.nf_separator_spinner)
Spinner separatorSpinner;
ArrayAdapter<Named<String>> separatorAdapter;
{ {
setPersistent(false); setPersistent(false);
@ -55,16 +62,34 @@ public class NumberFormatPreference extends DialogPreference {
ButterKnife.bind(this, view); ButterKnife.bind(this, view);
final SharedPreferences preferences = getSharedPreferences(); final SharedPreferences preferences = getSharedPreferences();
precisionSeekBar.setMax(15); final int maxPrecision = precisionSeekBar.getMaxTick();
precisionSeekBar.setProgress(Math.max(0, Math.min(15, Engine.Preferences.Output.precision.getPreference(preferences)))); precisionSeekBar.setMax(maxPrecision);
final ArrayAdapter<NotationItem> adapter = makeNumberFormatAdapter(); precisionSeekBar.setCurrentTick(Math.max(0, Math.min(maxPrecision, Output.precision.getPreference(preferences))));
notationSpinner.setAdapter(adapter); notationAdapter = makeNotationAdapter();
notationSpinner.setSelection(indexOf(adapter, Engine.Preferences.Output.notation.getPreference(preferences))); 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<NotationItem> 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 <T> int indexOf(ArrayAdapter<Named<T>> adapter, T item) {
for (int i = 0; i < adapter.getCount(); i++) { for (int i = 0; i < adapter.getCount(); i++) {
if (adapter.getItem(i).notation == notation) { if (adapter.getItem(i).item.equals(item)) {
return i; return i;
} }
} }
@ -72,29 +97,23 @@ public class NumberFormatPreference extends DialogPreference {
} }
@NonNull @NonNull
private ArrayAdapter<NotationItem> makeNumberFormatAdapter() { private ArrayAdapter<Named<Engine.Notation>> makeNotationAdapter() {
final ArrayAdapter<NotationItem> adapter = App.makeSimpleSpinnerAdapter(getContext()); final Context context = getContext();
for (Engine.Notation format : Engine.Notation.values()) { final ArrayAdapter<Named<Engine.Notation>> adapter = App.makeSimpleSpinnerAdapter(context);
adapter.add(new NotationItem(format)); for (Engine.Notation notation : Engine.Notation.values()) {
adapter.add(Named.create(notation, notation.name, context));
} }
adapter.sort(NaturalComparator.INSTANCE); adapter.sort(NaturalComparator.INSTANCE);
return adapter; return adapter;
} }
private final class NotationItem {
@NonNull @NonNull
final Engine.Notation notation; private ArrayAdapter<Named<String>> makeSeparatorAdapter() {
@NonNull final Context context = getContext();
final String name; final ArrayAdapter<Named<String>> adapter = App.makeSimpleSpinnerAdapter(context);
adapter.add(Named.create("", R.string.p_grouping_separator_no, context));
private NotationItem(@NonNull Engine.Notation notation) { adapter.add(Named.create("'", R.string.p_grouping_separator_apostrophe, context));
this.notation = notation; adapter.add(Named.create(" ", R.string.p_grouping_separator_space, context));
this.name = getContext().getString(notation.name); return adapter;
}
@Override
public String toString() {
return name;
}
} }
} }

View File

@ -152,15 +152,19 @@ public class DiscreteSeekBar extends SeekBar {
/** /**
* Set tick value * Set tick value
* *
* @param tickValue tick value in range [0, maxTick] * @param tick tick value in range [0, maxTick]
*/ */
public void setTickValue(int tickValue) { public void setCurrentTick(int tick) {
Check.isTrue(tickValue >= 0); Check.isTrue(tick >= 0);
Check.isTrue(tickValue <= getMaxTick()); Check.isTrue(tick <= getMaxTick());
mCurrentTick = tickValue; mCurrentTick = tick;
setProgress(getProgressForTick(mCurrentTick)); setProgress(getProgressForTick(mCurrentTick));
} }
public int getCurrentTick() {
return mCurrentTick;
}
public void setLabelColor(int color) { public void setLabelColor(int color) {
mLabelColor = ColorStateList.valueOf(color); mLabelColor = ColorStateList.valueOf(color);
updateLabelColor(); updateLabelColor();

View File

@ -31,4 +31,15 @@
app:labelsSize="12sp" app:labelsSize="12sp"
app:values="@array/cpp_prefs_precisions"/> app:values="@array/cpp_prefs_precisions"/>
<TextView
style="@style/CppLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Separator"/>
<Spinner
android:id="@+id/nf_separator_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout> </LinearLayout>

View File

@ -124,7 +124,7 @@
<string name="cpp_angles">Angles</string> <string name="cpp_angles">Angles</string>
<string name="cpp_radix">Radix</string> <string name="cpp_radix">Radix</string>
<string name="cpp_numeral_system">Numeral system</string> <string name="cpp_numeral_system">Numeral system</string>
<string name="cpp_number_format_simple">Simple</string> <string name="cpp_number_format_dec">Decimal</string>
<string name="cpp_number_format_eng">Engineering</string> <string name="cpp_number_format_eng">Engineering</string>
<string name="cpp_number_format_sci">Scientific</string> <string name="cpp_number_format_sci">Scientific</string>
</resources> </resources>