Don't store temporary properties

This commit is contained in:
serso 2016-02-17 16:13:36 +01:00
parent 0a121a9685
commit bf4e7b52fc
6 changed files with 66 additions and 73 deletions

View File

@ -60,7 +60,6 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import javax.annotation.Nonnull;
@ -73,7 +72,6 @@ import javax.inject.Singleton;
public class Calculator implements SharedPreferences.OnSharedPreferenceChangeListener {
public static final long NO_SEQUENCE = -1;
private static final long PREFERENCE_CHECK_INTERVAL = TimeUnit.MINUTES.toMillis(15);
@Nonnull
private final CalculatorEventContainer calculatorEventContainer = new ListCalculatorEventContainer();
@ -92,8 +90,6 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis
private volatile boolean calculateOnFly = true;
private long lastPreferredPreferenceCheck = 0L;
@Inject
PreferredPreferences preferredPreferences;
@Inject
@ -186,7 +182,7 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis
return;
}
checkPreferredPreferences();
preferredPreferences.check(false);
PreparedExpression pe = null;
try {
pe = prepare(e);
@ -241,22 +237,6 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis
return Collections.emptyList();
}
private void checkPreferredPreferences() {
if (shouldCheckPreferredPreferences()) {
preferredPreferences.check(false);
}
}
private synchronized boolean shouldCheckPreferredPreferences() {
final long now = System.currentTimeMillis();
if (now - lastPreferredPreferenceCheck > PREFERENCE_CHECK_INTERVAL) {
lastPreferredPreferenceCheck = now;
return true;
}
return false;
}
@Nonnull
public PreparedExpression prepare(@Nonnull String expression) throws ParseException {
return preprocessor.process(expression);

View File

@ -56,6 +56,8 @@ public class Display {
@Inject
Lazy<Notifier> notifier;
@Inject
Lazy<PreferredPreferences> preferredPreferences;
@Inject
ActivityLauncher launcher;
@Nullable
private DisplayView view;
@ -86,7 +88,7 @@ public class Display {
public void onCalculationFinished(@Nonnull CalculationFinishedEvent e) {
if (e.sequence < state.sequence) return;
setState(DisplayState.createValid(e.operation, e.result, e.stringResult, e.sequence));
if (!e.messages.isEmpty()) {
if (!e.messages.isEmpty() && preferredPreferences.get().isShowWarningDialog()) {
final Context context = view != null ? view.getContext() : application;
FixableErrorsActivity.show(context, e.messages);
}

View File

@ -22,6 +22,11 @@
package org.solovyev.android.calculator;
import static org.solovyev.android.Android.isPhoneModel;
import static org.solovyev.android.DeviceModel.samsung_galaxy_s;
import static org.solovyev.android.DeviceModel.samsung_galaxy_s_2;
import static org.solovyev.android.prefs.IntegerPreference.DEF_VALUE;
import android.app.Application;
import android.content.ContentResolver;
import android.content.Context;
@ -34,26 +39,28 @@ import android.support.annotation.LayoutRes;
import android.support.annotation.StyleRes;
import android.util.SparseArray;
import android.view.ContextThemeWrapper;
import jscl.AngleUnit;
import jscl.NumeralBase;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.language.Languages;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.calculator.preferences.PurchaseDialogActivity;
import org.solovyev.android.calculator.wizard.WizardActivity;
import org.solovyev.android.prefs.*;
import org.solovyev.android.prefs.BooleanPreference;
import org.solovyev.android.prefs.IntegerPreference;
import org.solovyev.android.prefs.NumberToStringPreference;
import org.solovyev.android.prefs.Preference;
import org.solovyev.android.prefs.StringPreference;
import jscl.AngleUnit;
import jscl.NumeralBase;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.text.DecimalFormatSymbols;
import java.util.EnumMap;
import java.util.Locale;
import java.util.Map;
import static org.solovyev.android.Android.isPhoneModel;
import static org.solovyev.android.DeviceModel.samsung_galaxy_s;
import static org.solovyev.android.DeviceModel.samsung_galaxy_s_2;
import static org.solovyev.android.prefs.IntegerPreference.DEF_VALUE;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public final class Preferences {
@ -64,21 +71,19 @@ public final class Preferences {
}
static void setDefaultValues(@Nonnull Application application, @Nonnull SharedPreferences preferences) {
SharedPreferences.Editor editor = preferences.edit();
// renew value after each application start
Gui.showFixableErrorDialog.putDefault(editor);
Gui.lastPreferredPreferencesCheck.putDefault(editor);
final int version = Preferences.appVersion.getPreference(preferences);
if (version == DEF_VALUE) {
final SharedPreferences.Editor editor = preferences.edit();
setInitialDefaultValues(application, preferences, editor);
editor.apply();
} else if (version > 143) {
if (!Gui.vibrateOnKeypress.isSet(preferences)) {
final SharedPreferences.Editor editor = preferences.edit();
//noinspection deprecation
Gui.vibrateOnKeypress.putPreference(editor, Gui.hapticFeedback.getPreference(preferences) > 0);
editor.apply();
}
}
editor.apply();
}
private static void setInitialDefaultValues(@Nonnull Application application, @Nonnull SharedPreferences preferences, @Nonnull SharedPreferences.Editor editor) {
@ -260,7 +265,9 @@ public final class Preferences {
public static class Gui {
public static final Preference<Theme> theme = StringPreference.ofEnum("org.solovyev.android.calculator.CalculatorActivity_calc_theme", Theme.material_theme, Theme.class);
public static final Preference<Layout> layout = StringPreference.ofEnum("org.solovyev.android.calculator.CalculatorActivity_calc_layout", Layout.simple, Layout.class);
public static final Preference<Layout> layout = StringPreference.ofEnum(
"org.solovyev.android.calculator.CalculatorActivity_calc_layout", Layout.simple,
Layout.class);
public static final Preference<String> language = StringPreference.of("gui.language", Languages.SYSTEM_LANGUAGE_CODE);
public static final Preference<Boolean> feedbackWindowShown = BooleanPreference.of("feedback_window_shown", false);
public static final Preference<Boolean> showReleaseNotes = BooleanPreference.of("org.solovyev.android.calculator.CalculatorActivity_show_release_notes", true);
@ -271,8 +278,6 @@ public final class Preferences {
public static final Preference<Boolean> preventScreenFromFading = BooleanPreference.of("preventScreenFromFading", true);
public static final Preference<Boolean> colorDisplay = BooleanPreference.of("org.solovyev.android.calculator.CalculatorModel_color_display", true);
public static final Preference<Boolean> vibrateOnKeypress = BooleanPreference.of("gui.vibrateOnKeypress", true);
public static final Preference<Boolean> showFixableErrorDialog = BooleanPreference.of("gui.showFixableErrorDialog", true);
public static final Preference<Long> lastPreferredPreferencesCheck = LongPreference.of("gui.lastPreferredPreferencesCheck", 0L);
@Deprecated
public static final Preference<Long> hapticFeedback = NumberToStringPreference.of("hapticFeedback", 60L, Long.class);

View File

@ -25,26 +25,30 @@ package org.solovyev.android.calculator;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import jscl.AngleUnit;
import jscl.NumeralBase;
import org.solovyev.android.calculator.errors.FixableError;
import org.solovyev.android.calculator.errors.FixableErrorType;
import org.solovyev.android.calculator.errors.FixableErrorsActivity;
import org.solovyev.android.msg.AndroidMessage;
import org.solovyev.common.msg.MessageType;
import jscl.AngleUnit;
import jscl.NumeralBase;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.ArrayList;
import static org.solovyev.android.calculator.Preferences.Gui.lastPreferredPreferencesCheck;
@Singleton
public class PreferredPreferences {
// one hour
private static final Long PREFERRED_PREFS_INTERVAL_TIME = 1000L * 60L * 60L;
private static final long PREFERRED_PREFS_INTERVAL_TIME = TimeUnit.MINUTES.toMillis(15);
private long lastCheckTime;
private boolean showWarningDialog = true;
@Inject
Application application;
@ -65,11 +69,11 @@ public class PreferredPreferences {
final long now = System.currentTimeMillis();
if (!force) {
if (!Preferences.Gui.showFixableErrorDialog.getPreference(preferences)) {
if (!showWarningDialog) {
// user has disabled calculation message dialogs until the next session
return;
}
if (!shouldCheck(now)) {
if (now - lastCheckTime < PREFERRED_PREFS_INTERVAL_TIME) {
return;
}
}
@ -90,13 +94,7 @@ public class PreferredPreferences {
}
FixableErrorsActivity.show(context, messages);
lastPreferredPreferencesCheck.putPreference(preferences, now);
}
private boolean shouldCheck(long now) {
final long lastCheckTime = lastPreferredPreferencesCheck.getPreference(preferences);
return now - lastCheckTime > PREFERRED_PREFS_INTERVAL_TIME;
lastCheckTime = now;
}
public void setPreferredAngleUnits() {
@ -105,7 +103,9 @@ public class PreferredPreferences {
public void setAngleUnits(@Nonnull AngleUnit angleUnit) {
Engine.Preferences.angleUnit.putPreference(preferences, angleUnit);
notifier.showMessage(new AndroidMessage(R.string.c_angle_units_changed_to, MessageType.info, application, angleUnit.name()));
notifier.showMessage(
new AndroidMessage(R.string.c_angle_units_changed_to, MessageType.info, application,
angleUnit.name()));
}
public void setPreferredNumeralBase() {
@ -114,6 +114,16 @@ public class PreferredPreferences {
public void setNumeralBase(@Nonnull NumeralBase numeralBase) {
Engine.Preferences.numeralBase.putPreference(preferences, numeralBase);
notifier.showMessage(new AndroidMessage(R.string.c_numeral_base_changed_to, MessageType.info, application, numeralBase.name()));
notifier.showMessage(
new AndroidMessage(R.string.c_numeral_base_changed_to, MessageType.info,
application, numeralBase.name()));
}
public boolean isShowWarningDialog() {
return showWarningDialog;
}
public void dontShowWarningDialog() {
showWarningDialog = false;
}
}

View File

@ -12,7 +12,6 @@ import org.solovyev.android.Check;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.AppComponent;
import org.solovyev.android.calculator.BaseDialogFragment;
import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.PreferredPreferences;
import org.solovyev.android.calculator.R;
@ -68,7 +67,7 @@ public class FixableErrorFragment extends BaseDialogFragment {
builder.setNeutralButton(R.string.cpp_dont_show_again, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Preferences.Gui.showFixableErrorDialog.putPreference(preferences, false);
preferredPreferences.dontShowWarningDialog();
dismiss();
}
});

View File

@ -22,16 +22,17 @@
package org.solovyev.android.calculator.errors;
import static org.solovyev.android.calculator.App.cast;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import org.solovyev.android.Activities;
import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.PreferredPreferences;
import org.solovyev.common.msg.Message;
import java.util.ArrayList;
@ -40,26 +41,22 @@ import java.util.List;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import static org.solovyev.android.calculator.App.cast;
public class FixableErrorsActivity extends AppCompatActivity {
public static final String EXTRA_ERRORS = "errors";
public static final String STATE_ERRORS = "errors";
@Inject
SharedPreferences preferences;
@Inject
PreferredPreferences preferredPreferences;
private ArrayList<FixableError> errors;
public static void show(@Nonnull Context context, @Nonnull List<Message> messages) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (Preferences.Gui.showFixableErrorDialog.getPreference(preferences)) {
final ArrayList<FixableError> errors = new ArrayList<>();
for (Message message : messages) {
errors.add(new FixableError(message));
}
show(context, errors);
final ArrayList<FixableError> errors = new ArrayList<>();
for (Message message : messages) {
errors.add(new FixableError(message));
}
show(context, errors);
}
public static void show(@Nonnull Context context, @Nonnull ArrayList<FixableError> errors) {
@ -101,7 +98,7 @@ public class FixableErrorsActivity extends AppCompatActivity {
finish();
return;
}
if (!Preferences.Gui.showFixableErrorDialog.getPreference(preferences)) {
if (!preferredPreferences.isShowWarningDialog()) {
finish();
return;
}