code review: remove unsafe casting to avoid app crash if a non-voice reply context leaks through

lint fixes
This commit is contained in:
davotoula
2025-12-15 18:55:34 +01:00
parent 555fc983f6
commit 2a0eeed55f
3 changed files with 10 additions and 14 deletions
@@ -20,7 +20,6 @@
*/ */
package com.vitorpamplona.amethyst.ui.actions.uploads package com.vitorpamplona.amethyst.ui.actions.uploads
import android.content.Context
import android.media.MediaPlayer import android.media.MediaPlayer
import android.util.Log import android.util.Log
import androidx.compose.foundation.background import androidx.compose.foundation.background
@@ -77,7 +76,7 @@ fun VoiceMessagePreview(
// Initialize MediaPlayer // Initialize MediaPlayer
DisposableEffect(voiceMetadata.url, localFile) { DisposableEffect(voiceMetadata.url, localFile) {
val player = createMediaPlayer(context, voiceMetadata.url, localFile) val player = createMediaPlayer(voiceMetadata.url, localFile)
player?.setOnCompletionListener { player?.setOnCompletionListener {
isPlaying = false isPlaying = false
progress = 0f progress = 0f
@@ -242,7 +241,6 @@ fun VoiceMessagePreview(
} }
private fun createMediaPlayer( private fun createMediaPlayer(
context: Context,
url: String, url: String,
localFile: File?, localFile: File?,
): MediaPlayer? = ): MediaPlayer? =
@@ -256,5 +254,6 @@ private fun createMediaPlayer(
prepare() prepare()
} }
} catch (e: Exception) { } catch (e: Exception) {
Log.w("VoiceMessagePreview", "Failed to create MediaPlayer", e)
null null
} }
@@ -479,16 +479,12 @@ private fun NewPostScreenBody(
) )
} }
BottomRowActions(postViewModel, accountViewModel) BottomRowActions(postViewModel)
} }
} }
@Composable @Composable
private fun BottomRowActions( private fun BottomRowActions(postViewModel: ShortNotePostViewModel) {
postViewModel: ShortNotePostViewModel,
accountViewModel: AccountViewModel,
) {
val context = LocalContext.current
val scrollState = rememberScrollState() val scrollState = rememberScrollState()
Row( Row(
modifier = modifier =
@@ -235,7 +235,7 @@ open class ShortNotePostViewModel :
fun hasLnAddress(): Boolean = account.userProfile().info?.lnAddress() != null fun hasLnAddress(): Boolean = account.userProfile().info?.lnAddress() != null
fun user(): User? = account.userProfile() fun user(): User = account.userProfile()
open fun init(accountVM: AccountViewModel) { open fun init(accountVM: AccountViewModel) {
this.accountViewModel = accountVM this.accountViewModel = accountVM
@@ -534,15 +534,16 @@ open class ShortNotePostViewModel :
private suspend fun createTemplate(): EventTemplate<out Event>? { private suspend fun createTemplate(): EventTemplate<out Event>? {
// Check if this is a voice message // Check if this is a voice message
voiceMetadata?.let { audioMeta -> voiceMetadata?.let { audioMeta ->
return if (originalNote != null) { // Only create voice reply if original note is also a VoiceEvent
val originalVoiceHint = originalNote?.toEventHint<VoiceEvent>()
return if (originalVoiceHint != null) {
// Create voice reply event // Create voice reply event
@Suppress("UNCHECKED_CAST")
VoiceReplyEvent.build( VoiceReplyEvent.build(
voiceMessage = audioMeta, voiceMessage = audioMeta,
replyingTo = originalNote!!.toEventHint<Event>() as com.vitorpamplona.quartz.nip01Core.hints.EventHintBundle<VoiceEvent>, replyingTo = originalVoiceHint,
) )
} else { } else {
// Create root voice event // Create root voice event (no reply or original is not a voice message)
VoiceEvent.build( VoiceEvent.build(
voiceMessage = audioMeta, voiceMessage = audioMeta,
) )