wizard added

This commit is contained in:
Sergey Solovyev 2013-06-16 22:43:19 +04:00
parent 7fcf6f4fdd
commit 80daac23dc
14 changed files with 559 additions and 5 deletions

View File

@ -106,24 +106,30 @@ public final class CalculatorPreferences {
} }
public static enum Layout { public static enum Layout {
main_calculator(R.layout.main_calculator), main_calculator(R.layout.main_calculator, R.string.p_layout_calculator),
main_calculator_mobile(R.layout.main_calculator_mobile), main_calculator_mobile(R.layout.main_calculator_mobile, R.string.p_layout_calculator_mobile),
// not used anymore // not used anymore
@Deprecated @Deprecated
main_cellphone(R.layout.main_calculator), main_cellphone(R.layout.main_calculator, 0),
simple(R.layout.main_calculator); simple(R.layout.main_calculator, R.string.p_layout_simple);
private final int layoutId; private final int layoutId;
private final int nameResId;
Layout(int layoutId) { Layout(int layoutId, int nameResId) {
this.layoutId = layoutId; this.layoutId = layoutId;
this.nameResId = nameResId;
} }
public int getLayoutId() { public int getLayoutId() {
return layoutId; return layoutId;
} }
public int getNameResId() {
return nameResId;
}
} }
} }

View File

@ -52,6 +52,11 @@
<activity android:label="@string/c_vars_and_constants" android:name=".math.edit.CalculatorVarsActivity"/> <activity android:label="@string/c_vars_and_constants" android:name=".math.edit.CalculatorVarsActivity"/>
<activity android:name=".CalculatorWikiActivity"/> <activity android:name=".CalculatorWikiActivity"/>
<activity android:name=".wizard.CalculatorWizardActivity" android:theme="@style/cpp_gray_dialog_theme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
</intent-filter>
</activity>
<activity android:label="@string/c_plot_graph" android:name=".plot.CalculatorPlotActivity"/> <activity android:label="@string/c_plot_graph" android:name=".plot.CalculatorPlotActivity"/>
<activity android:label="@string/cpp_plot_functions" android:name=".plot.CalculatorPlotFunctionsActivity" android:theme="@style/cpp_gray_dialog_theme"/> <activity android:label="@string/cpp_plot_functions" android:name=".plot.CalculatorPlotFunctionsActivity" android:theme="@style/cpp_gray_dialog_theme"/>

View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:id="@+id/wizard_layout"
style="?cpp_dialog_style"
a:orientation="vertical">
<FrameLayout
a:orientation="vertical"
a:id="@+id/wizard_content"
a:layout_width="match_parent"
a:layout_height="0dp"
a:layout_weight="1" />
<LinearLayout
a:orientation="horizontal"
a:layout_width="match_parent"
a:layout_height="wrap_content">
<Button
a:id="@+id/wizard_prev_button"
a:layout_height="wrap_content"
a:layout_width="0dp"
a:layout_weight="1"
a:text="Prev"/>
<Button
a:id="@+id/wizard_next_button"
a:layout_height="wrap_content"
a:layout_width="0dp"
a:layout_weight="1"
a:text="Next"/>
<Button
a:id="@+id/wizard_finish_button"
a:layout_height="wrap_content"
a:layout_width="0dp"
a:layout_weight="1"
a:visibility="gone"
a:text="Finish"/>
</LinearLayout>
</LinearLayout>

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:orientation="vertical">
<TextView
a:text="Choose layout"
a:layout_height="wrap_content"
a:layout_width="match_parent" />
<Spinner
a:id="@+id/wizard_mode_spinner"
a:layout_height="wrap_content"
a:layout_width="match_parent" />
</LinearLayout>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:text="@string/c_first_start_text"
/>

View File

@ -7,6 +7,7 @@ package org.solovyev.android.calculator;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.os.Build; import android.os.Build;
@ -28,6 +29,7 @@ import org.solovyev.android.Android;
import org.solovyev.android.Threads; import org.solovyev.android.Threads;
import org.solovyev.android.calculator.about.CalculatorReleaseNotesFragment; import org.solovyev.android.calculator.about.CalculatorReleaseNotesFragment;
import org.solovyev.android.calculator.plot.CalculatorPlotActivity; import org.solovyev.android.calculator.plot.CalculatorPlotActivity;
import org.solovyev.android.calculator.wizard.CalculatorWizardActivity;
import org.solovyev.android.fragments.FragmentUtils; import org.solovyev.android.fragments.FragmentUtils;
import org.solovyev.android.prefs.Preference; import org.solovyev.android.prefs.Preference;
import org.solovyev.common.Objects; import org.solovyev.common.Objects;
@ -130,6 +132,7 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Shar
boolean dialogShown = false; boolean dialogShown = false;
if (Objects.areEqual(savedVersion, CalculatorPreferences.appVersion.getDefaultValue())) { if (Objects.areEqual(savedVersion, CalculatorPreferences.appVersion.getDefaultValue())) {
// new start // new start
context.startActivity(new Intent(context, CalculatorWizardActivity.class));
final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(R.string.c_first_start_text); final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(R.string.c_first_start_text);
builder.setPositiveButton(android.R.string.ok, null); builder.setPositiveButton(android.R.string.ok, null);
builder.setTitle(R.string.c_first_start_text_title); builder.setTitle(R.string.c_first_start_text_title);

View File

@ -0,0 +1,149 @@
package org.solovyev.android.calculator.wizard;
import android.os.Bundle;
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;
import javax.annotation.Nonnull;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
/**
* User: serso
* Date: 6/16/13
* Time: 9:07 PM
*/
public final class CalculatorWizardActivity extends SherlockFragmentActivity {
private static final String FLOW = "flow";
private static final String STEP = "step";
/*
**********************************************************************
*
* FIELDS
*
**********************************************************************
*/
private WizardStep step;
private WizardFlow flow;
/*
**********************************************************************
*
* VIEWS
*
**********************************************************************
*/
private View prevButton;
private View nextButton;
private View finishButton;
private ViewGroup wizardContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cpp_wizard);
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;
if (savedInstanceState != null) {
wizardName = savedInstanceState.getString(FLOW);
step = (WizardStep) savedInstanceState.getSerializable(STEP);
}
flow = Wizard.getWizardFlow(wizardName != null ? wizardName : FirstTimeWizardFlow.NAME);
if (step == null) {
step = flow.getFirstStep();
}
setStep(step);
}
private void setStep(@Nonnull WizardStep step) {
if (this.step == null || !this.step.equals(step)) {
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);
}
}
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);
}
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();
}
});
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) {
setStep(prevStep);
}
});
}
}
}
@Override
protected void onSaveInstanceState(Bundle out) {
super.onSaveInstanceState(out);
out.putSerializable(STEP, step);
}
}

View File

@ -0,0 +1,110 @@
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.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragment;
import org.solovyev.android.calculator.CalculatorPreferences;
import org.solovyev.android.calculator.R;
import org.solovyev.android.list.ListItem;
import org.solovyev.android.list.ListItemAdapter;
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.main_calculator_mobile;
import static org.solovyev.android.calculator.CalculatorPreferences.Gui.Layout.simple;
/**
* User: serso
* Date: 6/16/13
* Time: 9:59 PM
*/
public final class ChooseModeWizardStep extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.cpp_wizard_step_choose_mode, null);
}
@Override
public void onViewCreated(View root, Bundle savedInstanceState) {
super.onViewCreated(root, savedInstanceState);
final Spinner layoutSpinner = (Spinner) root.findViewById(R.id.wizard_mode_spinner);
final List<ModeListItem> listItems = new ArrayList<ModeListItem>();
listItems.add(new ModeListItem(main_calculator));
listItems.add(new ModeListItem(main_calculator_mobile));
listItems.add(new ModeListItem(simple));
final ListItemAdapter<ModeListItem> adapter = ListItemAdapter.newInstance(getActivity(), listItems);
layoutSpinner.setAdapter(adapter);
layoutSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
final ModeListItem item = adapter.getItem(position);
CalculatorPreferences.Gui.layout.putPreference(preferences, item.layout);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
/*
**********************************************************************
*
* STATIC/INNER
*
**********************************************************************
*/
private static final class ModeListItem implements ListItem {
@Nonnull
private final CalculatorPreferences.Gui.Layout layout;
private ModeListItem(@Nonnull CalculatorPreferences.Gui.Layout layout) {
this.layout = layout;
}
@Nullable
@Override
public OnClickAction getOnClickAction() {
return null;
}
@Nullable
@Override
public OnClickAction getOnLongClickAction() {
return null;
}
@Nonnull
@Override
public View updateView(@Nonnull Context context, @Nonnull View view) {
return build(context);
}
@Nonnull
@Override
public View build(@Nonnull Context context) {
final TextView textView = new TextView(context);
textView.setText(layout.getNameResId());
return textView;
}
}
}

View File

