Skip to content

Add swipe gesture support for card navigation #2468

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

Open
wants to merge 2 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

import protect.card_locker.databinding.BarcodeSelectorActivityBinding;

import android.content.ClipboardManager;
import android.content.ClipData;

/**
* This activity is callable and will allow a user to enter
* barcode data and generate all barcodes possible for
Expand Down
58 changes: 58 additions & 0 deletions app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import android.text.style.ForegroundColorSpan;
import android.text.util.Linkify;
import android.util.Log;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
Expand Down Expand Up @@ -108,6 +110,9 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements

private long initTime = System.currentTimeMillis();

private GestureDetector gestureDetector;


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (settings.useVolumeKeysForNavigation()) {
Expand Down Expand Up @@ -256,6 +261,15 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = LoyaltyCardViewLayoutBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// Initialize GestureDetector
gestureDetector = new GestureDetector(this, new GestureListener());

// Set touch listener to detect swipe gestures
binding.getRoot().setOnTouchListener((v, event) -> {
Log.d(TAG, "Touch event detected: " + event.getAction()); // Log every touch action
return gestureDetector.onTouchEvent(event); // Let GestureDetector handle the event
});

Utils.applyWindowInsets(binding.getRoot());
Toolbar toolbar = binding.toolbar;
setSupportActionBar(toolbar);
Expand Down Expand Up @@ -1250,4 +1264,48 @@ private void setFullscreenModeSdkLessThan30() {
);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}

private class GestureListener extends GestureDetector.SimpleOnGestureListener {

@Override
public boolean onDown(MotionEvent e) {
// Log when a touch down event is detected
Log.d(TAG, "onDown: " + e.toString());
return true;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// Log every swipe gesture
Log.d(TAG, "onFling detected: e1=" + e1.toString() + ", e2=" + e2.toString());

float diffX = e1.getX() - e2.getX();
Log.d(TAG, "diffX = " + diffX); // Log the horizontal swipe distance

if (Math.abs(diffX) > 100) { // Adjust the threshold for a swipe
if (diffX > 0) {
// Left swipe
Log.d(TAG, "Left swipe detected");
if (initTime < (System.currentTimeMillis() - 1000)) {
prevNextCard(true); // Move to next card
}
} else {
// Right swipe
Log.d(TAG, "Right swipe detected");
if (initTime < (System.currentTimeMillis() - 1000)) {
prevNextCard(false); // Move to previous card
}
}
return true; // Indicate the gesture was handled
}
return false; // Gesture not recognized
}
}
}


1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@
<string name="addFromPkpass">Select a Passbook file (.pkpass)</string>
<string name="unsupportedFile">This file is not supported</string>
<string name="generic_error_please_retry">Sorry, something went wrong, please try again...</string>
<string name="barcodeCopied">Barcode copied to clipboard</string>
<string name="width">Width</string>
<string name="setBarcodeWidth">Set Barcode Width</string>
</resources>