mirror of
https://github.com/anyproto/anytype-kotlin.git
synced 2025-06-08 05:47:05 +09:00
Editor | Drag & drop sensitivity (#2068)
This commit is contained in:
parent
20fad57d37
commit
2db338e2f6
1 changed files with 46 additions and 2 deletions
|
@ -1,9 +1,12 @@
|
|||
package com.anytypeio.anytype.core_ui.features.editor
|
||||
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.view.MotionEvent
|
||||
import android.view.View
|
||||
import android.view.ViewConfiguration
|
||||
import timber.log.Timber
|
||||
import kotlin.math.abs
|
||||
|
||||
/**
|
||||
* @property [fallback] fallback method for processing touch event.
|
||||
|
@ -16,8 +19,28 @@ class EditorTouchProcessor(
|
|||
var onDragAndDropTrigger: () -> Unit = {}
|
||||
) {
|
||||
|
||||
private val actionHandler = Handler()
|
||||
private val dragAndDropTimeoutRunnable = Runnable { onDragAndDropTrigger() }
|
||||
val moves = mutableListOf<Float>()
|
||||
|
||||
private val actionHandler = Handler(Looper.getMainLooper())
|
||||
|
||||
private val dragAndDropTimeoutRunnable = Runnable {
|
||||
Timber.d("Runnable triggered")
|
||||
if (moves.size > 1) {
|
||||
val first = moves.first()
|
||||
val last = moves.last()
|
||||
val delta = abs(first - last)
|
||||
if (delta == 0f) {
|
||||
Timber.d("Runnable dispatched 1")
|
||||
onDragAndDropTrigger()
|
||||
} else {
|
||||
Timber.d("Runnable ignored 1")
|
||||
}
|
||||
} else {
|
||||
Timber.d("Runnable dispatched 2")
|
||||
onDragAndDropTrigger()
|
||||
}
|
||||
moves.clear()
|
||||
}
|
||||
|
||||
private var actionUpStartInMillis: Long = 0
|
||||
|
||||
|
@ -25,13 +48,31 @@ class EditorTouchProcessor(
|
|||
if (event != null) {
|
||||
when (event.action) {
|
||||
MotionEvent.ACTION_DOWN -> {
|
||||
Timber.d("ACTION DOWN")
|
||||
actionUpStartInMillis = System.currentTimeMillis()
|
||||
actionHandler.postDelayed(
|
||||
dragAndDropTimeoutRunnable,
|
||||
DND_TIMEOUT
|
||||
)
|
||||
moves.clear()
|
||||
}
|
||||
MotionEvent.ACTION_MOVE -> {
|
||||
Timber.d("ACTION MOVE: $event")
|
||||
moves.add(event.getY(0))
|
||||
if (moves.size > 1) {
|
||||
val first = moves.first()
|
||||
val last = moves.last()
|
||||
Timber.d("ACTION MOVE DELTA: ${abs(last-first)}")
|
||||
}
|
||||
}
|
||||
MotionEvent.ACTION_CANCEL -> {
|
||||
Timber.d("ACTION CANCEL")
|
||||
actionHandler.removeCallbacksAndMessages(null)
|
||||
moves.clear()
|
||||
}
|
||||
MotionEvent.ACTION_UP -> {
|
||||
moves.clear()
|
||||
Timber.d("ACTION UP")
|
||||
actionHandler.removeCallbacksAndMessages(null)
|
||||
return when (System.currentTimeMillis() - actionUpStartInMillis) {
|
||||
in LONG_PRESS_TIMEOUT..DND_TIMEOUT -> {
|
||||
|
@ -44,6 +85,9 @@ class EditorTouchProcessor(
|
|||
}
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
Timber.d("Ignored motion event: $event")
|
||||
}
|
||||
}
|
||||
}
|
||||
return fallback(event)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue