wizard added

This commit is contained in:
Sergey Solovyev
2013-06-18 00:44:44 +04:00
parent 6ef4354f2c
commit 4840b6e25b
14 changed files with 289 additions and 196 deletions

View File

@@ -0,0 +1,50 @@
package org.solovyev.android.calculator.wizard;
import android.content.SharedPreferences;
import jscl.AngleUnit;
import org.solovyev.android.calculator.CalculatorPreferences;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
import javax.annotation.Nonnull;
import static org.solovyev.android.calculator.CalculatorPreferences.Gui.Layout.main_calculator;
/**
* User: serso
* Date: 6/17/13
* Time: 9:30 PM
*/
enum CalculatorMode {
simple(R.string.cpp_wizard_mode_simple) {
@Override
protected void apply(@Nonnull SharedPreferences preferences) {
CalculatorPreferences.Gui.layout.putPreference(preferences, CalculatorPreferences.Gui.Layout.simple);
CalculatorPreferences.Calculations.preferredAngleUnits.putPreference(preferences, AngleUnit.deg);
AndroidCalculatorEngine.Preferences.scienceNotation.putPreference(preferences, false);
AndroidCalculatorEngine.Preferences.roundResult.putPreference(preferences, true);
}
},
engineer(R.string.cpp_wizard_mode_engineer) {
@Override
protected void apply(@Nonnull SharedPreferences preferences) {
CalculatorPreferences.Gui.layout.putPreference(preferences, main_calculator);
CalculatorPreferences.Calculations.preferredAngleUnits.putPreference(preferences, AngleUnit.rad);
AndroidCalculatorEngine.Preferences.scienceNotation.putPreference(preferences, true);
AndroidCalculatorEngine.Preferences.roundResult.putPreference(preferences, false);
}
};
private final int nameResId;
CalculatorMode(int nameResId) {
this.nameResId = nameResId;
}
int getNameResId() {
return nameResId;
}
protected abstract void apply(@Nonnull SharedPreferences preferences);
}

View File

