wizard added

This commit is contained in:
Sergey Solovyev 2013-06-18 20:43:58 +04:00
parent f77636e206
commit 6b5b291960
5 changed files with 102 additions and 4 deletions

View File

@ -47,4 +47,9 @@ enum CalculatorMode {
} }
protected abstract void apply(@Nonnull SharedPreferences preferences); protected abstract void apply(@Nonnull SharedPreferences preferences);
@Nonnull
static CalculatorMode getDefaultMode(){
return engineer;
}
} }

View File

@ -171,7 +171,7 @@ public final class CalculatorWizardActivity extends SherlockFragmentActivity {
Fragment newFragment = fm.findFragmentByTag(this.step.getFragmentTag()); Fragment newFragment = fm.findFragmentByTag(this.step.getFragmentTag());
if (newFragment == null) { 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()); ft.add(R.id.wizard_content, newFragment, this.step.getFragmentTag());
} else { } else {
ft.show(newFragment); ft.show(newFragment);

View File

@ -15,9 +15,10 @@ import org.solovyev.android.list.ListItemAdapter;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import static org.solovyev.android.calculator.wizard.CalculatorMode.simple; import static org.solovyev.android.calculator.wizard.CalculatorMode.getDefaultMode;
/** /**
* User: serso * User: serso
@ -26,9 +27,42 @@ import static org.solovyev.android.calculator.wizard.CalculatorMode.simple;
*/ */
public final class ChooseModeWizardStep extends SherlockFragment { public final class ChooseModeWizardStep extends SherlockFragment {
/*
**********************************************************************
*
* CONSTANTS
*
**********************************************************************
*/
static final String MODE = "mode";
/*
**********************************************************************
*
* FIELDS
*
**********************************************************************
*/
@Nullable @Nullable
private Spinner layoutSpinner; 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 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.cpp_wizard_step_choose_mode, null); 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); super.onViewCreated(root, savedInstanceState);
layoutSpinner = (Spinner) root.findViewById(R.id.wizard_mode_spinner); layoutSpinner = (Spinner) root.findViewById(R.id.wizard_mode_spinner);
final List<ModeListItem> listItems = new ArrayList<ModeListItem>(); final List<ModeListItem> listItems = new ArrayList<ModeListItem>();
for (CalculatorMode mode : CalculatorMode.values()) { for (CalculatorMode mode : CalculatorMode.values()) {
listItems.add(new ModeListItem(mode)); listItems.add(new ModeListItem(mode));
} }
layoutSpinner.setAdapter(ListItemAdapter.newInstance(getActivity(), listItems)); layoutSpinner.setAdapter(ListItemAdapter.newInstance(getActivity(), listItems));
final int position = Arrays.binarySearch(CalculatorMode.values(), mode);
if (position >= 0) {
layoutSpinner.setSelection(position);
}
} }
@Nonnull @Nonnull
CalculatorMode getSelectedMode() { CalculatorMode getSelectedMode() {
CalculatorMode mode = simple; CalculatorMode mode = getDefaultMode();
if (layoutSpinner != null) { if (layoutSpinner != null) {
final int position = layoutSpinner.getSelectedItemPosition(); final int position = layoutSpinner.getSelectedItemPosition();
@ -61,6 +101,13 @@ public final class ChooseModeWizardStep extends SherlockFragment {
return mode; return mode;
} }
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(MODE, mode);
}
/* /*
********************************************************************** **********************************************************************
* *

View File

@ -1,7 +1,12 @@
package org.solovyev.android.calculator.wizard; package org.solovyev.android.calculator.wizard;
import org.solovyev.android.prefs.Preference;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import static org.solovyev.android.calculator.wizard.CalculatorMode.getDefaultMode;
import static org.solovyev.android.prefs.StringPreference.ofEnum;
/** /**
* User: serso * User: serso
* Date: 6/16/13 * Date: 6/16/13
@ -23,4 +28,16 @@ public final class Wizard {
throw new IllegalArgumentException("Wizard flow " + name + " is not supported"); throw new IllegalArgumentException("Wizard flow " + name + " is not supported");
} }
} }
/*
**********************************************************************
*
* STATIC/INNER
*
**********************************************************************
*/
static final class Preferences {
static final Preference<CalculatorMode> mode = ofEnum("mode", getDefaultMode(), CalculatorMode.class);
}
} }

View File

@ -1,9 +1,16 @@
package org.solovyev.android.calculator.wizard; package org.solovyev.android.calculator.wizard;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import org.solovyev.android.calculator.CalculatorApplication;
import javax.annotation.Nonnull; 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 * User: serso
@ -22,6 +29,12 @@ enum WizardStep {
boolean onPrev(@Nonnull Fragment fragment) { boolean onPrev(@Nonnull Fragment fragment) {
return true; return true;
} }
@Nullable
@Override
Bundle getFragmentArgs() {
return null;
}
}, },
choose_mode(ChooseModeWizardStep.class) { choose_mode(ChooseModeWizardStep.class) {
@ -29,8 +42,11 @@ enum WizardStep {
boolean onNext(@Nonnull Fragment f) { boolean onNext(@Nonnull Fragment f) {
final ChooseModeWizardStep fragment = (ChooseModeWizardStep) f; final ChooseModeWizardStep fragment = (ChooseModeWizardStep) f;
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(f.getActivity());
final CalculatorMode mode = fragment.getSelectedMode(); final CalculatorMode mode = fragment.getSelectedMode();
mode.apply(PreferenceManager.getDefaultSharedPreferences(f.getActivity())); mode.apply(preferences);
Preferences.mode.putPreference(preferences, mode);
return true; return true;
} }
@ -39,6 +55,16 @@ enum WizardStep {
boolean onPrev(@Nonnull Fragment fragment) { boolean onPrev(@Nonnull Fragment fragment) {
return true; 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 @Nonnull
@ -59,4 +85,7 @@ enum WizardStep {
abstract boolean onNext(@Nonnull Fragment fragment); abstract boolean onNext(@Nonnull Fragment fragment);
abstract boolean onPrev(@Nonnull Fragment fragment); abstract boolean onPrev(@Nonnull Fragment fragment);
@Nullable
abstract Bundle getFragmentArgs();
} }