Skip to content

Commit 0243059

Browse files
committed
add navigating back and forth in PDF by swiping right or left
1 parent db16a93 commit 0243059

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

app/src/main/java/app/grapheneos/pdfviewer/GestureHelper.java

+31
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77
import android.view.ScaleGestureDetector;
88
import android.view.View;
99

10+
import androidx.annotation.NonNull;
11+
1012
/*
1113
The GestureHelper present a simple gesture api for the PdfViewer
1214
*/
1315

1416
class GestureHelper {
1517
public interface GestureListener {
1618
boolean onTapUp();
19+
20+
void onSwipeLeft();
21+
void onSwipeRight();
22+
1723
// Can be replaced with ratio when supported
1824
void onZoomIn(float value);
1925
void onZoomOut(float value);
@@ -29,6 +35,31 @@ static void attach(Context context, View gestureView, GestureListener listener)
2935
public boolean onSingleTapUp(MotionEvent motionEvent) {
3036
return listener.onTapUp();
3137
}
38+
39+
// inspired by https://www.geeksforgeeks.org/how-to-detect-swipe-direction-in-android/
40+
@Override
41+
public boolean onFling(@NonNull MotionEvent e1, @NonNull MotionEvent e2, float velocityX, float velocityY) {
42+
final int swipeThreshold = 100;
43+
final int swipeVelocityThreshold = 100;
44+
45+
final float diffX = e2.getX() - e1.getX();
46+
final float absDiffX = Math.abs(diffX);
47+
final float diffY = e2.getY() - e1.getY();
48+
final float absDiffY = Math.abs(diffY);
49+
50+
if (absDiffX > absDiffY // only handle horizontal swipe
51+
&& absDiffX > swipeThreshold
52+
&& Math.abs(velocityX) > swipeVelocityThreshold) {
53+
if (diffX > 0) {
54+
listener.onSwipeRight();
55+
} else {
56+
listener.onSwipeLeft();
57+
}
58+
return true;
59+
}
60+
61+
return false;
62+
}
3263
});
3364

3465
final ScaleGestureDetector scaleDetector = new ScaleGestureDetector(context,

app/src/main/java/app/grapheneos/pdfviewer/PdfViewer.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public void setDocumentProperties(final String properties) {
211211

212212
@JavascriptInterface
213213
public void showPasswordPrompt() {
214-
if (!getPasswordPromptFragment().isAdded()){
214+
if (!getPasswordPromptFragment().isAdded()) {
215215
getPasswordPromptFragment().show(getSupportFragmentManager(), PasswordPromptFragment.class.getName());
216216
}
217217
passwordValidationViewModel.passwordMissing();
@@ -362,6 +362,16 @@ public boolean onTapUp() {
362362
return false;
363363
}
364364

365+
@Override
366+
public void onSwipeLeft() {
367+
onJumpToPageInDocument(mPage + 1);
368+
}
369+
370+
@Override
371+
public void onSwipeRight() {
372+
onJumpToPageInDocument(mPage - 1);
373+
}
374+
365375
@Override
366376
public void onZoomIn(float value) {
367377
zoomIn(value, false);

0 commit comments

Comments
 (0)