@@ -44,11 +44,9 @@ import androidx.compose.runtime.Composable
4444import androidx.compose.runtime.remember
4545import androidx.compose.runtime.rememberCoroutineScope
4646import androidx.compose.ui.Modifier
47- import androidx.compose.ui.composed
4847import androidx.compose.ui.draw.clip
4948import androidx.compose.ui.graphics.graphicsLayer
5049import androidx.compose.ui.input.pointer.pointerInput
51- import androidx.compose.ui.platform.LocalDensity
5250import androidx.compose.ui.res.stringResource
5351import androidx.compose.ui.unit.Dp
5452import androidx.compose.ui.unit.dp
@@ -136,33 +134,45 @@ private fun Modifier.voiceRecordingGesture(
136134 onCancelRecording : () -> Unit = {},
137135 swipeToCancelThreshold : Dp = 200.dp,
138136 verticalThreshold : Dp = 80.dp,
139- ): Modifier = composed {
140- val density = LocalDensity .current
141- val swipeToCancelThresholdPx = with (density) { swipeToCancelThreshold.toPx() }
142- val verticalThresholdPx = with (density) { verticalThreshold.toPx() }
143- var offsetY = 0f
144- this
145- .pointerInput(Unit ) { detectTapGestures { onClick() } }
146- .pointerInput(Unit ) {
147- detectDragGesturesAfterLongPress(
148- onDragStart = {
149- onSwipeProgressChanged(0f )
150- offsetY = 0f
151- onStartRecording()
152- },
153- onDragCancel = { onCancelRecording() },
154- onDragEnd = { onFinishRecording() },
155- onDrag = { _, dragAmount ->
137+ ): Modifier = this
138+ .pointerInput(Unit ) { detectTapGestures { onClick() } }
139+ .pointerInput(Unit ) {
140+ var offsetY = 0f
141+ var dragging = false
142+ val swipeToCancelThresholdPx = swipeToCancelThreshold.toPx()
143+ val verticalThresholdPx = verticalThreshold.toPx()
144+
145+ detectDragGesturesAfterLongPress(
146+ onDragStart = {
147+ onSwipeProgressChanged(0f )
148+ offsetY = 0f
149+ dragging = true
150+ onStartRecording()
151+ },
152+ onDragCancel = {
153+ onCancelRecording()
154+ dragging = false
155+ },
156+ onDragEnd = {
157+ if (dragging) {
158+ onFinishRecording()
159+ }
160+ dragging = false
161+ },
162+ onDrag = { change, dragAmount ->
163+ if (dragging) {
156164 onSwipeProgressChanged(horizontalSwipeProgress() + dragAmount.x)
157165 offsetY + = dragAmount.y
158166 val offsetX = horizontalSwipeProgress()
159- if ((offsetX < 0 ) &&
167+ if (
168+ offsetX < 0 &&
160169 abs(offsetX) >= swipeToCancelThresholdPx &&
161170 abs(offsetY) <= verticalThresholdPx
162171 ) {
163172 onCancelRecording()
173+ dragging = false
164174 }
165175 }
166- )
167- }
168- }
176+ }
177+ )
178+ }
0 commit comments