Я пытаюсь поддерживать видимость заставки до тех пор, пока логическая переменная не сообщит приложению, к какому экрану следует перейти. Эту логическую переменную необходимо загрузить, а после этого заставку следует удалить.
Однако сейчас это не так. Экран приветствия отображается на короткое время (приблизительно 1 с), а затем приложение переходит к экрану, который должен отображаться, если пользователь завершил процесс регистрации. Что я делаю не так?
SplashScreenViewModel:
class SplashScreenViewModel @Inject constructor(
private val preferences: Preferences
) : ViewModel() {
private val _isLoading = MutableStateFlow(RequestState.Loading)
val isLoading = _isLoading.asStateFlow()
private val _startDestination: MutableState = mutableStateOf("welcome")
val startDestination: State = _startDestination
init {
viewModelScope.launch {
val onboardingCompleted = preferences.readOnboardingCompleted()
if (onboardingCompleted) {
_startDestination.value = "overview"
} else {
_startDestination.value = "welcome"
}
_isLoading.value = RequestState.Success()
}
}
}
MainActivity:
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
@Inject
lateinit var splashScreenViewModel: SplashScreenViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
installSplashScreen().setKeepOnScreenCondition {
splashScreenViewModel.isLoading.value is RequestState.Loading
}
setContent {
HealthTheme {
val startDestination by splashScreenViewModel.startDestination
val snackbarHostState = remember { SnackbarHostState() }
Scaffold(
modifier = Modifier.fillMaxSize(),
snackbarHost = { SnackbarHost(snackbarHostState) }
) { paddingValues ->
val navController = rememberNavController()
NavHost( // TODO: Navigation just for testing (to be improved)
navController = navController,
startDestination = startDestination
) {
// Composables with their routes...
Состояние запроса:
sealed interface RequestState {
data object Loading : RequestState
data class Success(val data: Any? = null) : RequestState
data class Error(val message: String) : RequestState
}
Подробнее здесь: https://stackoverflow.com/questions/772 ... is-ignored