Skip to content

Commit

Permalink
fix: inconsistent audio message length [WPB-3334] (#2031)
Browse files Browse the repository at this point in the history
  • Loading branch information
trOnk12 committed Aug 2, 2023
1 parent 12fd721 commit 6d48b50
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ data class AudioState(
val DEFAULT = AudioState(AudioMediaPlayingState.Stopped, 0, TotalTimeInMs.NotKnown)
}

// before the user decides to play audio message, we are not able to determine total time ourself using
// MediaPlayer API, we are relying on the info retrieved from the other client, until then
fun sanitizeTotalTime(otherClientTotalTime: Int): AudioState {
if (totalTimeInMs is TotalTimeInMs.NotKnown) {
if (otherClientTotalTime != 0) {
return copy(totalTimeInMs = TotalTimeInMs.Known(otherClientTotalTime))
}
// if the back-end returned the total time, we use that, in case it didn't we use what we get from
// the [ConversationAudioMessagePlayer.kt] which will emit the time once the users play the audio.
fun sanitizeTotalTime(otherClientTotalTime: Int): TotalTimeInMs {
if (otherClientTotalTime != 0) {
return TotalTimeInMs.Known(otherClientTotalTime)
}

return this
return totalTimeInMs
}

sealed class TotalTimeInMs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.wire.android.media.audiomessage

import android.content.Context
import android.media.MediaPlayer
import android.media.MediaPlayer.SEEK_CLOSEST_SYNC
import android.net.Uri
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.feature.asset.GetMessageAssetUseCase
Expand Down Expand Up @@ -110,7 +111,9 @@ class ConversationAudioMessagePlayer
audioMessageStateHistory = audioMessageStateHistory.toMutableMap().apply {
put(
audioMessageStateUpdate.messageId,
currentState.copy(totalTimeInMs = AudioState.TotalTimeInMs.Known(audioMessageStateUpdate.totalTimeInMs))
currentState.copy(
totalTimeInMs = AudioState.TotalTimeInMs.Known(audioMessageStateUpdate.totalTimeInMs)
)
)
}
}
Expand Down Expand Up @@ -241,7 +244,7 @@ class ConversationAudioMessagePlayer
val isAudioMessageCurrentlyPlaying = currentAudioMessageId == messageId

if (isAudioMessageCurrentlyPlaying) {
audioMediaPlayer.seekTo(position)
audioMediaPlayer.seekTo(position.toLong(), SEEK_CLOSEST_SYNC)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ fun MessageItem(

val isProfileRedirectEnabled =
header.userId != null &&
!(header.isSenderDeleted || header.isSenderUnavailable)
!(header.isSenderDeleted || header.isSenderUnavailable)

if (showAuthor) {
val avatarClickable = remember {
Expand Down Expand Up @@ -524,14 +524,14 @@ private fun MessageContent(
val audioMessageState: AudioState = audioMessagesState[message.header.messageId]
?: AudioState.DEFAULT

val adjustedMessageState: AudioState = remember(audioMessagesState) {
val totalTimeInMs = remember(audioMessageState.totalTimeInMs) {
audioMessageState.sanitizeTotalTime(messageContent.audioMessageDurationInMs.toInt())
}

AudioMessage(
audioMediaPlayingState = adjustedMessageState.audioMediaPlayingState,
totalTimeInMs = adjustedMessageState.totalTimeInMs,
currentPositionInMs = adjustedMessageState.currentPositionInMs,
audioMediaPlayingState = audioMessageState.audioMediaPlayingState,
totalTimeInMs = totalTimeInMs,
currentPositionInMs = audioMessageState.currentPositionInMs,
onPlayButtonClick = { onAudioClick(message.header.messageId) },
onSliderPositionChange = { position ->
onChangeAudioPosition(message.header.messageId, position.toInt())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ import com.wire.android.navigation.getBackNavArg
import com.wire.android.ui.home.conversations.ConversationSnackbarMessages
import com.wire.android.ui.home.conversations.ConversationSnackbarMessages.OnResetSession
import com.wire.android.ui.home.conversations.model.AssetBundle
import com.wire.kalium.logic.data.asset.AttachmentType
import com.wire.android.ui.home.conversations.model.UIMessage
import com.wire.android.ui.home.conversations.usecase.GetMessagesForConversationUseCase
import com.wire.android.util.FileManager
import com.wire.android.util.dispatchers.DispatcherProvider
import com.wire.android.util.startFileShareIntent
import com.wire.android.util.ui.UIText
import com.wire.kalium.logic.data.asset.AttachmentType
import com.wire.kalium.logic.data.conversation.ClientId
import com.wire.kalium.logic.data.id.QualifiedID
import com.wire.kalium.logic.data.id.QualifiedIdMapper
Expand Down

0 comments on commit 6d48b50

Please sign in to comment.