@@ -5,7 +5,6 @@ import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.ViewGroup;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import org.solovyev.android.calculator.R;
@@ -46,7 +45,6 @@ public final class CalculatorWizardActivity extends SherlockFragmentActivity {
private View prevButton;
private View nextButton;
private View finishButton;
private ViewGroup wizardContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -56,7 +54,6 @@ public final class CalculatorWizardActivity extends SherlockFragmentActivity {
prevButton = findViewById(R.id.wizard_prev_button);
nextButton = findViewById(R.id.wizard_next_button);
finishButton = findViewById(R.id.wizard_finish_button);
wizardContent = (ViewGroup) findViewById(R.id.wizard_content);
String wizardName = null;
WizardStep step = null;
@@ -79,62 +76,113 @@ public final class CalculatorWizardActivity extends SherlockFragmentActivity {
final FragmentManager fm = getSupportFragmentManager();
final FragmentTransaction ft = fm.beginTransaction();
if (this.step != null) {
final Fragment oldFragment = fm.findFragmentByTag(this.step.getFragmentTag());
if (oldFragment != null) {
ft.hide(oldFragment);
}
}
hideFragment(fm, ft);
this.step = step;
Fragment newFragment = fm.findFragmentByTag(step.getFragmentTag());
if(newFragment == null) {
newFragment = Fragment.instantiate(this, step.getFragmentClass().getName());
ft.add(R.id.wizard_content, newFragment, step.getFragmentTag());
} else {
ft.show(newFragment);
}
showFragment(fm, ft);
ft.commit();
final WizardStep nextStep = flow.getNextStep(step);
if (nextStep == null) {
finishButton.setVisibility(VISIBLE);
finishButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
initNextButton();
initPrevButton();
}
}
nextButton.setVisibility(GONE);
nextButton.setOnClickListener(null);
} else {
finishButton.setVisibility(GONE);
finishButton.setOnClickListener(null);
nextButton.setVisibility(VISIBLE);
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setStep(nextStep);
}
});
}
final WizardStep prevStep = flow.getPrevStep(step);
if (prevStep == null) {
prevButton.setVisibility(GONE);
prevButton.setOnClickListener(null);
} else {
prevButton.setVisibility(VISIBLE);
prevButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
private void initPrevButton() {
final WizardStep prevStep = flow.getPrevStep(step);
if (prevStep == null) {
prevButton.setVisibility(GONE);
prevButton.setOnClickListener(null);
} else {
prevButton.setVisibility(VISIBLE);
prevButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (tryGoPrev()) {
setStep(prevStep);
}
});
}
});
}
}
private void initNextButton() {
final WizardStep nextStep = flow.getNextStep(step);
if (nextStep == null) {
finishButton.setVisibility(VISIBLE);
finishButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (tryGoNext()) {
finish();
}
}
});
nextButton.setVisibility(GONE);
nextButton.setOnClickListener(null);
} else {
finishButton.setVisibility(GONE);
finishButton.setOnClickListener(null);
nextButton.setVisibility(VISIBLE);
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (tryGoNext()) {
setStep(nextStep);
}
}
});
}
}
private boolean tryGoPrev() {
if (this.step == null) {
return true;
} else {
final Fragment fragment = getSupportFragmentManager().findFragmentByTag(this.step.getFragmentTag());
if (fragment != null) {
return this.step.onPrev(fragment);
} else {
return true;
}
}
}
private boolean tryGoNext() {
if (this.step == null) {
return true;
} else {
final Fragment fragment = getSupportFragmentManager().findFragmentByTag(this.step.getFragmentTag());
if (fragment != null) {
return this.step.onNext(fragment);
} else {
return true;
}
}
}
@Nonnull
private Fragment showFragment(@Nonnull FragmentManager fm, @Nonnull FragmentTransaction ft) {
Fragment newFragment = fm.findFragmentByTag(this.step.getFragmentTag());
if (newFragment == null) {
newFragment = Fragment.instantiate(this, this.step.getFragmentClass().getName());
ft.add(R.id.wizard_content, newFragment, this.step.getFragmentTag());
} else {
ft.show(newFragment);
}
return newFragment;
}
private void hideFragment(@Nonnull FragmentManager fm, @Nonnull FragmentTransaction ft) {
if (this.step != null) {
final Fragment oldFragment = fm.findFragmentByTag(this.step.getFragmentTag());
if (oldFragment != null) {
ft.hide(oldFragment);
}
}
}
@@ -146,4 +194,11 @@ public final class CalculatorWizardActivity extends SherlockFragmentActivity {
out.putSerializable(STEP, step);
}
WizardStep getStep() {
return step;
}
WizardFlow getFlow() {
return flow;
}
}

View File

