Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add navigating back and forth in PDF by swiping right or left #282

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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