Skip to content

Commit 4158546

Browse files
Piotr Zawadzkizawadz88
authored andcommitted
Added an option to set Complete button text programmatically via StepViewModel
1 parent d0dd268 commit 4158546

File tree

7 files changed

+359
-7
lines changed

7 files changed

+359
-7
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public class DelayedTransitionStepFragmentSample extends Fragment implements Blo
257257
```
258258

259259
### Changing button labels & compound drawables per step
260-
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,
260+
Sometimes you might want to have different labels on the Next/Complete and/or Back navigation buttons on different steps e.g. use the default labels on the first few steps,
261261
but display 'Summary' just before the last page.
262262
You might also want to use your custom icons instead of the default navigation button compound drawables or not show the compound drawables for some of the buttons.
263263
<p><img src ="./gifs/custom-navigation-buttons.gif" width="360" /></p>
@@ -284,7 +284,9 @@ In such case you need to override the `getViewModel(int)` method from the `StepA
284284
.setBackButtonStartDrawableResId(R.drawable.ms_back_arrow);
285285
break;
286286
case 2:
287-
builder.setBackButtonLabel("Go back");
287+
builder
288+
.setBackButtonLabel("Go back")
289+
.setCompleteButtonLabel("I'm done!");
288290
break;
289291
default:
290292
throw new IllegalArgumentException("Unsupported position: " + position);
@@ -293,8 +295,6 @@ In such case you need to override the `getViewModel(int)` method from the `StepA
293295
}
294296
```
295297

296-
NOTE: To change Complete button's label you need use `ms_completeButtonText` attribute from StepperLayout.
297-
298298
### Custom styling
299299
Basic styling can be done by choosing the active and inactive step colors.
300300
There are some additional properties which can be changed directly from StepperLayout's attributes e.g. the background of bottom navigation buttons (see [StepperLayout attributes](#stepperlayout-attributes))

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
buildscript {
2-
ext.gradleAndroidVersion = '2.3.2'
3-
ext.kotlinVersion = '1.1.2-3'
2+
ext.gradleAndroidVersion = '2.3.3'
3+
ext.kotlinVersion = '1.1.3-2'
44
ext.bintrayVersion = '1.4'
55
ext.mavenGradlePluginVersion = '1.4.1'
66

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,8 @@ private void onUpdate(int newStepPosition, boolean userTriggeredChange) {
896896

897897
if (!isLast) {
898898
updateNextButton(viewModel);
899+
} else {
900+
updateCompleteButton(viewModel);
899901
}
900902

901903
setCompoundDrawablesForNavigationButtons(viewModel.getBackButtonStartDrawableResId(), viewModel.getNextButtonEndDrawableResId());
@@ -908,6 +910,15 @@ private void onUpdate(int newStepPosition, boolean userTriggeredChange) {
908910
}
909911
}
910912

913+
private void updateCompleteButton(@NonNull StepViewModel viewModel) {
914+
CharSequence completeButtonTextForStep = viewModel.getCompleteButtonLabel();
915+
if (completeButtonTextForStep == null) {
916+
mCompleteNavigationButton.setText(mCompleteButtonText);
917+
} else {
918+
mCompleteNavigationButton.setText(completeButtonTextForStep);
919+
}
920+
}
921+
911922
private void updateNextButton(@NonNull StepViewModel viewModel) {
912923
CharSequence nextButtonTextForStep = viewModel.getNextButtonLabel();
913924
if (nextButtonTextForStep == null) {

material-stepper/src/main/java/com/stepstone/stepper/viewmodel/StepViewModel.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ private StepViewModel() {}
4141
@Nullable
4242
private CharSequence mNextButtonLabel;
4343

44+
/**
45+
* Allows to override the text on the Complete button for this step.
46+
* This step has to be the last step for this to have any effect.
47+
* To do so you need to return a non-null String of the label.
48+
* By default this is {@code null}.
49+
*/
50+
@Nullable
51+
private CharSequence mCompleteButtonLabel;
52+
4453
/**
4554
* Allows to override the text on the Back button for this step.
4655
* To do so you need to return a non-null String of the label.
@@ -75,6 +84,11 @@ public CharSequence getNextButtonLabel() {
7584
return mNextButtonLabel;
7685
}
7786

87+
@Nullable
88+
public CharSequence getCompleteButtonLabel() {
89+
return mCompleteButtonLabel;
90+
}
91+
7892
@Nullable
7993
public CharSequence getBackButtonLabel() {
8094
return mBackButtonLabel;
@@ -101,6 +115,9 @@ public static class Builder {
101115
@Nullable
102116
private CharSequence mNextButtonLabel;
103117

118+
@Nullable
119+
private CharSequence mCompleteButtonLabel;
120+
104121
@Nullable
105122
private CharSequence mBackButtonLabel;
106123

@@ -163,6 +180,28 @@ public Builder setNextButtonLabel(@Nullable CharSequence nextButtonLabel) {
163180
return this;
164181
}
165182

183+
/**
184+
* Set the label of the complete button using the given resource id.
185+
*
186+
* @param completeButtonLabelId string resource ID for the Complete button
187+
* @return This Builder object to allow for chaining of calls to set methods
188+
*/
189+
public Builder setCompleteButtonLabel(@StringRes int completeButtonLabelId) {
190+
this.mCompleteButtonLabel = mContext.getString(completeButtonLabelId);
191+
return this;
192+
}
193+
194+
/**
195+
* Set the label of the complete button.
196+
*
197+
* @param completeButtonLabel CharSequence to be used as a Complete button label
198+
* @return This Builder object to allow for chaining of calls to set methods
199+
*/
200+
public Builder setCompleteButtonLabel(@Nullable CharSequence completeButtonLabel) {
201+
this.mCompleteButtonLabel = completeButtonLabel;
202+
return this;
203+
}
204+
166205
/**
167206
* Set the label of the back button using the given resource id.
168207
*
@@ -217,6 +256,7 @@ public StepViewModel create() {
217256
viewModel.mTitle = this.mTitle;
218257
viewModel.mBackButtonLabel = this.mBackButtonLabel;
219258
viewModel.mNextButtonLabel = this.mNextButtonLabel;
259+
viewModel.mCompleteButtonLabel = this.mCompleteButtonLabel;
220260
viewModel.mNextButtonEndDrawableResId = this.mNextButtonEndDrawableResId;
221261
viewModel.mBackButtonStartDrawableResId = this.mBackButtonStartDrawableResId;
222262
return viewModel;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.stepstone.stepper.test.assertion
2+
3+
import com.stepstone.stepper.viewmodel.StepViewModel
4+
import org.assertj.core.api.AbstractAssert
5+
import org.junit.Assert.assertEquals
6+
7+
/**
8+
* @author Piotr Zawadzki
9+
*/
10+
class StepViewModelAssert constructor(actual: StepViewModel) : AbstractAssert<StepViewModelAssert, StepViewModel>(actual, StepViewModelAssert::class.java) {
11+
12+
companion object {
13+
14+
fun assertThat(actual: StepViewModel): StepViewModelAssert {
15+
return StepViewModelAssert(actual)
16+
}
17+
18+
}
19+
20+
fun hasTitle(title: CharSequence?): StepViewModelAssert {
21+
assertEquals("Incorrect title!", title, actual.title)
22+
return this
23+
}
24+
25+
fun hasNextButtonLabel(nextButtonLabel: CharSequence?): StepViewModelAssert {
26+
assertEquals("Incorrect label for the Next button!", nextButtonLabel, actual.nextButtonLabel)
27+
return this
28+
}
29+
30+
fun hasCompleteButtonLabel(completeButtonLabel: CharSequence?): StepViewModelAssert {
31+
assertEquals("Incorrect label for the Complete button!", completeButtonLabel, actual.completeButtonLabel)
32+
return this
33+
}
34+
35+
fun hasBackButtonLabel(backButtonLabel: CharSequence?): StepViewModelAssert {
36+
assertEquals("Incorrect label for the Back button!", backButtonLabel, actual.backButtonLabel)
37+
return this
38+
}
39+
40+
fun hasNextButtonEndDrawableResId(nextButtonEndDrawableResId: Int): StepViewModelAssert {
41+
assertEquals("Incorrect drawable resource id for the Next button!", nextButtonEndDrawableResId, actual.nextButtonEndDrawableResId)
42+
return this
43+
}
44+
45+
fun hasBackButtonStartDrawableResId(backButtonStartDrawableResId: Int): StepViewModelAssert {
46+
assertEquals("Incorrect drawable resource id for the Back button!", backButtonStartDrawableResId, actual.backButtonStartDrawableResId)
47+
return this
48+
}
49+
50+
}

0 commit comments

Comments
 (0)