Skip to content

Commit 00fe215

Browse files
authored
[Jetchat] Fix RecordButton sending cancellation callback continuously (#1190)
- Fixes `RecordButton` sending `onCancelled` continuously until the finger is raised, then still sending `onFinished` - Refactors to not need `composed {}`
2 parents 3c77c2b + 04ad3ed commit 00fe215

File tree

1 file changed

+33
-23
lines changed
  • Jetchat/app/src/main/java/com/example/compose/jetchat/conversation

1 file changed

+33
-23
lines changed

Jetchat/app/src/main/java/com/example/compose/jetchat/conversation/RecordButton.kt

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,9 @@ import androidx.compose.runtime.Composable
4444
import androidx.compose.runtime.remember
4545
import androidx.compose.runtime.rememberCoroutineScope
4646
import androidx.compose.ui.Modifier
47-
import androidx.compose.ui.composed
4847
import androidx.compose.ui.draw.clip
4948
import androidx.compose.ui.graphics.graphicsLayer
5049
import androidx.compose.ui.input.pointer.pointerInput
51-
import androidx.compose.ui.platform.LocalDensity
5250
import androidx.compose.ui.res.stringResource
5351
import androidx.compose.ui.unit.Dp
5452
import 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

Comments
 (0)