From 938d38f310314afe93de3e3677b9b0c122cab0af Mon Sep 17 00:00:00 2001 From: heapifyman <274236+heapifyman@users.noreply.github.com> Date: Sat, 15 Jul 2023 18:12:37 +0200 Subject: [PATCH] add navigating back and forth in PDF by swiping right or left --- .../grapheneos/pdfviewer/GestureHelper.java | 31 +++++++++++++++++++ .../app/grapheneos/pdfviewer/PdfViewer.java | 12 ++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/app/grapheneos/pdfviewer/GestureHelper.java b/app/src/main/java/app/grapheneos/pdfviewer/GestureHelper.java index 2a291cdde..5e140c905 100644 --- a/app/src/main/java/app/grapheneos/pdfviewer/GestureHelper.java +++ b/app/src/main/java/app/grapheneos/pdfviewer/GestureHelper.java @@ -7,6 +7,8 @@ import android.view.ScaleGestureDetector; import android.view.View; +import androidx.annotation.NonNull; + /* The GestureHelper present a simple gesture api for the PdfViewer */ @@ -14,6 +16,10 @@ class GestureHelper { public interface GestureListener { boolean onTapUp(); + + void onSwipeLeft(); + void onSwipeRight(); + // Can be replaced with ratio when supported void onZoomIn(float value); void onZoomOut(float value); @@ -29,6 +35,31 @@ static void attach(Context context, View gestureView, GestureListener listener) public boolean onSingleTapUp(MotionEvent motionEvent) { return listener.onTapUp(); } + + // inspired by https://www.geeksforgeeks.org/how-to-detect-swipe-direction-in-android/ + @Override + public boolean onFling(@NonNull MotionEvent e1, @NonNull MotionEvent e2, float velocityX, float velocityY) { + final int swipeThreshold = 100; + final int swipeVelocityThreshold = 100; + + final float diffX = e2.getX() - e1.getX(); + final float absDiffX = Math.abs(diffX); + final float diffY = e2.getY() - e1.getY(); + final float absDiffY = Math.abs(diffY); + + if (absDiffX > absDiffY // only handle horizontal swipe + && absDiffX > swipeThreshold + && Math.abs(velocityX) > swipeVelocityThreshold) { + if (diffX > 0) { + listener.onSwipeRight(); + } else { + listener.onSwipeLeft(); + } + return true; + } + + return false; + } }); final ScaleGestureDetector scaleDetector = new ScaleGestureDetector(context, diff --git a/app/src/main/java/app/grapheneos/pdfviewer/PdfViewer.java b/app/src/main/java/app/grapheneos/pdfviewer/PdfViewer.java index 1c7e0a173..a568cdad3 100644 --- a/app/src/main/java/app/grapheneos/pdfviewer/PdfViewer.java +++ b/app/src/main/java/app/grapheneos/pdfviewer/PdfViewer.java @@ -211,7 +211,7 @@ public void setDocumentProperties(final String properties) { @JavascriptInterface public void showPasswordPrompt() { - if (!getPasswordPromptFragment().isAdded()){ + if (!getPasswordPromptFragment().isAdded()) { getPasswordPromptFragment().show(getSupportFragmentManager(), PasswordPromptFragment.class.getName()); } passwordValidationViewModel.passwordMissing(); @@ -362,6 +362,16 @@ public boolean onTapUp() { return false; } + @Override + public void onSwipeLeft() { + onJumpToPageInDocument(mPage + 1); + } + + @Override + public void onSwipeRight() { + onJumpToPageInDocument(mPage - 1); + } + @Override public void onZoomIn(float value) { zoomIn(value, false);