feat: Implement real-time UI updates for new messages in MessageRepository; refactor ChatViewModel to avoid message duplication

This commit is contained in:
k1ngsterr1
2026-01-16 16:43:15 +05:00
parent b1046f88e5
commit 6386164ae7
2 changed files with 38 additions and 131 deletions

View File

@@ -62,6 +62,10 @@ class MessageRepository private constructor(private val context: Context) {
private val _dialogs = MutableStateFlow<List<Dialog>>(emptyList())
val dialogs: StateFlow<List<Dialog>> = _dialogs.asStateFlow()
// 🔔 События новых сообщений для обновления UI в реальном времени
private val _newMessageEvents = MutableSharedFlow<String>(replay = 0, extraBufferCapacity = 10)
val newMessageEvents: SharedFlow<String> = _newMessageEvents.asSharedFlow()
// Текущий аккаунт
private var currentAccount: String? = null
private var currentPrivateKey: String? = null
@@ -321,6 +325,9 @@ class MessageRepository private constructor(private val context: Context) {
if (!stillExists) {
val message = entity.toMessage()
updateMessageCache(dialogKey, message)
// 🔔 Уведомляем UI о новом сообщении (emit dialogKey для обновления)
_newMessageEvents.tryEmit(dialogKey)
}
} catch (e: Exception) {