Simplify GestureListener
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
package app.lawnchair.gestures
|
||||
|
||||
import android.view.View.OnTouchListener
|
||||
import android.view.GestureDetector
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.view.MotionEvent
|
||||
import android.view.GestureDetector
|
||||
import android.view.GestureDetector.SimpleOnGestureListener
|
||||
import android.view.MotionEvent
|
||||
import android.view.View
|
||||
import java.lang.Exception
|
||||
import android.view.View.OnTouchListener
|
||||
import kotlin.math.abs
|
||||
|
||||
open class DirectionalGestureListener(ctx: Context?) : OnTouchListener {
|
||||
private val mGestureDetector: GestureDetector
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
override fun onTouch(v: View, event: MotionEvent): Boolean {
|
||||
return mGestureDetector.onTouchEvent(event)
|
||||
@@ -26,32 +26,29 @@ open class DirectionalGestureListener(ctx: Context?) : OnTouchListener {
|
||||
return true
|
||||
}
|
||||
|
||||
private fun shouldReactToSwipe(diff: Float, velocity: Float): Boolean =
|
||||
abs(diff) > SWIPE_THRESHOLD && abs(velocity) > SWIPE_VELOCITY_THRESHOLD
|
||||
|
||||
override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
|
||||
var result = false
|
||||
try {
|
||||
return try {
|
||||
val diffY = e2.y - e1.y
|
||||
val diffX = e2.x - e1.x
|
||||
if (abs(diffX) > abs(diffY)) {
|
||||
if (abs(diffX) > SWIPE_THRESHOLD && abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
|
||||
if (diffX > 0) {
|
||||
onSwipeRight()
|
||||
} else {
|
||||
onSwipeLeft()
|
||||
}
|
||||
result = true
|
||||
|
||||
when {
|
||||
abs(diffX) > abs(diffY) && shouldReactToSwipe(diffX, velocityX) -> {
|
||||
if (diffX > 0) onSwipeRight() else onSwipeLeft()
|
||||
true
|
||||
}
|
||||
} else if (abs(diffY) > SWIPE_THRESHOLD && abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
|
||||
if (diffY > 0) {
|
||||
onSwipeBottom()
|
||||
} else {
|
||||
onSwipeTop()
|
||||
shouldReactToSwipe(diffY, velocityY) -> {
|
||||
if (diffY > 0) onSwipeBottom() else onSwipeTop()
|
||||
true
|
||||
}
|
||||
result = true
|
||||
else -> false
|
||||
}
|
||||
} catch (exception: Exception) {
|
||||
exception.printStackTrace()
|
||||
false
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user