-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathStepperLayout.java
More file actions
1107 lines (910 loc) · 40.2 KB
/
Copy pathStepperLayout.java
File metadata and controls
1107 lines (910 loc) · 40.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2016 StepStone Services
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.stepstone.stepper;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.AttrRes;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.FloatRange;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.support.annotation.UiThread;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.view.ContextThemeWrapper;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.stepstone.stepper.adapter.StepAdapter;
import com.stepstone.stepper.internal.feedback.StepperFeedbackType;
import com.stepstone.stepper.internal.feedback.StepperFeedbackTypeFactory;
import com.stepstone.stepper.internal.type.AbstractStepperType;
import com.stepstone.stepper.internal.type.StepperTypeFactory;
import com.stepstone.stepper.internal.util.AnimationUtil;
import com.stepstone.stepper.internal.util.TintUtil;
import com.stepstone.stepper.internal.widget.ColorableProgressBar;
import com.stepstone.stepper.internal.widget.DottedProgressBar;
import com.stepstone.stepper.internal.widget.RightNavigationButton;
import com.stepstone.stepper.internal.widget.TabsContainer;
import com.stepstone.stepper.viewmodel.StepViewModel;
/**
* Stepper widget implemented according to the <a href="https://www.google.com/design/spec/components/steppers.html">Material documentation</a>.<br>
* It allows for setting three types of steppers:<br>
* - mobile dots stepper,<br>
* - mobile progress bar stepper,<br>
* - horizontal stepper with tabs.<br>
* Include this stepper in the layout XML file and choose a stepper type with <code>ms_stepperType</code>.<br>
* Check out <code>values/attrs.xml - StepperLayout</code> for a complete list of customisable properties.
*/
public class StepperLayout extends LinearLayout implements TabsContainer.TabItemListener {
public static final int DEFAULT_TAB_DIVIDER_WIDTH = -1;
/**
* A listener for events of {@link StepperLayout}.
*/
public interface StepperListener {
/**
* Called when all of the steps were completed successfully
*
* @param completeButton the complete button that was clicked to complete the flow
*/
void onCompleted(View completeButton);
/**
* Called when a verification error occurs for one of the steps
*
* @param verificationError verification error
*/
void onError(VerificationError verificationError);
/**
* Called when the current step position changes
*
* @param newStepPosition new step position
*/
void onStepSelected(int newStepPosition);
/**
* Called when the Previous step button was pressed while on the first step
* (the button is not present by default on first step).
*/
void onReturn();
StepperListener NULL = new StepperListener() {
@Override
public void onCompleted(View completeButton) {
}
@Override
public void onError(VerificationError verificationError) {
}
@Override
public void onStepSelected(int newStepPosition) {
}
@Override
public void onReturn() {
}
};
}
public abstract class AbstractOnButtonClickedCallback {
public StepperLayout getStepperLayout() {
return StepperLayout.this;
}
}
public class OnNextClickedCallback extends AbstractOnButtonClickedCallback {
@UiThread
public void goToNextStep() {
final int totalStepCount = mStepAdapter.getCount();
if (mCurrentStepPosition >= totalStepCount - 1) {
return;
}
mCurrentStepPosition++;
onUpdate(mCurrentStepPosition, true);
}
}
public class OnCompleteClickedCallback extends AbstractOnButtonClickedCallback {
@UiThread
public void complete() {
invalidateCurrentPosition();
mListener.onCompleted(mCompleteNavigationButton);
}
}
public class OnBackClickedCallback extends AbstractOnButtonClickedCallback {
@UiThread
public void goToPrevStep() {
if (mCurrentStepPosition <= 0) {
if (mShowBackButtonOnFirstStep) {
mListener.onReturn();
}
return;
}
mCurrentStepPosition--;
onUpdate(mCurrentStepPosition, true);
}
}
private class OnBackClickListener implements OnClickListener {
@Override
public void onClick(View v) {
onBackClicked();
}
}
private class OnNextClickListener implements OnClickListener {
@Override
public void onClick(View v) {
onNext();
}
}
private class OnCompleteClickListener implements OnClickListener {
@Override
public void onClick(View v) {
onComplete();
}
}
private ViewPager mPager;
private Button mBackNavigationButton;
private RightNavigationButton mNextNavigationButton;
private RightNavigationButton mCompleteNavigationButton;
private ViewGroup mStepNavigation;
private DottedProgressBar mDottedProgressBar;
private ColorableProgressBar mProgressBar;
private TabsContainer mTabsContainer;
private ColorStateList mBackButtonColor;
private ColorStateList mNextButtonColor;
private ColorStateList mCompleteButtonColor;
@ColorInt
private int mUnselectedColor;
@ColorInt
private int mSelectedColor;
@ColorInt
private int mErrorColor;
@DrawableRes
private int mBottomNavigationBackground;
@DrawableRes
private int mBackButtonBackground;
@DrawableRes
private int mNextButtonBackground;
@DrawableRes
private int mCompleteButtonBackground;
private int mTabStepDividerWidth = DEFAULT_TAB_DIVIDER_WIDTH;
private String mBackButtonText;
private String mNextButtonText;
private String mCompleteButtonText;
private boolean mShowBackButtonOnFirstStep;
private boolean mShowBottomNavigation;
private int mTypeIdentifier = AbstractStepperType.PROGRESS_BAR;
private int mFeedbackTypeMask = StepperFeedbackType.NONE;
private StepAdapter mStepAdapter;
private AbstractStepperType mStepperType;
private StepperFeedbackType mStepperFeedbackType;
@FloatRange(from = 0.0f, to = 1.0f)
private float mContentFadeAlpha = AnimationUtil.ALPHA_HALF;
@DrawableRes
private int mContentOverlayBackground;
private int mCurrentStepPosition;
private boolean mShowErrorStateEnabled;
private boolean mShowErrorStateOnBackEnabled;
private boolean mShowErrorMessageEnabled;
private boolean mTabNavigationEnabled;
private boolean mInProgress;
@StyleRes
private int mStepperLayoutTheme;
@NonNull
private StepperListener mListener = StepperListener.NULL;
public StepperLayout(Context context) {
this(context, null);
}
public StepperLayout(Context context, AttributeSet attrs) {
super(context, attrs);
//Fix for issue #60 with AS Preview editor
init(attrs, isInEditMode() ? 0 : R.attr.ms_stepperStyle);
}
public StepperLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs, defStyleAttr);
}
@Override
public final void setOrientation(@LinearLayoutCompat.OrientationMode int orientation) {
//only vertical orientation is supported
super.setOrientation(VERTICAL);
}
public void setListener(@NonNull StepperListener listener) {
this.mListener = listener;
}
/**
* Getter for mStepAdapter
*
* @return mStepAdapter
*/
public StepAdapter getAdapter() {
return mStepAdapter;
}
/**
* Sets the new step adapter and updates the stepper layout based on the new adapter.
*
* @param stepAdapter step adapter
*/
public void setAdapter(@NonNull StepAdapter stepAdapter) {
this.mStepAdapter = stepAdapter;
mPager.setAdapter(stepAdapter.getPagerAdapter());
mStepperType.onNewAdapter(stepAdapter);
// this is so that the fragments in the adapter can be created BEFORE the onUpdate() method call
mPager.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//noinspection deprecation
mPager.getViewTreeObserver().removeGlobalOnLayoutListener(this);
onUpdate(mCurrentStepPosition, false);
}
});
}
/**
* Sets the new step adapter and updates the stepper layout based on the new adapter.
*
* @param stepAdapter step adapter
* @param currentStepPosition the initial step position, must be in the range of the adapter item count
*/
public void setAdapter(@NonNull StepAdapter stepAdapter, @IntRange(from = 0) int currentStepPosition) {
this.mCurrentStepPosition = currentStepPosition;
setAdapter(stepAdapter);
}
/**
* Overrides the default page transformer used in the underlying {@link com.stepstone.stepper.internal.widget.StepViewPager}.
* If you're supporting RTL make sure your {@link android.support.v4.view.ViewPager.PageTransformer} accounts for it.
*
* @param pageTransformer new page transformer
* @see com.stepstone.stepper.internal.widget.StepViewPager
* @see com.stepstone.stepper.internal.widget.pagetransformer.StepPageTransformerFactory
*/
public void setPageTransformer(@Nullable ViewPager.PageTransformer pageTransformer) {
mPager.setPageTransformer(false, pageTransformer);
}
public int getSelectedColor() {
return mSelectedColor;
}
public int getUnselectedColor() {
return mUnselectedColor;
}
public int getErrorColor() {
return mErrorColor;
}
public int getTabStepDividerWidth() {
return mTabStepDividerWidth;
}
@Override
@UiThread
public void onTabClicked(int position) {
if (mTabNavigationEnabled) {
if (position > mCurrentStepPosition) {
onNext();
} else if (position < mCurrentStepPosition) {
setCurrentStepPosition(position);
}
}
}
/**
* This is an equivalent of clicking the Next/Complete button from the bottom navigation.
* Unlike {@link #setCurrentStepPosition(int)} this actually verifies the step.
*/
public void proceed() {
if (isLastPosition(mCurrentStepPosition)) {
onComplete();
} else {
onNext();
}
}
/**
* To be called when the user wants to go to the previous step.
*/
public void onBackClicked() {
Step step = findCurrentStep();
updateErrorFlagWhenGoingBack();
OnBackClickedCallback onBackClickedCallback = new OnBackClickedCallback();
if (step instanceof BlockingStep) {
((BlockingStep) step).onBackClicked(onBackClickedCallback);
} else {
onBackClickedCallback.goToPrevStep();
}
}
/**
* Sets the current step to the one at the provided index.
* This does not verify the current step.
*
* @param currentStepPosition new current step position
*/
public void setCurrentStepPosition(int currentStepPosition) {
int previousStepPosition = mCurrentStepPosition;
if (currentStepPosition < previousStepPosition) {
updateErrorFlagWhenGoingBack();
}
mCurrentStepPosition = currentStepPosition;
onUpdate(currentStepPosition, true);
}
/**
* Returns the position of the currently selected step.
*
* @return position of the currently selected step
*/
public int getCurrentStepPosition() {
return mCurrentStepPosition;
}
/**
* Sets whether the Next button in the bottom navigation bar should be in the
* 'verification failed' state i.e. still clickable but with an option to display it
* differently to indicate to the user that he cannot go to the next step yet.
*
* @param verificationFailed false if verification failed, true otherwise
*/
public void setNextButtonVerificationFailed(boolean verificationFailed) {
mNextNavigationButton.setVerificationFailed(verificationFailed);
}
/**
* Sets whether the Complete button in the bottom navigation bar should be in the
* 'verification failed' state i.e. still clickable but with an option to display it
* differently to indicate to the user that he cannot finish the process yet.
*
* @param verificationFailed false if verification failed, true otherwise
*/
public void setCompleteButtonVerificationFailed(boolean verificationFailed) {
mCompleteNavigationButton.setVerificationFailed(verificationFailed);
}
/**
* Sets whether the Next button in the bottom navigation bar should be enabled (clickable).
* setting this to <i>false</i> will make it unclickable.
*
* @param enabled true if the button should be clickable, false otherwise
*/
public void setNextButtonEnabled(boolean enabled) {
mNextNavigationButton.setEnabled(enabled);
}
/**
* Sets whether the Complete button in the bottom navigation bar should be enabled (clickable).
* setting this to <i>false</i> will make it unclickable.
*
* @param enabled true if the button should be clickable, false otherwise
*/
public void setCompleteButtonEnabled(boolean enabled) {
mCompleteNavigationButton.setEnabled(enabled);
}
/**
* Sets whether the Back button in the bottom navigation bar should be enabled (clickable).
* setting this to <i>false</i> will make it unclickable.
*
* @param enabled true if the button should be clickable, false otherwise
*/
public void setBackButtonEnabled(boolean enabled) {
mBackNavigationButton.setEnabled(enabled);
}
/**
* Set whether the bottom navigation bar (with Back/Next/Complete buttons) should be visible.
* <i>true</i> by default.
*
* @param showBottomNavigation true if bottom navigation should be visible, false otherwise
*/
public void setShowBottomNavigation(boolean showBottomNavigation) {
mStepNavigation.setVisibility(showBottomNavigation ? View.VISIBLE : View.GONE);
}
/**
* Set whether when going backwards should clear the error state from the Tab. Default is <code>false</code>.
*
* @param showErrorStateOnBack true if navigating backwards should keep the error state, false otherwise
* @deprecated see {@link #setShowErrorStateOnBackEnabled(boolean)}
*/
@Deprecated
public void setShowErrorStateOnBack(boolean showErrorStateOnBack) {
this.mShowErrorStateOnBackEnabled = showErrorStateOnBack;
}
/**
* Set whether when going backwards should clear the error state from the Tab. Default is <code>false</code>.
*
* @param showErrorStateOnBackEnabled true if navigating backwards should keep the error state, false otherwise
*/
public void setShowErrorStateOnBackEnabled(boolean showErrorStateOnBackEnabled) {
this.mShowErrorStateOnBackEnabled = showErrorStateOnBackEnabled;
}
/**
* Set whether the errors should be displayed when they occur or not. Default is <code>false</code>.
*
* @param showErrorState true if the errors should be displayed when they occur, false otherwise
* @deprecated see {@link #setShowErrorStateEnabled(boolean)}
*/
@Deprecated
public void setShowErrorState(boolean showErrorState) {
setShowErrorStateEnabled(showErrorState);
}
/**
* Set whether the errors should be displayed when they occur or not. Default is <code>false</code>.
*
* @param showErrorStateEnabled true if the errors should be displayed when they occur, false otherwise
*/
public void setShowErrorStateEnabled(boolean showErrorStateEnabled) {
this.mShowErrorStateEnabled = showErrorStateEnabled;
}
/**
* @return true if errors should be displayed when they occur
*/
public boolean isShowErrorStateEnabled() {
return mShowErrorStateEnabled;
}
/**
* @return true if when going backwards the error state from the Tab should be cleared
*/
public boolean isShowErrorStateOnBackEnabled() {
return mShowErrorStateOnBackEnabled;
}
/**
* Set whether an error message below step title should appear when an error occurs
*
* @param showErrorMessageEnabled true if an error message below step title should appear when an error occurs
*/
public void setShowErrorMessageEnabled(boolean showErrorMessageEnabled) {
this.mShowErrorMessageEnabled = showErrorMessageEnabled;
}
/**
* @return true if an error message below step title should appear when an error occurs
*/
public boolean isShowErrorMessageEnabled() {
return mShowErrorMessageEnabled;
}
/**
* @return true if step navigation is possible by clicking on the tabs directly, false otherwise
*/
public boolean isTabNavigationEnabled() {
return mTabNavigationEnabled;
}
/**
* Sets whether step navigation is possible by clicking on the tabs directly. Only applicable for 'tabs' type.
*
* @param tabNavigationEnabled true if step navigation is possible by clicking on the tabs directly, false otherwise
*/
public void setTabNavigationEnabled(boolean tabNavigationEnabled) {
mTabNavigationEnabled = tabNavigationEnabled;
}
/**
* Changes the text and compound drawable color of the Next bottom navigation button.
*
* @param newButtonColor new color state list
*/
public void setNextButtonColor(@NonNull ColorStateList newButtonColor) {
mNextButtonColor = newButtonColor;
TintUtil.tintTextView(mNextNavigationButton, mNextButtonColor);
}
/**
* Changes the text and compound drawable color of the Complete bottom navigation button.
*
* @param newButtonColor new color state list
*/
public void setCompleteButtonColor(@NonNull ColorStateList newButtonColor) {
mCompleteButtonColor = newButtonColor;
TintUtil.tintTextView(mCompleteNavigationButton, mCompleteButtonColor);
}
/**
* Changes the text and compound drawable color of the Back bottom navigation button.
*
* @param newButtonColor new color state list
*/
public void setBackButtonColor(@NonNull ColorStateList newButtonColor) {
mBackButtonColor = newButtonColor;
TintUtil.tintTextView(mBackNavigationButton, mBackButtonColor);
}
/**
* Changes the text and compound drawable color of the Next bottom navigation button.
*
* @param newButtonColor new color int
*/
public void setNextButtonColor(@ColorInt int newButtonColor) {
setNextButtonColor(ColorStateList.valueOf(newButtonColor));
}
/**
* Changes the text and compound drawable color of the Complete bottom navigation button.
*
* @param newButtonColor new color int
*/
public void setCompleteButtonColor(@ColorInt int newButtonColor) {
setCompleteButtonColor(ColorStateList.valueOf(newButtonColor));
}
/**
* Changes the text and compound drawable color of the Back bottom navigation button.
*
* @param newButtonColor new color int
*/
public void setBackButtonColor(@ColorInt int newButtonColor) {
setBackButtonColor(ColorStateList.valueOf(newButtonColor));
}
/**
* Changes the color for an active step, applied to dotted or progress bars
*
* @param newActiveStepColor the active step color integer
*/
public void setActiveStepColor(@ColorInt int newActiveStepColor) {
mSelectedColor = newActiveStepColor;
mDottedProgressBar.setSelectedColor(newActiveStepColor);
mProgressBar.setProgressColor(newActiveStepColor);
}
/**
* Changes the color for an inactive step, applied to dotted or progress bars
*
* @param newInactiveStepColor the inactive step color integer
*/
public void setInactiveStepColor(@ColorInt int newInactiveStepColor) {
mUnselectedColor = newInactiveStepColor;
mDottedProgressBar.setUnselectedColor(newInactiveStepColor);
mProgressBar.setProgressBackgroundColor(newInactiveStepColor);
}
/**
* Changes the bottom navigation background using a drawable resource
*
* @param newBottomNavigationBackground the drawable resource id
*/
public void setBottomNavigationBackground(@DrawableRes int newBottomNavigationBackground){
mBottomNavigationBackground = newBottomNavigationBackground;
mStepNavigation.setBackgroundResource(mBottomNavigationBackground);
}
/**
* Changes the bottom navigation background using a drawable
*
* @param newBottomNavigationBackground the drawable
*/
public void setBottomNavigationBackground(@NonNull Drawable newBottomNavigationBackground){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
mStepNavigation.setBackground(newBottomNavigationBackground);
}else {
mStepNavigation.setBackgroundDrawable(newBottomNavigationBackground);
}
}
/**
* Changes the bottom navigation background color
*
* @param newBottomNavigationBackground the color integer
*/
public void setBottomNavigationBackgroundColor(@ColorInt int newBottomNavigationBackground){
mStepNavigation.setBackgroundColor(newBottomNavigationBackground);
}
/**
* Updates the error state in the UI.
* It does nothing if showing error state is disabled.
* This is used internally to show the error on tabs.
*
* @param error not null if error should be shown, null otherwise
* @see #setShowErrorStateEnabled(boolean)
*/
public void updateErrorState(@Nullable VerificationError error) {
updateError(error);
if (mShowErrorStateEnabled) {
invalidateCurrentPosition();
}
}
/**
* Set the number of steps that should be retained to either side of the
* current step in the view hierarchy in an idle state. Steps beyond this
* limit will be recreated from the adapter when needed.
*
* @param limit How many steps will be kept offscreen in an idle state.
* @see ViewPager#setOffscreenPageLimit(int)
*/
public void setOffscreenPageLimit(int limit) {
mPager.setOffscreenPageLimit(limit);
}
/**
* Shows a progress indicator if not already shown. This does not have to be a progress bar and it depends on chosen stepper feedback types.
*
* @param progressMessage optional progress message if supported by the selected types
*/
public void showProgress(@NonNull String progressMessage) {
if (!mInProgress) {
mStepperFeedbackType.showProgress(progressMessage);
mInProgress = true;
}
}
/**
* Hides the progress indicator if visible.
*/
public void hideProgress() {
if (mInProgress) {
mInProgress = false;
mStepperFeedbackType.hideProgress();
}
}
/**
* Checks if there's an ongoing operation i.e. if {@link #showProgress(String)} was called and not followed by {@link #hideProgress()} yet.
*
* @return true if in progress, false otherwise
*/
public boolean isInProgress() {
return mInProgress;
}
/**
* Sets the mask for the stepper feedback type.
*
* @param feedbackTypeMask step feedback type mask, should contain one or more flags from {@link StepperFeedbackType}
*/
public void setFeedbackType(int feedbackTypeMask) {
mFeedbackTypeMask = feedbackTypeMask;
mStepperFeedbackType = StepperFeedbackTypeFactory.createType(mFeedbackTypeMask, this);
}
/**
* @return An alpha value from 0 to 1.0f to be used for the faded out view if 'content_fade' stepper feedback is set. 0.5f by default.
*/
@FloatRange(from = 0.0f, to = 1.0f)
public float getContentFadeAlpha() {
return mContentFadeAlpha;
}
/**
* @return Background res ID to be used for the overlay on top of the content
* if 'content_overlay' stepper feedback type is set. 0 if default background should be used.
*/
@DrawableRes
public int getContentOverlayBackground() {
return mContentOverlayBackground;
}
@SuppressWarnings("RestrictedApi")
private void init(AttributeSet attrs, @AttrRes int defStyleAttr) {
initDefaultValues();
extractValuesFromAttributes(attrs, defStyleAttr);
final Context context = getContext();
ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(context, context.getTheme());
contextThemeWrapper.setTheme(mStepperLayoutTheme);
LayoutInflater.from(contextThemeWrapper).inflate(R.layout.ms_stepper_layout, this, true);
setOrientation(VERTICAL);
bindViews();
mPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return true;
}
});
initNavigation();
mDottedProgressBar.setVisibility(GONE);
mProgressBar.setVisibility(GONE);
mTabsContainer.setVisibility(GONE);
mStepNavigation.setVisibility(mShowBottomNavigation ? View.VISIBLE : View.GONE);
mStepperType = StepperTypeFactory.createType(mTypeIdentifier, this);
mStepperFeedbackType = StepperFeedbackTypeFactory.createType(mFeedbackTypeMask, this);
}
private void initNavigation() {
if (mBottomNavigationBackground != 0) {
mStepNavigation.setBackgroundResource(mBottomNavigationBackground);
}
mBackNavigationButton.setText(mBackButtonText);
mNextNavigationButton.setText(mNextButtonText);
mCompleteNavigationButton.setText(mCompleteButtonText);
setBackgroundIfPresent(mBackButtonBackground, mBackNavigationButton);
setBackgroundIfPresent(mNextButtonBackground, mNextNavigationButton);
setBackgroundIfPresent(mCompleteButtonBackground, mCompleteNavigationButton);
mBackNavigationButton.setOnClickListener(new OnBackClickListener());
mNextNavigationButton.setOnClickListener(new OnNextClickListener());
mCompleteNavigationButton.setOnClickListener(new OnCompleteClickListener());
}
private void setCompoundDrawablesForNavigationButtons(@DrawableRes int backDrawableResId, @DrawableRes int nextDrawableResId) {
Drawable chevronStartDrawable = backDrawableResId != StepViewModel.NULL_DRAWABLE
? ResourcesCompat.getDrawable(getContext().getResources(), backDrawableResId, null)
: null;
Drawable chevronEndDrawable = nextDrawableResId != StepViewModel.NULL_DRAWABLE
? ResourcesCompat.getDrawable(getContext().getResources(), nextDrawableResId, null)
: null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
mBackNavigationButton.setCompoundDrawablesRelativeWithIntrinsicBounds(chevronStartDrawable, null, null, null);
} else {
mBackNavigationButton.setCompoundDrawablesWithIntrinsicBounds(chevronStartDrawable, null, null, null);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
mNextNavigationButton.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, chevronEndDrawable, null);
} else {
mNextNavigationButton.setCompoundDrawablesWithIntrinsicBounds(null, null, chevronEndDrawable, null);
}
TintUtil.tintTextView(mBackNavigationButton, mBackButtonColor);
TintUtil.tintTextView(mNextNavigationButton, mNextButtonColor);
TintUtil.tintTextView(mCompleteNavigationButton, mCompleteButtonColor);
}
private void setBackgroundIfPresent(@DrawableRes int backgroundRes, View view) {
if (backgroundRes != 0) {
view.setBackgroundResource(backgroundRes);
}
}
private void bindViews() {
mPager = (ViewPager) findViewById(R.id.ms_stepPager);
mBackNavigationButton = (Button) findViewById(R.id.ms_stepPrevButton);
mNextNavigationButton = (RightNavigationButton) findViewById(R.id.ms_stepNextButton);
mCompleteNavigationButton = (RightNavigationButton) findViewById(R.id.ms_stepCompleteButton);
mStepNavigation = (ViewGroup) findViewById(R.id.ms_bottomNavigation);
mDottedProgressBar = (DottedProgressBar) findViewById(R.id.ms_stepDottedProgressBar);
mProgressBar = (ColorableProgressBar) findViewById(R.id.ms_stepProgressBar);
mTabsContainer = (TabsContainer) findViewById(R.id.ms_stepTabsContainer);
}
private void extractValuesFromAttributes(AttributeSet attrs, @AttrRes int defStyleAttr) {
if (attrs != null) {
final TypedArray a = getContext().obtainStyledAttributes(
attrs, R.styleable.StepperLayout, defStyleAttr, 0);
if (a.hasValue(R.styleable.StepperLayout_ms_backButtonColor)) {
mBackButtonColor = a.getColorStateList(R.styleable.StepperLayout_ms_backButtonColor);
}
if (a.hasValue(R.styleable.StepperLayout_ms_nextButtonColor)) {
mNextButtonColor = a.getColorStateList(R.styleable.StepperLayout_ms_nextButtonColor);
}
if (a.hasValue(R.styleable.StepperLayout_ms_completeButtonColor)) {
mCompleteButtonColor = a.getColorStateList(R.styleable.StepperLayout_ms_completeButtonColor);
}
if (a.hasValue(R.styleable.StepperLayout_ms_activeStepColor)) {
mSelectedColor = a.getColor(R.styleable.StepperLayout_ms_activeStepColor, mSelectedColor);
}
if (a.hasValue(R.styleable.StepperLayout_ms_inactiveStepColor)) {
mUnselectedColor = a.getColor(R.styleable.StepperLayout_ms_inactiveStepColor, mUnselectedColor);
}
if (a.hasValue(R.styleable.StepperLayout_ms_errorColor)) {
mErrorColor = a.getColor(R.styleable.StepperLayout_ms_errorColor, mErrorColor);
}
if (a.hasValue(R.styleable.StepperLayout_ms_bottomNavigationBackground)) {
mBottomNavigationBackground = a.getResourceId(R.styleable.StepperLayout_ms_bottomNavigationBackground, 0);
}
if (a.hasValue(R.styleable.StepperLayout_ms_backButtonBackground)) {
mBackButtonBackground = a.getResourceId(R.styleable.StepperLayout_ms_backButtonBackground, 0);
}
if (a.hasValue(R.styleable.StepperLayout_ms_nextButtonBackground)) {
mNextButtonBackground = a.getResourceId(R.styleable.StepperLayout_ms_nextButtonBackground, 0);
}
if (a.hasValue(R.styleable.StepperLayout_ms_completeButtonBackground)) {
mCompleteButtonBackground = a.getResourceId(R.styleable.StepperLayout_ms_completeButtonBackground, 0);
}
if (a.hasValue(R.styleable.StepperLayout_ms_backButtonText)) {
mBackButtonText = a.getString(R.styleable.StepperLayout_ms_backButtonText);
}
if (a.hasValue(R.styleable.StepperLayout_ms_nextButtonText)) {
mNextButtonText = a.getString(R.styleable.StepperLayout_ms_nextButtonText);
}
if (a.hasValue(R.styleable.StepperLayout_ms_completeButtonText)) {
mCompleteButtonText = a.getString(R.styleable.StepperLayout_ms_completeButtonText);
}
if (a.hasValue(R.styleable.StepperLayout_ms_tabStepDividerWidth)) {
mTabStepDividerWidth = a.getDimensionPixelOffset(R.styleable.StepperLayout_ms_tabStepDividerWidth, -1);
}
mShowBackButtonOnFirstStep = a.getBoolean(R.styleable.StepperLayout_ms_showBackButtonOnFirstStep, false);
mShowBottomNavigation = a.getBoolean(R.styleable.StepperLayout_ms_showBottomNavigation, true);
mShowErrorStateEnabled = a.getBoolean(R.styleable.StepperLayout_ms_showErrorState, false);
mShowErrorStateEnabled = a.getBoolean(R.styleable.StepperLayout_ms_showErrorStateEnabled, mShowErrorStateEnabled);
if (a.hasValue(R.styleable.StepperLayout_ms_stepperType)) {
mTypeIdentifier = a.getInt(R.styleable.StepperLayout_ms_stepperType, AbstractStepperType.PROGRESS_BAR);
}
if (a.hasValue(R.styleable.StepperLayout_ms_stepperFeedbackType)) {
mFeedbackTypeMask = a.getInt(R.styleable.StepperLayout_ms_stepperFeedbackType, StepperFeedbackType.NONE);
}
if (a.hasValue(R.styleable.StepperLayout_ms_stepperFeedback_contentFadeAlpha)) {
mContentFadeAlpha = a.getFloat(R.styleable.StepperLayout_ms_stepperFeedback_contentFadeAlpha, AnimationUtil.ALPHA_HALF);
}
if (a.hasValue(R.styleable.StepperLayout_ms_stepperFeedback_contentOverlayBackground)) {
mContentOverlayBackground = a.getResourceId(R.styleable.StepperLayout_ms_stepperFeedback_contentOverlayBackground, 0);
}
mShowErrorStateOnBackEnabled = a.getBoolean(R.styleable.StepperLayout_ms_showErrorStateOnBack, false);
mShowErrorStateOnBackEnabled = a.getBoolean(R.styleable.StepperLayout_ms_showErrorStateOnBackEnabled, mShowErrorStateOnBackEnabled);
mShowErrorMessageEnabled = a.getBoolean(R.styleable.StepperLayout_ms_showErrorMessageEnabled, false);
mTabNavigationEnabled = a.getBoolean(R.styleable.StepperLayout_ms_tabNavigationEnabled, true);
mStepperLayoutTheme = a.getResourceId(R.styleable.StepperLayout_ms_stepperLayoutTheme, R.style.MSDefaultStepperLayoutTheme);
a.recycle();
}
}
private void initDefaultValues() {
mBackButtonColor = mNextButtonColor = mCompleteButtonColor =
ContextCompat.getColorStateList(getContext(), R.color.ms_bottomNavigationButtonTextColor);
mSelectedColor = ContextCompat.getColor(getContext(), R.color.ms_selectedColor);
mUnselectedColor = ContextCompat.getColor(getContext(), R.color.ms_unselectedColor);
mErrorColor = ContextCompat.getColor(getContext(), R.color.ms_errorColor);
mBackButtonText = getContext().getString(R.string.ms_back);
mNextButtonText = getContext().getString(R.string.ms_next);
mCompleteButtonText = getContext().getString(R.string.ms_complete);
}
private boolean isLastPosition(int position) {
return position == mStepAdapter.getCount() - 1;
}
private Step findCurrentStep() {
return mStepAdapter.findStep(mCurrentStepPosition);
}
private void updateErrorFlagWhenGoingBack() {
updateError(mShowErrorStateOnBackEnabled ? mStepperType.getErrorAtPosition(mCurrentStepPosition) : null);
}
@UiThread
private void onNext() {