From 3c03f3948d835e4978fec7b631fa0facb0aeba08 Mon Sep 17 00:00:00 2001 From: serso Date: Wed, 26 Jul 2017 10:07:32 +0200 Subject: [PATCH] Decrease precision in ENG mode and migrate the settings --- .../org/solovyev/android/calculator/Engine.java | 17 ++++++++++++++--- .../calculator/wizard/CalculatorMode.java | 2 +- .../org/solovyev/common/NumberFormatter.java | 14 ++++++++------ 3 files changed, 23 insertions(+), 10 deletions(-) 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 996e4343..5163b492 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Engine.java +++ b/app/src/main/java/org/solovyev/android/calculator/Engine.java @@ -31,6 +31,7 @@ import android.text.TextUtils; import com.squareup.otto.Bus; import org.solovyev.android.Check; +import org.solovyev.android.calculator.Preferences.Gui; import org.solovyev.android.calculator.functions.FunctionsRegistry; import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.operators.OperatorsRegistry; @@ -211,7 +212,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene if (preferences.contains("org.solovyev.android.calculator.CalculatorModel_round_result")) { final boolean round = preferences.getBoolean("org.solovyev.android.calculator.CalculatorModel_round_result", true); if (!round) { - Preferences.Output.precision.putPreference(editor, NumberFormatter.MAX_PRECISION); + Preferences.Output.precision.putPreference(editor, NumberFormatter.ENG_PRECISION); } } // #initPreferences rely on all changes to be committed @@ -226,7 +227,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene if (preferences.contains("engine.output.round")) { final boolean round = preferences.getBoolean("engine.output.round", true); if (!round) { - Preferences.Output.precision.putPreference(editor, NumberFormatter.MAX_PRECISION); + Preferences.Output.precision.putPreference(editor, NumberFormatter.ENG_PRECISION); } } // #initPreferences rely on all changes to be committed @@ -235,6 +236,16 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene // 1. It was forgotten for 0 version // 2. There is a bunch of new preferences initPreferences(editor); + } else if (oldVersion == 2) { + final Integer precision = Preferences.Output.precision.getPreference(preferences); + final Gui.Mode mode = Gui.mode.getPreference(preferences); + if (precision == NumberFormatter.MAX_PRECISION && mode == Gui.Mode.engineer) { + // this might reset a user set value but: + // 1. It's done only once + // 2. Most of the people will be happy with this change (worst case scenario - + // precision is set back to MAX_PRECISION again) + Preferences.Output.precision.putPreference(editor, NumberFormatter.ENG_PRECISION); + } } Preferences.version.putDefault(editor); editor.apply(); @@ -322,7 +333,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene public static final StringPreference multiplicationSign = StringPreference.of("engine.multiplicationSign", "×"); public static final StringPreference numeralBase = StringPreference.ofTypedValue("engine.numeralBase", "dec", EnumMapper.of(NumeralBase.class)); public static final StringPreference angleUnit = StringPreference.ofTypedValue("engine.angleUnit", "deg", EnumMapper.of(AngleUnit.class)); - public static final Preference version = IntegerPreference.of("engine.version", 2); + public static final Preference version = IntegerPreference.of("engine.version", 3); private static final List preferenceKeys = new ArrayList<>(); static { diff --git a/app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorMode.java b/app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorMode.java index d1be8929..197d0fb3 100644 --- a/app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorMode.java +++ b/app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorMode.java @@ -56,7 +56,7 @@ enum CalculatorMode { Preferences.Gui.mode.putPreference(editor, Preferences.Gui.Mode.engineer); Engine.Preferences.angleUnit.putPreference(editor, AngleUnit.rad); Engine.Preferences.Output.notation.putPreference(editor, Engine.Notation.eng); - Engine.Preferences.Output.precision.putPreference(editor, NumberFormatter.MAX_PRECISION); + Engine.Preferences.Output.precision.putPreference(editor, NumberFormatter.ENG_PRECISION); editor.apply(); } diff --git a/jscl/src/main/java/org/solovyev/common/NumberFormatter.java b/jscl/src/main/java/org/solovyev/common/NumberFormatter.java index ac5ec644..90ff3f38 100644 --- a/jscl/src/main/java/org/solovyev/common/NumberFormatter.java +++ b/jscl/src/main/java/org/solovyev/common/NumberFormatter.java @@ -1,5 +1,12 @@ package org.solovyev.common; +import static java.lang.Math.pow; + +import static midpcalc.Real.NumberFormat.FSE_ENG; +import static midpcalc.Real.NumberFormat.FSE_FIX; +import static midpcalc.Real.NumberFormat.FSE_NONE; +import static midpcalc.Real.NumberFormat.FSE_SCI; + import java.math.BigDecimal; import java.math.BigInteger; @@ -7,12 +14,6 @@ import javax.annotation.Nonnull; import midpcalc.Real; -import static java.lang.Math.pow; -import static midpcalc.Real.NumberFormat.FSE_ENG; -import static midpcalc.Real.NumberFormat.FSE_FIX; -import static midpcalc.Real.NumberFormat.FSE_NONE; -import static midpcalc.Real.NumberFormat.FSE_SCI; - public class NumberFormatter { public static final char NO_GROUPING = 0; @@ -20,6 +21,7 @@ public class NumberFormatter { public static final int DEFAULT_MAGNITUDE = 5; public static final int MIN_PRECISION = 1; public static final int MAX_PRECISION = 15; + public static final int ENG_PRECISION = 10; private final Real.NumberFormat numberFormat = new Real.NumberFormat(); private final Real real = new Real();