@@ -11,8 +11,11 @@ import android.graphics.drawable.AnimatedVectorDrawable
11
11
import android.graphics.drawable.GradientDrawable
12
12
import android.os.Handler
13
13
import android.os.Looper
14
+ import android.view.GestureDetector
14
15
import android.view.Gravity
15
16
import android.view.LayoutInflater
17
+ import android.view.MotionEvent
18
+ import android.view.View
16
19
import android.widget.ImageView
17
20
import android.widget.LinearLayout
18
21
import android.widget.ProgressBar
@@ -26,6 +29,7 @@ import com.facebook.react.bridge.WritableMap
26
29
import com.hjq.window.EasyWindow
27
30
import java.io.IOException
28
31
import java.net.URL
32
+ import kotlin.math.abs
29
33
import kotlin.math.roundToInt
30
34
31
35
@@ -38,9 +42,9 @@ class TingModule internal constructor(context: ReactApplicationContext) : TingSp
38
42
private var alertOptionInit: ReadableMap ? = null
39
43
40
44
@ReactMethod
41
- override fun toast (options : ReadableMap ) {
45
+ override fun toast (rnOptions : ReadableMap ) {
42
46
// get container View
43
- val options = toastOptionInit?.let { mergeMaps(it, options ) } ? : options
47
+ val options = toastOptionInit?.let { mergeMaps(it, rnOptions ) } ? : rnOptions
44
48
45
49
val container = getContainerView(R .layout.toast, options, " toast" )
46
50
val duration: Int = getDuration(options)
@@ -66,19 +70,46 @@ class TingModule internal constructor(context: ReactApplicationContext) : TingSp
66
70
setYOffset(48 )
67
71
setAnimStyle(toastAnim)
68
72
setOutsideTouchable(true )
69
- setOnClickListener(R .id.toast,
70
- EasyWindow .OnClickListener { toast: EasyWindow <* >, _: LinearLayout ? ->
71
- val shouldDismissByTap =
72
- if (options.hasKey(" shouldDismissByDrag" )) options.getBoolean(" shouldDismissByDrag" ) else true
73
- if (shouldDismissByTap) toast.cancel()
74
- })
73
+
74
+ if (options.hasKey(" shouldDismissByDrag" ) && options.getBoolean(" shouldDismissByDrag" )) {
75
+ // Define dragThreshold in density-independent pixels (dp)
76
+ val dragThresholdDP = 12
77
+ val scale = context.resources.displayMetrics.density
78
+ val dragThreshold = (dragThresholdDP * scale + 0.5f ).toInt()
79
+
80
+ // Add drag gesture recognizer
81
+ contentView?.let { contentView ->
82
+ val gestureDetector = GestureDetector (context, object : GestureDetector .SimpleOnGestureListener () {
83
+ override fun onScroll (e1 : MotionEvent , e2 : MotionEvent , distanceX : Float , distanceY : Float ): Boolean {
84
+ // Check if the user scrolls vertically and dismiss the toast window if needed
85
+ if (abs(distanceY) > abs(distanceX)) {
86
+ if (position == Gravity .TOP && distanceY > dragThreshold) { // Dismiss upward if toast is at the top
87
+ toastWindow.cancel()
88
+ return true
89
+ } else if (position == Gravity .BOTTOM && distanceY < - dragThreshold) { // Dismiss downward if toast is at the bottom
90
+ toastWindow.cancel()
91
+ return true
92
+ }
93
+ }
94
+
95
+ return super .onScroll(e1, e2, distanceX, distanceY)
96
+ }
97
+ })
98
+
99
+ contentView.setOnTouchListener(fun (_: View , event : MotionEvent ): Boolean {
100
+ gestureDetector.onTouchEvent(event)
101
+ return true // Consume the touch event
102
+ })
103
+ }
104
+ }
105
+
75
106
}.show()
76
107
}
77
108
}
78
109
79
110
@ReactMethod
80
- override fun alert (options : ReadableMap ) {
81
- val options = alertOptionInit?.let { mergeMaps(it, options ) } ? : options
111
+ override fun alert (rnOptions : ReadableMap ) {
112
+ val options = alertOptionInit?.let { mergeMaps(it, rnOptions ) } ? : rnOptions
82
113
83
114
val container = getContainerView(R .layout.alert, options, " alert" )
84
115
val duration: Int = getDuration(options)
0 commit comments