From 450b63b7a7b68cd6a256755bd492ee745b59d1b2 Mon Sep 17 00:00:00 2001 From: serso Date: Thu, 29 Jan 2015 19:50:33 +0100 Subject: [PATCH] Wizard exit confirmation dialog --- .../wizard/ChooseModeWizardStep.java | 14 +++- .../calculator/wizard/WizardActivity.java | 74 ++++++++++++++++++- .../solovyev/android/wizard/BaseWizard.java | 4 +- .../src/main/res/values-ru/strings.xml | 1 + android-app/src/main/res/values/strings.xml | 1 + 5 files changed, 85 insertions(+), 9 deletions(-) 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 0b020fbc..4ce824be 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 @@ -23,12 +23,10 @@ package org.solovyev.android.calculator.wizard; import android.os.Bundle; -import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.RadioButton; - import org.solovyev.android.calculator.R; import javax.annotation.Nonnull; @@ -41,7 +39,7 @@ import static org.solovyev.android.calculator.wizard.CalculatorMode.*; * Date: 6/16/13 * Time: 9:59 PM */ -public class ChooseModeWizardStep extends Fragment { +public class ChooseModeWizardStep extends WizardFragment { /* ********************************************************************** @@ -84,7 +82,15 @@ public class ChooseModeWizardStep extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - return inflater.inflate(R.layout.cpp_wizard_step_choose_mode, null); + final View view = super.onCreateView(inflater, container, savedInstanceState); + setupNextButton(R.string.acl_wizard_next); + setupPrevButton(R.string.acl_wizard_back); + return view; + } + + @Override + protected int getViewResId() { + return R.layout.cpp_wizard_step_choose_mode; } @Override diff --git a/android-app/src/main/java/org/solovyev/android/calculator/wizard/WizardActivity.java b/android-app/src/main/java/org/solovyev/android/calculator/wizard/WizardActivity.java index 9a29cc56..5ddbeb74 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/wizard/WizardActivity.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/wizard/WizardActivity.java @@ -1,5 +1,7 @@ package org.solovyev.android.calculator.wizard; +import android.app.AlertDialog; +import android.content.DialogInterface; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; @@ -13,10 +15,11 @@ import org.solovyev.android.calculator.R; import org.solovyev.android.wizard.*; import javax.annotation.Nonnull; +import javax.annotation.Nullable; public class WizardActivity extends BaseActivity implements WizardsAware { @Nonnull - private final WizardUi wizardUi = new WizardUi(this, this, 0); + private final WizardUi wizardUi = new WizardUi<>(this, this, 0); @Nonnull private ViewPager pager; @@ -31,6 +34,12 @@ public class WizardActivity extends BaseActivity implements WizardsAware { super(R.layout.cpp_activity_wizard); } + @Nullable + private AlertDialog dialog; + + @Nonnull + private final DialogListener dialogListener = new DialogListener(); + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -46,7 +55,9 @@ public class WizardActivity extends BaseActivity implements WizardsAware { titleIndicator.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { - wizardUi.setStep(flow.getStepAt(position)); + final WizardStep step = flow.getStepAt(position); + wizardUi.setStep(step); + wizardUi.getWizard().saveLastStep(step); } }); @@ -54,12 +65,16 @@ public class WizardActivity extends BaseActivity implements WizardsAware { final int position = flow.getPositionFor(wizardUi.getStep()); pager.setCurrentItem(position); } + + if (wizardUi.getWizard().getLastSavedStepName() == null) { + wizardUi.getWizard().saveLastStep(wizardUi.getStep()); + } } @Override public void onBackPressed() { if (pager.getCurrentItem() == 0) { - super.onBackPressed(); + finishWizardAbruptly(); } else { pager.setCurrentItem(pager.getCurrentItem() - 1); } @@ -88,6 +103,28 @@ public class WizardActivity extends BaseActivity implements WizardsAware { } public void finishWizardAbruptly() { + finishWizardAbruptly(false); + } + + public void finishWizardAbruptly(boolean confirmed) { + if (!confirmed) { + if (dialog != null) { + return; + } + + final AlertDialog.Builder b = new AlertDialog.Builder(this); + b.setTitle(getString(R.string.wizard_finish_confirmation_title)). + setMessage(R.string.acl_wizard_finish_confirmation). + setNegativeButton(R.string.c_no, dialogListener). + setPositiveButton(R.string.c_yes, dialogListener). + setOnCancelListener(dialogListener); + dialog = b.create(); + dialog.setOnDismissListener(dialogListener); + dialog.show(); + return; + } + + dismissDialog(); wizardUi.finishWizardAbruptly(); finish(); } @@ -121,6 +158,19 @@ public class WizardActivity extends BaseActivity implements WizardsAware { } } + @Override + protected void onDestroy() { + dismissDialog(); + super.onDestroy(); + } + + private void dismissDialog() { + if (dialog != null) { + dialog.dismiss(); + dialog = null; + } + } + private class WizardPagerAdapter extends FragmentStatePagerAdapter { @Nonnull private final ListWizardFlow flow; @@ -143,4 +193,22 @@ public class WizardActivity extends BaseActivity implements WizardsAware { return flow.getCount(); } } + + private class DialogListener implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener, DialogInterface.OnCancelListener { + @Override + public void onClick(DialogInterface dialog, int which) { + if (which == DialogInterface.BUTTON_POSITIVE) { + finishWizardAbruptly(true); + } + } + + public void onDismiss(DialogInterface d) { + dialog = null; + } + + @Override + public void onCancel(DialogInterface d) { + dialog = null; + } + } } diff --git a/android-app/src/main/java/org/solovyev/android/wizard/BaseWizard.java b/android-app/src/main/java/org/solovyev/android/wizard/BaseWizard.java index 42dd0259..b492e34c 100644 --- a/android-app/src/main/java/org/solovyev/android/wizard/BaseWizard.java +++ b/android-app/src/main/java/org/solovyev/android/wizard/BaseWizard.java @@ -37,7 +37,7 @@ public class BaseWizard implements Wizard { editor.putString(makeLastStepPreferenceKey(name), step.getName()); - editor.commit(); + editor.apply(); } @Override @@ -47,7 +47,7 @@ public class BaseWizard implements Wizard { editor.putBoolean(makeFinishedPreferenceKey(name), forceFinish || flow.getNextStep(step) == null); - editor.commit(); + editor.apply(); } @Nonnull diff --git a/android-app/src/main/res/values-ru/strings.xml b/android-app/src/main/res/values-ru/strings.xml index 6f41d40e..808e4532 100644 --- a/android-app/src/main/res/values-ru/strings.xml +++ b/android-app/src/main/res/values-ru/strings.xml @@ -14,4 +14,5 @@ Далее Назад Приложение настроено и готово к использованию. + Закончить? \ No newline at end of file diff --git a/android-app/src/main/res/values/strings.xml b/android-app/src/main/res/values/strings.xml index e0b074d8..895a93fa 100644 --- a/android-app/src/main/res/values/strings.xml +++ b/android-app/src/main/res/values/strings.xml @@ -1,6 +1,7 @@ Do you really want to finish wizard? + Finish? Next Back Finish