-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[Jetchat] Add recording audio button #1140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1464177
[Jetchat] Add recording audio button
JolandaVerhoef 1418624
🤖 Apply Spotless
JolandaVerhoef ba3b55f
[Jetchat] Add recording audio button - Review comments
JolandaVerhoef 69e2eb5
Merge remote-tracking branch 'github/jv/jetchat-audio-record' into jv…
JolandaVerhoef 2878e9a
🤖 Apply Spotless
JolandaVerhoef 81d688c
[Jetchat] Use composed modifier and better state management
JolandaVerhoef 5bbfaac
Merge remote-tracking branch 'github/jv/jetchat-audio-record' into jv…
JolandaVerhoef 89b45fe
🤖 Apply Spotless
JolandaVerhoef d19d283
[Jetchat] Better state management
JolandaVerhoef 159183a
Merge remote-tracking branch 'github/jv/jetchat-audio-record' into jv…
JolandaVerhoef 0271903
[Jetchat] Fix broken tests
JolandaVerhoef 204f62e
🤖 Apply Spotless
JolandaVerhoef File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
169 changes: 169 additions & 0 deletions
169
Jetchat/app/src/main/java/com/example/compose/jetchat/conversation/RecordButton.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| /* | ||
| * Copyright 2023 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.compose.jetchat.conversation | ||
|
|
||
| import androidx.compose.animation.animateColor | ||
| import androidx.compose.animation.core.Spring | ||
| import androidx.compose.animation.core.animateFloat | ||
| import androidx.compose.animation.core.spring | ||
| import androidx.compose.animation.core.tween | ||
| import androidx.compose.animation.core.updateTransition | ||
| import androidx.compose.foundation.background | ||
| import androidx.compose.foundation.clickable | ||
| import androidx.compose.foundation.gestures.detectDragGesturesAfterLongPress | ||
| import androidx.compose.foundation.gestures.detectTapGestures | ||
| import androidx.compose.foundation.layout.Box | ||
| import androidx.compose.foundation.layout.aspectRatio | ||
| import androidx.compose.foundation.layout.padding | ||
| import androidx.compose.foundation.layout.sizeIn | ||
| import androidx.compose.foundation.shape.CircleShape | ||
| import androidx.compose.material.icons.Icons | ||
| import androidx.compose.material.icons.filled.Mic | ||
| import androidx.compose.material3.ExperimentalMaterial3Api | ||
| import androidx.compose.material3.Icon | ||
| import androidx.compose.material3.LocalContentColor | ||
| import androidx.compose.material3.RichTooltipBox | ||
| import androidx.compose.material3.RichTooltipState | ||
| import androidx.compose.material3.Text | ||
| import androidx.compose.material3.contentColorFor | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.MutableState | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.rememberCoroutineScope | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.draw.clip | ||
| import androidx.compose.ui.graphics.graphicsLayer | ||
| import androidx.compose.ui.input.pointer.pointerInput | ||
| import androidx.compose.ui.platform.LocalDensity | ||
| import androidx.compose.ui.res.stringResource | ||
| import androidx.compose.ui.unit.dp | ||
| import com.example.compose.jetchat.R | ||
| import kotlinx.coroutines.launch | ||
| import kotlin.math.abs | ||
|
|
||
| val swipeToCancelThreshold: () -> Float | ||
| @Composable get() = with(LocalDensity.current) { { 200.dp.toPx() } } | ||
|
|
||
| private val verticalThreshold: () -> Float | ||
| @Composable get() = with(LocalDensity.current) { { 80.dp.toPx() } } | ||
|
|
||
| @OptIn(ExperimentalMaterial3Api::class) | ||
| @Composable | ||
| fun RecordButton( | ||
| recording: Boolean, | ||
| swipeOffset: MutableState<Float>, | ||
| onStartRecording: () -> Boolean, | ||
| onFinishRecording: () -> Unit, | ||
| onCancelRecording: () -> Unit, | ||
| modifier: Modifier = Modifier | ||
| ) { | ||
| val transition = updateTransition(targetState = recording, label = "record") | ||
| val scale = transition.animateFloat( | ||
| transitionSpec = { spring(Spring.DampingRatioMediumBouncy, Spring.StiffnessLow) }, | ||
| label = "record-scale", | ||
| targetValueByState = { rec -> if (rec) 2f else 1f } | ||
| ) | ||
| val containerAlpha = transition.animateFloat( | ||
| transitionSpec = { tween(2000) }, | ||
riggaroo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| label = "record-scale", | ||
| targetValueByState = { rec -> if (rec) 1f else 0f } | ||
| ) | ||
| val iconColor = transition.animateColor( | ||
| transitionSpec = { tween(200) }, | ||
| label = "record-scale", | ||
| targetValueByState = { rec -> | ||
| if (rec) contentColorFor(LocalContentColor.current) | ||
| else LocalContentColor.current | ||
| } | ||
| ) | ||
|
|
||
| Box { | ||
| // Background during recording | ||
| Box( | ||
| Modifier | ||
| .matchParentSize() | ||
| .aspectRatio(1f) | ||
| .graphicsLayer { | ||
| alpha = containerAlpha.value | ||
| scaleX = scale.value; scaleY = scale.value | ||
| } | ||
| .clip(CircleShape) | ||
| .background(LocalContentColor.current) | ||
| ) | ||
| val scope = rememberCoroutineScope() | ||
| val tooltipState = remember { RichTooltipState() } | ||
| RichTooltipBox( | ||
| text = { Text(stringResource(R.string.touch_and_hold_to_record)) }, | ||
| tooltipState = tooltipState | ||
| ) { | ||
| Icon( | ||
| Icons.Default.Mic, | ||
| contentDescription = "Record voice message", | ||
JolandaVerhoef marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| tint = iconColor.value, | ||
| modifier = modifier | ||
| .sizeIn(minWidth = 56.dp, minHeight = 6.dp) | ||
| .padding(18.dp) | ||
| .clickable { } | ||
| .voiceRecordingGesture( | ||
| swipeOffset, | ||
| swipeToCancelThreshold(), | ||
| verticalThreshold(), | ||
| { scope.launch { tooltipState.show() } }, | ||
| onStartRecording, | ||
| onFinishRecording, | ||
| onCancelRecording, | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun Modifier.voiceRecordingGesture( | ||
| horizontalSwipeProgress: MutableState<Float>, | ||
| swipeToCancelThreshold: Float, | ||
JolandaVerhoef marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| verticalThreshold: Float, | ||
| onClick: () -> Unit, | ||
| onStartRecording: () -> Boolean, | ||
| onFinishRecording: () -> Unit, | ||
| onCancelRecording: () -> Unit, | ||
| ): Modifier { | ||
| var offsetY = 0f | ||
| return this | ||
| .pointerInput(Unit) { detectTapGestures { onClick() } } | ||
| .pointerInput(Unit) { | ||
| detectDragGesturesAfterLongPress( | ||
| onDragStart = { | ||
| horizontalSwipeProgress.value = 0f | ||
| offsetY = 0f | ||
| onStartRecording() | ||
| }, | ||
| onDragCancel = { onCancelRecording() }, | ||
| onDragEnd = { onFinishRecording() }, | ||
| onDrag = { _, dragAmount -> | ||
| horizontalSwipeProgress.value += dragAmount.x | ||
| offsetY += dragAmount.y | ||
| val offsetX = horizontalSwipeProgress.value | ||
riggaroo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if ((offsetX < 0) && | ||
| abs(offsetX) >= swipeToCancelThreshold && | ||
| abs(offsetY) <= verticalThreshold | ||
| ) { | ||
| onCancelRecording() | ||
| } | ||
| } | ||
| ) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.