@ -0,0 +1,52 @@
package org.solovyev.android.calculator.wizard;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import static org.solovyev.android.calculator.wizard.WizardStep.welcome;
/**
* User: serso
* Date: 6/16/13
* Time: 9:25 PM
*/
final class FirstTimeWizardFlow implements WizardFlow {
public static final String NAME = "first-wizard";
@Nonnull
private final ListWizardFlow listWizardFlow;
FirstTimeWizardFlow() {
final List<WizardStep> wizardSteps = new ArrayList<WizardStep>();
wizardSteps.add(welcome);
//wizardSteps.add(choose_mode);
this.listWizardFlow = new ListWizardFlow(NAME, wizardSteps);
}
@Nonnull
@Override
public String getName() {
return listWizardFlow.getName();
}
@Nullable
@Override
public WizardStep getNextStep(@Nonnull WizardStep step) {
return listWizardFlow.getNextStep(step);
}
@Nullable
@Override
public WizardStep getPrevStep(@Nonnull WizardStep step) {
return listWizardFlow.getPrevStep(step);
}
@Nonnull
@Override
public WizardStep getFirstStep() {
return listWizardFlow.getFirstStep();
}
}

View File

@ -0,0 +1,58 @@
package org.solovyev.android.calculator.wizard;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
/**
* User: serso
* Date: 6/16/13
* Time: 9:30 PM
*/
public final class ListWizardFlow implements WizardFlow {
@Nonnull
private final String name;
@Nonnull
private final List<WizardStep> wizardSteps;
public ListWizardFlow(@Nonnull String name, @Nonnull List<WizardStep> wizardSteps) {
this.name = name;
this.wizardSteps = wizardSteps;
}
@Nonnull
@Override
public String getName() {
return name;
}
@Nullable
@Override
public WizardStep getNextStep(@Nonnull WizardStep step) {
final int i = wizardSteps.indexOf(step);
if (i >= 0 && i + 1 < wizardSteps.size()) {
return wizardSteps.get(i + 1);
} else {
return null;
}
}
@Nullable
@Override
public WizardStep getPrevStep(@Nonnull WizardStep step) {
final int i = wizardSteps.indexOf(step);
if (i >= 1) {
return wizardSteps.get(i - 1);
} else {
return null;
}
}
@Nonnull
@Override
public WizardStep getFirstStep() {
return wizardSteps.get(0);
}
}

View File

@ -0,0 +1,21 @@
package org.solovyev.android.calculator.wizard;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.actionbarsherlock.app.SherlockFragment;
import org.solovyev.android.calculator.R;
/**
* User: serso
* Date: 6/16/13
* Time: 9:44 PM
*/
public final class WelcomeWizardStep extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.cpp_wizard_step_welcome, null);
}
}

View File

@ -0,0 +1,24 @@
package org.solovyev.android.calculator.wizard;
import javax.annotation.Nonnull;
/**
* User: serso
* Date: 6/16/13
* Time: 9:23 PM
*/
public final class Wizard {
private Wizard() {
throw new AssertionError();
}
@Nonnull
public static WizardFlow getWizardFlow(@Nonnull String name) {
if(FirstTimeWizardFlow.NAME.equals(name)) {
return new FirstTimeWizardFlow();
} else {
throw new IllegalArgumentException("Wizard flow " + name + " is not supported");
}
}
}

View File

@ -0,0 +1,24 @@
package org.solovyev.android.calculator.wizard;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* User: serso
* Date: 6/16/13
* Time: 9:22 PM
*/
public interface WizardFlow {
@Nonnull
String getName();
@Nullable
WizardStep getNextStep(@Nonnull WizardStep step);
@Nullable
WizardStep getPrevStep(@Nonnull WizardStep step);
@Nonnull
WizardStep getFirstStep();
}

View File

@ -0,0 +1,32 @@
package org.solovyev.android.calculator.wizard;
import android.support.v4.app.Fragment;
import javax.annotation.Nonnull;
/**
* User: serso
* Date: 6/16/13
* Time: 9:17 PM
*/
enum WizardStep {
welcome(WelcomeWizardStep.class),
choose_mode(ChooseModeWizardStep.class);
@Nonnull
private final Class<? extends Fragment> fragmentClass;
WizardStep(@Nonnull Class<? extends Fragment> fragmentClass) {
this.fragmentClass = fragmentClass;
}
public String getFragmentTag() {
return name();
}
@Nonnull
Class<? extends Fragment> getFragmentClass() {
return fragmentClass;
}
}