Save last used dimensions/units in Converter

This commit is contained in:
serso 2016-04-20 14:36:02 +02:00
parent 24cb3b49b1
commit 557c04df89
3 changed files with 88 additions and 16 deletions

View File

@ -2,6 +2,7 @@ package org.solovyev.android.calculator;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import org.solovyev.android.prefs.BooleanPreference; import org.solovyev.android.prefs.BooleanPreference;
import org.solovyev.android.prefs.IntegerPreference; import org.solovyev.android.prefs.IntegerPreference;
import org.solovyev.android.prefs.Preference; import org.solovyev.android.prefs.Preference;
@ -61,4 +62,13 @@ public final class UiPreferences {
public void setShowFixableErrorDialog(boolean showFixableErrorDialog) { public void setShowFixableErrorDialog(boolean showFixableErrorDialog) {
this.showFixableErrorDialog = showFixableErrorDialog; this.showFixableErrorDialog = showFixableErrorDialog;
} }
public static final class Converter {
@NonNull
public static final Preference<Integer> lastDimension = IntegerPreference.of("converter.lastDimension", -1);
@NonNull
public static final Preference<Integer> lastUnitsFrom = IntegerPreference.of("converter.lastUnitsFrom", -1);
@NonNull
public static final Preference<Integer> lastUnitsTo = IntegerPreference.of("converter.lastUnitsTo", -1);
}
} }

View File

