Восстановлены голосовые
This commit is contained in:
@@ -574,7 +574,11 @@ class ChatDetailViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
fun sendVoiceMessage(file: File) {
|
||||
val chatId = currentChatId ?: return
|
||||
android.util.Log.d("ChatDetailVM", "sendVoiceMessage called with file: ${file.absolutePath}, size: ${file.length()}")
|
||||
val chatId = currentChatId ?: run {
|
||||
android.util.Log.e("ChatDetailVM", "sendVoiceMessage failed: currentChatId is null")
|
||||
return
|
||||
}
|
||||
val replyToId = _state.value.replyingMessage?.id
|
||||
_state.update { it.copy(replyingMessage = null) }
|
||||
|
||||
@@ -590,19 +594,21 @@ class ChatDetailViewModel @Inject constructor(
|
||||
mediaType = chats.domain.model.MediaType.AUDIO,
|
||||
media = emptyList(),
|
||||
senderName = "Вы",
|
||||
senderAvatar = null,
|
||||
reactions = emptyMap(),
|
||||
isRead = false,
|
||||
sequenceId = 0
|
||||
)
|
||||
|
||||
viewModelScope.launch {
|
||||
repository.saveMessage(tempMessage)
|
||||
}
|
||||
// Добавляем временное сообщение в стейт для индикации отправки
|
||||
_state.update { it.copy(messages = listOf(tempMessage) + it.messages) }
|
||||
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
android.util.Log.d("ChatDetailVM", "Starting voice upload...")
|
||||
// 1. Upload the audio file
|
||||
val url = repository.uploadMedia(file)
|
||||
android.util.Log.d("ChatDetailVM", "Voice upload successful, url: $url")
|
||||
|
||||
// 2. Send the message with the attachment
|
||||
val attachment = chats.data.remote.api.AttachmentRequest(
|
||||
@@ -612,19 +618,35 @@ class ChatDetailViewModel @Inject constructor(
|
||||
fileSize = file.length()
|
||||
)
|
||||
|
||||
android.util.Log.d("ChatDetailVM", "Sending message with voice attachment...")
|
||||
val sentMessage = repository.sendMessage(
|
||||
chatId = chatId,
|
||||
content = null,
|
||||
type = "audio",
|
||||
type = "media",
|
||||
attachments = listOf(attachment),
|
||||
replyToId = replyToId
|
||||
)
|
||||
android.util.Log.d("ChatDetailVM", "Voice message sent successfully: ${sentMessage.id}")
|
||||
|
||||
repository.deleteLocalMessage(tempId)
|
||||
repository.saveMessage(sentMessage)
|
||||
// Заменяем временное сообщение на настоящее
|
||||
_state.update { currentState ->
|
||||
val filtered = currentState.messages.filter { it.id != tempId }
|
||||
// Избегаем дубликатов, если SignalR уже добавил сообщение
|
||||
if (filtered.any { it.id == sentMessage.id }) {
|
||||
currentState.copy(messages = filtered)
|
||||
} else {
|
||||
currentState.copy(messages = listOf(sentMessage) + filtered)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
repository.deleteLocalMessage(tempId)
|
||||
_state.update { it.copy(error = "Ошибка отправки голосового: ${e.localizedMessage}") }
|
||||
android.util.Log.e("ChatDetailVM", "Error sending voice message", e)
|
||||
// Удаляем временное сообщение и показываем ошибку
|
||||
_state.update { currentState ->
|
||||
currentState.copy(
|
||||
messages = currentState.messages.filter { it.id != tempId },
|
||||
error = "Ошибка отправки голосового: ${e.localizedMessage}"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user