Skip to content

Commit 3eb62cd

Browse files
authored
Merge branch 'master' into error_back
2 parents 93b4108 + 4d392db commit 3eb62cd

27 files changed

+359
-134
lines changed

README.md

Lines changed: 47 additions & 28 deletions
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.1'
14+
compile 'com.stepstone.stepper:material-stepper:2.0.0'
1515
```
1616

1717
## Supported steppers
@@ -62,13 +62,6 @@ public class StepFragmentSample extends Fragment implements Step {
6262
return v;
6363
}
6464

65-
@Override
66-
@StringRes
67-
public int getName() {
68-
//return string resource ID for the tab title used when StepperLayout is in tabs mode
69-
return R.string.tab_title;
70-
}
71-
7265
@Override
7366
public VerificationError verifyStep() {
7467
//return null if the user can go to the next step, create a new VerificationError instance otherwise
@@ -88,15 +81,15 @@ public class StepFragmentSample extends Fragment implements Step {
8881
}
8982
```
9083

91-
### Extend AbstractStepAdapter
92-
AbstractStepAdapter extends [FragmentPagerAdapter](http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html)
84+
### Extend AbstractFragmentStepAdapter
85+
AbstractFragmentStepAdapter extends [FragmentPagerAdapter](http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html)
9386
but instead of overriding the method `getItem(int)` you must override the `createStep(int)` method.
9487

9588
```java
96-
public static class MyStepperAdapter extends AbstractStepAdapter {
89+
public static class MyStepperAdapter extends AbstractFragmentStepAdapter {
9790

98-
public MyStepperAdapter(FragmentManager fm) {
99-
super(fm);
91+
public MyStepperAdapter(FragmentManager fm, Context context) {
92+
super(fm, context);
10093
}
10194

10295
@Override
@@ -112,6 +105,15 @@ public static class MyStepperAdapter extends AbstractStepAdapter {
112105
public int getCount() {
113106
return 3;
114107
}
108+
109+
@NonNull
110+
@Override
111+
public StepViewModel getViewModel(@IntRange(from = 0) int position) {
112+
//Override this method to set Step title for the Tabs, not necessary for other stepper types
113+
return new StepViewModel.Builder(context)
114+
.setTitle(R.string.tab_title) //can be a CharSequence instead
115+
.create();
116+
}
115117
}
116118

117119
```
@@ -128,7 +130,7 @@ public class StepperActivity extends AppCompatActivity {
128130
super.onCreate(savedInstanceState);
129131
setContentView(R.layout.main);
130132
mStepperLayout = (StepperLayout) findViewById(R.id.stepperLayout);
131-
mStepperLayout.setAdapter(new MyStepperAdapter(getSupportFragmentManager()));
133+
mStepperLayout.setAdapter(new MyStepperAdapter(getSupportFragmentManager(), this));
132134
}
133135
}
134136
```
@@ -238,24 +240,36 @@ public class DelayedTransitionStepFragmentSample extends Fragment implements Blo
238240
}
239241
```
240242

241-
### Changing the Next button text per step
242-
Sometimes you might want to have different labels on the Next button on different steps e.g. use the default label on the first few steps,
243+
### Changing Back/Next button labels per step
244+
Sometimes you might want to have different labels on the Next and/or Back navigation buttons on different steps e.g. use the default labels on the first few steps,
243245
but display 'Summary' just before the last page.
244246
<p><img src ="./gifs/different-next-buttons.gif" width="360" height="640"/></p>
245-
In such case you need to override the `getNextButtonText(int)` method in the `AbstractStepAdapter` e.g.
247+
In such case you need to override the `getViewModel(int)` method from the `StepAdapter` e.g.
246248
```java
247-
@StringRes
248-
@Override
249-
public int getNextButtonText(int position) {
250-
switch (position) {
251-
case 0:
252-
return R.string.ms_next;
253-
case 1:
254-
return R.string.go_to_summary;
255-
default:
256-
throw new IllegalArgumentException("Unsupported position: " + position);
257-
}
249+
@NonNull
250+
@Override
251+
public StepViewModel getViewModel(@IntRange(from = 0) int position) {
252+
StepViewModel.Builder builder = new StepViewModel.Builder(context)
253+
.setTitle(R.string.tab_title);
254+
switch (position) {
255+
case 0:
256+
builder
257+
.setNextButtonLabel("This way")
258+
.setBackButtonLabel("Go to first");
259+
break;
260+
case 1:
261+
builder
262+
.setNextButtonLabel(R.string.go_to_summary)
263+
.setBackButtonLabel("Go to first");
264+
break;
265+
case 2:
266+
builder.setBackButtonLabel("Go back");
267+
break;
268+
default:
269+
throw new IllegalArgumentException("Unsupported position: " + position);
258270
}
271+
return builder.create();
272+
}
259273
```
260274

261275
### Using the same stepper styling across the application
@@ -293,6 +307,11 @@ This behaviour can be changed by setting ```ms_showBackButtonOnFirstStep``` to `
293307
```
294308
To get a callback when this button was pressed you need set a ```StepperListener``` and write your own custom return logic in the ```onReturn()``` method to e.g. close the Activity.
295309

