Created
March 13, 2024 13:14
-
-
Save LloydBlv/cb5bffe333dc719d877dc58067be9f45 to your computer and use it in GitHub Desktop.
Avoid using init{} block in ViewModels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SearchViewModel @Inject constructor( | |
private val searchUseCase: dagger.Lazy<SearchUseCase>, | |
private val wordsUseCase: GetWordsUseCase, | |
) : ViewModel() { | |
data class UiState( | |
val isLoading: Boolean = true, | |
val words: List<String> = emptyList() | |
) | |
val state: StateFlow<UiState> = flow { | |
emit(UiState(isLoading = true)) | |
val words = wordsUseCase.invoke() | |
emit(UiState(isLoading = false, words = words)) | |
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), UiState()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment