Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -4,10 +4,8 @@ import android.content.Context
|
|||||||
import androidx.compose.animation.*
|
import androidx.compose.animation.*
|
||||||
import androidx.compose.animation.core.*
|
import androidx.compose.animation.core.*
|
||||||
import androidx.compose.foundation.*
|
import androidx.compose.foundation.*
|
||||||
import androidx.compose.foundation.gestures.detectHorizontalDragGestures
|
|
||||||
import androidx.compose.foundation.gestures.awaitEachGesture
|
import androidx.compose.foundation.gestures.awaitEachGesture
|
||||||
import androidx.compose.foundation.gestures.awaitFirstDown
|
import androidx.compose.foundation.gestures.awaitFirstDown
|
||||||
import androidx.compose.foundation.gestures.horizontalDrag
|
|
||||||
import androidx.activity.compose.BackHandler
|
import androidx.activity.compose.BackHandler
|
||||||
import androidx.compose.foundation.layout.*
|
import androidx.compose.foundation.layout.*
|
||||||
import androidx.compose.foundation.lazy.LazyColumn
|
import androidx.compose.foundation.lazy.LazyColumn
|
||||||
@@ -28,6 +26,7 @@ import androidx.compose.ui.Modifier
|
|||||||
import androidx.compose.ui.draw.clip
|
import androidx.compose.ui.draw.clip
|
||||||
import androidx.compose.ui.draw.clipToBounds
|
import androidx.compose.ui.draw.clipToBounds
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
|
import androidx.compose.ui.input.pointer.changedToUpIgnoreConsumed
|
||||||
import androidx.compose.ui.input.pointer.pointerInput
|
import androidx.compose.ui.input.pointer.pointerInput
|
||||||
import androidx.compose.ui.input.pointer.positionChange
|
import androidx.compose.ui.input.pointer.positionChange
|
||||||
import androidx.compose.ui.input.pointer.util.VelocityTracker
|
import androidx.compose.ui.input.pointer.util.VelocityTracker
|
||||||
@@ -1694,6 +1693,8 @@ fun SwipeableDialogItem(
|
|||||||
.background(backgroundColor)
|
.background(backgroundColor)
|
||||||
.pointerInput(Unit) {
|
.pointerInput(Unit) {
|
||||||
val velocityTracker = VelocityTracker()
|
val velocityTracker = VelocityTracker()
|
||||||
|
val touchSlop = viewConfiguration.touchSlop
|
||||||
|
|
||||||
awaitEachGesture {
|
awaitEachGesture {
|
||||||
val down = awaitFirstDown(requireUnconsumed = false)
|
val down = awaitFirstDown(requireUnconsumed = false)
|
||||||
|
|
||||||
@@ -1701,22 +1702,58 @@ fun SwipeableDialogItem(
|
|||||||
if (isDrawerOpen) return@awaitEachGesture
|
if (isDrawerOpen) return@awaitEachGesture
|
||||||
|
|
||||||
velocityTracker.resetTracking()
|
velocityTracker.resetTracking()
|
||||||
var started = false
|
var totalDragX = 0f
|
||||||
try {
|
var totalDragY = 0f
|
||||||
horizontalDrag(down.id) { change ->
|
var passedSlop = false
|
||||||
val dragAmount = change.positionChange().x
|
var claimed = false
|
||||||
|
|
||||||
// First movement determines direction
|
while (true) {
|
||||||
if (!started) {
|
val event = awaitPointerEvent()
|
||||||
// Swipe right with actions closed — let drawer handle it
|
val change = event.changes.firstOrNull { it.id == down.id }
|
||||||
if (dragAmount > 0 && offsetX == 0f) {
|
?: break
|
||||||
return@horizontalDrag
|
if (change.changedToUpIgnoreConsumed()) break
|
||||||
|
|
||||||
|
val delta = change.positionChange()
|
||||||
|
totalDragX += delta.x
|
||||||
|
totalDragY += delta.y
|
||||||
|
|
||||||
|
if (!passedSlop) {
|
||||||
|
val dist = kotlin.math.sqrt(
|
||||||
|
totalDragX * totalDragX + totalDragY * totalDragY
|
||||||
|
)
|
||||||
|
if (dist < touchSlop) continue
|
||||||
|
|
||||||
|
val dominated = kotlin.math.abs(totalDragX) >
|
||||||
|
kotlin.math.abs(totalDragY) * 2.0f
|
||||||
|
|
||||||
|
when {
|
||||||
|
// Horizontal left swipe — reveal action buttons
|
||||||
|
dominated && totalDragX < 0 -> {
|
||||||
|
passedSlop = true
|
||||||
|
claimed = true
|
||||||
|
onSwipeStarted()
|
||||||
|
change.consume()
|
||||||
|
}
|
||||||
|
// Horizontal right swipe with buttons open — close them
|
||||||
|
dominated && totalDragX > 0 && offsetX != 0f -> {
|
||||||
|
passedSlop = true
|
||||||
|
claimed = true
|
||||||
|
change.consume()
|
||||||
|
}
|
||||||
|
// Right swipe with buttons closed — let drawer handle
|
||||||
|
totalDragX > 0 && offsetX == 0f -> break
|
||||||
|
// Vertical/diagonal — close buttons if open, let LazyColumn scroll
|
||||||
|
else -> {
|
||||||
|
if (offsetX != 0f) {
|
||||||
|
offsetX = 0f
|
||||||
|
onSwipeClosed()
|
||||||
|
}
|
||||||
|
break
|
||||||
}
|
}
|
||||||
started = true
|
|
||||||
onSwipeStarted()
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
val newOffset = offsetX + dragAmount
|
// Gesture is ours — update offset
|
||||||
|
val newOffset = offsetX + delta.x
|
||||||
offsetX = newOffset.coerceIn(-swipeWidthPx, 0f)
|
offsetX = newOffset.coerceIn(-swipeWidthPx, 0f)
|
||||||
velocityTracker.addPosition(
|
velocityTracker.addPosition(
|
||||||
change.uptimeMillis,
|
change.uptimeMillis,
|
||||||
@@ -1724,20 +1761,30 @@ fun SwipeableDialogItem(
|
|||||||
)
|
)
|
||||||
change.consume()
|
change.consume()
|
||||||
}
|
}
|
||||||
} catch (_: Exception) {
|
|
||||||
offsetX = 0f
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (started) {
|
// Snap animation
|
||||||
|
if (claimed) {
|
||||||
val velocity = velocityTracker.calculateVelocity().x
|
val velocity = velocityTracker.calculateVelocity().x
|
||||||
// Telegram-like: fling left (-velocity) OR dragged past 1/3
|
when {
|
||||||
val shouldOpen = velocity < -300f ||
|
// Rightward fling — always close
|
||||||
kotlin.math.abs(offsetX) > swipeWidthPx / 3
|
velocity > 150f -> {
|
||||||
if (shouldOpen) {
|
offsetX = 0f
|
||||||
offsetX = -swipeWidthPx
|
onSwipeClosed()
|
||||||
} else {
|
}
|
||||||
offsetX = 0f
|
// Strong leftward fling — always open
|
||||||
onSwipeClosed()
|
velocity < -300f -> {
|
||||||
|
offsetX = -swipeWidthPx
|
||||||
|
}
|
||||||
|
// Past halfway — stay open
|
||||||
|
kotlin.math.abs(offsetX) > swipeWidthPx / 2 -> {
|
||||||
|
offsetX = -swipeWidthPx
|
||||||
|
}
|
||||||
|
// Less than halfway — close
|
||||||
|
else -> {
|
||||||
|
offsetX = 0f
|
||||||
|
onSwipeClosed()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user