@ -3,6 +3,7 @@ package org.solovyev.android.calculator.converter;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
@ -20,18 +21,39 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo; import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.*; import android.widget.AdapterView;
import butterknife.Bind; import android.widget.ArrayAdapter;
import butterknife.ButterKnife; import android.widget.EditText;
import org.solovyev.android.calculator.*; import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.AppComponent;
import org.solovyev.android.calculator.AppModule;
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.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;
import org.solovyev.android.calculator.keyboard.FloatingNumberKeyboard; import org.solovyev.android.calculator.keyboard.FloatingNumberKeyboard;
import org.solovyev.android.calculator.math.MathUtils;
import org.solovyev.android.calculator.view.EditTextCompat; import org.solovyev.android.calculator.view.EditTextCompat;
import java.util.Comparator;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.inject.Inject; import javax.inject.Inject;
import java.util.Comparator;
import butterknife.Bind;
import butterknife.ButterKnife;
import static org.solovyev.android.calculator.UiPreferences.Converter.lastDimension;
import static org.solovyev.android.calculator.UiPreferences.Converter.lastUnitsFrom;
import static org.solovyev.android.calculator.UiPreferences.Converter.lastUnitsTo;
public class ConverterFragment extends BaseDialogFragment public class ConverterFragment extends BaseDialogFragment
implements AdapterView.OnItemSelectedListener, View.OnFocusChangeListener, TextView.OnEditorActionListener, View.OnClickListener, TextWatcher { implements AdapterView.OnItemSelectedListener, View.OnFocusChangeListener, TextView.OnEditorActionListener, View.OnClickListener, TextWatcher {
@ -41,6 +63,7 @@ public class ConverterFragment extends BaseDialogFragment
private static final String STATE_SELECTION_TO = "selection.to"; private static final String STATE_SELECTION_TO = "selection.to";
private static final String EXTRA_VALUE = "value"; private static final String EXTRA_VALUE = "value";
private static final NamedItemComparator COMPARATOR = new NamedItemComparator(); private static final NamedItemComparator COMPARATOR = new NamedItemComparator();
public static final int NONE = -1;
@NonNull @NonNull
private final FloatingKeyboardWindow keyboardWindow = new FloatingKeyboardWindow(null); private final FloatingKeyboardWindow keyboardWindow = new FloatingKeyboardWindow(null);
@ -50,6 +73,9 @@ public class ConverterFragment extends BaseDialogFragment
Clipboard clipboard; Clipboard clipboard;
@Inject @Inject
Keyboard keyboard; Keyboard keyboard;
@javax.inject.Named(AppModule.PREFS_UI)
@Inject
SharedPreferences uiPreferences;
@Inject @Inject
Editor editor; Editor editor;
@Bind(R.id.converter_dimensions_spinner) @Bind(R.id.converter_dimensions_spinner)
@ -72,8 +98,8 @@ public class ConverterFragment extends BaseDialogFragment
private ArrayAdapter<Named<Convertible>> adapterFrom; private ArrayAdapter<Named<Convertible>> adapterFrom;
private ArrayAdapter<Named<Convertible>> adapterTo; private ArrayAdapter<Named<Convertible>> adapterTo;
private int pendingFromSelection = View.NO_ID; private int pendingFromSelection = NONE;
private int pendingToSelection = View.NO_ID; private int pendingToSelection = NONE;
private boolean useSystemKeyboard = true; private boolean useSystemKeyboard = true;
public static void show(@Nonnull FragmentActivity activity) { public static void show(@Nonnull FragmentActivity activity) {
@ -149,10 +175,12 @@ public class ConverterFragment extends BaseDialogFragment
if (savedInstanceState == null) { if (savedInstanceState == null) {
editTextFrom.setText(String.valueOf(getArguments().getDouble(EXTRA_VALUE, 1f))); editTextFrom.setText(String.valueOf(getArguments().getDouble(EXTRA_VALUE, 1f)));
dimensionsSpinner.setSelection(0); pendingFromSelection = lastUnitsFrom.getPreference(uiPreferences);
pendingToSelection = lastUnitsTo.getPreference(uiPreferences);
dimensionsSpinner.setSelection(MathUtils.clamp(lastDimension.getPreference(uiPreferences), 0, dimensionsAdapter.getCount() - 1));
} else { } else {
pendingFromSelection = savedInstanceState.getInt(STATE_SELECTION_FROM, View.NO_ID); pendingFromSelection = savedInstanceState.getInt(STATE_SELECTION_FROM, NONE);
pendingToSelection = savedInstanceState.getInt(STATE_SELECTION_TO, View.NO_ID); pendingToSelection = savedInstanceState.getInt(STATE_SELECTION_TO, NONE);
} }
return view; return view;
@ -165,6 +193,20 @@ public class ConverterFragment extends BaseDialogFragment
outState.putInt(STATE_SELECTION_TO, spinnerTo.getSelectedItemPosition()); outState.putInt(STATE_SELECTION_TO, spinnerTo.getSelectedItemPosition());
} }
@Override
public void onPause() {
saveLastUsedValues();
super.onPause();
}
private void saveLastUsedValues() {
final SharedPreferences.Editor editor = uiPreferences.edit();
lastDimension.putPreference(editor, dimensionsSpinner.getSelectedItemPosition());
lastUnitsFrom.putPreference(editor, spinnerFrom.getSelectedItemPosition());
lastUnitsTo.putPreference(editor, spinnerTo.getSelectedItemPosition());
editor.apply();
}
@Override @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()) { switch (parent.getId()) {
@ -214,13 +256,17 @@ public class ConverterFragment extends BaseDialogFragment
adapterFrom.sort(COMPARATOR); adapterFrom.sort(COMPARATOR);
adapterFrom.setNotifyOnChange(true); adapterFrom.setNotifyOnChange(true);
adapterFrom.notifyDataSetChanged(); adapterFrom.notifyDataSetChanged();
spinnerFrom.setSelection(Math.max(0, Math.min(pendingFromSelection, adapterFrom.getCount() - 1))); if (pendingFromSelection != NONE) {
pendingFromSelection = View.NO_ID; spinnerFrom.setSelection(MathUtils.clamp(pendingFromSelection, 0, adapterFrom.getCount() - 1));
pendingFromSelection = NONE;
} else {
spinnerFrom.setSelection(MathUtils.clamp(spinnerFrom.getSelectedItemPosition(), 0, adapterFrom.getCount() - 1));
}
} }
private void updateUnitsTo(@NonNull ConvertibleDimension dimension, @NonNull Convertible except) { private void updateUnitsTo(@NonNull ConvertibleDimension dimension, @NonNull Convertible except) {
final Convertible selectedUnit; final Convertible selectedUnit;
if (pendingToSelection > View.NO_ID) { if (pendingToSelection > NONE) {
selectedUnit = null; selectedUnit = null;
} else { } else {
final int selectedPosition = spinnerTo.getSelectedItemPosition(); final int selectedPosition = spinnerTo.getSelectedItemPosition();
@ -245,8 +291,12 @@ public class ConverterFragment extends BaseDialogFragment
} }
} }
} }
spinnerTo.setSelection(Math.max(0, Math.min(pendingToSelection, adapterTo.getCount() - 1))); if (pendingToSelection != NONE) {
pendingToSelection = View.NO_ID; spinnerTo.setSelection(MathUtils.clamp(pendingToSelection, 0, adapterTo.getCount() - 1));
pendingToSelection = NONE;
} else {
spinnerTo.setSelection(MathUtils.clamp(spinnerTo.getSelectedItemPosition(), 0, adapterTo.getCount() - 1));
}
} }
@Override @Override
@ -358,7 +408,7 @@ public class ConverterFragment extends BaseDialogFragment
final Convertible oldFromUnit = adapterFrom.getItem(spinnerFrom.getSelectedItemPosition()).item; final Convertible oldFromUnit = adapterFrom.getItem(spinnerFrom.getSelectedItemPosition()).item;
final Convertible oldToUnit = adapterTo.getItem(spinnerTo.getSelectedItemPosition()).item; final Convertible oldToUnit = adapterTo.getItem(spinnerTo.getSelectedItemPosition()).item;
pendingToSelection = -1; pendingToSelection = NONE;
for (int i = 0; i < adapterFrom.getCount(); i++) { for (int i = 0; i < adapterFrom.getCount(); i++) {
pendingToSelection++; pendingToSelection++;
final Convertible unit = adapterFrom.getItem(i).item; final Convertible unit = adapterFrom.getItem(i).item;

View File

@ -0,0 +1,12 @@
package org.solovyev.android.calculator.math;
public class MathUtils {
public static int clamp(int value, int min, int max) {
return value < min ? min : (value > max ? max : value);
}
public static float clamp(float value, float min, float max) {
return value < min ? min : (value > max ? max : value);
}
}