From 0921aa829d8ac2be85d4037d2b4dd1016951f226 Mon Sep 17 00:00:00 2001 From: Murilo Bioni Caruso Date: Mon, 11 Dec 2023 10:35:13 -0300 Subject: [PATCH] Alterei o lifecycle do flow da lista de bolas --- .../alura/mundobola/ui/viewmodel/ListaDeBolasViewModel.kt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/src/main/java/br/com/alura/mundobola/ui/viewmodel/ListaDeBolasViewModel.kt b/app/src/main/java/br/com/alura/mundobola/ui/viewmodel/ListaDeBolasViewModel.kt index f880631..d21dfa5 100644 --- a/app/src/main/java/br/com/alura/mundobola/ui/viewmodel/ListaDeBolasViewModel.kt +++ b/app/src/main/java/br/com/alura/mundobola/ui/viewmodel/ListaDeBolasViewModel.kt @@ -9,7 +9,9 @@ import br.com.alura.mundobola.aplicacao.repositorio.MundoBolaRepositorio import br.com.alura.mundobola.ui.stateholder.ListaDeBolasUiState import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import javax.inject.Inject @@ -29,6 +31,11 @@ class ListaDeBolasViewModel @Inject constructor( private suspend fun carregaLista() { repositorio.listaDeBolas() + .stateIn( + viewModelScope, + SharingStarted.WhileSubscribed(5000), + emptyList() + ) .collect { lista -> _uiState.value = _uiState.value.copy( listaDeBolas = lista.map {