Skip to content

Commit

Permalink
add navigating back and forth in PDF by swiping right or left
Browse files Browse the repository at this point in the history
  • Loading branch information
heapifyman committed Sep 2, 2023
1 parent 06e7bfe commit e0c0f70
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
31 changes: 31 additions & 0 deletions app/src/main/java/app/grapheneos/pdfviewer/GestureHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
import android.view.ScaleGestureDetector;
import android.view.View;

import androidx.annotation.NonNull;

/*
The GestureHelper present a simple gesture api for the PdfViewer
*/

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);
Expand All @@ -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,
Expand Down
12 changes: 11 additions & 1 deletion app/src/main/java/app/grapheneos/pdfviewer/PdfViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e0c0f70

Please sign in to comment.