@@ -1,39 +1,30 @@
package org.solovyev.android.calculator.wizard;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Spinner;
import android.widget.TextView;
import jscl.AngleUnit;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.solovyev.android.calculator.CalculatorPreferences;
import com.actionbarsherlock.app.SherlockFragment;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
import org.solovyev.android.list.ListItem;
import org.solovyev.android.list.ListItemAdapter;
import com.actionbarsherlock.app.SherlockFragment;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import static org.solovyev.android.calculator.CalculatorPreferences.Gui.Layout.main_calculator;
import static org.solovyev.android.calculator.CalculatorPreferences.Gui.Layout.simple;
import static org.solovyev.android.calculator.wizard.CalculatorMode.simple;
/**
* User: serso
* Date: 6/16/13
* Time: 9:59 PM
*/
public final class ChooseModeWizardStep extends SherlockFragment implements WizardStepFragment {
public final class ChooseModeWizardStep extends SherlockFragment {
@Nullable
private Spinner layoutSpinner;
@@ -49,33 +40,25 @@ public final class ChooseModeWizardStep extends SherlockFragment implements Wiza
layoutSpinner = (Spinner) root.findViewById(R.id.wizard_mode_spinner);
final List<ModeListItem> listItems = new ArrayList<ModeListItem>();
for (Mode mode : Mode.values()) {
for (CalculatorMode mode : CalculatorMode.values()) {
listItems.add(new ModeListItem(mode));
}
layoutSpinner.setAdapter(ListItemAdapter.newInstance(getActivity(), listItems));
}
@Override
public boolean onNext() {
@Nonnull
CalculatorMode getSelectedMode() {
CalculatorMode mode = simple;
if (layoutSpinner != null) {
final int position = layoutSpinner.getSelectedItemPosition();
final Mode mode;
if (position >= 0 && position < Mode.values().length) {
mode = Mode.values()[position];
} else {
mode = Mode.Simple;
if (position >= 0 && position < CalculatorMode.values().length) {
mode = CalculatorMode.values()[position];
}
mode.apply(PreferenceManager.getDefaultSharedPreferences(getActivity()));
}
return true;
}
@Override
public boolean onPrev() {
return true;
return mode;
}
/*
@@ -89,9 +72,9 @@ public final class ChooseModeWizardStep extends SherlockFragment implements Wiza
private static final class ModeListItem implements ListItem {
@Nonnull
private final Mode mode;
private final CalculatorMode mode;
private ModeListItem(@Nonnull Mode mode) {
private ModeListItem(@Nonnull CalculatorMode mode) {
this.mode = mode;
}
@@ -122,37 +105,4 @@ public final class ChooseModeWizardStep extends SherlockFragment implements Wiza
}
}
private static enum Mode {
Simple(R.string.cpp_wizard_mode_simple) {
@Override
protected void apply(@Nonnull SharedPreferences preferences) {
CalculatorPreferences.Gui.layout.putPreference(preferences, simple);
CalculatorPreferences.Calculations.preferredAngleUnits.putPreference(preferences, AngleUnit.deg);
AndroidCalculatorEngine.Preferences.scienceNotation.putPreference(preferences, false);
AndroidCalculatorEngine.Preferences.roundResult.putPreference(preferences, true);
}
},
Engineer(R.string.cpp_wizard_mode_engineer) {
@Override
protected void apply(@Nonnull SharedPreferences preferences) {
CalculatorPreferences.Gui.layout.putPreference(preferences, main_calculator);
CalculatorPreferences.Calculations.preferredAngleUnits.putPreference(preferences, AngleUnit.rad);
AndroidCalculatorEngine.Preferences.scienceNotation.putPreference(preferences, true);
AndroidCalculatorEngine.Preferences.roundResult.putPreference(preferences, false);
}
};
private final int nameResId;
Mode(int nameResId) {
this.nameResId = nameResId;
}
private int getNameResId() {
return nameResId;
}
protected abstract void apply(@Nonnull SharedPreferences preferences);
}
}

View File

@@ -1,5 +1,6 @@
package org.solovyev.android.calculator.wizard;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import javax.annotation.Nonnull;
@@ -11,8 +12,34 @@ import javax.annotation.Nonnull;
*/
enum WizardStep {
welcome(WelcomeWizardStep.class),
choose_mode(ChooseModeWizardStep.class);
welcome(WelcomeWizardStep.class) {
@Override
boolean onNext(@Nonnull Fragment fragment) {
return true;
}
@Override
boolean onPrev(@Nonnull Fragment fragment) {
return true;
}
},
choose_mode(ChooseModeWizardStep.class) {
@Override
boolean onNext(@Nonnull Fragment f) {
final ChooseModeWizardStep fragment = (ChooseModeWizardStep) f;
final CalculatorMode mode = fragment.getSelectedMode();
mode.apply(PreferenceManager.getDefaultSharedPreferences(f.getActivity()));
return true;
}
@Override
boolean onPrev(@Nonnull Fragment fragment) {
return true;
}
};
@Nonnull
private final Class<? extends Fragment> fragmentClass;
@@ -29,4 +56,7 @@ enum WizardStep {
Class<? extends Fragment> getFragmentClass() {
return fragmentClass;
}
abstract boolean onNext(@Nonnull Fragment fragment);
abstract boolean onPrev(@Nonnull Fragment fragment);
}

View File

@@ -1,8 +0,0 @@
package org.solovyev.android.calculator.wizard;
public interface WizardStepFragment {
public boolean onNext();
public boolean onPrev();
}