Skip to content

Commit b58cc05

Browse files
committed
Fixed waveforms, they are now drawn in bounds
Signed-off-by: rapterjet2004 <juliuslinus1@gmail.com>
1 parent 9e7c818 commit b58cc05

3 files changed

Lines changed: 32 additions & 18 deletions

File tree

app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -924,10 +924,12 @@ class ChatActivity :
924924
// mediaController logic to viewModel, the goal is to avoid launching coroutines in
925925
// the ChatActivity. I want to clean up the whole syncVoice thing with Player.Listener
926926
onVoicePlayPauseClick = { onVoiceClick(it) },
927-
onVoiceSeek = { _, progress ->
927+
onVoiceSeek = { id, progress ->
928928
mediaController?.let { controller ->
929-
val pos = controller.duration * progress / 100f
930-
controller.seekTo(pos.toLong())
929+
if (id.toString() == controller.currentMediaItem?.mediaId) {
930+
val pos = controller.duration * progress / 100f
931+
controller.seekTo(pos.toLong())
932+
}
931933
}},
932934
onVoiceSpeedClick = { onVoiceSpeedClickCompose(it) },
933935

@@ -1131,16 +1133,20 @@ class ChatActivity :
11311133
message: ChatMessage
11321134
): Boolean {
11331135
val currentMessageId = message.jsonMessageId.toString()
1134-
if (controller.currentMediaItem?.mediaId != currentMessageId) {
1135-
val filename = message.fileParameters.name
1136-
if (filename.isEmpty()) {
1137-
return true
1138-
}
1136+
val filename = message.fileParameters.name
1137+
if (filename.isEmpty()) {
1138+
return true
1139+
}
1140+
1141+
val file = FileUtils.resolveSharedAttachmentFile(context.cacheDir, filename) ?: return true
1142+
val fileURI = file.toUri()
1143+
val filePath = fileURI.toString()
11391144

1140-
val file = FileUtils.resolveSharedAttachmentFile(context.cacheDir, filename) ?: return true
1141-
val fileURI = file.toUri()
1142-
val filePath = fileURI.toString()
1145+
chatViewModel.syncVoiceMessageUiState(message.apply {
1146+
voiceMessageDuration = getAudioDuration(filePath).toInt()
1147+
})
11431148

1149+
if (controller.currentMediaItem?.mediaId != currentMessageId) {
11441150
if (!file.exists()) {
11451151
downloadFileToCache(message, true) {
11461152
setupAndPlay(controller, message, filePath)
@@ -1160,6 +1166,10 @@ class ChatActivity :
11601166
val message = chatViewModel.getMessageById(messageId.toLong()).first()
11611167

11621168
mediaController?.let { controller ->
1169+
// If a message is still playing, it must be paused first before another can be loaded
1170+
val currentMessageId = message.jsonMessageId.toString()
1171+
if (controller.isPlaying && controller.currentMediaItem?.mediaId != currentMessageId) return@launch
1172+
11631173
chatViewModel.currentVoiceMessage = message
11641174

11651175
// If the controller is playing a new voice message, initialize

app/src/main/java/com/nextcloud/talk/ui/ComposeWaveformSeekbar.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import androidx.compose.foundation.background
1111
import androidx.compose.foundation.layout.Box
1212
import androidx.compose.foundation.layout.fillMaxWidth
1313
import androidx.compose.foundation.layout.height
14+
import androidx.compose.foundation.layout.padding
1415
import androidx.compose.foundation.layout.size
1516
import androidx.compose.foundation.shape.CircleShape
1617
import androidx.compose.material3.ExperimentalMaterial3Api
@@ -50,16 +51,18 @@ fun ComposeWaveformSeekBar(value: Float, onValueChange: (Float) -> Unit, modifie
5051
.drawWithCache {
5152
onDrawBehind {
5253
val height = this.size.height
53-
val width = this.size.width
54+
val width = this.size.width + 8.dp.value
5455
val midpoint = (this.size.height / 2f)
5556

5657
val barGap = (width - waveData.size * barWidth) / (waveData.size - 1).toFloat() + 1
5758
for (i in waveData.indices) {
5859
val x: Float = i * (barWidth + barGap)
59-
val y: Float = waveData[i] * height
6060

61-
// TODO - figure out why the thumb is offset early on and fix this
62-
val isXBeforeThumb = (x / this.size.width) <= value + 0.08
61+
if (x < 0f || x > size.width) continue
62+
63+
val y: Float = (waveData[i] * height).coerceIn(0f, midpoint)
64+
65+
val isXBeforeThumb = x <= value * width
6366

6467
drawLine(
6568
if (isXBeforeThumb) inversePrimary else onPrimaryContainer,
@@ -94,7 +97,8 @@ fun Preview() {
9497
{},
9598
modifier = Modifier
9699
.height(MAX_HEIGHT.dp)
97-
.fillMaxWidth(),
100+
.fillMaxWidth()
101+
.padding(8.dp),
98102
waveData,
99103
true
100104
)

app/src/main/java/com/nextcloud/talk/ui/chat/VoiceMessage.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fun VoiceMessage(
6868
remember(inversePrimaryColor) { inversePrimaryColor.toArgb() }
6969
val onPrimaryContainerColor = colorScheme.onPrimaryContainer
7070
remember(onPrimaryContainerColor) { onPrimaryContainerColor.toArgb() }
71-
val remainingSeconds = (typeContent.playedSeconds) // FIXME - make this to count down eventually
71+
val remainingSeconds = (typeContent.durationSeconds - typeContent.playedSeconds)
7272
val waveformData = remember(typeContent.waveform) {
7373
val floatArr = typeContent.waveform.toFloatArray()
7474
if (floatArr.size < WAVEFORM_SIZE) {
@@ -119,7 +119,7 @@ fun VoiceMessage(
119119
.fillMaxWidth()
120120
.padding(8.dp), // or weight(1f),
121121
waveformData,
122-
enabled = typeContent.isPlaying,
122+
enabled = true,
123123
)
124124

125125
TextButton(

0 commit comments

Comments
 (0)