capacitor-swipe-gestures-plugin 4.2.2 → 4.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
package com.swipeback;
|
|
2
2
|
|
|
3
|
+
import android.view.GestureDetector;
|
|
4
|
+
import android.view.MotionEvent;
|
|
5
|
+
import android.view.View;
|
|
6
|
+
import android.webkit.WebView;
|
|
7
|
+
|
|
3
8
|
import com.getcapacitor.JSObject;
|
|
4
9
|
import com.getcapacitor.Plugin;
|
|
5
10
|
import com.getcapacitor.PluginCall;
|
|
@@ -8,10 +13,71 @@ import com.getcapacitor.annotation.CapacitorPlugin;
|
|
|
8
13
|
|
|
9
14
|
@CapacitorPlugin(name = "CapacitorSwipeGesturesPlugin")
|
|
10
15
|
public class CapacitorSwipeGesturesPlugin extends Plugin {
|
|
16
|
+
private static final int SWIPE_THRESHOLD = 100;
|
|
17
|
+
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
|
|
18
|
+
private static final int EDGE_THRESHOLD = 100; // Pixels from edge to recognize as edge swipe
|
|
19
|
+
|
|
20
|
+
private GestureDetector gestureDetector;
|
|
21
|
+
private boolean gesturesEnabled = false;
|
|
22
|
+
private View.OnTouchListener touchListener;
|
|
11
23
|
|
|
12
24
|
@PluginMethod
|
|
13
25
|
public void enable(PluginCall call) {
|
|
14
|
-
|
|
26
|
+
final WebView webView = bridge.getWebView();
|
|
27
|
+
|
|
28
|
+
if (webView != null && !gesturesEnabled) {
|
|
29
|
+
// Create gesture detector if it doesn't exist
|
|
30
|
+
if (gestureDetector == null) {
|
|
31
|
+
gestureDetector = new GestureDetector(bridge.getActivity(), new GestureDetector.SimpleOnGestureListener() {
|
|
32
|
+
@Override
|
|
33
|
+
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
|
|
34
|
+
try {
|
|
35
|
+
float diffX = e2.getX() - e1.getX();
|
|
36
|
+
float diffY = e2.getY() - e1.getY();
|
|
37
|
+
|
|
38
|
+
// Ensure the swipe is primarily horizontal
|
|
39
|
+
if (Math.abs(diffX) > Math.abs(diffY)) {
|
|
40
|
+
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
|
|
41
|
+
// Right swipe (left to right)
|
|
42
|
+
if (diffX > 0) {
|
|
43
|
+
// Check if swipe started near left edge
|
|
44
|
+
if (e1.getX() < EDGE_THRESHOLD && webView.canGoBack()) {
|
|
45
|
+
webView.goBack();
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// Left swipe (right to left)
|
|
50
|
+
else {
|
|
51
|
+
// Check if swipe started near right edge
|
|
52
|
+
int viewWidth = webView.getWidth();
|
|
53
|
+
if (e1.getX() > (viewWidth - EDGE_THRESHOLD) && webView.canGoForward()) {
|
|
54
|
+
webView.goForward();
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
} catch (Exception exception) {
|
|
61
|
+
exception.printStackTrace();
|
|
62
|
+
}
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Create touch listener
|
|
69
|
+
touchListener = new View.OnTouchListener() {
|
|
70
|
+
@Override
|
|
71
|
+
public boolean onTouch(View v, MotionEvent event) {
|
|
72
|
+
boolean gestureConsumed = gestureDetector.onTouchEvent(event);
|
|
73
|
+
return gestureConsumed || v.onTouchEvent(event);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// Set touch listener on the WebView
|
|
78
|
+
webView.setOnTouchListener(touchListener);
|
|
79
|
+
gesturesEnabled = true;
|
|
80
|
+
}
|
|
15
81
|
|
|
16
82
|
JSObject ret = new JSObject();
|
|
17
83
|
ret.put("status", "enable");
|
|
@@ -20,8 +86,17 @@ public class CapacitorSwipeGesturesPlugin extends Plugin {
|
|
|
20
86
|
|
|
21
87
|
@PluginMethod
|
|
22
88
|
public void disable(PluginCall call) {
|
|
89
|
+
WebView webView = bridge.getWebView();
|
|
90
|
+
|
|
91
|
+
if (webView != null && gesturesEnabled) {
|
|
92
|
+
// Remove touch listener
|
|
93
|
+
webView.setOnTouchListener(null);
|
|
94
|
+
touchListener = null;
|
|
95
|
+
gesturesEnabled = false;
|
|
96
|
+
}
|
|
97
|
+
|
|
23
98
|
JSObject ret = new JSObject();
|
|
24
99
|
ret.put("status", "disable");
|
|
25
100
|
call.resolve(ret);
|
|
26
101
|
}
|
|
27
|
-
}
|
|
102
|
+
}
|