Skip to content

Commit 391e9bb

Browse files
Piotr Zawadzkizawadz88
authored andcommitted
Added a sample showing how to pass data between steps
1 parent 6131a92 commit 391e9bb

13 files changed

Lines changed: 309 additions & 3 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ In order to set that color:
215215
After clicking on the Next button if the user wants to e.g.:
216216
* save something in the database
217217
* make a network call on a separate Thread
218-
* simply save the data from the current step to some other component or parent Activity
218+
* simply save the data from the current step to some other component or parent Activity (see 'Passing data between steps' in the sample app for more details)
219219

220220
he can perform these operations and then invoke the `goToNextStep()` method of the `StepperLayout.OnNextClickedCallback` in the current Step.
221221
If the user wants to perform these operations on the final step, when clicking on the Complete button, he needs to invoke the `complete()` method of the `StepperLayout.OnCompleteClickedCallback`.

sample/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
<activity android:name=".NoFragmentsActivity" />
3838
<activity android:name=".ProceedProgrammaticallyActivity"
3939
android:windowSoftInputMode="stateVisible"/>
40+
<activity android:name=".PassDataBetweenStepsActivity"
41+
android:windowSoftInputMode="stateVisible"/>
4042
<activity android:name=".DisabledTabNavigationActivity" />
4143
<activity
4244
android:name=".CustomStepperLayoutThemeActivity"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Copyright 2017 StepStone Services
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.stepstone.stepper.sample;
18+
19+
public interface DataManager {
20+
21+
void saveData(String data);
22+
23+
String getData();
24+
25+
}

