Офлайн режим, начало

This commit is contained in:
Халимов Рустам
2026-04-20 01:00:35 +03:00
parent 7c66e1c0c0
commit 38e7184a0d
18 changed files with 214 additions and 35 deletions
@@ -14,6 +14,8 @@ import kotlinx.coroutines.launch
import javax.inject.Inject
import chats.data.repository.toDomain
private const val TAG = "ChatListViewModel"
data class ChatListState(
val chats: List<Chat> = emptyList(),
val isLoading: Boolean = false,
@@ -77,11 +79,26 @@ class ChatListViewModel @Inject constructor(
viewModelScope.launch {
_state.update { it.copy(isLoading = true) }
try {
val chats = repository.getChats()
_state.update { it.copy(chats = sortChats(chats), isLoading = false) }
// Пробуем загрузить из сети (это закэширует в Room)
repository.getChats()
} catch (e: Exception) {
_state.update { it.copy(isLoading = false, error = e.message) }
android.util.Log.d(TAG, "Initial load failed, will use cache")
}
// Подписываемся на Flow из Room (всегда работает, даже оффлайн)
repository.getChatsFlow()
.catch { e ->
android.util.Log.e(TAG, "Flow error", e)
emit(emptyList())
}
.collect { chats ->
_state.update {
it.copy(
chats = sortChats(chats),
isLoading = false
)
}
}
}
}