Второй этап выхода из офлайн режима

This commit is contained in:
Халимов Рустам
2026-04-20 10:46:09 +03:00
parent b2c29958b5
commit c839a9bc03
19 changed files with 899 additions and 84 deletions
@@ -4,9 +4,11 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import chats.data.remote.signalr.ChatEvent
import chats.data.remote.signalr.ChatHubClient
import chats.data.remote.signalr.ConnectionStatus
import chats.data.repository.toDomain
import chats.domain.model.Message
import chats.domain.repository.ChatRepository
import core.network.NetworkManager
import core.network.ServerConfig
import core.security.TokenManager
import dagger.hilt.android.lifecycle.HiltViewModel
@@ -59,6 +61,7 @@ class ChatDetailViewModel @Inject constructor(
private val tokenManager: TokenManager,
private val activeChatTracker: core.notifications.data.ActiveChatTracker,
private val signalrNotificationObserver: chats.data.remote.signalr.SignalRNotificationObserver,
private val networkManager: NetworkManager,
@dagger.hilt.android.qualifiers.ApplicationContext private val context: android.content.Context
) : ViewModel() {
@@ -114,12 +117,39 @@ class ChatDetailViewModel @Inject constructor(
}
loadChatInfo(chatId)
observeSignalRStatus(chatId)
observeSignalREvents(chatId)
observeNetworkStatus(chatId)
// Initial sync from network
refreshMessages(chatId)
}
private fun observeNetworkStatus(chatId: String) {
// При восстановлении сети обновляем сообщения
networkManager.isOnline
.filter { it } // Только переход в онлайн
.distinctUntilChanged()
.onEach {
android.util.Log.d(TAG, "Network restored in chat detail, refreshing messages")
kotlinx.coroutines.delay(1000) // Дадим сети стабилизироваться
refreshMessages(chatId)
}
.launchIn(viewModelScope)
}
private fun observeSignalRStatus(chatId: String) {
// При переподключении SignalR обновляем сообщения
signalrClient.status
.filter { it == ConnectionStatus.CONNECTED }
.distinctUntilChanged()
.onEach {
android.util.Log.d(TAG, "SignalR connected, refreshing messages for chat $chatId")
refreshMessages(chatId)
}
.launchIn(viewModelScope)
}
private fun updateMessages(messages: List<Message>) {
val sortedMessages = messages.sortedByDescending { it.sequenceId }
_state.update { it.copy(