Wizard improved

This commit is contained in:
Sergey Solovyev 2013-06-25 01:15:45 +04:00
parent 723fd1a461
commit 21e8581f1f
7 changed files with 127 additions and 10 deletions

View File

@ -52,6 +52,12 @@ final class AppWizardFlow implements WizardFlow {
return listWizardFlow.getName();
}
@Nullable
@Override
public WizardStep getStep(@Nonnull String name) {
return listWizardFlow.getStep(name);
}
@Nullable
@Override
public WizardStep getNextStep(@Nonnull WizardStep step) {

View File

@ -14,6 +14,8 @@ import javax.annotation.Nonnull;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static org.solovyev.android.calculator.wizard.Wizard.FLOW;
import static org.solovyev.android.calculator.wizard.Wizard.STEP;
/**
* User: serso
@ -22,9 +24,6 @@ import static android.view.View.VISIBLE;
*/
public final class CalculatorWizardActivity extends SherlockFragmentActivity {
static final String FLOW = "flow";
static final String STEP = "step";
/*
**********************************************************************
*
@ -58,14 +57,19 @@ public final class CalculatorWizardActivity extends SherlockFragmentActivity {
finishButton = findViewById(R.id.wizard_finish_button);
String wizardName = getIntent().getStringExtra(FLOW);
WizardStep step = (WizardStep) getIntent().getSerializableExtra(STEP);
String stepName = getIntent().getStringExtra(STEP);
if (savedInstanceState != null) {
wizardName = savedInstanceState.getString(FLOW);
step = (WizardStep) savedInstanceState.getSerializable(STEP);
stepName = savedInstanceState.getString(STEP);
}
flow = Wizard.getWizardFlow(wizardName != null ? wizardName : Wizard.FIRST_TIME_WIZARD);
WizardStep step = null;
if(stepName != null) {
step = flow.getStep(stepName);
}
if (step == null) {
step = flow.getFirstStep();
}
@ -115,7 +119,7 @@ public final class CalculatorWizardActivity extends SherlockFragmentActivity {
@Override
public void onClick(View v) {
if (tryGoNext()) {
finish();
finishFlow();
}
}
});
@ -138,6 +142,13 @@ public final class CalculatorWizardActivity extends SherlockFragmentActivity {
}
}
private void finishFlow() {
if (flow != null && step != null) {
Wizard.saveWizardFinished(flow, step);
}
finish();
}
private boolean tryGoPrev() {
if (this.step == null) {
return true;
@ -206,7 +217,16 @@ public final class CalculatorWizardActivity extends SherlockFragmentActivity {
super.onSaveInstanceState(out);
out.putString(FLOW, flow.getName());
out.putSerializable(STEP, step);
out.putString(STEP, step.getName());
}
@Override
protected void onPause() {
super.onPause();
if (flow != null && step != null) {
Wizard.saveLastWizardStep(flow, step);
}
}
WizardStep getStep() {
@ -230,4 +250,16 @@ public final class CalculatorWizardActivity extends SherlockFragmentActivity {
intent.putExtra(FLOW, name);
context.startActivity(intent);
}
public static void continueWizard(@Nonnull String name, @Nonnull Context context) {
final String step = Wizard.getLastSavedWizardStepName(name);
final Intent intent = new Intent(context, CalculatorWizardActivity.class);
intent.putExtra(FLOW, name);
if (step != null) {
intent.putExtra(STEP, step);
}
context.startActivity(intent);
}
}

View File

@ -1,5 +1,8 @@
package org.solovyev.android.calculator.wizard;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
@ -28,6 +31,18 @@ public final class ListWizardFlow implements WizardFlow {
return name;
}
@Nullable
@Override
public WizardStep getStep(@Nonnull final String name) {
return Iterables.find(wizardSteps, new Predicate<WizardStep>() {
@Override
public boolean apply(@Nullable WizardStep step) {
assert step != null;
return step.getName().equals(name);
}
}, null);
}
@Nullable
@Override
public WizardStep getNextStep(@Nonnull WizardStep step) {

View File

@ -1,12 +1,15 @@
package org.solovyev.android.calculator.wizard;
import org.solovyev.android.prefs.Preference;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import org.solovyev.android.calculator.CalculatorApplication;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static org.solovyev.android.calculator.wizard.AppWizardFlow.newDefaultWizardFlow;
import static org.solovyev.android.calculator.wizard.AppWizardFlow.newFirstTimeWizardFlow;
import static org.solovyev.android.prefs.StringPreference.ofEnum;
/**
* User: serso
@ -17,6 +20,9 @@ public final class Wizard {
public static final String FIRST_TIME_WIZARD = "first-wizard";
public static final String DEFAULT_WIZARD_FLOW = "app-wizard";
static final String FLOW = "flow";
static final String FLOW_FINISHED = "flow_finished";
static final String STEP = "step";
private Wizard() {
throw new AssertionError();
@ -32,4 +38,54 @@ public final class Wizard {
throw new IllegalArgumentException("Wizard flow " + name + " is not supported");
}
}
public static boolean isWizardFinished(@Nonnull String name, @Nonnull Context context) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getBoolean(makeFlowFinishedPreferenceKey(name), false);
}
static void saveLastWizardStep(@Nonnull WizardFlow flow, @Nonnull WizardStep step) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorApplication.getInstance());
final SharedPreferences.Editor editor = preferences.edit();
editor.putString(makeFlowStepPreferenceKey(flow), step.name());
editor.commit();
}
@Nullable
static String getLastSavedWizardStepName(@Nonnull String name) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorApplication.getInstance());
return preferences.getString(makeFlowStepPreferenceKey(name), null);
}
static void saveWizardFinished(@Nonnull WizardFlow flow, @Nonnull WizardStep step) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorApplication.getInstance());
final SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(makeFlowFinishedPreferenceKey(flow), flow.getNextStep(step) == null);
editor.commit();
}
@Nonnull
private static String makeFlowFinishedPreferenceKey(@Nonnull String flowName) {
return FLOW_FINISHED + ":" + flowName;
}
@Nonnull
private static String makeFlowStepPreferenceKey(@Nonnull WizardFlow flow) {
return makeFlowStepPreferenceKey(flow.getName());
}
@Nonnull
private static String makeFlowStepPreferenceKey(@Nonnull String flowName) {
return FLOW + ":" + flowName;
}
@Nonnull
private static String makeFlowFinishedPreferenceKey(@Nonnull WizardFlow flow) {
return makeFlowFinishedPreferenceKey(flow.getName());
}
}

View File

@ -13,6 +13,9 @@ public interface WizardFlow {
@Nonnull
String getName();
@Nullable
WizardStep getStep(@Nonnull String name);
@Nullable
WizardStep getNextStep(@Nonnull WizardStep step);

View File

@ -185,4 +185,9 @@ enum WizardStep {
public boolean isVisible() {
return true;
}
@Nonnull
public String getName() {
return name();
}
}

View File

@ -67,7 +67,7 @@ public class CalculatorWizardActivityTest {
public void testCreate() throws Exception {
final Intent intent = new Intent();
intent.setClass(activity, CalculatorWizardActivity.class);
intent.putExtra(CalculatorWizardActivity.FLOW, Wizard.DEFAULT_WIZARD_FLOW);
intent.putExtra(Wizard.FLOW, Wizard.DEFAULT_WIZARD_FLOW);
controller = Robolectric.buildActivity(CalculatorWizardActivity.class).withIntent(intent);
activity = controller.get();
controller.create(null);