Wizard exit confirmation dialog

This commit is contained in:
serso 2015-01-29 19:50:33 +01:00
parent eafafcdea8
commit 450b63b7a7
5 changed files with 85 additions and 9 deletions

View File

@ -23,12 +23,10 @@
package org.solovyev.android.calculator.wizard; package org.solovyev.android.calculator.wizard;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.RadioButton; import android.widget.RadioButton;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -41,7 +39,7 @@ import static org.solovyev.android.calculator.wizard.CalculatorMode.*;
* Date: 6/16/13 * Date: 6/16/13
* Time: 9:59 PM * Time: 9:59 PM
*/ */
public class ChooseModeWizardStep extends Fragment { public class ChooseModeWizardStep extends WizardFragment {
/* /*
********************************************************************** **********************************************************************
@ -84,7 +82,15 @@ public class ChooseModeWizardStep extends Fragment {
@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); 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 @Override

View File

@ -1,5 +1,7 @@
package org.solovyev.android.calculator.wizard; package org.solovyev.android.calculator.wizard;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
@ -13,10 +15,11 @@ import org.solovyev.android.calculator.R;
import org.solovyev.android.wizard.*; import org.solovyev.android.wizard.*;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class WizardActivity extends BaseActivity implements WizardsAware { public class WizardActivity extends BaseActivity implements WizardsAware {
@Nonnull @Nonnull
private final WizardUi<WizardActivity> wizardUi = new WizardUi<WizardActivity>(this, this, 0); private final WizardUi<WizardActivity> wizardUi = new WizardUi<>(this, this, 0);
@Nonnull @Nonnull
private ViewPager pager; private ViewPager pager;
@ -31,6 +34,12 @@ public class WizardActivity extends BaseActivity implements WizardsAware {
super(R.layout.cpp_activity_wizard); super(R.layout.cpp_activity_wizard);
} }
@Nullable
private AlertDialog dialog;
@Nonnull
private final DialogListener dialogListener = new DialogListener();
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -46,7 +55,9 @@ public class WizardActivity extends BaseActivity implements WizardsAware {
titleIndicator.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { titleIndicator.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override @Override
public void onPageSelected(int position) { 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()); final int position = flow.getPositionFor(wizardUi.getStep());
pager.setCurrentItem(position); pager.setCurrentItem(position);
} }
if (wizardUi.getWizard().getLastSavedStepName() == null) {
wizardUi.getWizard().saveLastStep(wizardUi.getStep());
}
} }
@Override @Override
public void onBackPressed() { public void onBackPressed() {
if (pager.getCurrentItem() == 0) { if (pager.getCurrentItem() == 0) {
super.onBackPressed(); finishWizardAbruptly();
} else { } else {
pager.setCurrentItem(pager.getCurrentItem() - 1); pager.setCurrentItem(pager.getCurrentItem() - 1);
} }
@ -88,6 +103,28 @@ public class WizardActivity extends BaseActivity implements WizardsAware {
} }
public void finishWizardAbruptly() { 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(); wizardUi.finishWizardAbruptly();
finish(); 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 { private class WizardPagerAdapter extends FragmentStatePagerAdapter {
@Nonnull @Nonnull
private final ListWizardFlow flow; private final ListWizardFlow flow;
@ -143,4 +193,22 @@ public class WizardActivity extends BaseActivity implements WizardsAware {
return flow.getCount(); 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;
}
}
} }

View File

@ -37,7 +37,7 @@ public class BaseWizard implements Wizard {
editor.putString(makeLastStepPreferenceKey(name), step.getName()); editor.putString(makeLastStepPreferenceKey(name), step.getName());
editor.commit(); editor.apply();
} }
@Override @Override
@ -47,7 +47,7 @@ public class BaseWizard implements Wizard {
editor.putBoolean(makeFinishedPreferenceKey(name), forceFinish || flow.getNextStep(step) == null); editor.putBoolean(makeFinishedPreferenceKey(name), forceFinish || flow.getNextStep(step) == null);
editor.commit(); editor.apply();
} }
@Nonnull @Nonnull

View File

@ -14,4 +14,5 @@
<string name="wizard_next">Далее</string> <string name="wizard_next">Далее</string>
<string name="wizard_prev">Назад</string> <string name="wizard_prev">Назад</string>
<string name="wizard_final">Приложение настроено и готово к использованию.</string> <string name="wizard_final">Приложение настроено и готово к использованию.</string>
<string name="wizard_finish_confirmation_title">Закончить?</string>
</resources> </resources>

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string name="acl_wizard_finish_confirmation">Do you really want to finish wizard?</string> <string name="acl_wizard_finish_confirmation">Do you really want to finish wizard?</string>
<string name="wizard_finish_confirmation_title">Finish?</string>
<string name="acl_wizard_next">Next</string> <string name="acl_wizard_next">Next</string>
<string name="acl_wizard_back">Back</string> <string name="acl_wizard_back">Back</string>
<string name="acl_wizard_finish">Finish</string> <string name="acl_wizard_finish">Finish</string>