Display duration of recorded voice message (#1733)
--------- Co-authored-by: ElementBot <benoitm+elementbot@element.io>
This commit is contained in:
parent
0e89080a50
commit
413ec4b5db
12 changed files with 119 additions and 40 deletions
|
|
@ -36,7 +36,7 @@ class VoiceMessageComposerPlayer @Inject constructor(
|
|||
|
||||
val state: Flow<State> = mediaPlayer.state.map { state ->
|
||||
if (lastPlayedMediaPath == null || lastPlayedMediaPath != state.mediaId) {
|
||||
return@map State.NotPlaying
|
||||
return@map State.NotLoaded
|
||||
}
|
||||
|
||||
State(
|
||||
|
|
@ -89,13 +89,15 @@ class VoiceMessageComposerPlayer @Inject constructor(
|
|||
val duration: Long,
|
||||
) {
|
||||
companion object {
|
||||
val NotPlaying = State(
|
||||
val NotLoaded = State(
|
||||
isPlaying = false,
|
||||
currentPosition = 0L,
|
||||
duration = 0L,
|
||||
)
|
||||
}
|
||||
|
||||
val isLoaded get() = this != NotLoaded
|
||||
|
||||
/**
|
||||
* The progress of this player between 0 and 1.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -42,14 +42,15 @@ import io.element.android.libraries.textcomposer.model.VoiceMessageState
|
|||
import io.element.android.libraries.voicerecorder.api.VoiceRecorder
|
||||
import io.element.android.libraries.voicerecorder.api.VoiceRecorderState
|
||||
import io.element.android.services.analytics.api.AnalyticsService
|
||||
import kotlinx.collections.immutable.toPersistentList
|
||||
import kotlinx.collections.immutable.ImmutableList
|
||||
import kotlinx.collections.immutable.toImmutableList
|
||||
import kotlinx.collections.immutable.toPersistentList
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
import javax.inject.Inject
|
||||
import kotlin.time.Duration
|
||||
import kotlin.time.Duration.Companion.milliseconds
|
||||
|
||||
@SingleIn(RoomScope::class)
|
||||
|
|
@ -72,8 +73,8 @@ class VoiceMessageComposerPresenter @Inject constructor(
|
|||
|
||||
val permissionState = permissionsPresenter.present()
|
||||
var isSending by remember { mutableStateOf(false) }
|
||||
val playerState by player.state.collectAsState(initial = VoiceMessageComposerPlayer.State.NotPlaying)
|
||||
val isPlaying by remember(playerState.isPlaying) { derivedStateOf { playerState.isPlaying } }
|
||||
val playerState by player.state.collectAsState(initial = VoiceMessageComposerPlayer.State.NotLoaded)
|
||||
val playerTime by remember(playerState, recorderState) { derivedStateOf { displayTime(playerState, recorderState) } }
|
||||
val waveform by remember(recorderState) { derivedStateOf { recorderState.finishedWaveform() } }
|
||||
|
||||
val onLifecycleEvent = { event: Lifecycle.Event ->
|
||||
|
|
@ -190,9 +191,10 @@ class VoiceMessageComposerPresenter @Inject constructor(
|
|||
)
|
||||
is VoiceRecorderState.Finished -> VoiceMessageState.Preview(
|
||||
isSending = isSending,
|
||||
isPlaying = isPlaying,
|
||||
isPlaying = playerState.isPlaying,
|
||||
showCursor = playerState.isLoaded && !isSending,
|
||||
playbackProgress = playerState.progress,
|
||||
time = playerState.currentPosition.milliseconds,
|
||||
time = playerTime,
|
||||
waveform = waveform,
|
||||
)
|
||||
else -> VoiceMessageState.Idle
|
||||
|
|
@ -259,3 +261,20 @@ private fun VoiceRecorderState.finishedWaveform(): ImmutableList<Float> =
|
|||
?.waveform
|
||||
.orEmpty()
|
||||
.toImmutableList()
|
||||
|
||||
/**
|
||||
* The time to display depending on the player state.
|
||||
*
|
||||
* Either the current position or total duration.
|
||||
*/
|
||||
private fun displayTime(
|
||||
playerState: VoiceMessageComposerPlayer.State,
|
||||
recording: VoiceRecorderState
|
||||
): Duration = when {
|
||||
playerState.isLoaded ->
|
||||
playerState.currentPosition.milliseconds
|
||||
recording is VoiceRecorderState.Finished ->
|
||||
recording.duration
|
||||
else ->
|
||||
0.milliseconds
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue