Skip to content

Commit c798a94

Browse files
authored
add option to intercept BACK button action for Blocking Steps (issue #25) (#27)
* - added onBackClicked option for BlockingStep * - updated library version to 1.1.1 * - added onBackClicked option for BlockingStep * - updated JavaDoc description
1 parent c60e0e8 commit c798a94

File tree

5 files changed

+65
-15
lines changed

5 files changed

+65
-15
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Quoting the [documentation](https://www.google.com/design/spec/components/steppe
1111
1212
## Download (from JCenter)
1313
```groovy
14-
compile 'com.stepstone.stepper:material-stepper:1.1.0'
14+
compile 'com.stepstone.stepper:material-stepper:1.1.1'
1515
```
1616

1717
## Supported steppers
@@ -206,6 +206,7 @@ In order to set that color:
206206
### Make an IO operation before going to the next step (optional)
207207
If the user wants to e.g. save something in the database or make a network call on a separate Thread after clicking on the Next button
208208
he can perform these operations and then invoke the `goToNextStep()` method of the `StepperLayout.OnNextClickedCallback` in the current Step.
209+
While operations are performed, and the user would like to go back you can cancel them and then invoke `onBackClicked()` method of the `StepperLayout.OnBackClickedCallback`.
209210
<p><img src ="./gifs/delayed-transition.gif" width="360" height="640"/></p>
210211
The fragment must implement `BlockingStep` instead of `Step`.
211212
Also, make sure that `goToNextStep()` gets called on the main thread.
@@ -227,6 +228,13 @@ public class DelayedTransitionStepFragmentSample extends Fragment implements Blo
227228
}, 2000L);
228229
}
229230

