Авторизация, нерабочий чат

This commit is contained in:
Халимов Рустам
2026-04-14 01:41:51 +03:00
parent 1fb1be47dd
commit dc051fa9ae
22 changed files with 160 additions and 49 deletions
@@ -1,6 +1,5 @@
package chats.presentation.chat_detail
import android.Manifest
import android.net.Uri
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
@@ -23,13 +22,13 @@ import chats.presentation.components.EmojiPicker
import chats.presentation.components.MessageBubble
import core.utils.VoiceRecorder
import core.utils.copyUriToFile
import kotlinx.coroutines.launch
import java.io.File
import ru.knot.messager.R
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ChatDetailScreen(
chatId: String,
chatName: String,
viewModel: ChatDetailViewModel,
onBack: () -> Unit
@@ -52,6 +51,11 @@ fun ChatDetailScreen(
}
}
// Загрузка данных чата при входе
LaunchedEffect(chatId) {
viewModel.setChatId(chatId)
}
// Автопрокрутка к последнему сообщению
LaunchedEffect(state.messages.size) {
if (state.messages.isNotEmpty()) {
@@ -112,7 +116,7 @@ fun ChatDetailScreen(
items(state.messages) { message ->
MessageBubble(
message = message,
isCurrentUser = message.senderId == "CURRENT_USER_ID" // TODO: Get from Auth
isCurrentUser = message.senderId == viewModel.getCurrentUserId()
)
}
}
@@ -163,7 +167,6 @@ fun ChatDetailScreen(
} else {
voiceRecorder.stopRecording()
isRecording = false
// TODO: Send voice file
}
}
) {
@@ -8,6 +8,7 @@ import chats.domain.model.Message
import chats.domain.repository.ChatRepository
import chats.data.repository.toDomain
import core.network.ServerConfig
import core.security.TokenManager
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
@@ -30,7 +31,8 @@ data class ChatDetailState(
class ChatDetailViewModel @Inject constructor(
private val repository: ChatRepository,
private val signalrClient: ChatHubClient,
private val serverConfig: ServerConfig
private val serverConfig: ServerConfig,
private val tokenManager: TokenManager
) : ViewModel() {
private val _state = MutableStateFlow(ChatDetailState())
@@ -48,6 +50,10 @@ class ChatDetailViewModel @Inject constructor(
) }
}
fun getCurrentUserId(): String {
return tokenManager.getUserId() ?: ""
}
fun setChatId(chatId: String) {
currentChatId = chatId
loadMessages(chatId)
@@ -58,7 +64,9 @@ class ChatDetailViewModel @Inject constructor(
viewModelScope.launch {
_state.update { it.copy(isLoading = true) }
try {
val messages = repository.getMessages(chatId)
// Бэкенд обычно возвращает сообщения от новых к старым.
// Для чата нам нужно наоборот: старые вверху, новые внизу.
val messages = repository.getMessages(chatId).reversed()
_state.update { it.copy(messages = messages, isLoading = false) }
} catch (e: Exception) {
_state.update { it.copy(isLoading = false, error = e.localizedMessage) }
@@ -146,6 +154,15 @@ class ChatDetailViewModel @Inject constructor(
_state.update { it.copy(error = "File too large") }
return
}
// Upload logic...
viewModelScope.launch {
try {
val url = repository.uploadMedia(file)
// After upload, we might want to send a message with this media
// For now, let's just log it or handle as per app requirements
} catch (e: Exception) {
_state.update { it.copy(error = e.localizedMessage) }
}
}
}
}