Browse Source

Make swipe up/down to dismiss post view more sensitive

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
a6a4b6292d
  1. 82
      app/src/main/java/awais/instagrabber/customviews/VerticalDragHelper.java
  2. 10
      app/src/main/java/awais/instagrabber/fragments/PostViewV2Fragment.java

82
app/src/main/java/awais/instagrabber/customviews/VerticalDragHelper.java

@ -1,24 +1,22 @@
package awais.instagrabber.customviews; package awais.instagrabber.customviews;
import android.content.Context; import android.content.Context;
import android.util.Log;
import android.view.GestureDetector; import android.view.GestureDetector;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewParent; import android.view.ViewParent;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
public class VerticalDragHelper { public class VerticalDragHelper {
private static final String TAG = "VerticalDragHelper";
private static final float PIXELS_PER_SECOND = 10;
// private static final String TAG = "VerticalDragHelper";
private static final double SWIPE_THRESHOLD_VELOCITY = 80;
private final View view; private final View view;
private GestureDetector gestureDetector; private GestureDetector gestureDetector;
private Context context; private Context context;
private float flingVelocity;
private double flingVelocity;
private OnVerticalDragListener onVerticalDragListener; private OnVerticalDragListener onVerticalDragListener;
private final GestureDetector.OnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() { private final GestureDetector.OnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() {
@ -31,32 +29,13 @@ public class VerticalDragHelper {
@Override @Override
public boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX, final float velocityY) { public boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX, final float velocityY) {
float maxFlingVelocity = ViewConfiguration.get(context).getScaledMaximumFlingVelocity();
float velocityPercentY = velocityY / maxFlingVelocity;
float normalizedVelocityY = velocityPercentY * PIXELS_PER_SECOND;
if (Math.abs(normalizedVelocityY) > 4) {
flingVelocity = normalizedVelocityY;
double yDir = e1.getY() - e2.getY();
// Log.d(TAG, "onFling: yDir: " + yDir);
if (yDir < -SWIPE_THRESHOLD_VELOCITY || yDir > SWIPE_THRESHOLD_VELOCITY) {
flingVelocity = yDir;
} }
return super.onFling(e1, e2, velocityX, velocityY); return super.onFling(e1, e2, velocityX, velocityY);
} }
};
private final GestureDetector.OnGestureListener dragPreventionGestureListener = new GestureDetector.SimpleOnGestureListener() {
float prevDistanceY = 0;
@Override
public boolean onScroll(final MotionEvent e1, final MotionEvent e2, final float distanceX, final float distanceY) {
Log.d(TAG, "onScroll: distanceX: " + distanceX + ", distanceY: " + distanceY);
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public boolean onSingleTapUp(final MotionEvent e) {
Log.d(TAG, "onSingleTapUp");
return super.onSingleTapUp(e);
}
}; };
private float prevRawY; private float prevRawY;
@ -64,7 +43,6 @@ public class VerticalDragHelper {
private float prevRawX; private float prevRawX;
private float dX; private float dX;
private float prevDY; private float prevDY;
private GestureDetector dragPreventionGestureDetector;
public VerticalDragHelper(@NonNull final View view) { public VerticalDragHelper(@NonNull final View view) {
this.view = view; this.view = view;
@ -80,14 +58,12 @@ public class VerticalDragHelper {
protected void init() { protected void init() {
gestureDetector = new GestureDetector(context, gestureListener); gestureDetector = new GestureDetector(context, gestureListener);
dragPreventionGestureDetector = new GestureDetector(context, dragPreventionGestureListener);
} }
public boolean onDragTouch(final MotionEvent event) { public boolean onDragTouch(final MotionEvent event) {
if (onVerticalDragListener == null) { if (onVerticalDragListener == null) {
return false; return false;
} }
// dragPreventionGestureDetector.onTouchEvent(event);
if (gestureDetector.onTouchEvent(event)) { if (gestureDetector.onTouchEvent(event)) {
return true; return true;
} }
@ -151,53 +127,11 @@ public class VerticalDragHelper {
return gestureDetector.onTouchEvent(event); return gestureDetector.onTouchEvent(event);
} }
private final static int DIRECTION_UP = 0;
private final static int DIRECTION_DOWN = 1;
float prevY = -1;
int edgeHitCount = 0;
float prevDirection = -1;
// private boolean shouldPreventDrag(final MotionEvent event) {
// switch (event.getAction()) {
// case MotionEvent.ACTION_DOWN:
// if (!firstDrag) {
// firstDrag = true;
// }
// return false;
// case MotionEvent.ACTION_MOVE:
// float y = event.getY();
// int direction = -2;
// if (prevY != -1) {
// final float dy = y - prevY;
// // Log.d(TAG, "shouldPreventDrag: dy: " + dy);
// if (dy > 0) {
// direction = DIRECTION_DOWN;
// // move direction is down
// } else {
// direction = DIRECTION_UP;
// // move direction is up
// }
// }
// prevY = y;
// if (prevDirection == direction) {
// edgeHitCount++;
// } else {
// edgeHitCount = 1;
// }
// if (edgeHitCount >= 2) {
// return false;
// }
// return true;
// break;
// }
// }
public interface OnVerticalDragListener { public interface OnVerticalDragListener {
void onDrag(final float dY); void onDrag(final float dY);
void onDragEnd(); void onDragEnd();
void onFling(final float flingVelocity);
void onFling(final double flingVelocity);
} }
} }

10
app/src/main/java/awais/instagrabber/fragments/PostViewV2Fragment.java

@ -125,8 +125,8 @@ public class PostViewV2Fragment extends SharedElementTransitionDialogFragment {
@Override @Override
public void onDragEnd() { public void onDragEnd() {
// animate and dismiss if user drags the view more that 30% of the view // animate and dismiss if user drags the view more that 30% of the view
if (Math.abs(binding.getRoot().getY()) > Utils.displayMetrics.heightPixels * 0.35) {
animateAndDismiss(binding.getRoot().getY() < 0 ? -1 : 1);
if (Math.abs(binding.getRoot().getY()) > Utils.displayMetrics.heightPixels * 0.25) {
animateAndDismiss(binding.getRoot().getY() < 0 ? 1 : -1);
return; return;
} }
// animate back the view to proper position // animate back the view to proper position
@ -134,16 +134,16 @@ public class PostViewV2Fragment extends SharedElementTransitionDialogFragment {
} }
@Override @Override
public void onFling(final float flingVelocity) {
public void onFling(final double flingVelocity) {
// animate and dismiss if user flings up/down // animate and dismiss if user flings up/down
animateAndDismiss(flingVelocity < 0 ? -1 : 1);
animateAndDismiss(flingVelocity > 0 ? 1 : -1);
} }
private void animateAndDismiss(final int direction) { private void animateAndDismiss(final int direction) {
final int height = binding.getRoot().getHeight(); final int height = binding.getRoot().getHeight();
final int finalYDist = height + Utils.getStatusBarHeight(context); final int finalYDist = height + Utils.getStatusBarHeight(context);
// less than 0 means up direction, else down // less than 0 means up direction, else down
final int finalY = direction < 0 ? -finalYDist : finalYDist;
final int finalY = direction > 0 ? -finalYDist : finalYDist;
animateY(binding.getRoot(), finalY, 200, new AnimatorListenerAdapter() { animateY(binding.getRoot(), finalY, 200, new AnimatorListenerAdapter() {
@Override @Override
public void onAnimationEnd(final Animator animation) { public void onAnimationEnd(final Animator animation) {

Loading…
Cancel
Save