231+
@Override
232+
@UiThread
233+
public void onBackClicked(StepperLayout.OnBackClickedCallback callback) {
234+
Toast.makeText(this.getContext(), "Your custom back action. Here you should cancel currently running operations", Toast.LENGTH_SHORT).show();
235+
callback.goToPrevStep();
236+
}
237+
230238
}
231239
```
232240

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919

2020
POM_GROUP_ID=com.stepstone.stepper
2121
POM_ARTIFACT_ID=material-stepper
22-
POM_VERSION=1.1.0
22+
POM_VERSION=1.1.1

material-stepper/src/main/java/com/stepstone/stepper/BlockingStep.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,20 @@ public interface BlockingStep extends Step {
3232
* and the user can go to the next step. This is so that the current step might perform
3333
* some last minute operations e.g. a network call before switching to the next step.
3434
* {@link StepperLayout.OnNextClickedCallback#goToNextStep()} must be called once these operations finish.
35+
*
3536
* @param callback callback to call once the user wishes to finally switch to the next step
3637
*/
3738
@UiThread
3839
void onNextClicked(StepperLayout.OnNextClickedCallback callback);
3940

41+
/**
42+
* Notifies this step that the previous button/tab was clicked. This is so that the current step might perform
43+
* some last minute operations e.g. a network call before switching to previous step.
44+
* {@link StepperLayout.OnBackClickedCallback#goToPrevStep()} must be called once these operations finish.
45+
*
46+
* @param callback callback to call once the user wishes to finally switch to the previous step
47+
*/
48+
@UiThread
49+
void onBackClicked(StepperLayout.OnBackClickedCallback callback);
50+
4051
}

material-stepper/src/main/java/com/stepstone/stepper/StepperLayout.java

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,21 @@ public interface StepperListener {
6969

7070
/**
7171
* Called when all of the steps were completed successfully
72+
*
7273
* @param completeButton the complete button that was clicked to complete the flow
7374
*/
7475
void onCompleted(View completeButton);
7576

7677
/**
7778
* Called when a verification error occurs for one of the steps
79+
*
7880
* @param verificationError verification error
7981
*/
8082
void onError(VerificationError verificationError);
8183

8284
/**
8385
* Called when the current step position changes
86+
*
8487
* @param newStepPosition new step position
8588
*/
8689
void onStepSelected(int newStepPosition);
@@ -125,6 +128,23 @@ public final void goToNextStep() {
125128
}
126129

127130
}
131+
132+
public final class OnBackClickedCallback {
133+
134+
@UiThread
135+
public final void goToPrevStep() {
136+
if (mCurrentStepPosition <= 0) {
137+
if (mShowBackButtonOnFirstStep) {
138+
mListener.onReturn();
139+
}
140+
return;
141+
}
142+
mCurrentStepPosition--;
143+
onUpdate(mCurrentStepPosition);
144+
}
145+
146+
}
147+
128148
private ViewPager mPager;
129149

130150
private Button mBackNavigationButton;
@@ -143,9 +163,9 @@ public final void goToNextStep() {
143163

144164
private ColorStateList mBackButtonColor;
145165

146-
private ColorStateList mNextButtonColor;
166+
private ColorStateList mNextButtonColor;
147167

148-
private ColorStateList mCompleteButtonColor;
168+
private ColorStateList mCompleteButtonColor;
149169

150170
@ColorInt
151171
private int mUnselectedColor;
@@ -226,6 +246,7 @@ public void setListener(@NonNull StepperListener listener) {
226246

227247
/**
228248
* Sets the new step adapter and updates the stepper layout based on the new adapter.
249+
*
229250
* @param stepAdapter step adapter
230251
*/
231252
public void setAdapter(@NonNull AbstractStepAdapter stepAdapter) {
@@ -246,7 +267,8 @@ public void run() {
246267

247268
/**
248269
* Sets the new step adapter and updates the stepper layout based on the new adapter.
249-
* @param stepAdapter step adapter
270+
*
271+
* @param stepAdapter step adapter
250272
* @param currentStepPosition the initial step position, must be in the range of the adapter item count
251273
*/
252274
public void setAdapter(@NonNull AbstractStepAdapter stepAdapter, @IntRange(from = 0) int currentStepPosition) {
@@ -256,6 +278,7 @@ public void setAdapter(@NonNull AbstractStepAdapter stepAdapter, @IntRange(from
256278

257279
/**
258280
* Overrides the default page transformer used in the underlying {@link ViewPager}
281+
*
259282
* @param pageTransformer new page transformer
260283
*/
261284
public void setPageTransformer(@Nullable ViewPager.PageTransformer pageTransformer) {
@@ -277,9 +300,9 @@ public int getTabStepDividerWidth() {
277300
@Override
278301
@UiThread
279302
public void onTabClicked(int position) {
280-
if (position > mCurrentStepPosition){
303+
if (position > mCurrentStepPosition) {
281304
onNext();
282-
} else if (position < mCurrentStepPosition){
305+
} else if (position < mCurrentStepPosition) {
283306
setCurrentStepPosition(position);
284307
}
285308
}
@@ -455,14 +478,14 @@ private Step findCurrentStep() {
455478
}
456479

457480
private void onPrevious() {
458-
if (mCurrentStepPosition <= 0) {
459-
if (mShowBackButtonOnFirstStep) {
460-
mListener.onReturn();
461-
}
462-
return;
481+
Step step = findCurrentStep();
482+
483+
OnBackClickedCallback onBackClickedCallback = new OnBackClickedCallback();
484+
if (step instanceof BlockingStep) {
485+
((BlockingStep) step).onBackClicked(onBackClickedCallback);
486+
} else {
487+
onBackClickedCallback.goToPrevStep();
463488
}
464-
mCurrentStepPosition--;
465-
onUpdate(mCurrentStepPosition);
466489
}
467490

468491
@UiThread
@@ -474,7 +497,7 @@ private void onNext() {
474497
}
475498
OnNextClickedCallback onNextClickedCallback = new OnNextClickedCallback();
476499
if (step instanceof BlockingStep) {
477-
((BlockingStep)step).onNextClicked(onNextClickedCallback);
500+
((BlockingStep) step).onNextClicked(onNextClickedCallback);
478501
} else {
479502
onNextClickedCallback.goToNextStep();
480503
}

sample/src/main/java/com/stepstone/stepper/sample/step/DelayedTransitionStepFragmentSample.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import android.view.View;
3030
import android.view.ViewGroup;
3131
import android.widget.Button;
32+
import android.widget.Toast;
3233

3334
import com.stepstone.stepper.BlockingStep;
3435
import com.stepstone.stepper.StepperLayout;
@@ -116,6 +117,13 @@ public void run() {
116117
}, 2000L);
117118
}
118119

120+
@Override
121+
@UiThread
122+
public void onBackClicked(StepperLayout.OnBackClickedCallback callback) {
123+
Toast.makeText(this.getContext(), "Your custom back action. Here you should cancel currently running operations", Toast.LENGTH_SHORT).show();
124+
callback.goToPrevStep();
125+
}
126+
119127
@Override
120128
public void onSaveInstanceState(Bundle outState) {
121129
outState.putInt(CLICKS_KEY, i);

0 commit comments

Comments
 (0)