Skip to content

Commit

Permalink
Separate settings for different exit gestures (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvasilkov committed May 5, 2018
1 parent d370a1b commit 4414a74
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
53 changes: 47 additions & 6 deletions library/src/main/java/com/alexvasilkov/gestures/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ public class Settings {
private boolean isDoubleTapEnabled = true;

/*
* Whether to detect and animate exit from gesture views.
* Which gestures to use to detect exit.
*/
private boolean isExitEnabled = true;
private ExitType exitType = ExitType.ALL;

/*
* Counter for gestures disabling calls.
Expand Down Expand Up @@ -195,8 +195,8 @@ public void initFromAttributes(@NonNull Context context, @Nullable AttributeSet
R.styleable.GestureView_gest_restrictRotation, isRestrictRotation);
isDoubleTapEnabled = arr.getBoolean(
R.styleable.GestureView_gest_doubleTapEnabled, isDoubleTapEnabled);
isExitEnabled = arr.getBoolean(
R.styleable.GestureView_gest_exitEnabled, isExitEnabled);
exitType = arr.getBoolean(
R.styleable.GestureView_gest_exitEnabled, true) ? exitType : ExitType.NONE;
animationsDuration = arr.getInt(
R.styleable.GestureView_gest_animationDuration, (int) animationsDuration);

Expand Down Expand Up @@ -491,8 +491,23 @@ public Settings setDoubleTapEnabled(boolean enabled) {
* @param enabled Whether exit gesture should be enabled or not
* @return Current settings object for calls chaining
*/
@SuppressWarnings("unused") // Public API
public Settings setExitEnabled(boolean enabled) {
isExitEnabled = enabled;
exitType = enabled ? ExitType.ALL : ExitType.NONE;
return this;
}

/**
* Sets which gestures to use to detect exit.
* <p>
* Default value is {@link ExitType#ALL}.
*
* @param type Exit type
* @return Current settings object for calls chaining
*/
@SuppressWarnings("unused") // Public API
public Settings setExitType(ExitType type) {
exitType = type;
return this;
}

Expand Down Expand Up @@ -678,7 +693,11 @@ public boolean isDoubleTapEnabled() {
}

public boolean isExitEnabled() {
return isGesturesEnabled() && isExitEnabled;
return getExitType() != ExitType.NONE;
}

public ExitType getExitType() {
return isGesturesEnabled() ? exitType : ExitType.NONE;
}

public boolean isGesturesEnabled() {
Expand Down Expand Up @@ -770,4 +789,26 @@ public enum Bounds {
NONE
}

public enum ExitType {
/**
* To detect both scroll and zoom exit gestures.
*/
ALL,

/**
* To detect only scroll-to-exit gesture.
*/
SCROLL,

/**
* To detect only zoom-to-exit gesture.
*/
ZOOM,

/**
* Do not detect exit gestures.
*/
NONE
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.alexvasilkov.gestures.GestureController;
import com.alexvasilkov.gestures.GestureControllerForPager;
import com.alexvasilkov.gestures.Settings.ExitType;
import com.alexvasilkov.gestures.State;
import com.alexvasilkov.gestures.animation.ViewPositionAnimator;
import com.alexvasilkov.gestures.utils.GravityUtils;
Expand Down Expand Up @@ -86,7 +87,7 @@ public boolean onScroll(float dx, float dy) {
// Also, we can detect scroll only if image is zoomed out and it reached movement bounds.

if (!skipScrollDetection && !isExitDetected() && canDetectExit()
&& !isZoomInAction && !isRotationInAction && isZoomedOut() && !canScroll(dy)) {
&& canDetectScroll() && !canScroll(dy)) {

totalScrollX += dx;
totalScrollY += dy;
Expand Down Expand Up @@ -166,12 +167,11 @@ public boolean onScale(float scaleFactor) {
// Exit by zoom should not be detected if rotation is currently in place.
// Also, we can detect zoom only if image is zoomed out and we are zooming out.

if (!isZoomedOut()) {
if (!canDetectZoom()) {
skipZoomDetection = true;
}

if (!skipZoomDetection && !isExitDetected() && canDetectExit() && !isRotationInAction
&& isZoomedOut() && scaleFactor < 1f) {
if (!skipZoomDetection && !isExitDetected() && canDetectExit() && scaleFactor < 1f) {

// Waiting until we zoomed enough to trigger exit detection
zoomAccumulator *= scaleFactor;
Expand Down Expand Up @@ -235,6 +235,19 @@ private boolean canDetectExit() {
&& !animatorView.getPositionAnimator().isLeaving();
}

private boolean canDetectScroll() {
ExitType exitType = controller.getSettings().getExitType();
return (exitType == ExitType.ALL || exitType == ExitType.SCROLL)
&& !isZoomInAction && !isRotationInAction && isZoomedOut();
}

private boolean canDetectZoom() {
ExitType exitType = controller.getSettings().getExitType();
return (exitType == ExitType.ALL || exitType == ExitType.ZOOM)
&& !isRotationInAction && isZoomedOut();
}


private boolean canScroll(float dy) {
if (!controller.getSettings().isRestrictBounds()) {
return true;
Expand Down

0 comments on commit 4414a74

Please sign in to comment.