Skip to content

Instantly share code, notes, and snippets.

@LloydBlv
Created March 13, 2024 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LloydBlv/f92f335c98905f9ec7b7c86b2173a644 to your computer and use it in GitHub Desktop.
Save LloydBlv/f92f335c98905f9ec7b7c86b2173a644 to your computer and use it in GitHub Desktop.
Demonstration of bad implementation in ViewModels
class SearchViewModel @Inject constructor(
private val searchUseCase: dagger.Lazy<SearchUseCase>,
private val wordsUseCase: GetWordsUseCase,
) : ViewModel() {
data class UiState(
val isLoading: Boolean,
val words: List<String> = emptyList()
)
init {
getWords()
}
val _state = MutableStateFlow(UiState(isLoading = true))
val state: StateFlow<UiState>
get() = _state.asStateFlow()
private fun getWords() {
viewModelScope.launch {
_state.update { UiState(isLoading = true) }
val words = wordsUseCase.invoke()
_state.update { UiState(isLoading = false, words = words) }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment