Skip to content

Commit

Permalink
Add showing error on failed swipe (#77)
Browse files Browse the repository at this point in the history
* Add ISlideErrorHandler
 * Better code formatting
  • Loading branch information
bezmian authored and radzio committed Feb 20, 2017
1 parent 253c7c8 commit db289f7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package agency.tango.materialintroscreen;

public interface ISlideErrorHandler {
void handleError();
}
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,13 @@ public void onFinish() {
}
});

viewPager.registerSlideErrorHandler(new ISlideErrorHandler() {
@Override
public void handleError() {
errorOccurred(adapter.getItem(viewPager.getCurrentItem()));
}
});

viewPager.addOnPageChangeListener(new ViewBehavioursOnPageChangeListener(adapter)
.registerViewTranslationWrapper(nextButtonTranslationWrapper)
.registerViewTranslationWrapper(backButtonTranslationWrapper)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.view.KeyEvent;
import android.view.MotionEvent;

import agency.tango.materialintroscreen.ISlideErrorHandler;
import agency.tango.materialintroscreen.adapter.SlidesAdapter;

@SuppressWarnings("PMD.SingularField")
Expand All @@ -16,6 +17,7 @@ public class SwipeableViewPager extends CustomViewPager {
private int currentIt;
private boolean swipingAllowed;
private boolean alphaExitTransitionEnabled = false;
private ISlideErrorHandler errorHandler;

public SwipeableViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
Expand Down Expand Up @@ -55,13 +57,15 @@ public boolean onTouchEvent(final MotionEvent event) {
resolveSwipingRightAllowed();
return super.onTouchEvent(event);
case (MotionEvent.ACTION_MOVE):
if (!swipingAllowed && startPos - event.getX() > 16) {
if (isSwipingNotAllowed(event)) {
errorHandler.handleError();
return true;
}
return super.onTouchEvent(event);
case (MotionEvent.ACTION_UP):
if (!swipingAllowed && startPos - event.getX() > 16) {
if (isSwipingNotAllowed(event)) {
smoothScrollTo(getWidth() * currentIt, 0);
errorHandler.handleError();
return true;
}
startPos = 0;
Expand All @@ -81,6 +85,10 @@ public boolean executeKeyEvent(KeyEvent event) {
return false;
}

public void registerSlideErrorHandler(ISlideErrorHandler handler) {
errorHandler = handler;
}

public void moveToNextPage() {
setCurrentItem(getCurrentItem() + 1, true);
}
Expand All @@ -105,6 +113,10 @@ public boolean alphaExitTransitionEnabled() {
return alphaExitTransitionEnabled && swipingAllowed;
}

private boolean isSwipingNotAllowed(MotionEvent event) {
return !swipingAllowed && startPos - event.getX() > 16;
}

private void resolveSwipingRightAllowed() {
if (getAdapter().shouldLockSlide(getCurrentItem())) {
setSwipingRightAllowed(false);
Expand Down

0 comments on commit db289f7

Please sign in to comment.