diff --git a/android-app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorMode.java b/android-app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorMode.java index b6d0b062..97765cea 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorMode.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorMode.java @@ -47,4 +47,9 @@ enum CalculatorMode { } protected abstract void apply(@Nonnull SharedPreferences preferences); + + @Nonnull + static CalculatorMode getDefaultMode(){ + return engineer; + } } diff --git a/android-app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorWizardActivity.java b/android-app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorWizardActivity.java index bac018ce..1fdf1992 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorWizardActivity.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorWizardActivity.java @@ -171,7 +171,7 @@ public final class CalculatorWizardActivity extends SherlockFragmentActivity { Fragment newFragment = fm.findFragmentByTag(this.step.getFragmentTag()); if (newFragment == null) { - newFragment = Fragment.instantiate(this, this.step.getFragmentClass().getName()); + newFragment = Fragment.instantiate(this, this.step.getFragmentClass().getName(), this.step.getFragmentArgs()); ft.add(R.id.wizard_content, newFragment, this.step.getFragmentTag()); } else { ft.show(newFragment); diff --git a/android-app/src/main/java/org/solovyev/android/calculator/wizard/ChooseModeWizardStep.java b/android-app/src/main/java/org/solovyev/android/calculator/wizard/ChooseModeWizardStep.java index 9cb6e9a5..4251df39 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/wizard/ChooseModeWizardStep.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/wizard/ChooseModeWizardStep.java @@ -15,9 +15,10 @@ import org.solovyev.android.list.ListItemAdapter; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; -import static org.solovyev.android.calculator.wizard.CalculatorMode.simple; +import static org.solovyev.android.calculator.wizard.CalculatorMode.getDefaultMode; /** * User: serso @@ -26,9 +27,42 @@ import static org.solovyev.android.calculator.wizard.CalculatorMode.simple; */ public final class ChooseModeWizardStep extends SherlockFragment { + /* + ********************************************************************** + * + * CONSTANTS + * + ********************************************************************** + */ + + static final String MODE = "mode"; + + /* + ********************************************************************** + * + * FIELDS + * + ********************************************************************** + */ + @Nullable private Spinner layoutSpinner; + private CalculatorMode mode; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + if(savedInstanceState != null) { + mode = (CalculatorMode) savedInstanceState.getSerializable(MODE); + } + + if (mode == null) { + mode = (CalculatorMode) getArguments().getSerializable(MODE); + } + } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.cpp_wizard_step_choose_mode, null); @@ -39,16 +73,22 @@ public final class ChooseModeWizardStep extends SherlockFragment { super.onViewCreated(root, savedInstanceState); layoutSpinner = (Spinner) root.findViewById(R.id.wizard_mode_spinner); + final List listItems = new ArrayList(); for (CalculatorMode mode : CalculatorMode.values()) { listItems.add(new ModeListItem(mode)); } layoutSpinner.setAdapter(ListItemAdapter.newInstance(getActivity(), listItems)); + + final int position = Arrays.binarySearch(CalculatorMode.values(), mode); + if (position >= 0) { + layoutSpinner.setSelection(position); + } } @Nonnull CalculatorMode getSelectedMode() { - CalculatorMode mode = simple; + CalculatorMode mode = getDefaultMode(); if (layoutSpinner != null) { final int position = layoutSpinner.getSelectedItemPosition(); @@ -61,6 +101,13 @@ public final class ChooseModeWizardStep extends SherlockFragment { return mode; } + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + + outState.putSerializable(MODE, mode); + } + /* ********************************************************************** * diff --git a/android-app/src/main/java/org/solovyev/android/calculator/wizard/Wizard.java b/android-app/src/main/java/org/solovyev/android/calculator/wizard/Wizard.java index d35347e8..3d532e42 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/wizard/Wizard.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/wizard/Wizard.java @@ -1,7 +1,12 @@ package org.solovyev.android.calculator.wizard; +import org.solovyev.android.prefs.Preference; + import javax.annotation.Nonnull; +import static org.solovyev.android.calculator.wizard.CalculatorMode.getDefaultMode; +import static org.solovyev.android.prefs.StringPreference.ofEnum; + /** * User: serso * Date: 6/16/13 @@ -23,4 +28,16 @@ public final class Wizard { throw new IllegalArgumentException("Wizard flow " + name + " is not supported"); } } + + /* + ********************************************************************** + * + * STATIC/INNER + * + ********************************************************************** + */ + + static final class Preferences { + static final Preference mode = ofEnum("mode", getDefaultMode(), CalculatorMode.class); + } } diff --git a/android-app/src/main/java/org/solovyev/android/calculator/wizard/WizardStep.java b/android-app/src/main/java/org/solovyev/android/calculator/wizard/WizardStep.java index e51b4638..4fa04380 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/wizard/WizardStep.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/wizard/WizardStep.java @@ -1,9 +1,16 @@ package org.solovyev.android.calculator.wizard; +import android.content.SharedPreferences; +import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.app.Fragment; +import org.solovyev.android.calculator.CalculatorApplication; import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import static org.solovyev.android.calculator.wizard.ChooseModeWizardStep.MODE; +import static org.solovyev.android.calculator.wizard.Wizard.Preferences; /** * User: serso @@ -22,6 +29,12 @@ enum WizardStep { boolean onPrev(@Nonnull Fragment fragment) { return true; } + + @Nullable + @Override + Bundle getFragmentArgs() { + return null; + } }, choose_mode(ChooseModeWizardStep.class) { @@ -29,8 +42,11 @@ enum WizardStep { boolean onNext(@Nonnull Fragment f) { final ChooseModeWizardStep fragment = (ChooseModeWizardStep) f; + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(f.getActivity()); + final CalculatorMode mode = fragment.getSelectedMode(); - mode.apply(PreferenceManager.getDefaultSharedPreferences(f.getActivity())); + mode.apply(preferences); + Preferences.mode.putPreference(preferences, mode); return true; } @@ -39,6 +55,16 @@ enum WizardStep { boolean onPrev(@Nonnull Fragment fragment) { return true; } + + @Nullable + @Override + Bundle getFragmentArgs() { + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorApplication.getInstance()); + + final Bundle bundle = new Bundle(); + bundle.putSerializable(MODE, Preferences.mode.getPreference(preferences)); + return bundle; + } }; @Nonnull @@ -59,4 +85,7 @@ enum WizardStep { abstract boolean onNext(@Nonnull Fragment fragment); abstract boolean onPrev(@Nonnull Fragment fragment); + + @Nullable + abstract Bundle getFragmentArgs(); }