fixup! Number format preference
This commit is contained in:
parent
5a9bcdede7
commit
10daedc21b
@ -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<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));
|
||||
@ -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<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 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<T> {
|
||||
public class Named<T> {
|
||||
@NonNull
|
||||
public final T item;
|
||||
@NonNull
|
||||
@ -18,12 +18,12 @@ class Named<T> {
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@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));
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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 {
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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<Named<Engine.Notation>> notationAdapter;
|
||||
@Bind(R.id.nf_precision_seekbar)
|
||||
SeekBar precisionSeekBar;
|
||||
DiscreteSeekBar precisionSeekBar;
|
||||
@Bind(R.id.nf_separator_spinner)
|
||||
Spinner separatorSpinner;
|
||||
ArrayAdapter<Named<String>> 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<NotationItem> 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<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++) {
|
||||
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<NotationItem> makeNumberFormatAdapter() {
|
||||
final ArrayAdapter<NotationItem> adapter = App.makeSimpleSpinnerAdapter(getContext());
|
||||
for (Engine.Notation format : Engine.Notation.values()) {
|
||||
adapter.add(new NotationItem(format));
|
||||
private ArrayAdapter<Named<Engine.Notation>> makeNotationAdapter() {
|
||||
final Context context = getContext();
|
||||
final ArrayAdapter<Named<Engine.Notation>> 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<Named<String>> 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));
|
||||
return adapter;
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -31,4 +31,15 @@
|
||||
app:labelsSize="12sp"
|
||||
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>
|
@ -124,7 +124,7 @@
|
||||
<string name="cpp_angles">Angles</string>
|
||||
<string name="cpp_radix">Radix</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_sci">Scientific</string>
|
||||
</resources>
|
||||
|
Loading…
Reference in New Issue
Block a user