Mario Adam
05/03/2023, 10:14 AMLaunchedEffect
on the LoginForm? Or having a viewmodel on the LoginForm for login and a separate viewmodel on the RegionPicker (with a LaunchedEffect
in this sub-component)?
Thread in Slack ConversationFlorian N'Gbala
05/03/2023, 2:49 PMinit
method (if you need it right away) and then you could set them in a mutableState so that your composable will be recomposed when the data is ready.
I would not load the regions using a LaunchEffect
in my composable. Indeed, in my opinion, your composables should be as “dumb” as possible and should not know how to fetch the regions.
I would rather do something like that :
The viewModel
class LoginViewModel (getRegionsUseCase: getRegionUseCase): ViewModel(){
private val _regions: MutableStateFlow<List<String>> = MutableStateFlow(emptyList())
val regions: StateFlow<List<String>> = _regions.asStateFlow()
init {
viewModelScope.launch {
_regions.value = getRegionsUseCase()
}
}
fun login(username: String, password: String){
// Do what you need to login.
}
}
The composable :
@Composable
fun LoginScreen(
viewModel :LoginViewModel = hiltViewModel()
) {
val regions : List<String> by viewModel.regions.collectAsStateWithLifecycle()
// Do anything you want with the regions in your composable.
}
I hope it will help you 😇dorche
05/03/2023, 3:09 PMStylianos Gakis
05/03/2023, 7:01 PMgetContractCoverageUseCase
in this case would emit, and then you get your next value. To control when that emits something, I like using Turbine.