sample/src/main/java/com/stepstone/stepper/sample/MainActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ private SampleItemAdapter() {
8080
new SampleItem(getString(R.string.show_back_button), getString(R.string.show_back_button_description), ReturnButtonActivity.class),
8181
new SampleItem(getString(R.string.no_fragments), getString(R.string.no_fragments_description), NoFragmentsActivity.class),
8282
new SampleItem(getString(R.string.proceed_programmatically), getString(R.string.proceed_programmatically_description), ProceedProgrammaticallyActivity.class),
83+
new SampleItem(getString(R.string.passing_data_between_steps), getString(R.string.passing_data_between_steps_description), PassDataBetweenStepsActivity.class),
8384
new SampleItem(getString(R.string.disabled_tab_navigation), getString(R.string.disabled_tab_navigation_description), DisabledTabNavigationActivity.class),
8485
new SampleItem(getString(R.string.custom_stepperlayout_theme), getString(R.string.custom_stepperlayout_theme_description), CustomStepperLayoutThemeActivity.class)
8586
);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Copyright 2017 StepStone Services
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.stepstone.stepper.sample;
18+
19+
import android.os.Bundle;
20+
import android.support.v7.app.AppCompatActivity;
21+
22+
import com.stepstone.stepper.StepperLayout;
23+
import com.stepstone.stepper.sample.adapter.PassDataBetweenStepsFragmentStepAdapter;
24+
25+
import butterknife.Bind;
26+
import butterknife.ButterKnife;
27+
28+
public class PassDataBetweenStepsActivity extends AppCompatActivity implements DataManager {
29+
30+
private static final String CURRENT_STEP_POSITION_KEY = "position";
31+
32+
private static final String DATA = "data";
33+
34+
@Bind(R.id.stepperLayout)
35+
StepperLayout mStepperLayout;
36+
37+
private String mData;
38+
39+
@Override
40+
protected void onCreate(Bundle savedInstanceState) {
41+
super.onCreate(savedInstanceState);
42+
setTitle("Stepper sample");
43+
44+
setContentView(R.layout.activity_pass_data_between_steps);
45+
ButterKnife.bind(this);
46+
int startingStepPosition = savedInstanceState != null ? savedInstanceState.getInt(CURRENT_STEP_POSITION_KEY) : 0;
47+
mData = savedInstanceState != null ? savedInstanceState.getString(DATA) : null;
48+
mStepperLayout.setAdapter(new PassDataBetweenStepsFragmentStepAdapter(getSupportFragmentManager(), this), startingStepPosition);
49+
}
50+
51+
@Override
52+
protected void onSaveInstanceState(Bundle outState) {
53+
outState.putInt(CURRENT_STEP_POSITION_KEY, mStepperLayout.getCurrentStepPosition());
54+
outState.putString(DATA, mData);
55+
super.onSaveInstanceState(outState);
56+
}
57+
58+
@Override
59+
public void onBackPressed() {
60+
final int currentStepPosition = mStepperLayout.getCurrentStepPosition();
61+
if (currentStepPosition > 0) {
62+
mStepperLayout.onBackClicked();
63+
} else {
64+
finish();
65+
}
66+
}
67+
68+
@Override
69+
public void saveData(String data) {
70+
mData = data;
71+
}
72+
73+
public String getData() {
74+
return mData;
75+
}
76+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.stepstone.stepper.sample.adapter;
2+
3+
import android.content.Context;
4+
import android.support.annotation.NonNull;
5+
import android.support.v4.app.FragmentManager;
6+
7+
import com.stepstone.stepper.Step;
8+
import com.stepstone.stepper.adapter.AbstractFragmentStepAdapter;
9+
import com.stepstone.stepper.sample.step.fragment.PassDataBetweenStepsFirstStepFragment;
10+
import com.stepstone.stepper.sample.step.fragment.PassDataBetweenStepsSecondStepFragment;
11+
12+
public class PassDataBetweenStepsFragmentStepAdapter extends AbstractFragmentStepAdapter {
13+
14+
public PassDataBetweenStepsFragmentStepAdapter(@NonNull FragmentManager fm, @NonNull Context context) {
15+
super(fm, context);
16+
}
17+
18+
@Override
19+
public Step createStep(int position) {
20+
switch (position) {
21+
case 0:
22+
return PassDataBetweenStepsFirstStepFragment.newInstance();
23+
case 1:
24+
return PassDataBetweenStepsSecondStepFragment.newInstance();
25+
default:
26+
throw new IllegalArgumentException("Unsupported position: " + position);
27+
}
28+
}
29+
30+
@Override
31+
public int getCount() {
32+
return 2;
33+
}
34+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Copyright 2017 StepStone Services
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.stepstone.stepper.sample.step.fragment;
18+
19+
import android.content.Context;
20+
import android.support.annotation.NonNull;
21+
import android.support.annotation.UiThread;
22+
import android.widget.EditText;
23+
24+
import com.stepstone.stepper.BlockingStep;
25+
import com.stepstone.stepper.StepperLayout;
26+
import com.stepstone.stepper.VerificationError;
27+
import com.stepstone.stepper.sample.DataManager;
28+
import com.stepstone.stepper.sample.R;
29+
30+
import butterknife.Bind;
31+
32+
public class PassDataBetweenStepsFirstStepFragment extends ButterKnifeFragment implements BlockingStep {
33+
34+
public static PassDataBetweenStepsFirstStepFragment newInstance() {
35+
return new PassDataBetweenStepsFirstStepFragment();
36+
}
37+
38+
@Bind(R.id.editText)
39+
EditText editText;
40+
41+
private DataManager dataManager;
42+
43+
@Override
44+
public void onAttach(Context context) {
45+
super.onAttach(context);
46+
if (context instanceof DataManager) {
47+
dataManager = (DataManager) context;
48+
} else {
49+
throw new IllegalStateException("Activity must implement DataManager interface!");
50+
}
51+
}
52+
53+
@Override
54+
public VerificationError verifyStep() {
55+
return null;
56+
}
57+
58+
@Override
59+
public void onSelected() {
60+
}
61+
62+
@Override
63+
public void onError(@NonNull VerificationError error) {
64+
}
65+
66+
@Override
67+
@UiThread
68+
public void onNextClicked(final StepperLayout.OnNextClickedCallback callback) {
69+
String enteredText = editText.getText().toString();
70+
dataManager.saveData(enteredText);
71+
callback.goToNextStep();
72+
}
73+
74+
@Override
75+
public void onCompleteClicked(StepperLayout.OnCompleteClickedCallback callback) {
76+
callback.complete();
77+
}
78+
79+
@Override
80+
@UiThread
81+
public void onBackClicked(StepperLayout.OnBackClickedCallback callback) {
82+
callback.goToPrevStep();
83+
}
84+
85+
@Override
86+
protected int getLayoutResId() {
87+
return R.layout.fragment_step_form;
88+
}
89+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Copyright 2017 StepStone Services
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.stepstone.stepper.sample.step.fragment;
18+
19+
import android.content.Context;
20+
import android.support.annotation.NonNull;
21+
import android.widget.TextView;
22+
23+
import com.stepstone.stepper.Step;
24+
import com.stepstone.stepper.VerificationError;
25+
import com.stepstone.stepper.sample.DataManager;
26+
import com.stepstone.stepper.sample.R;
27+
28+
import butterknife.Bind;
29+
30+
public class PassDataBetweenStepsSecondStepFragment extends ButterKnifeFragment implements Step {
31+
32+
public static PassDataBetweenStepsSecondStepFragment newInstance() {
33+
return new PassDataBetweenStepsSecondStepFragment();
34+
}
35+
36+
@Bind(R.id.stepContent)
37+
TextView stepContent;
38+
39+
private DataManager dataManager;
40+
41+
@Override
42+
public void onAttach(Context context) {
43+
super.onAttach(context);
44+
if (context instanceof DataManager) {
45+
dataManager = (DataManager) context;
46+
} else {
47+
throw new IllegalStateException("Activity must implement DataManager interface!");
48+
}
49+
}
50+
51+
@Override
52+
public VerificationError verifyStep() {
53+
return null;
54+
}
55+
56+
@Override
57+
public void onSelected() {
58+
stepContent.setText("Entered text:\n" + dataManager.getData());
59+
}
60+
61+
@Override
62+
public void onError(@NonNull VerificationError error) {
63+
}
64+
65+
@Override
66+
protected int getLayoutResId() {
67+
return R.layout.fragment_with_text_content;
68+
}
69+
}

sample/src/main/java/com/stepstone/stepper/sample/step/fragment/StepperFeedbackStepFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,6 @@ public void onBackClicked(StepperLayout.OnBackClickedCallback callback) {
9191

9292
@Override
9393
protected int getLayoutResId() {
94-
return R.layout.fragment_step_stepper_feedback;
94+
return R.layout.fragment_with_text_content;
9595
}
9696
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<com.stepstone.stepper.StepperLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
android:id="@+id/stepperLayout"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:orientation="vertical"
8+
app:ms_stepperType="dots"
9+
app:ms_showErrorState="true" />

0 commit comments

Comments
 (0)