310+
### Using with Views instead of Fragments
311+
It is possible to use this library without the need to rely on Fragments.
312+
To do so you need to use ```AbstractStepAdapter``` instead of ```AbstractFragmentStepAdapter```.
313+
For an example of how to use it with views please see the sample app.
314+
296315
### Advanced usage
297316
For other examples, e.g. persisting state on rotation, displaying errors, changing whether the user can go to the next step, etc. check out the sample app.
298317

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ configure(allprojects) {
2020
/* Android config and dependency versions */
2121
ext {
2222
androidMinSdkVersion = 14
23-
androidTargetSdkVersion = 24
24-
androidCompileSdkVersion = 24
23+
androidTargetSdkVersion = 25
24+
androidCompileSdkVersion = 25
2525
androidBuildToolsVersion = "25.0.2"
26-
androidSupportLibraryVersion = "25.0.0"
26+
androidSupportLibraryVersion = "25.1.0"
2727

2828
/* Sample only */
2929
butterknifeVersion = "7.0.1"

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.1
22+
POM_VERSION=2.0.0

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,12 @@
1717
package com.stepstone.stepper;
1818

1919
import android.support.annotation.NonNull;
20-
import android.support.annotation.StringRes;
2120

2221
/**
2322
* A base step interface which all {@link StepperLayout} steps must implement.
2423
*/
2524
public interface Step {
2625

27-
/**
28-
* Returns a String resource ID for the displayable name of the step.
29-
* @return a String resource ID for the displayable name of the step
30-
*/
31-
@StringRes int getName();
32-
3326
/**
3427
* Checks if the stepper can go to the next step after this step.<br>
3528
* <b>This does not mean the user clicked on the Next/Complete button.</b><br>

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

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.stepstone.stepper.type.TabsStepperType;
5151
import com.stepstone.stepper.util.AnimationUtil;
5252
import com.stepstone.stepper.util.TintUtil;
53+
import com.stepstone.stepper.viewmodel.StepViewModel;
5354

5455
/**
5556
* Stepper widget implemented according to the <a href="https://www.google.com/design/spec/components/steppers.html">Material documentation</a>.<br>
@@ -338,6 +339,7 @@ public void setCompleteButtonVerificationFailed(boolean verificationFailed) {
338339
}
339340

340341
/**
342+
341343
* Set whether when going backwards should clear the error state from the Tab. Default is false.
342344
* @param mShowErrorStateOnBack
343345
*/
@@ -351,6 +353,17 @@ public void setShowErrorStateOnBack(boolean mShowErrorStateOnBack) {
351353
*/
352354
public void setShowErrorState(boolean mShowErrorState) {
353355
this.mShowErrorState = mShowErrorState;
356+
357+
/**
358+
* Set the number of steps that should be retained to either side of the
359+
* current step in the view hierarchy in an idle state. Steps beyond this
360+
* limit will be recreated from the adapter when needed.
361+
*
362+
* @param limit How many steps will be kept offscreen in an idle state.
363+
* @see ViewPager#setOffscreenPageLimit(int)
364+
*/
365+
public void setOffscreenPageLimit(int limit) {
366+
mPager.setOffscreenPageLimit(limit);
354367
}
355368

356369
private void init(AttributeSet attrs, @AttrRes int defStyleAttr) {
@@ -576,19 +589,18 @@ private void onComplete(View completeButton) {
576589

577590
private void onUpdate(int newStepPosition, boolean animate) {
578591
mPager.setCurrentItem(newStepPosition);
579-
boolean isLast = isLastPosition(newStepPosition);
580-
boolean isFirst = newStepPosition == 0;
592+
final boolean isLast = isLastPosition(newStepPosition);
593+
final boolean isFirst = newStepPosition == 0;
581594
AnimationUtil.fadeViewVisibility(mNextNavigationButton, isLast ? View.GONE : View.VISIBLE, animate);
582595
AnimationUtil.fadeViewVisibility(mCompleteNavigationButton, !isLast ? View.GONE : View.VISIBLE, animate);
583596
AnimationUtil.fadeViewVisibility(mBackNavigationButton, isFirst && !mShowBackButtonOnFirstStep ? View.GONE : View.VISIBLE, animate);
584597

598+
final StepViewModel viewModel = mStepAdapter.getViewModel(newStepPosition);
599+
600+
updateBackButtonText(viewModel);
601+
585602
if (!isLast) {
586-
int nextButtonTextForStep = mStepAdapter.getNextButtonText(newStepPosition);
587-
if (nextButtonTextForStep == StepAdapter.DEFAULT_NEXT_BUTTON_TEXT) {
588-
mNextNavigationButton.setText(mNextButtonText);
589-
} else {
590-
mNextNavigationButton.setText(nextButtonTextForStep);
591-
}
603+
updateNextButtonText(viewModel);
592604
}
593605

594606
//needs to be here in case user for any reason decide to change whether or not to show errors when going back.
@@ -603,4 +615,22 @@ private void onUpdate(int newStepPosition, boolean animate) {
603615
step.onSelected();
604616
}
605617
}
618+
619+
private void updateNextButtonText(@NonNull StepViewModel viewModel) {
620+
CharSequence nextButtonTextForStep = viewModel.getNextButtonLabel();
621+
if (nextButtonTextForStep == null) {
622+
mNextNavigationButton.setText(mNextButtonText);
623+
} else {
624+
mNextNavigationButton.setText(nextButtonTextForStep);
625+
}
626+
}
627+
628+
private void updateBackButtonText(@NonNull StepViewModel viewModel) {
629+
CharSequence backButtonTextForStep = viewModel.getBackButtonLabel();
630+
if (backButtonTextForStep == null) {
631+
mBackNavigationButton.setText(mBackButtonText);
632+
} else {
633+
mBackNavigationButton.setText(backButtonTextForStep);
634+
}
635+
}
606636
}

material-stepper/src/main/java/com/stepstone/stepper/adapter/AbstractFragmentStepAdapter.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@
1616

1717
package com.stepstone.stepper.adapter;
1818

19-
import android.support.annotation.StringRes;
19+
import android.content.Context;
20+
import android.support.annotation.IntRange;
21+
import android.support.annotation.NonNull;
2022
import android.support.v4.app.Fragment;
2123
import android.support.v4.app.FragmentManager;
2224
import android.support.v4.app.FragmentPagerAdapter;
2325
import android.support.v4.view.PagerAdapter;
2426
import android.support.v4.view.ViewPager;
2527

2628
import com.stepstone.stepper.Step;
29+
import com.stepstone.stepper.viewmodel.StepViewModel;
2730

2831
/**
2932
* A base adapter class which returns step fragments to use inside of the {@link com.stepstone.stepper.StepperLayout}.
@@ -32,29 +35,35 @@ public abstract class AbstractFragmentStepAdapter
3235
extends FragmentPagerAdapter
3336
implements StepAdapter {
3437

38+
@NonNull
3539
private final FragmentManager mFragmentManager;
3640

37-
public AbstractFragmentStepAdapter(FragmentManager fm) {
41+
@NonNull
42+
protected final Context context;
43+
44+
public AbstractFragmentStepAdapter(@NonNull FragmentManager fm, @NonNull Context context) {
3845
super(fm);
39-
mFragmentManager = fm;
46+
this.mFragmentManager = fm;
47+
this.context = context;
4048
}
4149

4250
@Override
43-
public final Fragment getItem(int position) {
51+
public final Fragment getItem(@IntRange(from = 0) int position) {
4452
return (Fragment) createStep(position);
4553
}
4654

4755
/** {@inheritDoc} */
4856
@SuppressWarnings("unchecked")
49-
public Step findStep(ViewPager viewPager, int position) {
57+
public Step findStep(ViewPager viewPager, @IntRange(from = 0) int position) {
5058
String fragmentTag = "android:switcher:" + viewPager.getId() + ":" + this.getItemId(position);
5159
return (Step) mFragmentManager.findFragmentByTag(fragmentTag);
5260
}
5361

5462
/** {@inheritDoc} */
55-
@StringRes
56-
public int getNextButtonText(int position) {
57-
return DEFAULT_NEXT_BUTTON_TEXT;
63+
@NonNull
64+
@Override
65+
public StepViewModel getViewModel(@IntRange(from = 0) int position) {
66+
return new StepViewModel.Builder(context).create();
5867
}
5968

6069
/** {@inheritDoc} */

material-stepper/src/main/java/com/stepstone/stepper/adapter/AbstractStepAdapter.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
11
package com.stepstone.stepper.adapter;
22

3+
import android.content.Context;
4+
import android.support.annotation.IntRange;
5+
import android.support.annotation.NonNull;
36
import android.support.v4.view.PagerAdapter;
47

8+
import com.stepstone.stepper.viewmodel.StepViewModel;
9+
510
/**
611
* A base adapter class which returns step to use inside of the {@link com.stepstone.stepper.StepperLayout}.
712
* This class is intended to be inherited if you need to use {@link com.stepstone.stepper.StepperLayout} without fragments.
813
* Otherwise, you should use {@link AbstractFragmentStepAdapter}
914
*/
10-
public abstract class AbstractStepAdapter extends PagerAdapter implements StepAdapter {
15+
public abstract class AbstractStepAdapter
16+
extends PagerAdapter
17+
implements StepAdapter {
18+
19+
@NonNull
20+
protected final Context context;
21+
22+
public AbstractStepAdapter(@NonNull Context context) {
23+
this.context = context;
24+
}
1125

1226
/** {@inheritDoc} */
27+
@NonNull
1328
@Override
14-
public int getNextButtonText(int position) {
15-
return DEFAULT_NEXT_BUTTON_TEXT;
29+
public StepViewModel getViewModel(@IntRange(from = 0) int position) {
30+
return new StepViewModel.Builder(context).create();
1631
}
1732

1833
/** {@inheritDoc} */

0 commit comments

Comments
 (0)