Pablo
07/02/2024, 8:19 PMPablo
07/02/2024, 8:20 PM@Composable
fun LoadingScreen(
viewModel: LoadingScreenViewModel = viewModel(factory = LoadingScreenViewModel.factory(LocalContext.current)),
modifier: Modifier = Modifier
) {
val uiState = viewModel.uiState
Column(
modifier = modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = "Loading Screen"
)
Spacer(
modifier = Modifier.height(32.dp)
)
CircularProgressIndicator(
modifier = Modifier.width(64.dp),
color = MaterialTheme.colorScheme.secondary,
trackColor = MaterialTheme.colorScheme.surfaceVariant,
)
}
}
The viewmodel:
sealed interface UiState {
object Success : UiState
object Error : UiState
object Loading : UiState
}
class LoadingScreenViewModel(
val context: Context
): ViewModel() {
private val _uiState = MutableStateFlow(UiState.Loading)
val uiState: StateFlow<UiState> = _uiState.asStateFlow()
init {
decode()
}
private fun decode() {
viewModelScope.launch {
withContext(Dispatchers.Default) {
App.decodeApp(context)
}
_uiState.value = UiState.Success
}
}
private fun openNextScreen() {
//open next screen
//where should I call this method?
}
companion object {
fun factory(myString: Context) : ViewModelProvider.Factory = viewModelFactory {
initializer {
LoadingScreenViewModel(
myString
)
}
}
}
}
Pablo
07/02/2024, 8:59 PM_uiState.value = UiState.Success
when the decode has ended, but should I simply call the openNextScreen() function after the state change? or should by some way listen to that state change for calling the function automatically? how?