Pablo
05/10/2022, 8:51 PMViewModel
like this?
@HiltViewModel
class FooViewModel @Inject constructor(
@DispatchersIO private val dispatcher: CoroutineDispatcher,
val useCase: MyUseCase,
) : ViewModel() {
private val _state = MutableStateFlow<State>(State.Loading)
val state: StateFlow<State> = _state
init {
getFoo()
}
private fun getFoo() {
viewModelScope.launch(dispatcher) {
usecase()
.fold(
{
_state.value = State.Error
},
{ items ->
_state.value = State.Data(items)
})
}
}
I've tried with turbine but don't know how to test the init
method, is there any easy way to do it with the newer version of coroutines? I can't verify first is State.Loading
then if success the state is State.Data
Also I was reading this https://github.com/Kotlin/kotlinx.coroutines/issues/3143