From eb1c733153b391d4fd14a29e0aa08c2627743b89 Mon Sep 17 00:00:00 2001 From: serso Date: Wed, 4 May 2016 23:39:46 +0200 Subject: [PATCH] Number format preferences screen --- .../solovyev/android/calculator/Engine.java | 78 ++++++------ .../NumberFormatExamplesPreference.java | 59 +++++++++ .../preferences/NumberFormatPreference.java | 119 ------------------ .../preferences/PrecisionPreference.java | 72 +++++++++++ .../preferences/PreferencesActivity.java | 1 + .../preferences/PreferencesFragment.java | 99 ++++++++++++++- .../android/prefs/StringPreference.java | 5 + .../solovyev/common/text/CharacterMapper.java | 27 ++++ .../res/layout/preference_number_format.xml | 45 ------- .../main/res/layout/preference_precision.xml | 11 ++ .../main/res/values/text_non_translatable.xml | 1 - app/src/main/res/xml/preferences.xml | 4 + .../main/res/xml/preferences_calculations.xml | 24 ---- .../res/xml/preferences_number_format.xml | 49 ++++++++ .../org/solovyev/common/NumberFormatter.java | 3 +- 15 files changed, 358 insertions(+), 239 deletions(-) create mode 100644 app/src/main/java/org/solovyev/android/calculator/preferences/NumberFormatExamplesPreference.java delete mode 100644 app/src/main/java/org/solovyev/android/calculator/preferences/NumberFormatPreference.java create mode 100644 app/src/main/java/org/solovyev/android/calculator/preferences/PrecisionPreference.java create mode 100644 app/src/main/java/org/solovyev/common/text/CharacterMapper.java delete mode 100644 app/src/main/res/layout/preference_number_format.xml create mode 100644 app/src/main/res/layout/preference_precision.xml create mode 100644 app/src/main/res/xml/preferences_number_format.xml 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 e9939b9d..d9d0f779 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Engine.java +++ b/app/src/main/java/org/solovyev/android/calculator/Engine.java @@ -22,35 +22,12 @@ package org.solovyev.android.calculator; +import android.content.Context; import android.content.SharedPreferences; +import android.support.annotation.NonNull; import android.support.annotation.StringRes; import android.text.TextUtils; - import com.squareup.otto.Bus; - -import org.solovyev.android.Check; -import org.solovyev.android.calculator.functions.FunctionsRegistry; -import org.solovyev.android.calculator.operators.OperatorsRegistry; -import org.solovyev.android.calculator.operators.PostfixFunctionsRegistry; -import org.solovyev.android.prefs.BooleanPreference; -import org.solovyev.android.prefs.CharacterPreference; -import org.solovyev.android.prefs.IntegerPreference; -import org.solovyev.android.prefs.Preference; -import org.solovyev.android.prefs.StringPreference; -import org.solovyev.common.NumberFormatter; -import org.solovyev.common.text.EnumMapper; -import org.solovyev.common.text.NumberMapper; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.concurrent.Executor; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import javax.inject.Inject; -import javax.inject.Singleton; - import jscl.AngleUnit; import jscl.JsclMathEngine; import jscl.MathEngine; @@ -59,6 +36,25 @@ import jscl.math.operator.Operator; import jscl.text.Identifier; import jscl.text.Parser; import midpcalc.Real; +import org.solovyev.android.Check; +import org.solovyev.android.calculator.functions.FunctionsRegistry; +import org.solovyev.android.calculator.operators.OperatorsRegistry; +import org.solovyev.android.calculator.operators.PostfixFunctionsRegistry; +import org.solovyev.android.calculator.preferences.PreferenceEntry; +import org.solovyev.android.prefs.*; +import org.solovyev.common.text.CharacterMapper; +import org.solovyev.common.NumberFormatter; +import org.solovyev.common.text.EnumMapper; +import org.solovyev.common.text.NumberMapper; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.inject.Inject; +import javax.inject.Singleton; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.Executor; @Singleton public class Engine implements SharedPreferences.OnSharedPreferenceChangeListener { @@ -98,13 +94,6 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene this.mathEngine.setGroupingSeparator(JsclMathEngine.GROUPING_SEPARATOR_DEFAULT); } - private static void migratePreference(@Nonnull SharedPreferences preferences, @Nonnull BooleanPreference preference, @Nonnull String oldKey, @Nonnull SharedPreferences.Editor editor) { - if (!preferences.contains(oldKey)) { - return; - } - editor.putBoolean(preference.getKey(), preferences.getBoolean(oldKey, false)); - } - private static void migratePreference(@Nonnull SharedPreferences preferences, @Nonnull StringPreference preference, @Nonnull String oldKey, @Nonnull SharedPreferences.Editor editor) { if (!preferences.contains(oldKey)) { return; @@ -112,15 +101,6 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene editor.putString(preference.getKey(), preferences.getString(oldKey, null)); } - private static void migratePreference(@Nonnull SharedPreferences preferences, @Nonnull CharacterPreference preference, @Nonnull String oldKey, @Nonnull SharedPreferences.Editor editor) { - if (!preferences.contains(oldKey)) { - return; - } - final String s = preferences.getString(oldKey, null); - editor.putInt(preference.getKey(), TextUtils.isEmpty(s) ? 0 : s.charAt(0)); - } - - public static boolean isValidName(@Nullable String name) { if (!TextUtils.isEmpty(name)) { try { @@ -261,7 +241,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene } } - public enum Notation { + public enum Notation implements PreferenceEntry { 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); @@ -274,6 +254,18 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene this.id = id; this.name = name; } + + @NonNull + @Override + public CharSequence getName(@NonNull Context context) { + return context.getString(name); + } + + @NonNull + @Override + public CharSequence getId() { + return name(); + } } public static class ChangedEvent { @@ -307,7 +299,7 @@ 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)); public static final StringPreference notation = StringPreference.ofEnum("engine.output.notation", Notation.dec, Notation.class); - public static final CharacterPreference separator = CharacterPreference.of("engine.output.separator", JsclMathEngine.GROUPING_SEPARATOR_DEFAULT); + public static final StringPreference separator = StringPreference.ofTypedValue("engine.output.separator", JsclMathEngine.GROUPING_SEPARATOR_DEFAULT, CharacterMapper.INSTANCE); } } } diff --git a/app/src/main/java/org/solovyev/android/calculator/preferences/NumberFormatExamplesPreference.java b/app/src/main/java/org/solovyev/android/calculator/preferences/NumberFormatExamplesPreference.java new file mode 100644 index 00000000..9eabbbfe --- /dev/null +++ b/app/src/main/java/org/solovyev/android/calculator/preferences/NumberFormatExamplesPreference.java @@ -0,0 +1,59 @@ +package org.solovyev.android.calculator.preferences; + +import android.content.Context; +import android.graphics.Typeface; +import android.util.AttributeSet; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; +import jscl.JsclMathEngine; +import org.solovyev.android.material.preferences.Preference; + +@SuppressWarnings("unused") +public class NumberFormatExamplesPreference extends Preference { + public NumberFormatExamplesPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + } + + public NumberFormatExamplesPreference(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + public NumberFormatExamplesPreference(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public NumberFormatExamplesPreference(Context context) { + super(context); + } + + public void update(JsclMathEngine engine) { + final StringBuilder examples = new StringBuilder(); + examples.append(" 1/3 = ").append(engine.format(1d / 3)).append("\n"); + examples.append(" √2 = ").append(engine.format(Math.sqrt(2d))).append("\n"); + examples.append("\n"); + examples.append(" 1000 = ").append(engine.format(1000d)).append("\n"); + examples.append(" 1000000 = ").append(engine.format(1000000d)).append("\n"); + examples.append(" 11^10 = ").append(engine.format(Math.pow(11d, 10))).append("\n"); + examples.append(" 10^24 = ").append(engine.format(Math.pow(10d, 24))).append("\n"); + examples.append("\n"); + examples.append(" 0.001 = ").append(engine.format(0.001d)).append("\n"); + examples.append("0.000001 = ").append(engine.format(0.000001d)).append("\n"); + examples.append(" 11^−10 = ").append(engine.format(Math.pow(11d, -10))).append("\n"); + examples.append(" 10^−24 = ").append(engine.format(Math.pow(10d, -24))); + setSummary(examples); + } + + @Override + protected View onCreateView(ViewGroup parent) { + final View view = super.onCreateView(parent); + final View summary = view.findViewById(android.R.id.summary); + if (summary instanceof TextView) { + final TextView textView = (TextView) summary; + textView.setMaxLines(12); + textView.setLines(12); + textView.setTypeface(Typeface.MONOSPACE); + } + return view; + } +} 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 deleted file mode 100644 index f921f8a6..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/preferences/NumberFormatPreference.java +++ /dev/null @@ -1,119 +0,0 @@ -package org.solovyev.android.calculator.preferences; - -import android.annotation.TargetApi; -import android.content.Context; -import android.content.SharedPreferences; -import android.os.Build; -import android.preference.DialogPreference; -import android.support.annotation.NonNull; -import android.util.AttributeSet; -import android.view.View; -import android.widget.ArrayAdapter; -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 org.solovyev.common.NumberFormatter; - -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) - DiscreteSeekBar precisionSeekBar; - @Bind(R.id.nf_separator_spinner) - Spinner separatorSpinner; - ArrayAdapter> separatorAdapter; - - { - setPersistent(false); - setDialogLayoutResource(R.layout.preference_number_format); - } - - @TargetApi(Build.VERSION_CODES.LOLLIPOP) - public NumberFormatPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { - super(context, attrs, defStyleAttr, defStyleRes); - } - - public NumberFormatPreference(Context context, AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr); - } - - public NumberFormatPreference(Context context, AttributeSet attrs) { - super(context, attrs); - } - - @TargetApi(Build.VERSION_CODES.LOLLIPOP) - public NumberFormatPreference(Context context) { - super(context); - } - - @Override - protected void onBindDialogView(View view) { - super.onBindDialogView(view); - ButterKnife.bind(this, view); - - final SharedPreferences preferences = getSharedPreferences(); - precisionSeekBar.setMax(NumberFormatter.MAX_PRECISION); - precisionSeekBar.setCurrentTick(Math.max(0, Math.min(NumberFormatter.MAX_PRECISION, 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, Output.separator.getPreference(preferences))); - } - - @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); - Output.separator.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).item.equals(item)) { - return i; - } - } - return -1; - } - - @NonNull - 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; - } - - @NonNull - private ArrayAdapter> makeSeparatorAdapter() { - final Context context = getContext(); - final ArrayAdapter> adapter = App.makeSimpleSpinnerAdapter(context); - adapter.add(Named.create(NumberFormatter.NO_GROUPING, 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/calculator/preferences/PrecisionPreference.java b/app/src/main/java/org/solovyev/android/calculator/preferences/PrecisionPreference.java new file mode 100644 index 00000000..2b62f450 --- /dev/null +++ b/app/src/main/java/org/solovyev/android/calculator/preferences/PrecisionPreference.java @@ -0,0 +1,72 @@ +package org.solovyev.android.calculator.preferences; + +import android.annotation.TargetApi; +import android.content.Context; +import android.content.SharedPreferences; +import android.os.Build; +import android.preference.DialogPreference; +import android.util.AttributeSet; +import android.view.View; +import butterknife.Bind; +import butterknife.ButterKnife; +import org.solovyev.android.calculator.Engine; +import org.solovyev.android.calculator.R; +import org.solovyev.android.views.DiscreteSeekBar; + +import static org.solovyev.common.NumberFormatter.MAX_PRECISION; +import static org.solovyev.common.NumberFormatter.MIN_PRECISION; + +@SuppressWarnings("unused") +public class PrecisionPreference extends DialogPreference { + + @Bind(R.id.precision_seekbar) + DiscreteSeekBar seekBar; + + { + setPersistent(false); + setDialogLayoutResource(R.layout.preference_precision); + } + + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + public PrecisionPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + } + + public PrecisionPreference(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + public PrecisionPreference(Context context, AttributeSet attrs) { + super(context, attrs); + } + + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + public PrecisionPreference(Context context) { + super(context); + } + + @Override + protected void onBindDialogView(View view) { + super.onBindDialogView(view); + ButterKnife.bind(this, view); + + final SharedPreferences preferences = getSharedPreferences(); + seekBar.setMax(MAX_PRECISION - 1); + final int precision = Math.max(MIN_PRECISION, Math.min(MAX_PRECISION, Engine.Preferences.Output.precision.getPreference(preferences))); + seekBar.setCurrentTick(precision - 1); + } + + @Override + protected void onDialogClosed(boolean save) { + super.onDialogClosed(save); + if (!save) { + return; + } + final int precision = seekBar.getCurrentTick() + 1; + if (callChangeListener(precision)) { + final SharedPreferences.Editor editor = getSharedPreferences().edit(); + Engine.Preferences.Output.precision.putPreference(editor, precision); + editor.apply(); + } + } +} diff --git a/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesActivity.java b/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesActivity.java index ca7312cf..930f5313 100644 --- a/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesActivity.java @@ -35,6 +35,7 @@ public class PreferencesActivity extends BaseActivity implements SharedPreferenc static { preferenceDefs.append(R.xml.preferences, new PrefDef("screen-main", R.string.cpp_settings)); + preferenceDefs.append(R.xml.preferences_number_format, new PrefDef("screen-number-format", R.string.c_prefs_calculations_category)); preferenceDefs.append(R.xml.preferences_calculations, new PrefDef("screen-calculations", R.string.c_prefs_calculations_category)); preferenceDefs.append(R.xml.preferences_appearance, new PrefDef("screen-appearance", R.string.c_prefs_appearance_category)); preferenceDefs.append(R.xml.preferences_other, new PrefDef("screen-other", R.string.c_prefs_other_category)); diff --git a/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesFragment.java b/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesFragment.java index 1f441473..d46c5751 100644 --- a/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesFragment.java @@ -11,8 +11,11 @@ import android.support.v4.app.FragmentActivity; import android.util.SparseArray; import android.view.View; import android.widget.ListView; - +import com.squareup.otto.Bus; +import com.squareup.otto.Subscribe; +import jscl.JsclMathEngine; import org.solovyev.android.calculator.AdView; +import org.solovyev.android.calculator.Engine; import org.solovyev.android.calculator.Preferences; import org.solovyev.android.calculator.Preferences.Gui.Theme; import org.solovyev.android.calculator.R; @@ -22,14 +25,15 @@ import org.solovyev.android.checkout.BillingRequests; import org.solovyev.android.checkout.Checkout; import org.solovyev.android.checkout.ProductTypes; import org.solovyev.android.checkout.RequestListener; +import org.solovyev.android.prefs.StringPreference; import org.solovyev.android.wizard.Wizards; - -import java.util.Arrays; -import java.util.List; +import org.solovyev.common.text.CharacterMapper; import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.inject.Inject; +import java.util.Arrays; +import java.util.List; import static org.solovyev.android.calculator.App.cast; import static org.solovyev.android.calculator.wizard.CalculatorWizards.DEFAULT_WIZARD_FLOW; @@ -48,11 +52,15 @@ public class PreferencesFragment extends org.solovyev.android.material.preferenc Languages languages; @Inject Wizards wizards; + @Inject + JsclMathEngine engine; + @Inject + Bus bus; @Nonnull - public static PreferencesFragment create(int preferencesResId, int layoutResId) { + public static PreferencesFragment create(int preferences, int layout) { final PreferencesFragment fragment = new PreferencesFragment(); - fragment.setArguments(createArguments(preferencesResId, layoutResId, NO_THEME)); + fragment.setArguments(createArguments(preferences, layout, NO_THEME)); return fragment; } @@ -62,6 +70,7 @@ public class PreferencesFragment extends org.solovyev.android.material.preferenc cast(this).getComponent().inject(this); preferences.registerOnSharedPreferenceChangeListener(this); + bus.register(this); } private void setPreferenceIntent(int xml, @Nonnull PreferencesActivity.PrefDef def) { @@ -108,6 +117,11 @@ public class PreferencesFragment extends org.solovyev.android.material.preferenc } }); } + } else if (preference == R.xml.preferences_number_format) { + prepareListPreference(Engine.Preferences.Output.notation, Engine.Notation.class); + preparePrecisionPreference(); + prepareSeparatorPreference(); + prepareNumberFormatExamplesPreference(); } prepareLanguagePreference(preference); @@ -136,6 +150,73 @@ public class PreferencesFragment extends org.solovyev.android.material.preferenc }); } + private void prepareNumberFormatExamplesPreference() { + final NumberFormatExamplesPreference preference = (NumberFormatExamplesPreference) preferenceManager.findPreference("numberFormat.examples"); + if (preference == null) { + return; + } + preference.update(engine); + } + + private void prepareSeparatorPreference() { + final ListPreference preference = (ListPreference) preferenceManager.findPreference(Engine.Preferences.Output.separator.getKey()); + preference.setSummary(separatorName(Engine.Preferences.Output.separator.getPreference(preferences))); + preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference p, Object newValue) { + preference.setSummary(separatorName(CharacterMapper.INSTANCE.parseValue(String.valueOf(newValue)))); + return true; + } + }); + } + + private int separatorName(char separator) { + switch (separator) { + case '\'': + return R.string.p_grouping_separator_apostrophe; + case ' ': + return R.string.p_grouping_separator_space; + case 0: + return R.string.p_grouping_separator_no; + } + return R.string.p_grouping_separator_no; + } + + private void preparePrecisionPreference() { + final PrecisionPreference preference = (PrecisionPreference) preferenceManager.findPreference(Engine.Preferences.Output.precision.getKey()); + preference.setSummary(String.valueOf(Engine.Preferences.Output.precision.getPreference(preferences))); + preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference p, Object newValue) { + preference.setSummary(String.valueOf(newValue)); + return true; + } + }); + } + + private & PreferenceEntry> void prepareListPreference(@Nonnull final StringPreference p, @Nonnull Class type) { + final ListPreference preference = (ListPreference) preferenceManager.findPreference(p.getKey()); + if (preference == null) { + return; + } + final E[] entries = type.getEnumConstants(); + final FragmentActivity activity = getActivity(); + populate(preference, entries); + preference.setSummary(p.getPreference(preferences).getName(activity)); + preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference p, Object newValue) { + for (E entry : entries) { + if (entry.getId().equals(newValue)) { + preference.setSummary(entry.getName(activity)); + break; + } + } + return true; + } + }); + } + private void prepareLayoutPreference(int preference) { if (preference != R.xml.preferences_appearance) { return; @@ -220,6 +301,11 @@ public class PreferencesFragment extends org.solovyev.android.material.preferenc public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { } + @Subscribe + public void onEngineChanged(Engine.ChangedEvent e) { + prepareNumberFormatExamplesPreference(); + } + @Override public void onResume() { super.onResume(); @@ -246,6 +332,7 @@ public class PreferencesFragment extends org.solovyev.android.material.preferenc @Override public void onDestroy() { + bus.unregister(this); preferences.unregisterOnSharedPreferenceChangeListener(this); super.onDestroy(); } diff --git a/app/src/main/java/org/solovyev/android/prefs/StringPreference.java b/app/src/main/java/org/solovyev/android/prefs/StringPreference.java index 0341ea4d..0c561e72 100644 --- a/app/src/main/java/org/solovyev/android/prefs/StringPreference.java +++ b/app/src/main/java/org/solovyev/android/prefs/StringPreference.java @@ -56,6 +56,11 @@ public final class StringPreference extends AbstractPreference { return new StringPreference(key, mapper.parseValue(defaultValue), mapper); } + @Nonnull + public static StringPreference ofTypedValue(@Nonnull String key, @Nullable T defaultValue, @Nonnull Mapper mapper) { + return new StringPreference(key, defaultValue, mapper); + } + @Nonnull public static StringPreference ofEnum(@Nonnull String key, @Nullable T defaultValue, @Nonnull Class enumType) { return new StringPreference(key, defaultValue, EnumMapper.of(enumType)); diff --git a/app/src/main/java/org/solovyev/common/text/CharacterMapper.java b/app/src/main/java/org/solovyev/common/text/CharacterMapper.java new file mode 100644 index 00000000..a2b79391 --- /dev/null +++ b/app/src/main/java/org/solovyev/common/text/CharacterMapper.java @@ -0,0 +1,27 @@ +package org.solovyev.common.text; + +import android.text.TextUtils; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public class CharacterMapper implements Mapper { + + @Nonnull + public static final CharacterMapper INSTANCE = new CharacterMapper(); + + private CharacterMapper() { + } + + @Nonnull + @Override + public String formatValue(@Nullable Character value) throws IllegalArgumentException { + return value == null || value == 0 ? "" : String.valueOf(value); + } + + @Nonnull + @Override + public Character parseValue(@Nullable String value) throws IllegalArgumentException { + return TextUtils.isEmpty(value) ? 0 : value.charAt(0); + } +} diff --git a/app/src/main/res/layout/preference_number_format.xml b/app/src/main/res/layout/preference_number_format.xml deleted file mode 100644 index 14921c8e..00000000 --- a/app/src/main/res/layout/preference_number_format.xml +++ /dev/null @@ -1,45 +0,0 @@ - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/preference_precision.xml b/app/src/main/res/layout/preference_precision.xml new file mode 100644 index 00000000..072a035f --- /dev/null +++ b/app/src/main/res/layout/preference_precision.xml @@ -0,0 +1,11 @@ + + + \ No newline at end of file diff --git a/app/src/main/res/values/text_non_translatable.xml b/app/src/main/res/values/text_non_translatable.xml index 85d6359d..ecd1f7b4 100644 --- a/app/src/main/res/values/text_non_translatable.xml +++ b/app/src/main/res/values/text_non_translatable.xml @@ -18,7 +18,6 @@ E %1$s (AMOLED) - 0 1 2 3 diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 0ffdadcc..fdebae46 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -35,6 +35,10 @@ a:key="restart_wizard" a:title="@string/cpp_restart_wizard" /> + + diff --git a/app/src/main/res/xml/preferences_calculations.xml b/app/src/main/res/xml/preferences_calculations.xml index bffa19a4..3db7afa5 100644 --- a/app/src/main/res/xml/preferences_calculations.xml +++ b/app/src/main/res/xml/preferences_calculations.xml @@ -24,30 +24,6 @@ - - - - - - - - + + + + + + + + + + + + + + \ No newline at end of file diff --git a/jscl/src/main/java/org/solovyev/common/NumberFormatter.java b/jscl/src/main/java/org/solovyev/common/NumberFormatter.java index 5d424c9f..c1dadf8f 100644 --- a/jscl/src/main/java/org/solovyev/common/NumberFormatter.java +++ b/jscl/src/main/java/org/solovyev/common/NumberFormatter.java @@ -14,6 +14,7 @@ public class NumberFormatter { public static final char NO_GROUPING = 0; public static final int NO_ROUNDING = -1; public static final int DEFAULT_MAGNITUDE = 5; + public static final int MIN_PRECISION = 1; public static final int MAX_PRECISION = 15; private final Real.NumberFormat numberFormat = new Real.NumberFormat(); @@ -39,7 +40,7 @@ public class NumberFormatter { } public void setPrecision(int precision) { - this.precision = precision; + this.precision = Math.max(MIN_PRECISION, Math.min(precision, MAX_PRECISION)); } public void setGroupingSeparator(char groupingSeparator) {